Skip to content

Commit

Permalink
Merge pull request #119 from Jylpah/dev-1.0
Browse files Browse the repository at this point in the history
version 1.2.2
  • Loading branch information
Jylpah authored Jan 7, 2024
2 parents ab42ae0 + 5d6e83d commit e4e9f4a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "pyutils"
version = "1.2.1"
version = "1.2.2"
authors = [{ name = "Jylpah", email = "[email protected]" }]
description = "Misc Python utils and classes"
readme = "README.md"
Expand Down
6 changes: 6 additions & 0 deletions src/pyutils/iterablequeue.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ def __init__(self, count_items: bool = True, **kwargs):
@property
def is_filled(self) -> bool:
return self._filled.is_set()

@property
def is_done(self) -> bool:
return self.is_filled and self.empty() and not self.has_wip

@property
def maxsize(self) -> int:
Expand All @@ -62,6 +66,8 @@ def _maxsize(self) -> int:
def full(self) -> bool:
return self._Q.full()



def check_done(self) -> bool:
if self.is_filled and self.empty() and not self.has_wip:
self._done.set()
Expand Down
16 changes: 11 additions & 5 deletions tests/test_iterablequeue.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ async def test_1_put_get_async(test_interablequeue_int: IterableQueue[int]):
assert Q.qsize() == QSIZE - 1, f"qsize() returned {Q.qsize()}, should be {QSIZE-1}"
try:
await Q.put(1)
assert False, "Queue is done and put() should raise an exception"
assert False, "Queue is filled and put() should raise an exception"
except QueueDone:
pass # Queue is done and put() should raise an exception

assert not Q.is_done, "is_done returned True even queue is not finished"
consumer: Task = create_task(_consumer_int(Q))
try:
async with timeout(5):
Expand Down Expand Up @@ -132,6 +132,7 @@ async def test_3_multiple_producers(test_interablequeue_int: IterableQueue[int])
for _ in range(THREADS):
workers.append(create_task(_producer_int(Q, N, finish=True, wait=0.05)))
try:
assert not Q.is_done, "is_done returned True even queue is not finished"
async with timeout(10):
async for _ in Q:
pass
Expand Down Expand Up @@ -181,8 +182,10 @@ async def test_5_empty_join(test_interablequeue_int: IterableQueue[int]):
"""Test for await join when an empty queue is finished"""
Q = test_interablequeue_int
producer: Task = create_task(_producer_int(Q, n=0, finish=True, wait=2))
assert not Q.is_done, "is_done returned True even queue is not finished"
consumer: Task = create_task(_consumer_int(Q))
try:

async with timeout(3):
await Q.join()
assert (
Expand Down Expand Up @@ -215,9 +218,11 @@ async def test_6_finish_full_queue(test_interablequeue_int: IterableQueue[int]):
assert (
Q.empty()
), f"Queue should be empty: qsize={Q._Q.qsize()}: {Q._Q.get_nowait()}, {Q._Q.get_nowait()}"
assert Q.is_done, "Queue is not done"
except TimeoutError:
assert False, "await IterableQueue.join() failed with an empty queue finished"
await sleep(0.1)
assert Q.is_done, "Queue is not done"
producer.cancel()


Expand All @@ -232,9 +237,10 @@ async def test_7_aiter(test_interablequeue_int: IterableQueue[int]):
await sleep(0.5)
async for i in Q:
assert i >= 0, "Did not receive an int"
assert (
True
), "Queue is done after 3 secs and the join() should finish before timeout(5)"
assert Q.is_done, "Queue is not done"
# assert (
# True
# ), "Queue is done after 3 secs and the join() should finish before timeout(5)"
except TimeoutError:
assert False, "await IterableQueue.join() failed with an empty queue finished"

Expand Down

0 comments on commit e4e9f4a

Please sign in to comment.