|
2 | 2 |
|
3 | 3 | # pyre-unsafe |
4 | 4 |
|
5 | | -import subprocess |
| 5 | +import multiprocessing |
6 | 6 | import sys |
7 | | -import tempfile |
8 | | -import textwrap |
9 | 7 | import unittest |
10 | 8 | from pathlib import Path |
11 | 9 |
|
@@ -207,183 +205,162 @@ def foo(a, b): |
207 | 205 | # detection. |
208 | 206 | force_uncompile(foo) |
209 | 207 |
|
| 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 | + |
210 | 219 | def test_compile_no_config(self) -> None: |
211 | 220 | """ |
212 | 221 | Test how code behaves when it forces compilation without any other |
213 | 222 | configuration or options enabled. |
214 | 223 | """ |
215 | 224 |
|
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) |
219 | 229 |
|
220 | | - def inc(x): |
221 | | - return x + 1 |
| 230 | + @staticmethod |
| 231 | + def auto_test() -> None: |
| 232 | + import cinderx.jit |
222 | 233 |
|
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 |
227 | 236 |
|
228 | | - test_file = Path(tmp_dir) / "mod.py" |
229 | | - test_file.write_text(code) |
| 237 | + cinderx.jit.auto() |
230 | 238 |
|
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) |
236 | 249 |
|
237 | 250 | def test_auto(self) -> None: |
238 | 251 | """ |
239 | 252 | Basic test for cinderx.jit.auto(). |
240 | 253 | """ |
241 | 254 |
|
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) |
248 | 259 |
|
249 | | - cinderx.jit.auto() |
| 260 | + @staticmethod |
| 261 | + def auto_predefined_test() -> None: |
| 262 | + import cinderx.jit |
250 | 263 |
|
251 | | - def inc(x): |
252 | | - return x + 1 |
| 264 | + def predefined(x): |
| 265 | + return x + x |
253 | 266 |
|
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() |
258 | 268 |
|
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) |
262 | 273 |
|
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) |
271 | 276 |
|
272 | 277 | def test_auto_predefined(self) -> None: |
273 | 278 | """ |
274 | 279 | Test that cinderx.jit.auto() works for functions that were defined |
275 | 280 | before it was called. |
276 | 281 | """ |
277 | 282 |
|
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) |
284 | 287 |
|
285 | | - cinderx.jit.auto() |
| 288 | + @staticmethod |
| 289 | + def compile_after_n_calls_test() -> None: |
| 290 | + import cinderx.jit |
286 | 291 |
|
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) |
291 | 293 |
|
292 | | - predefined(1001) |
293 | | - assert cinderx.jit.is_jit_compiled(predefined) |
294 | | - """) |
| 294 | + def inc(x): |
| 295 | + return x + 1 |
295 | 296 |
|
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) |
298 | 301 |
|
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) |
304 | 304 |
|
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. |
309 | 306 |
|
310 | | - with tempfile.TemporaryDirectory() as tmp_dir: |
311 | | - code = textwrap.dedent(""" |
312 | | - import cinderx.jit |
| 307 | + cinderx.jit.compile_after_n_calls(5) |
313 | 308 |
|
314 | | - cinderx.jit.compile_after_n_calls(2) |
| 309 | + def dec(x): |
| 310 | + return x - 1 |
315 | 311 |
|
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) |
318 | 319 |
|
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) |
323 | 322 |
|
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 | + """ |
328 | 327 |
|
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) |
330 | 334 |
|
331 | | - def dec(x): |
332 | | - return x - 1 |
| 335 | + @staticmethod |
| 336 | + def compile_after_n_calls_predefined_test() -> None: |
| 337 | + import cinderx.jit |
333 | 338 |
|
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 |
341 | 341 |
|
342 | | - dec(6) |
343 | | - assert cinderx.jit.is_jit_compiled(dec) |
344 | | - """) |
| 342 | + cinderx.jit.compile_after_n_calls(2) |
345 | 343 |
|
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) |
348 | 348 |
|
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) |
354 | 351 |
|
355 | 352 | def test_compile_after_n_calls_predefined(self) -> None: |
356 | 353 | """ |
357 | 354 | Test that cinderx.jit.compile_after_n_calls() works for functions that |
358 | 355 | were defined before it was called. |
359 | 356 | """ |
360 | 357 |
|
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) |
387 | 364 |
|
388 | 365 |
|
389 | 366 | if __name__ == "__main__": |
|
0 commit comments