Skip to content

Commit 7b26dc6

Browse files
DinoVmeta-codesync[bot]
authored andcommitted
Move some tests away from using subprocess.run
Summary: subprocess.run isn't supported in buck tests. We have a bunch of tests that are using it. They don't work when we run as a par on Python 3.15 and they're now failing on the nightly 3.14 runs where we're presumably picking up the system Python in combination w/ native python. This moves some of the offending tests away from using subprocess.run. Reviewed By: alexmalyshev Differential Revision: D90211488 fbshipit-source-id: 233445ca5e53bfa6279dcda264c5907e8f4c9595
1 parent b5b6d6a commit 7b26dc6

File tree

2 files changed

+124
-158
lines changed

2 files changed

+124
-158
lines changed

cinderx/PythonLib/test_cinderx/test_jit_disable.py

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

33
# pyre-unsafe
44

5-
import subprocess
5+
import multiprocessing
66
import sys
7-
import tempfile
8-
import textwrap
97
import unittest
108
from pathlib import Path
119

@@ -207,183 +205,162 @@ def foo(a, b):
207205
# detection.
208206
force_uncompile(foo)
209207

208+
@staticmethod
209+
def compile_no_config_test() -> None:
210+
import cinderx.jit
211+
212+
def inc(x):
213+
return x + 1
214+
215+
assert not cinderx.jit.is_jit_compiled(inc)
216+
cinderx.jit.force_compile(inc)
217+
assert cinderx.jit.is_jit_compiled(inc)
218+
210219
def test_compile_no_config(self) -> None:
211220
"""
212221
Test how code behaves when it forces compilation without any other
213222
configuration or options enabled.
214223
"""
215224

216-
with tempfile.TemporaryDirectory() as tmp_dir:
217-
code = textwrap.dedent("""
218-
import cinderx.jit
225+
p = multiprocessing.Process(target=DisableEnableTests.compile_no_config_test)
226+
p.start()
227+
p.join()
228+
self.assertEqual(p.exitcode, 0)
219229

220-
def inc(x):
221-
return x + 1
230+
@staticmethod
231+
def auto_test() -> None:
232+
import cinderx.jit
222233

223-
assert not cinderx.jit.is_jit_compiled(inc)
224-
cinderx.jit.force_compile(inc)
225-
assert cinderx.jit.is_jit_compiled(inc)
226-
""")
234+
def predefined(x):
235+
return x + x
227236

228-
test_file = Path(tmp_dir) / "mod.py"
229-
test_file.write_text(code)
237+
cinderx.jit.auto()
230238

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

237250
def test_auto(self) -> None:
238251
"""
239252
Basic test for cinderx.jit.auto().
240253
"""
241254

242-
with tempfile.TemporaryDirectory() as tmp_dir:
243-
code = textwrap.dedent("""
244-
import cinderx.jit
245-
246-
def predefined(x):
247-
return x + x
255+
p = multiprocessing.Process(target=DisableEnableTests.auto_test)
256+
p.start()
257+
p.join()
258+
self.assertEqual(p.exitcode, 0)
248259

249-
cinderx.jit.auto()
260+
@staticmethod
261+
def auto_predefined_test() -> None:
262+
import cinderx.jit
250263

251-
def inc(x):
252-
return x + 1
264+
def predefined(x):
265+
return x + x
253266

254-
assert not cinderx.jit.is_jit_compiled(inc)
255-
for i in range(1000):
256-
inc(i)
257-
assert not cinderx.jit.is_jit_compiled(inc)
267+
cinderx.jit.auto()
258268

259-
inc(1001)
260-
assert cinderx.jit.is_jit_compiled(inc)
261-
""")
269+
assert not cinderx.jit.is_jit_compiled(predefined)
270+
for i in range(1000):
271+
predefined(i)
272+
assert not cinderx.jit.is_jit_compiled(predefined)
262273

263-
test_file = Path(tmp_dir) / "mod.py"
264-
test_file.write_text(code)
265-
266-
subprocess.run(
267-
[sys.executable, str(test_file)],
268-
check=True,
269-
env={"PYTHONPATH": CINDERX_PATH},
270-
)
274+
predefined(1001)
275+
assert cinderx.jit.is_jit_compiled(predefined)
271276

272277
def test_auto_predefined(self) -> None:
273278
"""
274279
Test that cinderx.jit.auto() works for functions that were defined
275280
before it was called.
276281
"""
277282

278-
with tempfile.TemporaryDirectory() as tmp_dir:
279-
code = textwrap.dedent("""
280-
import cinderx.jit
281-
282-
def predefined(x):
283-
return x + x
283+
p = multiprocessing.Process(target=DisableEnableTests.auto_predefined_test)
284+
p.start()
285+
p.join()
286+
self.assertEqual(p.exitcode, 0)
284287

285-
cinderx.jit.auto()
288+
@staticmethod
289+
def compile_after_n_calls_test() -> None:
290+
import cinderx.jit
286291

287-
assert not cinderx.jit.is_jit_compiled(predefined)
288-
for i in range(1000):
289-
predefined(i)
290-
assert not cinderx.jit.is_jit_compiled(predefined)
292+
cinderx.jit.compile_after_n_calls(2)
291293

292-
predefined(1001)
293-
assert cinderx.jit.is_jit_compiled(predefined)
294-
""")
294+
def inc(x):
295+
return x + 1
295296

