def tearDown(self): reap_children() if not (FREEBSD or NETBSD): # Make sure we closed all resources. # NetBSD opens a UNIX socket to /var/log/run. cons = thisproc.connections(kind='all') assert not cons, cons
def tearDown(self): safe_rmpath(TESTFN) reap_children() if not NETBSD: # Make sure we closed all resources. # NetBSD opens a UNIX socket to /var/log/run. cons = thisproc.connections(kind='all') assert not cons, cons
def test_reap_children(self): subp = get_test_subprocess() p = psutil.Process(subp.pid) assert p.is_running() reap_children() assert not p.is_running() assert not psutil.tests._pids_started assert not psutil.tests._subprocesses_started
def test_reap_children(self): subp = self.spawn_testproc() p = psutil.Process(subp.pid) assert p.is_running() reap_children() assert not p.is_running() assert not psutil.tests._pids_started assert not psutil.tests._subprocesses_started
def test_pid_exists_2(self): reap_children() pids = psutil.pids() for pid in pids: try: assert psutil.pid_exists(pid) except AssertionError: # in case the process disappeared in meantime fail only # if it is no longer in psutil.pids() time.sleep(.1) if pid in psutil.pids(): self.fail(pid) pids = range(max(pids) + 5000, max(pids) + 6000) for pid in pids: self.assertFalse(psutil.pid_exists(pid), msg=pid)
def subprocess_supports_unicode(name): """Return True if both the fs and the subprocess module can deal with a unicode file name. """ if PY3: return True try: safe_rmpath(name) create_exe(name) get_test_subprocess(cmd=[name]) except UnicodeEncodeError: return False else: reap_children() return True
def run(self, suite): ser_suite, par_suite = self._split_suite(suite) par_suite = self._parallelize(par_suite) # run parallel cprint("starting parallel tests using %s workers" % NWORKERS, "green", bold=True) t = time.time() par = self._run(par_suite) par_elapsed = time.time() - t # At this point we should have N zombies (the workers), which # will disappear with wait(). orphans = psutil.Process().children() gone, alive = psutil.wait_procs(orphans, timeout=1) if alive: cprint("alive processes %s" % alive, "red") reap_children() # run serial t = time.time() ser = self._run(ser_suite) ser_elapsed = time.time() - t # print if not par.wasSuccessful() and ser_suite.countTestCases() > 0: par.printErrors() # print them again at the bottom par_fails, par_errs, par_skips = map( len, (par.failures, par.errors, par.skipped)) ser_fails, ser_errs, ser_skips = map( len, (ser.failures, ser.errors, ser.skipped)) print( textwrap.dedent(""" +----------+----------+----------+----------+----------+----------+ | | total | failures | errors | skipped | time | +----------+----------+----------+----------+----------+----------+ | parallel | %3s | %3s | %3s | %3s | %.2fs | +----------+----------+----------+----------+----------+----------+ | serial | %3s | %3s | %3s | %3s | %.2fs | +----------+----------+----------+----------+----------+----------+ """ % (par.testsRun, par_fails, par_errs, par_skips, par_elapsed, ser.testsRun, ser_fails, ser_errs, ser_skips, ser_elapsed))) print( "Ran %s tests in %.3fs using %s workers" % (par.testsRun + ser.testsRun, par_elapsed + ser_elapsed, NWORKERS)) ok = par.wasSuccessful() and ser.wasSuccessful() self._exit(ok)
def run_parallel(self): """Run tests in parallel.""" ser_suite, par_suite = self.loader.parallel() par_suite = self._parallelize_suite(par_suite) # run parallel print("starting parallel tests using %s workers" % NWORKERS) t = time.time() par = self._run(par_suite) par_elapsed = time.time() - t # cleanup workers and test subprocesses orphans = psutil.Process().children() gone, alive = psutil.wait_procs(orphans, timeout=1) if alive: print_color("alive processes %s" % alive, "red") reap_children() # run serial t = time.time() ser = self._run(ser_suite) ser_elapsed = time.time() - t # print if not par.wasSuccessful(): par.printErrors() # print them again at the bottom par_fails, par_errs, par_skips = map( len, (par.failures, par.errors, par.skipped)) ser_fails, ser_errs, ser_skips = map( len, (ser.failures, ser.errors, ser.skipped)) print("-" * 70) print( textwrap.dedent(""" +----------+----------+----------+----------+----------+----------+ | | total | failures | errors | skipped | time | +----------+----------+----------+----------+----------+----------+ | parallel | %3s | %3s | %3s | %3s | %.2fs | +----------+----------+----------+----------+----------+----------+ | serial | %3s | %3s | %3s | %3s | %.2fs | +----------+----------+----------+----------+----------+----------+ """ % (par.testsRun, par_fails, par_errs, par_skips, par_elapsed, ser.testsRun, ser_fails, ser_errs, ser_skips, ser_elapsed))) print( "Ran %s tests in %.3fs using %s workers" % (par.testsRun + ser.testsRun, par_elapsed + ser_elapsed, NWORKERS)) ok = par.wasSuccessful() and ser.wasSuccessful() self._finalize(ok)
def test_create_proc_children_pair(self): p1, p2 = create_proc_children_pair() self.assertNotEqual(p1.pid, p2.pid) assert p1.is_running() assert p2.is_running() children = psutil.Process().children(recursive=True) self.assertEqual(len(children), 2) self.assertIn(p1, children) self.assertIn(p2, children) self.assertEqual(p1.ppid(), os.getpid()) self.assertEqual(p2.ppid(), p1.pid) # make sure both of them are cleaned up reap_children() assert not p1.is_running() assert not p2.is_running() assert not psutil.tests._pids_started assert not psutil.tests._subprocesses_started
def tearDown(self): reap_children() safe_rmpath(self.funky_name)
def tearDown(self): reap_children()
def test_reap_children(self): subp = get_test_subprocess() assert psutil.pid_exists(subp.pid) reap_children() assert not psutil.pid_exists(subp.pid)
def tearDownClass(cls): reap_children(recursive=True)
def tearDown(self): safe_rmpath(TESTFN) reap_children() # make sure we closed all resources cons = psutil.Process().connections(kind='all') assert not cons, cons
def test_combos(self): reap_children() def check_conn(proc, conn, family, type, laddr, raddr, status, kinds): all_kinds = ("all", "inet", "inet4", "inet6", "tcp", "tcp4", "tcp6", "udp", "udp4", "udp6") self.check_connection_ntuple(conn) self.assertEqual(conn.family, family) self.assertEqual(conn.type, type) self.assertEqual(conn.laddr, laddr) self.assertEqual(conn.raddr, raddr) self.assertEqual(conn.status, status) for kind in all_kinds: cons = proc.connections(kind=kind) if kind in kinds: assert cons else: assert not cons, cons # compare against system-wide connections # XXX Solaris can't retrieve system-wide UNIX # sockets. if HAS_CONNECTIONS_UNIX: self.compare_procsys_connections(proc.pid, [conn]) tcp_template = textwrap.dedent(""" import socket, time s = socket.socket({family}, socket.SOCK_STREAM) s.bind(('{addr}', 0)) s.listen(5) with open('{testfn}', 'w') as f: f.write(str(s.getsockname()[:2])) time.sleep(60) """) udp_template = textwrap.dedent(""" import socket, time s = socket.socket({family}, socket.SOCK_DGRAM) s.bind(('{addr}', 0)) with open('{testfn}', 'w') as f: f.write(str(s.getsockname()[:2])) time.sleep(60) """) # must be relative on Windows testfile = os.path.basename(self.get_testfn(dir=os.getcwd())) tcp4_template = tcp_template.format( family=int(AF_INET), addr="127.0.0.1", testfn=testfile) udp4_template = udp_template.format( family=int(AF_INET), addr="127.0.0.1", testfn=testfile) tcp6_template = tcp_template.format( family=int(AF_INET6), addr="::1", testfn=testfile) udp6_template = udp_template.format( family=int(AF_INET6), addr="::1", testfn=testfile) # launch various subprocess instantiating a socket of various # families and types to enrich psutil results tcp4_proc = self.pyrun(tcp4_template) tcp4_addr = eval(wait_for_file(testfile, delete=True)) udp4_proc = self.pyrun(udp4_template) udp4_addr = eval(wait_for_file(testfile, delete=True)) if supports_ipv6(): tcp6_proc = self.pyrun(tcp6_template) tcp6_addr = eval(wait_for_file(testfile, delete=True)) udp6_proc = self.pyrun(udp6_template) udp6_addr = eval(wait_for_file(testfile, delete=True)) else: tcp6_proc = None udp6_proc = None tcp6_addr = None udp6_addr = None for p in thisproc.children(): cons = p.connections() self.assertEqual(len(cons), 1) for conn in cons: # TCP v4 if p.pid == tcp4_proc.pid: check_conn(p, conn, AF_INET, SOCK_STREAM, tcp4_addr, (), psutil.CONN_LISTEN, ("all", "inet", "inet4", "tcp", "tcp4")) # UDP v4 elif p.pid == udp4_proc.pid: check_conn(p, conn, AF_INET, SOCK_DGRAM, udp4_addr, (), psutil.CONN_NONE, ("all", "inet", "inet4", "udp", "udp4")) # TCP v6 elif p.pid == getattr(tcp6_proc, "pid", None): check_conn(p, conn, AF_INET6, SOCK_STREAM, tcp6_addr, (), psutil.CONN_LISTEN, ("all", "inet", "inet6", "tcp", "tcp6")) # UDP v6 elif p.pid == getattr(udp6_proc, "pid", None): check_conn(p, conn, AF_INET6, SOCK_DGRAM, udp6_addr, (), psutil.CONN_NONE, ("all", "inet", "inet6", "udp", "udp6"))
def tearDownClass(cls): super(TestTerminatedProcessLeaks, cls).tearDownClass() reap_children()
def tearDownClass(cls): reap_children() safe_rmpath(cls.funky_name)
def test_reap_children(self): subp = get_test_subprocess() p = psutil.Process(subp.pid) assert p.is_running() reap_children() assert not p.is_running()
def tearDown(self): self.proc32.communicate() self.proc64.communicate() reap_children()
def tearDownClass(cls): reap_children()
def tearDownClass(cls): super().tearDownClass() reap_children()