Skip to content

Commit

Permalink
Merge pull request #7849 from drew2a/fix/7753
Browse files Browse the repository at this point in the history
Update pytest configuration to include the `--forced-gc` option
  • Loading branch information
drew2a authored Jan 24, 2024
2 parents 998b445 + 1f68229 commit 60d66f3
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/vars/pytest.env
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
PYTEST_ARGUMENTS='--randomly-seed=1 --disable-warnings --verbose --durations=3 --showlocals'

PYTEST_CORE_ARGUMENTS='./src/tribler/core ${PYTEST_ARGUMENTS}'
PYTEST_CORE_ARGUMENTS='./src/tribler/core ${PYTEST_ARGUMENTS} --forced-gc'
PYTEST_CORE_ARGUMENTS_WIN='${PYTEST_CORE_ARGUMENTS}'
PYTEST_CORE_ARGUMENTS_LINUX='${PYTEST_CORE_ARGUMENTS} --looptime'
PYTEST_CORE_ARGUMENTS_MAC='${PYTEST_CORE_ARGUMENTS} --looptime'
Expand Down
10 changes: 7 additions & 3 deletions src/tribler/core/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ def pytest_configure(config):
logging.getLogger('faker.factory').propagate = False


def pytest_addoption(parser):
parser.addoption("--forced-gc", action="store_true", help="Enable forced garbage collection")


@pytest.hookimpl
def pytest_cmdline_main(config: Config):
""" Enable extended logging if the verbose option is used """
Expand Down Expand Up @@ -70,7 +74,7 @@ def pytest_runtest_protocol(item: Function, log=True, nextitem=None):


@pytest.fixture(autouse=True)
def ensure_gc():
def ensure_gc(request):
""" 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.
Expand All @@ -87,8 +91,8 @@ def ensure_gc():
#
# 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()
if request.config.getoption("--forced-gc"):
gc.collect()


@pytest.fixture
Expand Down
23 changes: 0 additions & 23 deletions src/tribler/gui/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import gc
import logging
import time

Expand Down Expand Up @@ -62,25 +61,3 @@ 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 60d66f3

Please sign in to comment.