Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(46685)

Side by Side Diff: Lib/test/_test_multiprocessing.py

Issue 16510: Using appropriate checks in tests
Patch Set: Created 5 years, 10 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Lib/test/test_module.py ('k') | Lib/test/test_namespace_pkgs.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/env python3 1 #!/usr/bin/env python3
2 2
3 # 3 #
4 # Unit tests for the multiprocessing package 4 # Unit tests for the multiprocessing package
5 # 5 #
6 6
7 import unittest 7 import unittest
8 import queue as pyqueue 8 import queue as pyqueue
9 import time 9 import time
10 import io 10 import io
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after
197 ALLOWED_TYPES = ('processes', 'threads') 197 ALLOWED_TYPES = ('processes', 'threads')
198 198
199 def test_current(self): 199 def test_current(self):
200 if self.TYPE == 'threads': 200 if self.TYPE == 'threads':
201 return 201 return
202 202
203 current = self.current_process() 203 current = self.current_process()
204 authkey = current.authkey 204 authkey = current.authkey
205 205
206 self.assertTrue(current.is_alive()) 206 self.assertTrue(current.is_alive())
207 self.assertTrue(not current.daemon) 207 self.assertFalse(current.daemon)
208 self.assertIsInstance(authkey, bytes) 208 self.assertIsInstance(authkey, bytes)
209 self.assertTrue(len(authkey) > 0) 209 self.assertGreater(len(authkey), 0)
210 self.assertEqual(current.ident, os.getpid()) 210 self.assertEqual(current.ident, os.getpid())
211 self.assertEqual(current.exitcode, None) 211 self.assertEqual(current.exitcode, None)
212 212
213 def test_daemon_argument(self): 213 def test_daemon_argument(self):
214 if self.TYPE == "threads": 214 if self.TYPE == "threads":
215 return 215 return
216 216
217 # By default uses the current process's daemon flag. 217 # By default uses the current process's daemon flag.
218 proc0 = self.Process(target=self._test) 218 proc0 = self.Process(target=self._test)
219 self.assertEqual(proc0.daemon, self.current_process().daemon) 219 self.assertEqual(proc0.daemon, self.current_process().daemon)
(...skipping 22 matching lines...) Expand all
242 target=self._test, args=args, kwargs=kwargs, name=name 242 target=self._test, args=args, kwargs=kwargs, name=name
243 ) 243 )
244 p.daemon = True 244 p.daemon = True
245 current = self.current_process() 245 current = self.current_process()
246 246
247 if self.TYPE != 'threads': 247 if self.TYPE != 'threads':
248 self.assertEqual(p.authkey, current.authkey) 248 self.assertEqual(p.authkey, current.authkey)
249 self.assertEqual(p.is_alive(), False) 249 self.assertEqual(p.is_alive(), False)
250 self.assertEqual(p.daemon, True) 250 self.assertEqual(p.daemon, True)
251 self.assertNotIn(p, self.active_children()) 251 self.assertNotIn(p, self.active_children())
252 self.assertTrue(type(self.active_children()) is list) 252 self.assertIs(type(self.active_children()), list)
253 self.assertEqual(p.exitcode, None) 253 self.assertEqual(p.exitcode, None)
254 254
255 p.start() 255 p.start()
256 256
257 self.assertEqual(p.exitcode, None) 257 self.assertEqual(p.exitcode, None)
258 self.assertEqual(p.is_alive(), True) 258 self.assertEqual(p.is_alive(), True)
259 self.assertIn(p, self.active_children()) 259 self.assertIn(p, self.active_children())
260 260
261 self.assertEqual(q.get(), args[1:]) 261 self.assertEqual(q.get(), args[1:])
262 self.assertEqual(q.get(), kwargs) 262 self.assertEqual(q.get(), kwargs)
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
325 p.join() 325 p.join()
326 326
327 # XXX sometimes get p.exitcode == 0 on Windows ... 327 # XXX sometimes get p.exitcode == 0 on Windows ...
328 #self.assertEqual(p.exitcode, -signal.SIGTERM) 328 #self.assertEqual(p.exitcode, -signal.SIGTERM)
329 329
330 def test_cpu_count(self): 330 def test_cpu_count(self):
331 try: 331 try:
332 cpus = multiprocessing.cpu_count() 332 cpus = multiprocessing.cpu_count()
333 except NotImplementedError: 333 except NotImplementedError:
334 cpus = 1 334 cpus = 1
335 self.assertTrue(type(cpus) is int) 335 self.assertIs(type(cpus), int)
336 self.assertTrue(cpus >= 1) 336 self.assertGreaterEqual(cpus, 1)
337 337
338 def test_active_children(self): 338 def test_active_children(self):
339 self.assertEqual(type(self.active_children()), list) 339 self.assertEqual(type(self.active_children()), list)
340 340
341 p = self.Process(target=time.sleep, args=(DELTA,)) 341 p = self.Process(target=time.sleep, args=(DELTA,))
342 self.assertNotIn(p, self.active_children()) 342 self.assertNotIn(p, self.active_children())
343 343
344 p.daemon = True 344 p.daemon = True
345 p.start() 345 p.start()
346 self.assertIn(p, self.active_children()) 346 self.assertIn(p, self.active_children())
(...skipping 1275 matching lines...) Expand 10 before | Expand all | Expand 10 after
1622 1622
1623 def test_namespace(self): 1623 def test_namespace(self):
1624 n = self.Namespace() 1624 n = self.Namespace()
1625 n.name = 'Bob' 1625 n.name = 'Bob'
1626 n.job = 'Builder' 1626 n.job = 'Builder'
1627 n._hidden = 'hidden' 1627 n._hidden = 'hidden'
1628 self.assertEqual((n.name, n.job), ('Bob', 'Builder')) 1628 self.assertEqual((n.name, n.job), ('Bob', 'Builder'))
1629 del n.job 1629 del n.job
1630 self.assertEqual(str(n), "Namespace(name='Bob')") 1630 self.assertEqual(str(n), "Namespace(name='Bob')")
1631 self.assertTrue(hasattr(n, 'name')) 1631 self.assertTrue(hasattr(n, 'name'))
1632 self.assertTrue(not hasattr(n, 'job')) 1632 self.assertFalse(hasattr(n, 'job'))
1633 1633
1634 # 1634 #
1635 # 1635 #
1636 # 1636 #
1637 1637
1638 def sqr(x, wait=0.0): 1638 def sqr(x, wait=0.0):
1639 time.sleep(wait) 1639 time.sleep(wait)
1640 return x*x 1640 return x*x
1641 1641
1642 def mul(x, y): 1642 def mul(x, y):
(...skipping 1236 matching lines...) Expand 10 before | Expand all | Expand 10 after
2879 # Quick test that logging works -- does not test logging output 2879 # Quick test that logging works -- does not test logging output
2880 # 2880 #
2881 2881
2882 class _TestLogging(BaseTestCase): 2882 class _TestLogging(BaseTestCase):
2883 2883
2884 ALLOWED_TYPES = ('processes',) 2884 ALLOWED_TYPES = ('processes',)
2885 2885
2886 def test_enable_logging(self): 2886 def test_enable_logging(self):
2887 logger = multiprocessing.get_logger() 2887 logger = multiprocessing.get_logger()
2888 logger.setLevel(util.SUBWARNING) 2888 logger.setLevel(util.SUBWARNING)
2889 self.assertTrue(logger is not None) 2889 self.assertIsNotNone(logger)
2890 logger.debug('this will not be printed') 2890 logger.debug('this will not be printed')
2891 logger.info('nor will this') 2891 logger.info('nor will this')
2892 logger.setLevel(LOG_LEVEL) 2892 logger.setLevel(LOG_LEVEL)
2893 2893
2894 @classmethod 2894 @classmethod
2895 def _test_level(cls, conn): 2895 def _test_level(cls, conn):
2896 logger = multiprocessing.get_logger() 2896 logger = multiprocessing.get_logger()
2897 conn.send(logger.getEffectiveLevel()) 2897 conn.send(logger.getEffectiveLevel())
2898 2898
2899 def test_level(self): 2899 def test_level(self):
(...skipping 716 matching lines...) Expand 10 before | Expand all | Expand 10 after
3616 count += 1 3616 count += 1
3617 finally: 3617 finally:
3618 multiprocessing.set_start_method(old_method, force=True) 3618 multiprocessing.set_start_method(old_method, force=True)
3619 self.assertGreaterEqual(count, 1) 3619 self.assertGreaterEqual(count, 1)
3620 3620
3621 def test_get_all(self): 3621 def test_get_all(self):
3622 methods = multiprocessing.get_all_start_methods() 3622 methods = multiprocessing.get_all_start_methods()
3623 if sys.platform == 'win32': 3623 if sys.platform == 'win32':
3624 self.assertEqual(methods, ['spawn']) 3624 self.assertEqual(methods, ['spawn'])
3625 else: 3625 else:
3626 self.assertTrue(methods == ['fork', 'spawn'] or 3626 self.assertIn(methods, (['fork', 'spawn'],
3627 methods == ['fork', 'spawn', 'forkserver']) 3627 ['fork', 'spawn', 'forkserver']))
3628 3628
3629 # 3629 #
3630 # Check that killing process does not leak named semaphores 3630 # Check that killing process does not leak named semaphores
3631 # 3631 #
3632 3632
3633 @unittest.skipIf(sys.platform == "win32", 3633 @unittest.skipIf(sys.platform == "win32",
3634 "test semantics don't make sense on Windows") 3634 "test semantics don't make sense on Windows")
3635 class TestSemaphoreTracker(unittest.TestCase): 3635 class TestSemaphoreTracker(unittest.TestCase):
3636 def test_semaphore_tracker(self): 3636 def test_semaphore_tracker(self):
3637 import subprocess 3637 import subprocess
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after
3826 tmp = set(multiprocessing.process._dangling) - set(dangling[0]) 3826 tmp = set(multiprocessing.process._dangling) - set(dangling[0])
3827 if tmp: 3827 if tmp:
3828 print('Dangling processes:', tmp, file=sys.stderr) 3828 print('Dangling processes:', tmp, file=sys.stderr)
3829 del tmp 3829 del tmp
3830 tmp = set(threading._dangling) - set(dangling[1]) 3830 tmp = set(threading._dangling) - set(dangling[1])
3831 if tmp: 3831 if tmp:
3832 print('Dangling threads:', tmp, file=sys.stderr) 3832 print('Dangling threads:', tmp, file=sys.stderr)
3833 3833
3834 remote_globs['setUpModule'] = setUpModule 3834 remote_globs['setUpModule'] = setUpModule
3835 remote_globs['tearDownModule'] = tearDownModule 3835 remote_globs['tearDownModule'] = tearDownModule
OLDNEW
« no previous file with comments | « Lib/test/test_module.py ('k') | Lib/test/test_namespace_pkgs.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+