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)
|
r.removeHandler(h)
|
||||||
h.close()
|
h.close()
|
||||||
|
|
||||||
def test_multiprocessing(self):
|
@staticmethod # pickled as target of child process in the following test
|
||||||
r = logging.makeLogRecord({})
|
def _extract_logrecord_process_name(key, logMultiprocessing, conn=None):
|
||||||
self.assertEqual(r.processName, 'MainProcess')
|
prev_logMultiprocessing = logging.logMultiprocessing
|
||||||
|
logging.logMultiprocessing = logMultiprocessing
|
||||||
try:
|
try:
|
||||||
import multiprocessing as mp
|
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({})
|
r = logging.makeLogRecord({})
|
||||||
self.assertEqual(r.processName, mp.current_process().name)
|
self.assertEqual(r.processName, 'MainProcess')
|
||||||
except ImportError:
|
|
||||||
pass
|
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):
|
def test_optional(self):
|
||||||
r = logging.makeLogRecord({})
|
r = logging.makeLogRecord({})
|
||||||
|
|
Loading…
Reference in New Issue