Skip to content

Commit e9bd308

Browse files
committed
fix: make test better
Signed-off-by: yihong0618 <zouzou0208@gmail.com>
1 parent 08de5c3 commit e9bd308

1 file changed

Lines changed: 22 additions & 25 deletions

File tree

Lib/test/test_profiling/test_sampling_profiler.py

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,13 @@
2222
from profiling.sampling.gecko_collector import GeckoCollector
2323

2424
from test.support.os_helper import unlink
25-
from test.support import force_not_colorized_test_class, SHORT_TIMEOUT
25+
from test.support import (
26+
force_not_colorized_test_class,
27+
SHORT_TIMEOUT,
28+
script_helper,
29+
os_helper,
30+
SuppressCrashReport,
31+
)
2632
from test.support.socket_helper import find_unused_port
2733
from test.support import requires_subprocess, is_emscripten
2834
from test.support import captured_stdout, captured_stderr
@@ -3011,33 +3017,24 @@ def worker(x):
30113017
results = list(executor.map(worker, [1, 2, 3]))
30123018
print(f"Results: {results}")
30133019
'''
3014-
with tempfile.NamedTemporaryFile(
3015-
mode='w', suffix='.py', delete=False
3016-
) as script_file:
3017-
script_file.write(test_script)
3018-
script_file.flush()
3019-
script_name = script_file.name
3020-
3021-
self.addCleanup(os.unlink, script_name)
3022-
3023-
result = subprocess.run(
3024-
[
3025-
sys.executable,
3026-
"-m", "profiling.sampling.sample",
3027-
"-d", "1",
3028-
"-i", "100000",
3029-
script_name,
3030-
],
3031-
capture_output=True,
3032-
text=True,
3033-
timeout=10
3034-
)
3020+
# Use test helpers to spawn a real Python subprocess so that
3021+
# PermissionError (if any) is emitted by the child on stderr and
3022+
# can be handled consistently with other tests.
3023+
with os_helper.temp_dir() as temp_dir:
3024+
script = script_helper.make_script(
3025+
temp_dir, 'test_process_pool_executor_pickle', test_script
3026+
)
3027+
with SuppressCrashReport():
3028+
with script_helper.spawn_python(script, stderr=subprocess.PIPE) as proc:
3029+
proc.wait()
3030+
stdout = proc.stdout.read()
3031+
stderr = proc.stderr.read()
30353032

3036-
if "PermissionError" in result.stderr:
3033+
if b"PermissionError" in stderr:
30373034
self.skipTest("Insufficient permissions for remote profiling")
30383035

3039-
self.assertIn("Results: [2, 4, 6]", result.stdout)
3040-
self.assertNotIn("Can't pickle", result.stderr)
3036+
self.assertIn(b"Results: [2, 4, 6]", stdout)
3037+
self.assertNotIn(b"Can't pickle", stderr)
30413038

30423039

30433040
if __name__ == "__main__":

0 commit comments

Comments
 (0)