From 9adee4a12f977c79865b3652ad51218080e4f696 Mon Sep 17 00:00:00 2001 From: Matt Wozniski Date: Wed, 6 Mar 2024 18:05:35 -0500 Subject: [PATCH] Fix a flaky test When this test was selected to be run with `pytest -k` or `--lf`, it would fail because of allocations made by the platform threading implementation: - 8.0MiB allocated here: __pthread_create_2_1 PyThread_start_new_thread ... When it was run as part of the larger test suite, it would pass, because the test suite had already started other threads, and paid that cost in a place where we weren't looking for leaks. Since we can't tell pthreads not to cache the pthread_t for later reuse, the best we can do here is just raise all of our thresholds for this test by a constant factor, so that the ones that are supposed to fail will fail with or without an extra 8 MB allocation, and the ones that are supposed to pass will pass with or without it as well. This commit scales all of the sizes and limits used by this test up by a factor of 4096 (and drops one of the test vectors, since we don't need to leak 400 MB just to prove a point when we already know we fail after leaking 40 MB). Signed-off-by: Matt Wozniski --- tests/test_pytest_memray.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/tests/test_pytest_memray.py b/tests/test_pytest_memray.py index 0ac4eec..7eb4a0b 100644 --- a/tests/test_pytest_memray.py +++ b/tests/test_pytest_memray.py @@ -689,10 +689,9 @@ def test_memory_alloc_fails(): @pytest.mark.parametrize( "size, outcome", [ - (1, ExitCode.OK), - (1024 * 1 / 10, ExitCode.OK), - (1024 * 1, ExitCode.TESTS_FAILED), - (1024 * 10, ExitCode.TESTS_FAILED), + (4 * 1024, ExitCode.OK), + (0.4 * 1024 * 1024, ExitCode.OK), + (4 * 1024 * 1024, ExitCode.TESTS_FAILED), ], ) def test_leak_marker_in_a_thread( @@ -708,7 +707,7 @@ def allocating_func(): for _ in range(10): allocator.valloc({size}) # No free call here - @pytest.mark.limit_leaks("5KB") + @pytest.mark.limit_leaks("20MB") def test_memory_alloc_fails(): t = threading.Thread(target=allocating_func) t.start()