Skip to content

Commit b047815

Browse files
committed
fixup! Add new commands to memray attach
Signed-off-by: Pablo Galindo <[email protected]>
1 parent fa9b5d4 commit b047815

File tree

1 file changed

+48
-6
lines changed

1 file changed

+48
-6
lines changed

tests/integration/test_attach.py

+48-6
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def baz():
5252
"""
5353

5454

55-
def generate_command(method, output, aggregate):
55+
def generate_command(method, output, *args):
5656
cmd = [
5757
sys.executable,
5858
"-m",
@@ -66,17 +66,19 @@ def generate_command(method, output, aggregate):
6666
str(output),
6767
]
6868

69-
if aggregate:
70-
cmd.append("--aggregate")
69+
if args:
70+
cmd.extend(args)
7171

7272
return cmd
7373

7474

75-
def run_process(cmd):
75+
def run_process(cmd, wait_for_stderr=False):
76+
process_stderr = ""
7677
tracked_process = subprocess.Popen(
7778
[sys.executable, "-uc", PROGRAM],
7879
stdin=subprocess.PIPE,
7980
stdout=subprocess.PIPE,
81+
stderr=subprocess.PIPE,
8082
text=True,
8183
)
8284

@@ -100,12 +102,17 @@ def run_process(cmd):
100102
raise
101103
finally:
102104
tracked_process.stdin.write("1\n")
105+
if wait_for_stderr:
106+
process_stderr = tracked_process.stderr.readline()
107+
while "WARNING" not in process_stderr:
108+
process_stderr = tracked_process.stderr.readline()
103109
tracked_process.stdin.close()
104110
tracked_process.wait()
105111

106112
# THEN
107113
assert "" == tracked_process.stdout.read()
108114
assert tracked_process.returncode == 0
115+
return process_stderr
109116

110117

111118
def get_call_stack(allocation):
@@ -127,7 +134,7 @@ def test_basic_attach(tmp_path, method):
127134

128135
# GIVEN
129136
output = tmp_path / "test.bin"
130-
attach_cmd = generate_command(method, output, aggregate=False)
137+
attach_cmd = generate_command(method, output)
131138

132139
# WHEN
133140
run_process(attach_cmd)
@@ -145,7 +152,7 @@ def test_aggregated_attach(tmp_path, method):
145152

146153
# GIVEN
147154
output = tmp_path / "test.bin"
148-
attach_cmd = generate_command(method, output, aggregate=True)
155+
attach_cmd = generate_command(method, output, "--aggregate")
149156

150157
# WHEN
151158
run_process(attach_cmd)
@@ -160,3 +167,38 @@ def test_aggregated_attach(tmp_path, method):
160167

161168
(valloc,) = get_relevant_vallocs(reader.get_high_watermark_allocation_records())
162169
assert get_call_stack(valloc) == ["valloc", "baz", "bar", "foo", "<module>"]
170+
171+
172+
@pytest.mark.parametrize("method", ["lldb", "gdb"])
173+
def test_attach_heap(tmp_path, method):
174+
if not debugger_available(method):
175+
pytest.skip(f"a supported {method} debugger isn't installed")
176+
177+
# GIVEN
178+
limit = 50 * 1024 * 1024
179+
output = tmp_path / "test.bin"
180+
attach_cmd = generate_command(method, output, "--heap-limit", str(limit))
181+
182+
# WHEN
183+
process_stderr = run_process(attach_cmd, wait_for_stderr=True)
184+
185+
# THEN
186+
assert "memray: Deactivating tracking: heap size has reached" in process_stderr
187+
assert f" the limit was {limit}" in process_stderr
188+
189+
190+
@pytest.mark.parametrize("method", ["lldb", "gdb"])
191+
def test_attach_time(tmp_path, method):
192+
if not debugger_available(method):
193+
pytest.skip(f"a supported {method} debugger isn't installed")
194+
195+
# GIVEN
196+
limit = 50 * 1024 * 1024
197+
output = tmp_path / "test.bin"
198+
attach_cmd = generate_command(method, output, "--duration", "1")
199+
200+
# WHEN
201+
process_stderr = run_process(attach_cmd, wait_for_stderr=True)
202+
203+
# THEN
204+
assert "memray: Deactivating tracking: 1 seconds have elapsed" in process_stderr

0 commit comments

Comments
 (0)