From 19006b1c175bd0508d323f4d245ee867f0a1cf77 Mon Sep 17 00:00:00 2001 From: joshuaalbert Date: Thu, 17 Oct 2024 00:04:55 +0200 Subject: [PATCH] * Do #23 --- fair_async_rlock/fair_async_rlock.py | 3 +++ fair_async_rlock/tests/test_fair_async_rlock.py | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/fair_async_rlock/fair_async_rlock.py b/fair_async_rlock/fair_async_rlock.py index 46067f2..931ca5c 100644 --- a/fair_async_rlock/fair_async_rlock.py +++ b/fair_async_rlock/fair_async_rlock.py @@ -22,6 +22,9 @@ def is_owner(self, task=None): task = asyncio.current_task() return self._owner == task + def locked(self) -> bool: + return self._owner is not None + async def acquire(self): """Acquire the lock.""" me = asyncio.current_task() diff --git a/fair_async_rlock/tests/test_fair_async_rlock.py b/fair_async_rlock/tests/test_fair_async_rlock.py index 9abbb7f..89a220c 100644 --- a/fair_async_rlock/tests/test_fair_async_rlock.py +++ b/fair_async_rlock/tests/test_fair_async_rlock.py @@ -652,3 +652,13 @@ async def task3(): t3 = asyncio.create_task(task3()) await asyncio.gather(t1, t2, t3) + +@pytest.mark.asyncio +def test_locked(): + lock = FairAsyncRLock() + assert not lock.locked() + async def task(): + async with lock: + assert lock.locked() + asyncio.run(task()) + assert not lock.locked() \ No newline at end of file