Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
almarklein committed Dec 6, 2024
1 parent 15b1660 commit 6dbdc39
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
4 changes: 3 additions & 1 deletion rendercanvas/_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ async def _loop_task(self):
# Stop when there are no more canvases
break
elif self.__should_stop >= 2:
# force a stop without waiting for the canvases to close
# Force a stop without waiting for the canvases to close.
# We could call event.close() for the remaining canvases, but technically they have not closed.
# Since this case is considered a failure, better be honest than consistent, I think.
break
elif self.__should_stop:
# Close all remaining canvases. Loop will stop in a next iteration.
Expand Down
30 changes: 28 additions & 2 deletions tests/test_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,18 @@ class CanvasGroup(BaseCanvasGroup):
pass


class FakeEventEmitter:
is_closed = False

async def close(self):
self.is_closed = True


class FakeCanvas:
def __init__(self, refuse_close=False):
self.refuse_close = refuse_close
self.is_closed = False
self._events = FakeEventEmitter()

def _rc_gui_poll(self):
pass
Expand Down Expand Up @@ -156,8 +164,11 @@ def test_run_loop_and_close_canvases():
print(et)
assert 0.25 < et < 0.45

assert canvas1._events.is_closed
assert canvas2._events.is_closed


def test_run_loop_and_close_with_method():
def test_run_loop_and_close_by_loop_stop():
# Close, then wait at most one tick to close canvases, and another to conform close.
loop = AsyncioLoop()
group = CanvasGroup(loop)
Expand All @@ -177,6 +188,9 @@ def test_run_loop_and_close_with_method():
print(et)
assert 0.25 < et < 0.55

assert canvas1._events.is_closed
assert canvas2._events.is_closed


def test_run_loop_and_close_by_deletion():
# Make the canvases be deleted by the gc.
Expand All @@ -185,6 +199,8 @@ def test_run_loop_and_close_by_deletion():
group = CanvasGroup(loop)

canvases = [FakeCanvas() for _ in range(2)]
events1 = canvases[0]._events
events2 = canvases[1]._events
for canvas in canvases:
group._register_canvas(canvas, fake_task)
del canvas
Expand All @@ -199,6 +215,9 @@ def test_run_loop_and_close_by_deletion():
print(et)
assert 0.25 < et < 0.55

assert events1.is_closed
assert events2.is_closed


def test_run_loop_and_close_by_deletion_real():
# Stop by deleting canvases, with a real canvas.
Expand Down Expand Up @@ -246,6 +265,9 @@ def interrupt_soon():
print(et)
assert 0.25 < et < 0.55

assert canvas1._events.is_closed
assert canvas2._events.is_closed


def test_run_loop_and_interrupt_harder():
# In the next tick after the second interupt, it stops the loop without closing the canvases
Expand Down Expand Up @@ -277,9 +299,13 @@ def interrupt_soon():
print(et)
assert 0.6 < et < 0.75

# Now the close event is not send!
assert not canvas1._events.is_closed
assert not canvas2._events.is_closed


def test_loop_threaded():
t = threading.Thread(target=test_run_loop_and_close_with_method)
t = threading.Thread(target=test_run_loop_and_close_by_loop_stop)
t.start()
t.join()

Expand Down

0 comments on commit 6dbdc39

Please sign in to comment.