bpo-38762: Extend logging.test_multiprocessing to cover missing cases. (GH-22142)
This commit is contained in:
parent
2de50f2600
commit
3fd69991f4
|
@ -4354,15 +4354,65 @@ class LogRecordTest(BaseTest):
|
|||
r.removeHandler(h)
|
||||
h.close()
|
||||
|
||||
def test_multiprocessing(self):
|
||||
r = logging.makeLogRecord({})
|
||||
self.assertEqual(r.processName, 'MainProcess')
|
||||
@staticmethod # pickled as target of child process in the following test
|
||||
def _extract_logrecord_process_name(key, logMultiprocessing, conn=None):
|
||||
prev_logMultiprocessing = logging.logMultiprocessing
|
||||
logging.logMultiprocessing = logMultiprocessing
|
||||
try:
|
||||
import multiprocessing as mp
|
||||
name = mp.current_process().name
|
||||
|
||||
r1 = logging.makeLogRecord({'msg': f'msg1_{key}'})
|
||||
del sys.modules['multiprocessing']
|
||||
r2 = logging.makeLogRecord({'msg': f'msg2_{key}'})
|
||||
|
||||
results = {'processName' : name,
|
||||
'r1.processName': r1.processName,
|
||||
'r2.processName': r2.processName,
|
||||
}
|
||||
finally:
|
||||
logging.logMultiprocessing = prev_logMultiprocessing
|
||||
if conn:
|
||||
conn.send(results)
|
||||
else:
|
||||
return results
|
||||
|
||||
def test_multiprocessing(self):
|
||||
multiprocessing_imported = 'multiprocessing' in sys.modules
|
||||
try:
|
||||
# logMultiprocessing is True by default
|
||||
self.assertEqual(logging.logMultiprocessing, True)
|
||||
|
||||
LOG_MULTI_PROCESSING = True
|
||||
# When logMultiprocessing == True:
|
||||
# In the main process processName = 'MainProcess'
|
||||
r = logging.makeLogRecord({})
|
||||
self.assertEqual(r.processName, mp.current_process().name)
|
||||
except ImportError:
|
||||
pass
|
||||
self.assertEqual(r.processName, 'MainProcess')
|
||||
|
||||
results = self._extract_logrecord_process_name(1, LOG_MULTI_PROCESSING)
|
||||
self.assertEqual('MainProcess', results['processName'])
|
||||
self.assertEqual('MainProcess', results['r1.processName'])
|
||||
self.assertEqual('MainProcess', results['r2.processName'])
|
||||
|
||||
# In other processes, processName is correct when multiprocessing in imported,
|
||||
# but it is (incorrectly) defaulted to 'MainProcess' otherwise (bpo-38762).
|
||||
import multiprocessing
|
||||
parent_conn, child_conn = multiprocessing.Pipe()
|
||||
p = multiprocessing.Process(
|
||||
target=self._extract_logrecord_process_name,
|
||||
args=(2, LOG_MULTI_PROCESSING, child_conn,)
|
||||
)
|
||||
p.start()
|
||||
results = parent_conn.recv()
|
||||
self.assertNotEqual('MainProcess', results['processName'])
|
||||
self.assertEqual(results['processName'], results['r1.processName'])
|
||||
self.assertEqual('MainProcess', results['r2.processName'])
|
||||
p.join()
|
||||
|
||||
finally:
|
||||
if multiprocessing_imported:
|
||||
import multiprocessing
|
||||
|
||||
|
||||
def test_optional(self):
|
||||
r = logging.makeLogRecord({})
|
||||
|
|
Loading…
Reference in New Issue