296-
test_file = Path(tmp_dir) / "mod.py"
297-
test_file.write_text(code)
297+
assert not cinderx.jit.is_jit_compiled(inc)
298+
inc(1)
299+
inc(2)
300+
assert not cinderx.jit.is_jit_compiled(inc)
298301

299-
subprocess.run(
300-
[sys.executable, str(test_file)],
301-
check=True,
302-
env={"PYTHONPATH": CINDERX_PATH},
303-
)
302+
inc(3)
303+
assert cinderx.jit.is_jit_compiled(inc)
304304

305-
def test_compile_after_n_calls(self) -> None:
306-
"""
307-
Basic test for cinderx.jit.compile_after_n_calls().
308-
"""
305+
# Change the setting and see it takes affect.
309306

310-
with tempfile.TemporaryDirectory() as tmp_dir:
311-
code = textwrap.dedent("""
312-
import cinderx.jit
307+
cinderx.jit.compile_after_n_calls(5)
313308

314-
cinderx.jit.compile_after_n_calls(2)
309+
def dec(x):
310+
return x - 1
315311

316-
def inc(x):
317-
return x + 1
312+
assert not cinderx.jit.is_jit_compiled(dec)
313+
dec(1)
314+
dec(2)
315+
dec(3)
316+
dec(4)
317+
dec(5)
318+
assert not cinderx.jit.is_jit_compiled(dec)
318319

319-
assert not cinderx.jit.is_jit_compiled(inc)
320-
inc(1)
321-
inc(2)
322-
assert not cinderx.jit.is_jit_compiled(inc)
320+
dec(6)
321+
assert cinderx.jit.is_jit_compiled(dec)
323322

324-
inc(3)
325-
assert cinderx.jit.is_jit_compiled(inc)
326-
327-
# Change the setting and see it takes affect.
323+
def test_compile_after_n_calls(self) -> None:
324+
"""
325+
Basic test for cinderx.jit.compile_after_n_calls().
326+
"""
328327

329-
cinderx.jit.compile_after_n_calls(5)
328+
p = multiprocessing.Process(
329+
target=DisableEnableTests.compile_after_n_calls_test
330+
)
331+
p.start()
332+
p.join()
333+
self.assertEqual(p.exitcode, 0)
330334

331-
def dec(x):
332-
return x - 1
335+
@staticmethod
336+
def compile_after_n_calls_predefined_test() -> None:
337+
import cinderx.jit
333338

334-
assert not cinderx.jit.is_jit_compiled(dec)
335-
dec(1)
336-
dec(2)
337-
dec(3)
338-
dec(4)
339-
dec(5)
340-
assert not cinderx.jit.is_jit_compiled(dec)
339+
def predefined(x):
340+
return x + x
341341

342-
dec(6)
343-
assert cinderx.jit.is_jit_compiled(dec)
344-
""")
342+
cinderx.jit.compile_after_n_calls(2)
345343

346-
test_file = Path(tmp_dir) / "mod.py"
347-
test_file.write_text(code)
344+
assert not cinderx.jit.is_jit_compiled(predefined)
345+
predefined(1)
346+
predefined(2)
347+
assert not cinderx.jit.is_jit_compiled(predefined)
348348

349-
subprocess.run(
350-
[sys.executable, str(test_file)],
351-
check=True,
352-
env={"PYTHONPATH": CINDERX_PATH},
353-
)
349+
predefined(3)
350+
assert cinderx.jit.is_jit_compiled(predefined)
354351

355352
def test_compile_after_n_calls_predefined(self) -> None:
356353
"""
357354
Test that cinderx.jit.compile_after_n_calls() works for functions that
358355
were defined before it was called.
359356
"""
360357

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

388365

389366
if __name__ == "__main__":

cinderx/PythonLib/test_cinderx/test_jitlist.py

Lines changed: 19 additions & 30 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
78
import os
89
import subprocess
910
import sys
1011
import tempfile
11-
import textwrap
1212
import unittest
1313
from pathlib import Path
1414

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

174-
def test_precompile_all(self) -> None:
175-
# Has to be run under a separate process because precompile_all will mess up the
176-
# other JIT-related tests.
177-
code = textwrap.dedent(
178-
"""
179-
import cinderx.jit
174+
@staticmethod
175+
def precompile_all_test() -> None:
176+
import cinderx.jit
180177

181-
def func():
182-
return 24
178+
def func() -> int:
179+
return 24
183180

184-
assert not cinderx.jit.is_jit_compiled(func)
185-
cinderx.jit.lazy_compile(func)
186-
assert not cinderx.jit.is_jit_compiled(func)
181+
assert not cinderx.jit.is_jit_compiled(func)
182+
cinderx.jit.lazy_compile(func)
183+
assert not cinderx.jit.is_jit_compiled(func)
187184

188-
assert cinderx.jit.precompile_all(workers=2)
189-
assert cinderx.jit.is_jit_compiled(func)
185+
assert cinderx.jit.precompile_all(workers=2)
186+
assert cinderx.jit.is_jit_compiled(func)
190187

191-
print(func())
192-
"""
193-
)
188+
assert func() == 24
194189

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

209198
def test_read_jit_list(self) -> None:
210199
def victim() -> None:

0 commit comments

Comments
 (0)