diff --git a/tests/test_base.py b/tests/test_base.py index b1a79365..7414a310 100644 --- a/tests/test_base.py +++ b/tests/test_base.py @@ -852,6 +852,15 @@ async def main(): self.assertEqual(OK, 5) self.assertEqual(NOT_OK, 0) + def test_loop_call_later_handle_when(self): + cb = lambda: False # NoQA + delay = 1.0 + loop_t = self.loop.time() + handle = self.loop.call_later(delay, cb) + self.assertAlmostEqual(handle.when(), loop_t + delay, places=2) + handle.cancel() + self.assertTrue(handle.cancelled()) + class TestBaseAIO(_TestBase, AIOTestCase): pass diff --git a/uvloop/cbhandles.pyx b/uvloop/cbhandles.pyx index 2fe386f7..6d6a0b04 100644 --- a/uvloop/cbhandles.pyx +++ b/uvloop/cbhandles.pyx @@ -308,6 +308,9 @@ cdef class TimerHandle: def cancel(self): self._cancel() + def when(self): + return self.timer.get_when() * 1e-3 + cdef format_callback_name(func): if hasattr(func, '__qualname__'): diff --git a/uvloop/handles/timer.pxd b/uvloop/handles/timer.pxd index e3ac4ea6..fda23b67 100644 --- a/uvloop/handles/timer.pxd +++ b/uvloop/handles/timer.pxd @@ -4,12 +4,14 @@ cdef class UVTimer(UVHandle): object ctx bint running uint64_t timeout + uint64_t start_t cdef _init(self, Loop loop, method_t callback, object ctx, uint64_t timeout) cdef stop(self) cdef start(self) + cdef get_when(self) @staticmethod cdef UVTimer new(Loop loop, method_t callback, object ctx, diff --git a/uvloop/handles/timer.pyx b/uvloop/handles/timer.pyx index d8c3a4aa..6ea5ff50 100644 --- a/uvloop/handles/timer.pyx +++ b/uvloop/handles/timer.pyx @@ -23,6 +23,7 @@ cdef class UVTimer(UVHandle): self.ctx = ctx self.running = 0 self.timeout = timeout + self.start_t = 0 cdef stop(self): cdef int err @@ -47,6 +48,7 @@ cdef class UVTimer(UVHandle): if self.running == 0: # Update libuv internal time. uv.uv_update_time(self._loop.uvloop) # void + self.start_t = uv.uv_now(self._loop.uvloop) err = uv.uv_timer_start(self._handle, __uvtimer_callback, @@ -57,6 +59,9 @@ cdef class UVTimer(UVHandle): return self.running = 1 + cdef get_when(self): + return self.start_t + self.timeout + @staticmethod cdef UVTimer new(Loop loop, method_t callback, object ctx, uint64_t timeout):