Skip to content

Commit 8ed9d82

Browse files
DinoVmeta-codesync[bot]
authored andcommitted
Back out "Move some tests away from using subprocess.run"
Summary: We've found a better way to deal with this than using multiprocessing, and the multiprocessing version is still failing when we have binary incompatibilities between the platform runtime and the bundled runtime. This switches back to subprocess.run with the new `subprocess_env`. Original Phabricator Diff: D90211488 Reviewed By: czardoz Differential Revision: D91907006 fbshipit-source-id: 0f7b162bdebad368126bbcc6386f60677fdd907d
1 parent 4fbe186 commit 8ed9d82

2 files changed

Lines changed: 159 additions & 125 deletions

File tree

cinderx/PythonLib/test_cinderx/test_jit_disable.py

Lines changed: 129 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
# pyre-unsafe
44

5-
import multiprocessing
5+
import subprocess
66
import sys
7+
import tempfile
8+
import textwrap
79
import unittest
810
from pathlib import Path
911

@@ -19,7 +21,7 @@
1921
lazy_compile,
2022
pause as pause_jit,
2123
)
22-
from cinderx.test_support import passUnless
24+
from cinderx.test_support import passUnless, subprocess_env
2325

2426

2527
@passUnless(is_jit_enabled(), "Tests functionality on the JIT")
@@ -204,162 +206,183 @@ def foo(a, b):
204206
# detection.
205207
force_uncompile(foo)
206208

207-
@staticmethod
208-
def compile_no_config_test() -> None:
209-
import cinderx.jit
210-
211-
def inc(x):
212-
return x + 1
213-
214-
assert not cinderx.jit.is_jit_compiled(inc)
215-
cinderx.jit.force_compile(inc)
216-
assert cinderx.jit.is_jit_compiled(inc)
217-
218209
def test_compile_no_config(self) -> None:
219210
"""
220211
Test how code behaves when it forces compilation without any other
221212
configuration or options enabled.
222213
"""
223214

224-
p = multiprocessing.Process(target=DisableEnableTests.compile_no_config_test)
225-
p.start()
226-
p.join()
227-
self.assertEqual(p.exitcode, 0)
215+
with tempfile.TemporaryDirectory() as tmp_dir:
216+
code = textwrap.dedent("""
217+
import cinderx.jit
228218
229-
@staticmethod
230-
def auto_test() -> None:
231-
import cinderx.jit
219+
def inc(x):
220+
return x + 1
232221
233-
def predefined(x):
234-
return x + x
222+
assert not cinderx.jit.is_jit_compiled(inc)
223+
cinderx.jit.force_compile(inc)
224+
assert cinderx.jit.is_jit_compiled(inc)
225+
""")
235226

236-
cinderx.jit.auto()
227+
test_file = Path(tmp_dir) / "mod.py"
228+
test_file.write_text(code)
237229

238-
def inc(x):
239-
return x + 1
240-
241-
assert not cinderx.jit.is_jit_compiled(inc)
242-
for i in range(1000):
243-
inc(i)
244-
assert not cinderx.jit.is_jit_compiled(inc)
245-
246-
inc(1001)
247-
assert cinderx.jit.is_jit_compiled(inc)
230+
subprocess.run(
231+
[sys.executable, str(test_file)],
232+
check=True,
233+
env=subprocess_env(),
234+
)
248235

249236
def test_auto(self) -> None:
250237
"""
251238
Basic test for cinderx.jit.auto().
252239
"""
253240

254-
p = multiprocessing.Process(target=DisableEnableTests.auto_test)
255-
p.start()
256-
p.join()
257-
self.assertEqual(p.exitcode, 0)
241+
with tempfile.TemporaryDirectory() as tmp_dir:
242+
code = textwrap.dedent("""
243+
import cinderx.jit
244+
245+
def predefined(x):
246+
return x + x
258247
259-
@staticmethod
260-
def auto_predefined_test() -> None:
261-
import cinderx.jit
248+
cinderx.jit.auto()
262249
263-
def predefined(x):
264-
return x + x
250+
def inc(x):
251+
return x + 1
265252
266-
cinderx.jit.auto()
253+
assert not cinderx.jit.is_jit_compiled(inc)
254+
for i in range(1000):
255+
inc(i)
256+
assert not cinderx.jit.is_jit_compiled(inc)
267257
268-
assert not cinderx.jit.is_jit_compiled(predefined)
269-
for i in range(1000):
270-
predefined(i)
271-
assert not cinderx.jit.is_jit_compiled(predefined)
258+
inc(1001)
259+
assert cinderx.jit.is_jit_compiled(inc)
260+
""")
272261

