File tree Expand file tree Collapse file tree 2 files changed +35
-5
lines changed Expand file tree Collapse file tree 2 files changed +35
-5
lines changed Original file line number Diff line number Diff line change @@ -52,10 +52,10 @@ def reschedule(self, when: Optional[float]) -> None:
52
52
self ._timeout_handler = None
53
53
else :
54
54
loop = events .get_running_loop ()
55
- self . _timeout_handler = loop .call_at (
56
- when ,
57
- self . _on_timeout ,
58
- )
55
+ if loop .time () >= when :
56
+ self . _timeout_handler = loop . call_soon ( self . _on_timeout )
57
+ else :
58
+ self . _timeout_handler = loop . call_at ( when , self . _on_timeout )
59
59
60
60
def expired (self ) -> bool :
61
61
"""Is timeout expired during execution?"""
@@ -126,7 +126,13 @@ def timeout(delay: Optional[float]) -> Timeout:
126
126
into TimeoutError.
127
127
"""
128
128
loop = events .get_running_loop ()
129
- return Timeout (loop .time () + delay if delay is not None else None )
129
+ if delay is None :
130
+ return Timeout (None )
131
+
132
+ if delay <= 0 :
133
+ return Timeout (0 )
134
+
135
+ return Timeout (loop .time () + delay )
130
136
131
137
132
138
def timeout_at (when : Optional [float ]) -> Timeout :
Original file line number Diff line number Diff line change @@ -105,6 +105,30 @@ async def test_timeout_zero(self):
105
105
self .assertLess (t1 - t0 , 2 )
106
106
self .assertTrue (t0 <= cm .when () <= t1 )
107
107
108
+ async def test_timeout_zero_sleep_zero (self ):
109
+ loop = asyncio .get_running_loop ()
110
+ t0 = loop .time ()
111
+ with self .assertRaises (TimeoutError ):
112
+ async with asyncio .timeout (0 ) as cm :
113
+ await asyncio .sleep (0 )
114
+ t1 = loop .time ()
115
+ self .assertTrue (cm .expired ())
116
+ # 2 sec for slow CI boxes
117
+ self .assertLess (t1 - t0 , 2 )
118
+ self .assertTrue (t0 <= cm .when () <= t1 )
119
+
120
+ async def test_timeout_in_the_past_sleep_zero (self ):
121
+
8592
loop = asyncio .get_running_loop ()
122
+ t0 = loop .time ()
123
+ with self .assertRaises (TimeoutError ):
124
+ async with asyncio .timeout (- 11 ) as cm :
125
+ await asyncio .sleep (0 )
126
+ t1 = loop .time ()
127
+ self .assertTrue (cm .expired ())
128
+ # 2 sec for slow CI boxes
129
+ self .assertLess (t1 - t0 , 2 )
130
+ self .assertTrue (t0 <= cm .when () <= t1 )
131
+
108
132
async def test_foreign_exception_passed (self ):
109
133
with self .assertRaises (KeyError ):
110
134
async with asyncio .timeout (0.01 ) as cm :
You can’t perform that action at this time.
0 commit comments