Skip to content

Commit

Permalink
tests: Ignore non-valloc stacks in --stacks test
Browse files Browse the repository at this point in the history
This fixes test flakiness caused by allocations made by the
interpreter's eval loop itself.

Signed-off-by: Matt Wozniski <[email protected]>
  • Loading branch information
godlygeek committed Sep 25, 2023
1 parent 7a851e8 commit c02b7e4
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions tests/test_pytest_memray.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import re
import xml.etree.ElementTree as ET
from types import SimpleNamespace
from unittest.mock import ANY
Expand All @@ -10,6 +11,25 @@
from pytest import ExitCode
from pytest import Pytester

from pytest_memray.marks import StackFrame


def extract_stacks(test_output: str) -> list[list[StackFrame]]:
ret: list[list[StackFrame]] = []
before_start = True
for line in test_output.splitlines():
if before_start:
if "List of allocations:" in line:
before_start = False
elif "allocated here" in line:
ret.append([])
elif (match := re.match(r"^ {8}([^:]+):(.*):(\d+)$", line)) is not None:
ret[-1].append(
StackFrame(function=match[1], filename=match[2], lineno=int(match[3]))
)

return ret


def test_help_message(pytester: Pytester) -> None:
result = pytester.runpytest("--help")
Expand Down Expand Up @@ -176,10 +196,11 @@ def test_foo():

assert result.ret == ExitCode.TESTS_FAILED

output = result.stdout.str()

assert "valloc:" in output
assert output.count("rec:") == min(num_stacks - 1, 10)
stacks = extract_stacks(result.stdout.str())
valloc_stacks = [stack for stack in stacks if stack[0].function == "valloc"]
(valloc_stack,) = valloc_stacks
num_rec_frames = sum(1 for frame in valloc_stack if frame.function == "rec")
assert num_rec_frames == min(num_stacks - 1, 10)


@pytest.mark.parametrize("native", [True, False])
Expand Down

0 comments on commit c02b7e4

Please sign in to comment.