1313
1414import zipfile , unittest
1515import time
16+ import tracemalloc
1617import sys
1718import unittest .mock as mock
1819
@@ -99,6 +100,9 @@ def setUp(self):
99100 # It will contain enough copies of self.data to reach about 8 GiB.
100101 self .datacount = 8 * 1024 ** 3 // len (self .data )
101102
103+ # memory usage should not exceed 10 MiB
104+ self .allowed_memory = 10 * 1024 ** 2
105+
102106 def _write_large_file (self , fh ):
103107 next_time = time .monotonic () + _PRINT_WORKING_MSG_INTERVAL
104108 for num in range (self .datacount ):
@@ -117,8 +121,12 @@ def test_strip_removed_large_file(self):
117121 # Try the temp file. If we do TESTFN2, then it hogs
118122 # gigabytes of disk space for the duration of the test.
119123 with TemporaryFile () as f :
124+ tracemalloc .start ()
120125 self ._test_strip_removed_large_file (f )
121126 self .assertFalse (f .closed )
127+ current , peak = tracemalloc .get_traced_memory ()
128+ tracemalloc .stop ()
129+ self .assertLess (peak , self .allowed_memory )
122130
123131 def _test_strip_removed_large_file (self , f ):
124132 file = 'file.txt'
@@ -140,8 +148,12 @@ def test_strip_removed_file_before_large_file(self):
140148 # Try the temp file. If we do TESTFN2, then it hogs
141149 # gigabytes of disk space for the duration of the test.
142150 with TemporaryFile () as f :
151+ tracemalloc .start ()
143152 self ._test_strip_removed_file_before_large_file (f )
144153 self .assertFalse (f .closed )
154+ current , peak = tracemalloc .get_traced_memory ()
155+ tracemalloc .stop ()
156+ self .assertLess (peak , self .allowed_memory )
145157
146158 def _test_strip_removed_file_before_large_file (self , f ):
147159 file = 'file.txt'
@@ -163,8 +175,12 @@ def test_strip_removed_large_file_with_dd(self):
163175 # Try the temp file. If we do TESTFN2, then it hogs
164176 # gigabytes of disk space for the duration of the test.
165177 with TemporaryFile () as f :
178+ tracemalloc .start ()
166179 self ._test_strip_removed_large_file_with_dd (f )
167180 self .assertFalse (f .closed )
181+ current , peak = tracemalloc .get_traced_memory ()
182+ tracemalloc .stop ()
183+ self .assertLess (peak , self .allowed_memory )
168184
169185 def _test_strip_removed_large_file_with_dd (self , f ):
170186 file = 'file.txt'
@@ -190,8 +206,12 @@ def test_strip_removed_large_file_with_dd_no_sig(self):
190206 # Try the temp file. If we do TESTFN2, then it hogs
191207 # gigabytes of disk space for the duration of the test.
192208 with TemporaryFile () as f :
209+ tracemalloc .start ()
193210 self ._test_strip_removed_large_file_with_dd_no_sig (f )
194211 self .assertFalse (f .closed )
212+ current , peak = tracemalloc .get_traced_memory ()
213+ tracemalloc .stop ()
214+ self .assertLess (peak , self .allowed_memory )
195215
196216 def _test_strip_removed_large_file_with_dd_no_sig (self , f ):
197217 # Reduce data to 400 MiB for this test, as it's especially slow...
0 commit comments