Skip to content

Commit 62b2af9

Browse files
Jens Jorgensenelprans
Jens Jorgensen
authored andcommitted
Add support for <timer handle>.when()
1 parent 20febe0 commit 62b2af9

File tree

4 files changed

+19
-0
lines changed

4 files changed

+19
-0
lines changed

Diff for: tests/test_base.py

+9
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,15 @@ async def main():
852852
self.assertEqual(OK, 5)
853853
self.assertEqual(NOT_OK, 0)
854854

855+
def test_loop_call_later_handle_when(self):
856+
cb = lambda: False # NoQA
857+
delay = 1.0
858+
loop_t = self.loop.time()
859+
handle = self.loop.call_later(delay, cb)
860+
self.assertAlmostEqual(handle.when(), loop_t + delay, places=2)
861+
handle.cancel()
862+
self.assertTrue(handle.cancelled())
863+
855864

856865
class TestBaseAIO(_TestBase, AIOTestCase):
857866
pass

Diff for: uvloop/cbhandles.pyx

+3
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,9 @@ cdef class TimerHandle:
308308
def cancel(self):
309309
self._cancel()
310310

311+
def when(self):
312+
return self.timer.get_when() * 1e-3
313+
311314

312315
cdef format_callback_name(func):
313316
if hasattr(func, '__qualname__'):

Diff for: uvloop/handles/timer.pxd

+2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ cdef class UVTimer(UVHandle):
44
object ctx
55
bint running
66
uint64_t timeout
7+
uint64_t start_t
78

89
cdef _init(self, Loop loop, method_t callback, object ctx,
910
uint64_t timeout)
1011

1112
cdef stop(self)
1213
cdef start(self)
14+
cdef get_when(self)
1315

1416
@staticmethod
1517
cdef UVTimer new(Loop loop, method_t callback, object ctx,

Diff for: uvloop/handles/timer.pyx

+5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ cdef class UVTimer(UVHandle):
2323
self.ctx = ctx
2424
self.running = 0
2525
self.timeout = timeout
26+
self.start_t = 0
2627

2728
cdef stop(self):
2829
cdef int err
@@ -47,6 +48,7 @@ cdef class UVTimer(UVHandle):
4748
if self.running == 0:
4849
# Update libuv internal time.
4950
uv.uv_update_time(self._loop.uvloop) # void
51+
self.start_t = uv.uv_now(self._loop.uvloop)
5052

5153
err = uv.uv_timer_start(<uv.uv_timer_t*>self._handle,
5254
__uvtimer_callback,
@@ -57,6 +59,9 @@ cdef class UVTimer(UVHandle):
5759
return
5860
self.running = 1
5961

62+
cdef get_when(self):
63+
return self.start_t + self.timeout
64+
6065
@staticmethod
6166
cdef UVTimer new(Loop loop, method_t callback, object ctx,
6267
uint64_t timeout):

0 commit comments

Comments
 (0)