Skip to content

Commit

Permalink
work harder to collect garbage during test runs
Browse files Browse the repository at this point in the history
  • Loading branch information
njsmith committed Jun 15, 2017
1 parent 90833eb commit 7388a97
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
9 changes: 6 additions & 3 deletions trio/_core/tests/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import pytest
import attr

from .tutil import check_sequence_matches
from .tutil import check_sequence_matches, gc_collect_harder
from ...testing import (
wait_all_tasks_blocked, Sequencer, assert_yields,
)
Expand Down Expand Up @@ -41,7 +41,8 @@ def ignore_coroutine_never_awaited_warnings():
finally:
# Make sure to trigger any coroutine __del__ methods now, before
# we leave the context manager.
gc.collect()
gc_collect_harder()


def test_basic():
async def trivial(x):
Expand Down Expand Up @@ -883,7 +884,7 @@ async def main():
# Because this crashes, various __del__ methods print complaints on
# stderr. Make sure that they get run now, so the output is attached to
# this test.
gc.collect()
gc_collect_harder()


def test_error_in_run_loop():
Expand Down Expand Up @@ -1490,6 +1491,8 @@ async def f(): # pragma: no cover
bad_call(len, [1, 2, 3])
assert "appears to be synchronous" in str(excinfo.value)

# Make sure no references are kept around to keep anything alive
del excinfo

def test_calling_asyncio_function_gives_nice_error():
async def misguided():
Expand Down
15 changes: 14 additions & 1 deletion trio/_core/tests/tutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,26 @@

import pytest

import gc

# See trio/tests/conftest.py for the other half of this
slow = pytest.mark.skipif(
not pytest.config.getoption("--run-slow", True),
reason="use --run-slow to run slow tests",
)

from ... import _core
def gc_collect_harder():
# In the test suite we sometimes want to call gc.collect() to make sure
# that any objects with noisy __del__ methods (e.g. unawaited coroutines)
# get collected before we continue, so their noise doesn't leak into
# unrelated tests.
#
# On PyPy, coroutine objects (for example) can survive at least 1 round of
# garbage collection, because executing their __del__ method to print the
# warning can cause them to be resurrected. So we call collect a few times
# to make sure.
for _ in range(4):
gc.collect()

# template is like:
# [1, {2.1, 2.2}, 3] -> matches [1, 2.1, 3] or [1, 2.2, 3]
Expand Down

0 comments on commit 7388a97

Please sign in to comment.