Skip to content

Commit

Permalink
Merge pull request #7542 from drew2a/fix/call_gc
Browse files Browse the repository at this point in the history
Add `ensure_gc` for each test call
  • Loading branch information
drew2a authored Jul 13, 2023
2 parents fd8e050 + 0c34e78 commit 701fd9d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/tribler/core/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import gc
import logging
import platform
import sys
Expand Down Expand Up @@ -64,6 +65,29 @@ def pytest_runtest_protocol(item: Function, log=True, nextitem=None):
yield



@pytest.fixture(autouse=True)
def ensure_gc():
""" Ensure that the garbage collector runs after each test.
This is critical for test stability as we use Libtorrent and need to ensure all its destructors are called. """
# For this fixture, it is necessary for it to be called as late as possible within the current test's scope.
# Therefore it should be placed at the first place in the "function" scope.
# If there are two or more autouse fixtures within this scope, the order should be explicitly set through using
# this fixture as a dependency.
# See the discussion in https://github.com/Tribler/tribler/pull/7542 for more information.

yield
# Without "yield" the fixture triggers the garbage collection at the beginning of the (next) test.
# For that reason, the errors triggered during the garbage collection phase will take place not in the erroneous
# test but in the randomly scheduled next test. Usually, these errors are silently suppressed, as any exception in
# __del__ methods is silently suppressed, but they still can somehow affect the test.
#
# By adding the yield we move the garbage collection phase to the end of the current test, to not affect the next
# test.

gc.collect()


@pytest.fixture
def free_port():
return default_network_utils.get_random_free_port(start=1024, stop=50000)
Expand Down
23 changes: 23 additions & 0 deletions src/tribler/gui/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import gc
import logging
import time

Expand Down Expand Up @@ -61,3 +62,25 @@ def pytest_runtest_protocol(item, log=True, nextitem=None):
total = time.time() - pytest_start_time
if enable_extended_logging:
print(f' in {duration:.3f}s ({total:.1f}s in total)', end='')


@pytest.fixture(autouse=True)
def ensure_gc():
""" Ensure that the garbage collector runs after each test.
This is critical for test stability as we use Libtorrent and need to ensure all its destructors are called. """
# For this fixture, it is necessary for it to be called as late as possible within the current test's scope.
# Therefore it should be placed at the first place in the "function" scope.
# If there are two or more autouse fixtures within this scope, the order should be explicitly set through using
# this fixture as a dependency.
# See the discussion in https://github.com/Tribler/tribler/pull/7542 for more information.

yield
# Without "yield" the fixture triggers the garbage collection at the beginning of the (next) test.
# For that reason, the errors triggered during the garbage collection phase will take place not in the erroneous
# test but in the randomly scheduled next test. Usually, these errors are silently suppressed, as any exception in
# __del__ methods is silently suppressed, but they still can somehow affect the test.
#
# By adding the yield we move the garbage collection phase to the end of the current test, to not affect the next
# test.

gc.collect()

0 comments on commit 701fd9d

Please sign in to comment.