Skip to content

Commit

Permalink
Fix a flaky test
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
godlygeek authored and pablogsal committed Mar 6, 2024
1 parent 784ddf1 commit 9adee4a
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions tests/test_pytest_memray.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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()
Expand Down

0 comments on commit 9adee4a

Please sign in to comment.