Skip to content

Commit

Permalink
bpo-38762: Extend logging.test_multiprocessing to cover missing cases. (
Browse files Browse the repository at this point in the history
  • Loading branch information
iritkatriel committed Sep 8, 2020
1 parent 2de50f2 commit 3fd6999
Showing 1 changed file with 56 additions and 6 deletions.
62 changes: 56 additions & 6 deletions Lib/test/test_logging.py
Expand Up @@ -4354,15 +4354,65 @@ def test_dict_arg(self):
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({})
Expand Down

0 comments on commit 3fd6999

Please sign in to comment.