def test_acquire_same_lock_twice_blocking_without_timeout(self): assert not self.redis.exists(self.redlock.key) with ContextTimer() as timer: assert self.redlock.acquire() assert self.redis.exists(self.redlock.key) assert self.redlock.acquire() assert self.redis.exists(self.redlock.key) assert timer.elapsed() >= self.redlock.auto_release_time
def setUp(self): super().setUp() self.timer = ContextTimer()
class ContextTimerTests(TestCase): ACCURACY = 50 # in milliseconds def setUp(self): super().setUp() self.timer = ContextTimer() def _confirm_elapsed(self, expected): got = round(self.timer.elapsed() / self.ACCURACY) * self.ACCURACY assert got == expected, '{} != {}'.format(got, expected) def test_start_stop_and_elapsed(self): # timer hasn't been started with self.assertRaises(RuntimeError): self.timer.elapsed() with self.assertRaises(RuntimeError): self.timer.stop() # timer has been started but not stopped self.timer.start() with self.assertRaises(RuntimeError): self.timer.start() time.sleep(0.1) self._confirm_elapsed(1*100) self.timer.stop() # timer has been stopped with self.assertRaises(RuntimeError): self.timer.start() time.sleep(0.1) self._confirm_elapsed(1*100) with self.assertRaises(RuntimeError): self.timer.stop() def test_context_manager(self): with self.timer: self._confirm_elapsed(0) for iteration in range(1, 3): with self.subTest(iteration=iteration): time.sleep(0.1) self._confirm_elapsed(iteration*100) self._confirm_elapsed(iteration*100) time.sleep(0.1) self._confirm_elapsed(iteration*100) with self.assertRaises(RuntimeError), self.timer: ...
class ContextTimerTests(TestCase): ACCURACY = 50 # in milliseconds def setUp(self): super().setUp() self.timer = ContextTimer() def _confirm_elapsed(self, expected): got = round(self.timer.elapsed() / self.ACCURACY) * self.ACCURACY assert got == expected, '{} != {}'.format(got, expected) def test_start_stop_and_elapsed(self): # timer hasn't been started with self.assertRaises(RuntimeError): self.timer.elapsed() with self.assertRaises(RuntimeError): self.timer.stop() # timer has been started but not stopped self.timer.start() with self.assertRaises(RuntimeError): self.timer.start() time.sleep(0.1) self._confirm_elapsed(1 * 100) self.timer.stop() # timer has been stopped with self.assertRaises(RuntimeError): self.timer.start() time.sleep(0.1) self._confirm_elapsed(1 * 100) with self.assertRaises(RuntimeError): self.timer.stop() def test_context_manager(self): with self.timer: self._confirm_elapsed(0) for iteration in range(1, 3): with self.subTest(iteration=iteration): time.sleep(0.1) self._confirm_elapsed(iteration * 100) self._confirm_elapsed(iteration * 100) time.sleep(0.1) self._confirm_elapsed(iteration * 100) with self.assertRaises(RuntimeError), self.timer: ...