Skip to content

Commit

Permalink
Add a test of GC on live backends
Browse files Browse the repository at this point in the history
  • Loading branch information
samschott committed Aug 7, 2023
1 parent 8bf3bb4 commit 9b3f80d
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions testbed/tests/test_window.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import gc
import io
import traceback
import weakref
from importlib import import_module
from pathlib import Path
from unittest.mock import Mock
Expand Down Expand Up @@ -183,6 +185,32 @@ async def test_secondary_window(app, second_window, second_window_probe):

assert second_window not in app.windows

async def test_secondary_window_cleanup(app_probe):
"""Memory for windows is cleaned up when windows are deleted."""
# Create and show a window with content. We can't use the second_window fixture
# because the fixture will retain a reference, preventing garbage collection.
second_window = toga.Window()
second_window.content = toga.Box()
second_window.show()
await app_probe.redraw("Secondary Window has been created")

# Retain a weak reference to the window to check garbage collection
window_ref = weakref.ref(second_window)
impl_ref = weakref.ref(second_window._impl)

second_window.close()
await app_probe.redraw("Secondary window has been closed")

# Clear the local reference to the window (which should be the last reference),
# and force a garbage collection pass. This should cause deletion of both the
# interface and impl of the window.
second_window = None
gc.collect()

# Assert that the weak references are now dead.
assert window_ref() is None
assert impl_ref() is None

@pytest.mark.parametrize(
"second_window_kwargs",
[dict(title="Secondary Window", position=(200, 300), size=(300, 200))],
Expand Down

0 comments on commit 9b3f80d

Please sign in to comment.