@@ -52,7 +52,7 @@ def baz():
52
52
"""
53
53
54
54
55
- def generate_command (method , output , aggregate ):
55
+ def generate_command (method , output , * args ):
56
56
cmd = [
57
57
sys .executable ,
58
58
"-m" ,
@@ -66,17 +66,19 @@ def generate_command(method, output, aggregate):
66
66
str (output ),
67
67
]
68
68
69
- if aggregate :
70
- cmd .append ( "--aggregate" )
69
+ if args :
70
+ cmd .extend ( args )
71
71
72
72
return cmd
73
73
74
74
75
- def run_process (cmd ):
75
+ def run_process (cmd , wait_for_stderr = False ):
76
+ process_stderr = ""
76
77
tracked_process = subprocess .Popen (
77
78
[sys .executable , "-uc" , PROGRAM ],
78
79
stdin = subprocess .PIPE ,
79
80
stdout = subprocess .PIPE ,
81
+ stderr = subprocess .PIPE ,
80
82
text = True ,
81
83
)
82
84
@@ -100,12 +102,17 @@ def run_process(cmd):
100
102
raise
101
103
finally :
102
104
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 ()
103
109
tracked_process .stdin .close ()
104
110
tracked_process .wait ()
105
111
106
112
# THEN
107
113
assert "" == tracked_process .stdout .read ()
108
114
assert tracked_process .returncode == 0
115
+ return process_stderr
109
116
110
117
111
118
def get_call_stack (allocation ):
@@ -127,7 +134,7 @@ def test_basic_attach(tmp_path, method):
127
134
128
135
# GIVEN
129
136
output = tmp_path / "test.bin"
130
- attach_cmd = generate_command (method , output , aggregate = False )
137
+ attach_cmd = generate_command (method , output )
131
138
132
139
# WHEN
133
140
run_process (attach_cmd )
@@ -145,7 +152,7 @@ def test_aggregated_attach(tmp_path, method):
145
152
146
153
# GIVEN
147
154
output = tmp_path / "test.bin"
148
- attach_cmd = generate_command (method , output , aggregate = True )
155
+ attach_cmd = generate_command (method , output , "-- aggregate" )
149
156
150
157
# WHEN
151
158
run_process (attach_cmd )
@@ -160,3 +167,38 @@ def test_aggregated_attach(tmp_path, method):
160
167
161
168
(valloc ,) = get_relevant_vallocs (reader .get_high_watermark_allocation_records ())
162
169
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