|
16 | 16 | """Tests for rate_limit_wrapper."""
|
17 | 17 |
|
18 | 18 | import time
|
| 19 | +from typing import Any, List, Protocol |
19 | 20 | from unittest import mock
|
20 | 21 |
|
21 | 22 | from absl.testing import absltest
|
@@ -46,6 +47,17 @@ def _get_base_env():
|
46 | 47 | return env
|
47 | 48 |
|
48 | 49 |
|
| 50 | +class _FnWithTimestamps(Protocol): |
| 51 | + """A function with `timestamp` and `timestamps` attributes.""" |
| 52 | + |
| 53 | + timestamp: float |
| 54 | + timestamps: List[float] |
| 55 | + |
| 56 | + |
| 57 | +def _with_timestamp(fn: Any) -> _FnWithTimestamps: |
| 58 | + return fn |
| 59 | + |
| 60 | + |
49 | 61 | class RateLimitWrapperTest(parameterized.TestCase):
|
50 | 62 |
|
51 | 63 | @parameterized.named_parameters(
|
@@ -107,6 +119,7 @@ def test_enabled_sleep_type_before(self, mock_sleep):
|
107 | 119 | _ = wrapper.reset()
|
108 | 120 | mock_sleep.assert_not_called() # It should never sleep during reset().
|
109 | 121 |
|
| 122 | + @_with_timestamp |
110 | 123 | def _sleep_fn(sleep_time):
|
111 | 124 | _sleep_fn.timestamp = time.time()
|
112 | 125 | self.assertBetween(sleep_time, 0.0, 33.33)
|
@@ -145,6 +158,7 @@ def test_enabled_sleep_type_after(self, mock_sleep):
|
145 | 158 | _ = wrapper.reset()
|
146 | 159 | mock_sleep.assert_not_called() # It should never sleep during reset().
|
147 | 160 |
|
| 161 | + @_with_timestamp |
148 | 162 | def _sleep_fn(sleep_time):
|
149 | 163 | _sleep_fn.timestamp = time.time()
|
150 | 164 | self.assertBetween(sleep_time, 0.0, 33.33)
|
@@ -185,12 +199,14 @@ def test_enabled_sleep_type_after_with_repeat(self, mock_sleep):
|
185 | 199 | _ = wrapper.reset()
|
186 | 200 | mock_sleep.assert_not_called() # It should never sleep during reset().
|
187 | 201 |
|
| 202 | + @_with_timestamp |
188 | 203 | def _sleep_fn(sleep_time):
|
189 | 204 | _sleep_fn.timestamp = time.time()
|
190 | 205 | self.assertBetween(sleep_time, 0.0, 33.33)
|
191 | 206 |
|
192 | 207 | mock_sleep.side_effect = _sleep_fn
|
193 | 208 |
|
| 209 | + @_with_timestamp |
194 | 210 | def _step_fn(action):
|
195 | 211 | # On even calls the action should be the actual agent action, but on odd
|
196 | 212 | # calls they should be REPEATs.
|
|
0 commit comments