273-
predefined(1001)
274-
assert cinderx.jit.is_jit_compiled(predefined)
262+
test_file = Path(tmp_dir) / "mod.py"
263+
test_file.write_text(code)
264+
265+
subprocess.run(
266+
[sys.executable, str(test_file)],
267+
check=True,
268+
env=subprocess_env(),
269+
)
275270

276271
def test_auto_predefined(self) -> None:
277272
"""
278273
Test that cinderx.jit.auto() works for functions that were defined
279274
before it was called.
280275
"""
281276

282-
p = multiprocessing.Process(target=DisableEnableTests.auto_predefined_test)
283-
p.start()
284-
p.join()
285-
self.assertEqual(p.exitcode, 0)
286-
287-
@staticmethod
288-
def compile_after_n_calls_test() -> None:
289-
import cinderx.jit
290-
291-
cinderx.jit.compile_after_n_calls(2)
277+
with tempfile.TemporaryDirectory() as tmp_dir:
278+
code = textwrap.dedent("""
279+
import cinderx.jit
292280
293-
def inc(x):
294-
return x + 1
281+
def predefined(x):
282+
return x + x
295283
296-
assert not cinderx.jit.is_jit_compiled(inc)
297-
inc(1)
298-
inc(2)
299-
assert not cinderx.jit.is_jit_compiled(inc)
284+
cinderx.jit.auto()
300285
301-
inc(3)
302-
assert cinderx.jit.is_jit_compiled(inc)
286+
assert not cinderx.jit.is_jit_compiled(predefined)
287+
for i in range(1000):
288+
predefined(i)
289+
assert not cinderx.jit.is_jit_compiled(predefined)
303290
304-
# Change the setting and see it takes affect.
291+
predefined(1001)
292+
assert cinderx.jit.is_jit_compiled(predefined)
293+
""")
305294

306-
cinderx.jit.compile_after_n_calls(5)
295+
test_file = Path(tmp_dir) / "mod.py"
296+
test_file.write_text(code)
307297

308-
def dec(x):
309-
return x - 1
310-
311-
assert not cinderx.jit.is_jit_compiled(dec)
312-
dec(1)
313-
dec(2)
314-
dec(3)
315-
dec(4)
316-
dec(5)
317-
assert not cinderx.jit.is_jit_compiled(dec)
318-
319-
dec(6)
320-
assert cinderx.jit.is_jit_compiled(dec)
298+
subprocess.run(
299+
[sys.executable, str(test_file)],
300+
check=True,
301+
env=subprocess_env(),
302+
)
321303

322304
def test_compile_after_n_calls(self) -> None:
323305
"""
324306
Basic test for cinderx.jit.compile_after_n_calls().
325307
"""
326308

327-
p = multiprocessing.Process(
328-
target=DisableEnableTests.compile_after_n_calls_test
329-
)
330-
p.start()
331-
p.join()
332-
self.assertEqual(p.exitcode, 0)
309+
with tempfile.TemporaryDirectory() as tmp_dir:
310+
code = textwrap.dedent("""
311+
import cinderx.jit
312+
313+
cinderx.jit.compile_after_n_calls(2)
333314
334-
@staticmethod
335-
def compile_after_n_calls_predefined_test() -> None:
336-
import cinderx.jit
315+
def inc(x):
316+
return x + 1
337317
338-
def predefined(x):
339-
return x + x
318+
assert not cinderx.jit.is_jit_compiled(inc)
319+
inc(1)
320+
inc(2)
321+
assert not cinderx.jit.is_jit_compiled(inc)
340322
341-
cinderx.jit.compile_after_n_calls(2)
323+
inc(3)
324+
assert cinderx.jit.is_jit_compiled(inc)
342325
343-
assert not cinderx.jit.is_jit_compiled(predefined)
344-
predefined(1)
345-
predefined(2)
346-
assert not cinderx.jit.is_jit_compiled(predefined)
326+
# Change the setting and see it takes affect.
347327
348-
predefined(3)
349-
assert cinderx.jit.is_jit_compiled(predefined)
328+
cinderx.jit.compile_after_n_calls(5)
329+
330+
def dec(x):
331+
return x - 1
332+
333+
assert not cinderx.jit.is_jit_compiled(dec)
334+
dec(1)
335+
dec(2)
336+
dec(3)
337+
dec(4)
338+
dec(5)
339+
assert not cinderx.jit.is_jit_compiled(dec)
340+
341+
dec(6)
342+
assert cinderx.jit.is_jit_compiled(dec)
343+
""")
344+
345+
test_file = Path(tmp_dir) / "mod.py"
346+
test_file.write_text(code)
347+
348+
subprocess.run(
349+
[sys.executable, str(test_file)],
350+
check=True,
351+
env=subprocess_env(),
352+
)
350353

