Skip to content

Commit

Permalink
* format code
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshuaalbert committed Jan 28, 2024
1 parent 7d4b4c6 commit 7ddf01d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
8 changes: 5 additions & 3 deletions fair_async_rlock/fair_async_rlock.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import asyncio
from collections import deque

__all__ = ['FairAsyncRLock']
__all__ = [
'FairAsyncRLock'
]


class FairAsyncRLock:
Expand Down Expand Up @@ -46,9 +48,9 @@ async def acquire(self):
self._owner = me
self._count = 1
except asyncio.CancelledError:
try: # if in queue, then cancelled before release
try: # if in queue, then cancelled before release
self._queue.remove(event)
except ValueError: # otherwise, release happened, this was next, and we simulate passing on
except ValueError: # otherwise, release happened, this was next, and we simulate passing on
self._wait_event = False
self._owner = me
self._count = 1
Expand Down
8 changes: 4 additions & 4 deletions fair_async_rlock/tests/test_fair_async_rlock.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,15 +375,14 @@ async def task2():
t2_ac.set()
await asyncio.sleep(1.) # Let's ensure the lock is held for a bit


task1 = asyncio.create_task(task1())
task2 = asyncio.create_task(task2())
await asyncio.sleep(0.1) # Yield control to allow tasks to start
task2.cancel()
with pytest.raises(asyncio.CancelledError):
await task2
assert not t2_ac.is_set() # shouldn't acquire
t1_done.set() # Let T1 finish
assert not t2_ac.is_set() # shouldn't acquire
t1_done.set() # Let T1 finish
await task1 # Ensure task1 has a chance to release the lock
# Ensure that lock is not owned and queue is empty after cancellation
assert lock._owner is None
Expand Down Expand Up @@ -610,6 +609,7 @@ async def task4():
await asyncio.wait([t4], timeout=1)
assert task4_acquired.is_set()


@pytest.mark.asyncio
async def test_gh17_regression():
lock = FairAsyncRLock()
Expand Down Expand Up @@ -651,4 +651,4 @@ async def task3():
t2 = asyncio.create_task(task2())
t3 = asyncio.create_task(task3())

await asyncio.gather(t1, t2, t3)
await asyncio.gather(t1, t2, t3)

0 comments on commit 7ddf01d

Please sign in to comment.