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

Delta Between Two Patch Sets: Lib/test/_test_multiprocessing.py

Issue 16510: Using appropriate checks in tests
Left Patch Set: Created 6 years, 9 months ago
Right Patch Set: Created 5 years, 6 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:
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/test/test_module.py ('k') | Lib/test/test_os.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
(no file at all)
1 # 1 #
2 # Unit tests for the multiprocessing package 2 # Unit tests for the multiprocessing package
3 # 3 #
4 4
5 import unittest 5 import unittest
6 import queue as pyqueue 6 import queue as pyqueue
7 import time 7 import time
8 import io 8 import io
9 import itertools 9 import itertools
10 import sys 10 import sys
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after
195 ALLOWED_TYPES = ('processes', 'threads') 195 ALLOWED_TYPES = ('processes', 'threads')
196 196
197 def test_current(self): 197 def test_current(self):
198 if self.TYPE == 'threads': 198 if self.TYPE == 'threads':
199 self.skipTest('test not appropriate for {}'.format(self.TYPE)) 199 self.skipTest('test not appropriate for {}'.format(self.TYPE))
200 200
201 current = self.current_process() 201 current = self.current_process()
202 authkey = current.authkey 202 authkey = current.authkey
203 203
204 self.assertTrue(current.is_alive()) 204 self.assertTrue(current.is_alive())
205 self.assertTrue(not current.daemon) 205 self.assertFalse(current.daemon)
206 self.assertIsInstance(authkey, bytes) 206 self.assertIsInstance(authkey, bytes)
207 self.assertTrue(len(authkey) > 0) 207 self.assertGreater(len(authkey), 0)
208 self.assertEqual(current.ident, os.getpid()) 208 self.assertEqual(current.ident, os.getpid())
209 self.assertEqual(current.exitcode, None) 209 self.assertEqual(current.exitcode, None)
210 210
211 def test_daemon_argument(self): 211 def test_daemon_argument(self):
212 if self.TYPE == "threads": 212 if self.TYPE == "threads":
213 self.skipTest('test not appropriate for {}'.format(self.TYPE)) 213 self.skipTest('test not appropriate for {}'.format(self.TYPE))
214 214
215 # By default uses the current process's daemon flag. 215 # By default uses the current process's daemon flag.
216 proc0 = self.Process(target=self._test) 216 proc0 = self.Process(target=self._test)
217 self.assertEqual(proc0.daemon, self.current_process().daemon) 217 self.assertEqual(proc0.daemon, self.current_process().daemon)
(...skipping 22 matching lines...) Expand all
240 target=self._test, args=args, kwargs=kwargs, name=name 240 target=self._test, args=args, kwargs=kwargs, name=name
241 ) 241 )
242 p.daemon = True 242 p.daemon = True
243 current = self.current_process() 243 current = self.current_process()
244 244
245 if self.TYPE != 'threads': 245 if self.TYPE != 'threads':
246 self.assertEqual(p.authkey, current.authkey) 246 self.assertEqual(p.authkey, current.authkey)
247 self.assertEqual(p.is_alive(), False) 247 self.assertEqual(p.is_alive(), False)
248 self.assertEqual(p.daemon, True) 248 self.assertEqual(p.daemon, True)
249 self.assertNotIn(p, self.active_children()) 249 self.assertNotIn(p, self.active_children())
250 self.assertTrue(type(self.active_children()) is list) 250 self.assertIs(type(self.active_children()), list)
251 self.assertEqual(p.exitcode, None) 251 self.assertEqual(p.exitcode, None)
252 252
253 p.start() 253 p.start()
254 254
255 self.assertEqual(p.exitcode, None) 255 self.assertEqual(p.exitcode, None)
256 self.assertEqual(p.is_alive(), True) 256 self.assertEqual(p.is_alive(), True)
257 self.assertIn(p, self.active_children()) 257 self.assertIn(p, self.active_children())
258 258
259 self.assertEqual(q.get(), args[1:]) 259 self.assertEqual(q.get(), args[1:])
260 self.assertEqual(q.get(), kwargs) 260 self.assertEqual(q.get(), kwargs)
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
323 p.join() 323 p.join()
324 324
325 # XXX sometimes get p.exitcode == 0 on Windows ... 325 # XXX sometimes get p.exitcode == 0 on Windows ...
326 #self.assertEqual(p.exitcode, -signal.SIGTERM) 326 #self.assertEqual(p.exitcode, -signal.SIGTERM)
327 327
328 def test_cpu_count(self): 328 def test_cpu_count(self):
329 try: 329 try:
330 cpus = multiprocessing.cpu_count() 330 cpus = multiprocessing.cpu_count()
331 except NotImplementedError: 331 except NotImplementedError:
332 cpus = 1 332 cpus = 1
333 self.assertTrue(type(cpus) is int) 333 self.assertIs(type(cpus), int)
334 self.assertTrue(cpus >= 1) 334 self.assertGreaterEqual(cpus, 1)
335 335
336 def test_active_children(self): 336 def test_active_children(self):
337 self.assertEqual(type(self.active_children()), list) 337 self.assertEqual(type(self.active_children()), list)
338 338
339 p = self.Process(target=time.sleep, args=(DELTA,)) 339 p = self.Process(target=time.sleep, args=(DELTA,))
340 self.assertNotIn(p, self.active_children()) 340 self.assertNotIn(p, self.active_children())
341 341
342 p.daemon = True 342 p.daemon = True
343 p.start() 343 p.start()
344 self.assertIn(p, self.active_children()) 344 self.assertIn(p, self.active_children())
(...skipping 1272 matching lines...) Expand 10 before | Expand all | Expand 10 after
1617 1617
1618 def test_namespace(self): 1618 def test_namespace(self):
1619 n = self.Namespace() 1619 n = self.Namespace()
1620 n.name = 'Bob' 1620 n.name = 'Bob'
1621 n.job = 'Builder' 1621 n.job = 'Builder'
1622 n._hidden = 'hidden' 1622 n._hidden = 'hidden'
1623 self.assertEqual((n.name, n.job), ('Bob', 'Builder')) 1623 self.assertEqual((n.name, n.job), ('Bob', 'Builder'))
1624 del n.job 1624 del n.job
1625 self.assertEqual(str(n), "Namespace(name='Bob')") 1625 self.assertEqual(str(n), "Namespace(name='Bob')")
1626 self.assertTrue(hasattr(n, 'name')) 1626 self.assertTrue(hasattr(n, 'name'))
1627 self.assertTrue(not hasattr(n, 'job')) 1627 self.assertFalse(hasattr(n, 'job'))
1628 1628
1629 # 1629 #
1630 # 1630 #
1631 # 1631 #
1632 1632
1633 def sqr(x, wait=0.0): 1633 def sqr(x, wait=0.0):
1634 time.sleep(wait) 1634 time.sleep(wait)
1635 return x*x 1635 return x*x
1636 1636
1637 def mul(x, y): 1637 def mul(x, y):
(...skipping 1236 matching lines...) Expand 10 before | Expand all | Expand 10 after
2874 # Quick test that logging works -- does not test logging output 2874 # Quick test that logging works -- does not test logging output
2875 # 2875 #
2876 2876
2877 class _TestLogging(BaseTestCase): 2877 class _TestLogging(BaseTestCase):
2878 2878
2879 ALLOWED_TYPES = ('processes',) 2879 ALLOWED_TYPES = ('processes',)
2880 2880
2881 def test_enable_logging(self): 2881 def test_enable_logging(self):
2882 logger = multiprocessing.get_logger() 2882 logger = multiprocessing.get_logger()
2883 logger.setLevel(util.SUBWARNING) 2883 logger.setLevel(util.SUBWARNING)
2884 self.assertTrue(logger is not None) 2884 self.assertIsNotNone(logger)
2885 logger.debug('this will not be printed') 2885 logger.debug('this will not be printed')
2886 logger.info('nor will this') 2886 logger.info('nor will this')
2887 logger.setLevel(LOG_LEVEL) 2887 logger.setLevel(LOG_LEVEL)
2888 2888
2889 @classmethod 2889 @classmethod
2890 def _test_level(cls, conn): 2890 def _test_level(cls, conn):
2891 logger = multiprocessing.get_logger() 2891 logger = multiprocessing.get_logger()
2892 conn.send(logger.getEffectiveLevel()) 2892 conn.send(logger.getEffectiveLevel())
2893 2893
2894 def test_level(self): 2894 def test_level(self):
(...skipping 716 matching lines...) Expand 10 before | Expand all | Expand 10 after
3611 count += 1 3611 count += 1
3612 finally: 3612 finally:
3613 multiprocessing.set_start_method(old_method, force=True) 3613 multiprocessing.set_start_method(old_method, force=True)
3614 self.assertGreaterEqual(count, 1) 3614 self.assertGreaterEqual(count, 1)
3615 3615
3616 def test_get_all(self): 3616 def test_get_all(self):
3617 methods = multiprocessing.get_all_start_methods() 3617 methods = multiprocessing.get_all_start_methods()
3618 if sys.platform == 'win32': 3618 if sys.platform == 'win32':
3619 self.assertEqual(methods, ['spawn']) 3619 self.assertEqual(methods, ['spawn'])
3620 else: 3620 else:
3621 self.assertTrue(methods == ['fork', 'spawn'] or 3621 self.assertIn(methods, (['fork', 'spawn'],
3622 methods == ['fork', 'spawn', 'forkserver']) 3622 ['fork', 'spawn', 'forkserver']))
3623 3623
3624 # 3624 #
3625 # Check that killing process does not leak named semaphores 3625 # Check that killing process does not leak named semaphores
3626 # 3626 #
3627 3627
3628 @unittest.skipIf(sys.platform == "win32", 3628 @unittest.skipIf(sys.platform == "win32",
3629 "test semantics don't make sense on Windows") 3629 "test semantics don't make sense on Windows")
3630 class TestSemaphoreTracker(unittest.TestCase): 3630 class TestSemaphoreTracker(unittest.TestCase):
3631 def test_semaphore_tracker(self): 3631 def test_semaphore_tracker(self):
3632 import subprocess 3632 import subprocess
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after
3821 tmp = set(multiprocessing.process._dangling) - set(dangling[0]) 3821 tmp = set(multiprocessing.process._dangling) - set(dangling[0])
3822 if tmp: 3822 if tmp:
3823 print('Dangling processes:', tmp, file=sys.stderr) 3823 print('Dangling processes:', tmp, file=sys.stderr)
3824 del tmp 3824 del tmp
3825 tmp = set(threading._dangling) - set(dangling[1]) 3825 tmp = set(threading._dangling) - set(dangling[1])
3826 if tmp: 3826 if tmp:
3827 print('Dangling threads:', tmp, file=sys.stderr) 3827 print('Dangling threads:', tmp, file=sys.stderr)
3828 3828
3829 remote_globs['setUpModule'] = setUpModule 3829 remote_globs['setUpModule'] = setUpModule
3830 remote_globs['tearDownModule'] = tearDownModule 3830 remote_globs['tearDownModule'] = tearDownModule
LEFTRIGHT

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