Make test_multiprocessing_spawn.py compatible with pytest (#50408)

Summary:
This file is currently failing with

```
file /home/gaoxiang/pytorch-tf32/test/test_multiprocessing_spawn.py, line 13
  def test_success_func(i):
E       fixture 'i' not found
>       available fixtures: cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, include_metadata_in_junit_xml, json_metadata, metadata, monkeypatch, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory
>       use 'pytest --fixtures [testpath]' for help on them.

/home/gaoxiang/pytorch-tf32/test/test_multiprocessing_spawn.py:13
________________________________________________________________________________________________________________ ERROR at setup of test_success_single_arg_func ________________________________________________________________________________________________________________
file /home/gaoxiang/pytorch-tf32/test/test_multiprocessing_spawn.py, line 17
  def test_success_single_arg_func(i, arg):
E       fixture 'i' not found
>       available fixtures: cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, include_metadata_in_junit_xml, json_metadata, metadata, monkeypatch, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory
>       use 'pytest --fixtures [testpath]' for help on them.

/home/gaoxiang/pytorch-tf32/test/test_multiprocessing_spawn.py:17
_________________________________________________________________________________________________________________ ERROR at setup of test_exception_single_func _________________________________________________________________________________________________________________
file /home/gaoxiang/pytorch-tf32/test/test_multiprocessing_spawn.py, line 22
  def test_exception_single_func(i, arg):
E       fixture 'i' not found
>       available fixtures: cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, include_metadata_in_junit_xml, json_metadata, metadata, monkeypatch, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory
>       use 'pytest --fixtures [testpath]' for help on them.

/home/gaoxiang/pytorch-tf32/test/test_multiprocessing_spawn.py:22
__________________________________________________________________________________________________________________ ERROR at setup of test_exception_all_func ___________________________________________________________________________________________________________________
file /home/gaoxiang/pytorch-tf32/test/test_multiprocessing_spawn.py, line 28
  def test_exception_all_func(i):
E       fixture 'i' not found
>       available fixtures: cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, include_metadata_in_junit_xml, json_metadata, metadata, monkeypatch, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory
>       use 'pytest --fixtures [testpath]' for help on them.

/home/gaoxiang/pytorch-tf32/test/test_multiprocessing_spawn.py:28
_________________________________________________________________________________________________________________ ERROR at setup of test_terminate_signal_func _________________________________________________________________________________________________________________
file /home/gaoxiang/pytorch-tf32/test/test_multiprocessing_spawn.py, line 33
  def test_terminate_signal_func(i):
E       fixture 'i' not found
>       available fixtures: cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, include_metadata_in_junit_xml, json_metadata, metadata, monkeypatch, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory
>       use 'pytest --fixtures [testpath]' for help on them.

/home/gaoxiang/pytorch-tf32/test/test_multiprocessing_spawn.py:33
__________________________________________________________________________________________________________________ ERROR at setup of test_terminate_exit_func __________________________________________________________________________________________________________________
file /home/gaoxiang/pytorch-tf32/test/test_multiprocessing_spawn.py, line 39
  def test_terminate_exit_func(i, arg):
E       fixture 'i' not found
>       available fixtures: cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, include_metadata_in_junit_xml, json_metadata, metadata, monkeypatch, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory
>       use 'pytest --fixtures [testpath]' for help on them.

/home/gaoxiang/pytorch-tf32/test/test_multiprocessing_spawn.py:39
___________________________________________________________________________________________________________ ERROR at setup of test_success_first_then_exception_func ___________________________________________________________________________________________________________
file /home/gaoxiang/pytorch-tf32/test/test_multiprocessing_spawn.py, line 45
  def test_success_first_then_exception_func(i, arg):
E       fixture 'i' not found
>       available fixtures: cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, include_metadata_in_junit_xml, json_metadata, metadata, monkeypatch, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory
>       use 'pytest --fixtures [testpath]' for help on them.

/home/gaoxiang/pytorch-tf32/test/test_multiprocessing_spawn.py:45
___________________________________________________________________________________________________________________ ERROR at setup of test_nested_child_body ___________________________________________________________________________________________________________________
file /home/gaoxiang/pytorch-tf32/test/test_multiprocessing_spawn.py, line 52
  def test_nested_child_body(i, ready_queue, nested_child_sleep):
E       fixture 'i' not found
>       available fixtures: cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, include_metadata_in_junit_xml, json_metadata, metadata, monkeypatch, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory
>       use 'pytest --fixtures [testpath]' for help on them.

/home/gaoxiang/pytorch-tf32/test/test_multiprocessing_spawn.py:52
_____________________________________________________________________________________________________________________ ERROR at setup of test_infinite_task _____________________________________________________________________________________________________________________
file /home/gaoxiang/pytorch-tf32/test/test_multiprocessing_spawn.py, line 57
  def test_infinite_task(i):
E       fixture 'i' not found
>       available fixtures: cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, include_metadata_in_junit_xml, json_metadata, metadata, monkeypatch, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory
>       use 'pytest --fixtures [testpath]' for help on them.

/home/gaoxiang/pytorch-tf32/test/test_multiprocessing_spawn.py:57
_____________________________________________________________________________________________________________________ ERROR at setup of test_process_exit ______________________________________________________________________________________________________________________
file /home/gaoxiang/pytorch-tf32/test/test_multiprocessing_spawn.py, line 62
  def test_process_exit(idx):
E       fixture 'idx' not found
>       available fixtures: cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, include_metadata_in_junit_xml, json_metadata, metadata, monkeypatch, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory
>       use 'pytest --fixtures [testpath]' for help on them.

/home/gaoxiang/pytorch-tf32/test/test_multiprocessing_spawn.py:62
________________________________________________________________________________________________________________________ ERROR at setup of test_nested _________________________________________________________________________________________________________________________
file /home/gaoxiang/pytorch-tf32/test/test_multiprocessing_spawn.py, line 66
  def test_nested(i, pids_queue, nested_child_sleep, start_method):
E       fixture 'i' not found
>       available fixtures: cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, include_metadata_in_junit_xml, json_metadata, metadata, monkeypatch, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory
>       use 'pytest --fixtures [testpath]' for help on them.
```
when running with pytest. This is because pytest considers anything starting with `test_` as a test, so I renamed it to `_test_...` to prevent this from happening.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/50408

Reviewed By: bdhirsh

Differential Revision: D34118341

Pulled By: VitalyFedyunin

fbshipit-source-id: 7c74843462b79df351e3c60f313ef388a9e0df4e
This commit is contained in:
Xiang Gao 2022-02-10 07:57:14 -08:00 committed by Facebook GitHub Bot
parent 71f524bd1c
commit fd8b66bea0

View File

@ -12,65 +12,65 @@ from torch.testing._internal.common_utils import (TestCase, run_tests, IS_WINDOW
import torch.multiprocessing as mp import torch.multiprocessing as mp
def test_success_func(i): def _test_success_func(i):
pass pass
def test_success_single_arg_func(i, arg): def _test_success_single_arg_func(i, arg):
if arg: if arg:
arg.put(i) arg.put(i)
def test_exception_single_func(i, arg): def _test_exception_single_func(i, arg):
if i == arg: if i == arg:
raise ValueError("legitimate exception from process %d" % i) raise ValueError("legitimate exception from process %d" % i)
time.sleep(1.0) time.sleep(1.0)
def test_exception_all_func(i): def _test_exception_all_func(i):
time.sleep(random.random() / 10) time.sleep(random.random() / 10)
raise ValueError("legitimate exception from process %d" % i) raise ValueError("legitimate exception from process %d" % i)
def test_terminate_signal_func(i): def _test_terminate_signal_func(i):
if i == 0: if i == 0:
os.kill(os.getpid(), signal.SIGABRT) os.kill(os.getpid(), signal.SIGABRT)
time.sleep(1.0) time.sleep(1.0)
def test_terminate_exit_func(i, arg): def _test_terminate_exit_func(i, arg):
if i == 0: if i == 0:
sys.exit(arg) sys.exit(arg)
time.sleep(1.0) time.sleep(1.0)
def test_success_first_then_exception_func(i, arg): def _test_success_first_then_exception_func(i, arg):
if i == 0: if i == 0:
return return
time.sleep(0.1) time.sleep(0.1)
raise ValueError("legitimate exception") raise ValueError("legitimate exception")
def test_nested_child_body(i, ready_queue, nested_child_sleep): def _test_nested_child_body(i, ready_queue, nested_child_sleep):
ready_queue.put(None) ready_queue.put(None)
time.sleep(nested_child_sleep) time.sleep(nested_child_sleep)
def test_infinite_task(i): def _test_infinite_task(i):
while True: while True:
time.sleep(1) time.sleep(1)
def test_process_exit(idx): def _test_process_exit(idx):
sys.exit(12) sys.exit(12)
def test_nested(i, pids_queue, nested_child_sleep, start_method): def _test_nested(i, pids_queue, nested_child_sleep, start_method):
context = mp.get_context(start_method) context = mp.get_context(start_method)
nested_child_ready_queue = context.Queue() nested_child_ready_queue = context.Queue()
nprocs = 2 nprocs = 2
mp_context = mp.start_processes( mp_context = mp.start_processes(
fn=test_nested_child_body, fn=_test_nested_child_body,
args=(nested_child_ready_queue, nested_child_sleep), args=(nested_child_ready_queue, nested_child_sleep),
nprocs=nprocs, nprocs=nprocs,
join=False, join=False,
@ -91,10 +91,10 @@ class _TestMultiProcessing(object):
start_method = None start_method = None
def test_success(self): def test_success(self):
mp.start_processes(test_success_func, nprocs=2, start_method=self.start_method) mp.start_processes(_test_success_func, nprocs=2, start_method=self.start_method)
def test_success_non_blocking(self): def test_success_non_blocking(self):
mp_context = mp.start_processes(test_success_func, nprocs=2, join=False, start_method=self.start_method) mp_context = mp.start_processes(_test_success_func, nprocs=2, join=False, start_method=self.start_method)
# After all processes (nproc=2) have joined it must return True # After all processes (nproc=2) have joined it must return True
mp_context.join(timeout=None) mp_context.join(timeout=None)
@ -104,7 +104,7 @@ class _TestMultiProcessing(object):
def test_first_argument_index(self): def test_first_argument_index(self):
context = mp.get_context(self.start_method) context = mp.get_context(self.start_method)
queue = context.SimpleQueue() queue = context.SimpleQueue()
mp.start_processes(test_success_single_arg_func, args=(queue,), nprocs=2, start_method=self.start_method) mp.start_processes(_test_success_single_arg_func, args=(queue,), nprocs=2, start_method=self.start_method)
self.assertEqual([0, 1], sorted([queue.get(), queue.get()])) self.assertEqual([0, 1], sorted([queue.get(), queue.get()]))
def test_exception_single(self): def test_exception_single(self):
@ -114,14 +114,14 @@ class _TestMultiProcessing(object):
Exception, Exception,
"\nValueError: legitimate exception from process %d$" % i, "\nValueError: legitimate exception from process %d$" % i,
): ):
mp.start_processes(test_exception_single_func, args=(i,), nprocs=nprocs, start_method=self.start_method) mp.start_processes(_test_exception_single_func, args=(i,), nprocs=nprocs, start_method=self.start_method)
def test_exception_all(self): def test_exception_all(self):
with self.assertRaisesRegex( with self.assertRaisesRegex(
Exception, Exception,
"\nValueError: legitimate exception from process (0|1)$", "\nValueError: legitimate exception from process (0|1)$",
): ):
mp.start_processes(test_exception_all_func, nprocs=2, start_method=self.start_method) mp.start_processes(_test_exception_all_func, nprocs=2, start_method=self.start_method)
def test_terminate_signal(self): def test_terminate_signal(self):
# SIGABRT is aliased with SIGIOT # SIGABRT is aliased with SIGIOT
@ -136,7 +136,7 @@ class _TestMultiProcessing(object):
message = "process 0 terminated with exit code 22" message = "process 0 terminated with exit code 22"
with self.assertRaisesRegex(Exception, message): with self.assertRaisesRegex(Exception, message):
mp.start_processes(test_terminate_signal_func, nprocs=2, start_method=self.start_method) mp.start_processes(_test_terminate_signal_func, nprocs=2, start_method=self.start_method)
def test_terminate_exit(self): def test_terminate_exit(self):
exitcode = 123 exitcode = 123
@ -144,7 +144,7 @@ class _TestMultiProcessing(object):
Exception, Exception,
"process 0 terminated with exit code %d" % exitcode, "process 0 terminated with exit code %d" % exitcode,
): ):
mp.start_processes(test_terminate_exit_func, args=(exitcode,), nprocs=2, start_method=self.start_method) mp.start_processes(_test_terminate_exit_func, args=(exitcode,), nprocs=2, start_method=self.start_method)
def test_success_first_then_exception(self): def test_success_first_then_exception(self):
exitcode = 123 exitcode = 123
@ -152,18 +152,18 @@ class _TestMultiProcessing(object):
Exception, Exception,
"ValueError: legitimate exception", "ValueError: legitimate exception",
): ):
mp.start_processes(test_success_first_then_exception_func, args=(exitcode,), nprocs=2, start_method=self.start_method) mp.start_processes(_test_success_first_then_exception_func, args=(exitcode,), nprocs=2, start_method=self.start_method)
@unittest.skipIf( @unittest.skipIf(
sys.platform != "linux", sys.platform != "linux",
"Only runs on Linux; requires prctl(2)", "Only runs on Linux; requires prctl(2)",
) )
def test_nested(self): def _test_nested(self):
context = mp.get_context(self.start_method) context = mp.get_context(self.start_method)
pids_queue = context.Queue() pids_queue = context.Queue()
nested_child_sleep = 20.0 nested_child_sleep = 20.0
mp_context = mp.start_processes( mp_context = mp.start_processes(
fn=test_nested, fn=_test_nested,
args=(pids_queue, nested_child_sleep, self.start_method), args=(pids_queue, nested_child_sleep, self.start_method),
nprocs=1, nprocs=1,
join=False, join=False,
@ -197,18 +197,18 @@ class SpawnTest(TestCase, _TestMultiProcessing):
def test_exception_raises(self): def test_exception_raises(self):
with self.assertRaises(mp.ProcessRaisedException): with self.assertRaises(mp.ProcessRaisedException):
mp.spawn(test_success_first_then_exception_func, args=(), nprocs=1) mp.spawn(_test_success_first_then_exception_func, args=(), nprocs=1)
def test_signal_raises(self): def test_signal_raises(self):
context = mp.spawn(test_infinite_task, args=(), nprocs=1, join=False) context = mp.spawn(_test_infinite_task, args=(), nprocs=1, join=False)
for pid in context.pids(): for pid in context.pids():
os.kill(pid, signal.SIGTERM) os.kill(pid, signal.SIGTERM)
with self.assertRaises(mp.ProcessExitedException): with self.assertRaises(mp.ProcessExitedException):
context.join() context.join()
def test_process_exited(self): def _test_process_exited(self):
with self.assertRaises(mp.ProcessExitedException) as e: with self.assertRaises(mp.ProcessExitedException) as e:
mp.spawn(test_process_exit, args=(), nprocs=1) mp.spawn(_test_process_exit, args=(), nprocs=1)
self.assertEqual(12, e.exit_code) self.assertEqual(12, e.exit_code)