MCPcopy Create free account
hub / github.com/python/cpython / test_cancel_release_race

Method test_cancel_release_race

Lib/test/test_asyncio/test_locks.py:194–237  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

192 await tc
193
194 async def test_cancel_release_race(self):
195 # Issue 32734
196 # Acquire 4 locks, cancel second, release first
197 # and 2 locks are taken at once.
198 loop = asyncio.get_running_loop()
199 lock = asyncio.Lock()
200 lock_count = 0
201 call_count = 0
202
203 async def lockit():
204 nonlocal lock_count
205 nonlocal call_count
206 call_count += 1
207 await lock.acquire()
208 lock_count += 1
209
210 def trigger():
211 t1.cancel()
212 lock.release()
213
214 await lock.acquire()
215
216 t1 = asyncio.create_task(lockit())
217 t2 = asyncio.create_task(lockit())
218 t3 = asyncio.create_task(lockit())
219
220 # Start scheduled tasks
221 await asyncio.sleep(0)
222
223 loop.call_soon(trigger)
224 with self.assertRaises(asyncio.CancelledError):
225 # Wait for cancellation
226 await t1
227
228 # Make sure only one lock was taken
229 self.assertEqual(lock_count, 1)
230 # While 3 calls were made to lockit()
231 self.assertEqual(call_count, 3)
232 self.assertTrue(t1.cancelled() and t2.done())
233
234 # Cleanup the task that is stuck on acquire.
235 t3.cancel()
236 await asyncio.sleep(0)
237 self.assertTrue(t3.cancelled())
238
239 async def test_finished_waiter_cancelled(self):
240 lock = asyncio.Lock()

Callers

nothing calls this directly

Calls 11

acquireMethod · 0.95
LockMethod · 0.80
assertTrueMethod · 0.80
create_taskMethod · 0.45
sleepMethod · 0.45
call_soonMethod · 0.45
assertRaisesMethod · 0.45
assertEqualMethod · 0.45
cancelledMethod · 0.45
doneMethod · 0.45
cancelMethod · 0.45

Tested by

no test coverage detected