|
2 | 2 |
|
3 | 3 | # pyre-unsafe |
4 | 4 |
|
5 | | -import multiprocessing |
| 5 | +import subprocess |
6 | 6 | import sys |
| 7 | +import tempfile |
| 8 | +import textwrap |
7 | 9 | import unittest |
8 | 10 | from pathlib import Path |
9 | 11 |
|
|
19 | 21 | lazy_compile, |
20 | 22 | pause as pause_jit, |
21 | 23 | ) |
22 | | -from cinderx.test_support import passUnless |
| 24 | +from cinderx.test_support import passUnless, subprocess_env |
23 | 25 |
|
24 | 26 |
|
25 | 27 | @passUnless(is_jit_enabled(), "Tests functionality on the JIT") |
@@ -204,162 +206,183 @@ def foo(a, b): |
204 | 206 | # detection. |
205 | 207 | force_uncompile(foo) |
206 | 208 |
|
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 | | - |
218 | 209 | def test_compile_no_config(self) -> None: |
219 | 210 | """ |
220 | 211 | Test how code behaves when it forces compilation without any other |
221 | 212 | configuration or options enabled. |
222 | 213 | """ |
223 | 214 |
|
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 |
228 | 218 |
|
229 | | - @staticmethod |
230 | | - def auto_test() -> None: |
231 | | - import cinderx.jit |
| 219 | + def inc(x): |
| 220 | + return x + 1 |
232 | 221 |
|
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 | + """) |
235 | 226 |
|
236 | | - cinderx.jit.auto() |
| 227 | + test_file = Path(tmp_dir) / "mod.py" |
| 228 | + test_file.write_text(code) |
237 | 229 |
|
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 | + ) |
248 | 235 |
|
249 | 236 | def test_auto(self) -> None: |
250 | 237 | """ |
251 | 238 | Basic test for cinderx.jit.auto(). |
252 | 239 | """ |
253 | 240 |
|
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 |
258 | 247 |
|
259 | | - @staticmethod |
260 | | - def auto_predefined_test() -> None: |
261 | | - import cinderx.jit |
| 248 | + cinderx.jit.auto() |
262 | 249 |
|
263 | | - def predefined(x): |
264 | | - return x + x |
| 250 | + def inc(x): |
| 251 | + return x + 1 |
265 | 252 |
|
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) |
267 | 257 |
|
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 | + """) |
272 | 261 |
|
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 | + ) |
275 | 270 |
|
276 | 271 | def test_auto_predefined(self) -> None: |
277 | 272 | """ |
278 | 273 | Test that cinderx.jit.auto() works for functions that were defined |
279 | 274 | before it was called. |
280 | 275 | """ |
281 | 276 |
|
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 |
292 | 280 |
|
293 | | - def inc(x): |
294 | | - return x + 1 |
| 281 | + def predefined(x): |
| 282 | + return x + x |
295 | 283 |
|
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() |
300 | 285 |
|
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) |
303 | 290 |
|
304 | | - # Change the setting and see it takes affect. |
| 291 | + predefined(1001) |
| 292 | + assert cinderx.jit.is_jit_compiled(predefined) |
| 293 | + """) |
305 | 294 |
|
306 | | - cinderx.jit.compile_after_n_calls(5) |
| 295 | + test_file = Path(tmp_dir) / "mod.py" |
| 296 | + test_file.write_text(code) |
307 | 297 |
|
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 | + ) |
321 | 303 |
|
322 | 304 | def test_compile_after_n_calls(self) -> None: |
323 | 305 | """ |
324 | 306 | Basic test for cinderx.jit.compile_after_n_calls(). |
325 | 307 | """ |
326 | 308 |
|
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) |
333 | 314 |
|
334 | | - @staticmethod |
335 | | - def compile_after_n_calls_predefined_test() -> None: |
336 | | - import cinderx.jit |
| 315 | + def inc(x): |
| 316 | + return x + 1 |
337 | 317 |
|
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) |
340 | 322 |
|
341 | | - cinderx.jit.compile_after_n_calls(2) |
| 323 | + inc(3) |
| 324 | + assert cinderx.jit.is_jit_compiled(inc) |
342 | 325 |
|
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. |
347 | 327 |
|
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 | + ) |
350 | 353 |
|
351 | 354 | def test_compile_after_n_calls_predefined(self) -> None: |
352 | 355 | """ |
353 | 356 | Test that cinderx.jit.compile_after_n_calls() works for functions that |
354 | 357 | were defined before it was called. |
355 | 358 | """ |
356 | 359 |
|
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 | + ) |
363 | 386 |
|
364 | 387 |
|
365 | 388 | if __name__ == "__main__": |
|
0 commit comments