Skip to content

Commit

Permalink
fix: exclude name from CounterLock repr when name is empty (#498)
Browse files Browse the repository at this point in the history
* fix: exclude name from CounterLock repr when name is empty

* Update test_counter.py
  • Loading branch information
BobTheBuidler authored Dec 19, 2024
1 parent 3e02aca commit f2f4a76
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
6 changes: 5 additions & 1 deletion a_sync/primitives/locks/counter.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,11 @@ cdef class CounterLock(_DebugDaemonMixin):
"""
cdef dict[long long, Event] events = self._events
cdef dict[long long, Py_ssize_t] waiters = {v: len((<Event>events[v])._waiters) for v in self._heap}
return "<CounterLock name={} value={} waiters={}>".format(self.__name.decode("utf-8"), self._value, waiters)
cdef str name = self.__name.decode("utf-8")
if name:
return "<CounterLock name={} value={} waiters={}>".format(self.__name.decode("utf-8"), self._value, waiters)
else:
return "<CounterLock value={} waiters={}>".format(self._value, waiters)

cpdef bint is_ready(self, long long v):
"""A function that indicates whether the current counter value is greater than or equal to a given value."""
Expand Down
15 changes: 15 additions & 0 deletions tests/primitives/test_counter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,21 @@

@pytest.mark.asyncio_cooperative
async def test_counter_lock():
counter = CounterLock()
assert counter._name == ""
assert repr(counter) == "<CounterLock value=0 waiters={}>"
coro = counter.wait_for(1)
task = asyncio.create_task(coro)
await asyncio.sleep(0)
assert repr(counter) == "<CounterLock value=0 waiters={1: 1}>"
counter.set(1)
await asyncio.sleep(0)
assert task.done() and task.result() is True
assert repr(counter) == "<CounterLock value=1 waiters={}>"


@pytest.mark.asyncio_cooperative
async def test_counter_lock_with_name():
counter = CounterLock(name="test")
assert counter._name == "test"
assert repr(counter) == "<CounterLock name=test value=0 waiters={}>"
Expand Down

0 comments on commit f2f4a76

Please sign in to comment.