351354
def test_compile_after_n_calls_predefined(self) -> None:
352355
"""
353356
Test that cinderx.jit.compile_after_n_calls() works for functions that
354357
were defined before it was called.
355358
"""
356359

357-
p = multiprocessing.Process(
358-
target=DisableEnableTests.compile_after_n_calls_predefined_test
359-
)
360-
p.start()
361-
p.join()
362-
self.assertEqual(p.exitcode, 0)
360+
with tempfile.TemporaryDirectory() as tmp_dir:
361+
code = textwrap.dedent("""
362+
import cinderx.jit
363+
364+
def predefined(x):
365+
return x + x
366+
367+
cinderx.jit.compile_after_n_calls(2)
368+
369+
assert not cinderx.jit.is_jit_compiled(predefined)
370+
predefined(1)
371+
predefined(2)
372+
assert not cinderx.jit.is_jit_compiled(predefined)
373+
374+
predefined(3)
375+
assert cinderx.jit.is_jit_compiled(predefined)
376+
""")
377+
378+
test_file = Path(tmp_dir) / "mod.py"
379+
test_file.write_text(code)
380+
381+
subprocess.run(
382+
[sys.executable, str(test_file)],
383+
check=True,
384+
env=subprocess_env(),
385+
)
363386

364387

365388
if __name__ == "__main__":

cinderx/PythonLib/test_cinderx/test_jitlist.py

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
# This is in its own file as it modifies the global JIT-list.
66

7-
import multiprocessing
87
import os
98
import subprocess
109
import sys
1110
import tempfile
11+
import textwrap
1212
import unittest
1313
from pathlib import Path
1414

@@ -170,29 +170,40 @@ def test_batch_compile_nested_func(self) -> None:
170170
self.assertEqual(proc.returncode, 0, proc.stderr)
171171
self.assertEqual(b"42\n", proc.stdout, proc.stdout)
172172

173-
@staticmethod
174-
def precompile_all_test() -> None:
175-
import cinderx.jit
173+
def test_precompile_all(self) -> None:
174+
# Has to be run under a separate process because precompile_all will mess up the
175+
# other JIT-related tests.
176+
code = textwrap.dedent(
177+
"""
178+
import cinderx.jit
176179
177-
def func() -> int:
178-
return 24
180+
def func():
181+
return 24
179182
180-
assert not cinderx.jit.is_jit_compiled(func)
181-
cinderx.jit.lazy_compile(func)
182-
assert not cinderx.jit.is_jit_compiled(func)
183+
assert not cinderx.jit.is_jit_compiled(func)
184+
cinderx.jit.lazy_compile(func)
185+
assert not cinderx.jit.is_jit_compiled(func)
183186
184-
assert cinderx.jit.precompile_all(workers=2)
185-
assert cinderx.jit.is_jit_compiled(func)
187+
assert cinderx.jit.precompile_all(workers=2)
188+
assert cinderx.jit.is_jit_compiled(func)
186189
187-
assert func() == 24
190+
print(func())
191+
"""
192+
)
188193

189-
def test_precompile_all(self) -> None:
190-
# Has to be run under a separate process because precompile_all will mess up the
191-
# other JIT-related tests.
192-
p = multiprocessing.Process(target=JitListTest.precompile_all_test)
193-
p.start()
194-
p.join()
195-
self.assertEqual(p.exitcode, 0)
194+
with tempfile.TemporaryDirectory() as tmp:
195+
dirpath = Path(tmp)
196+
codepath = dirpath / "mod.py"
197+
codepath.write_text(code)
198+
proc = subprocess.run(
199+
[sys.executable, "mod.py"],
200+
stdout=subprocess.PIPE,
201+
cwd=tmp,
202+
encoding=ENCODING,
203+
env=subprocess_env(),
204+
)
205+
self.assertEqual(proc.returncode, 0, proc)
206+
self.assertEqual(proc.stdout.strip(), "24")
196207

197208
def test_read_jit_list(self) -> None:
198209
def victim() -> None:

0 commit comments

Comments
 (0)