-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import logging | ||
import multiprocessing | ||
import os | ||
import sys | ||
from textwrap import dedent | ||
|
||
from craft_cli import emit, EmitterMode | ||
|
||
logger = logging.getLogger() | ||
|
||
|
||
def strip_timestamps(text: str) -> str: | ||
lines = [] | ||
for line in text.splitlines(): | ||
lines.append(line.split(" ", maxsplit=2)[-1]) | ||
return "\n".join(lines) | ||
|
||
|
||
def callable_func(): | ||
logger.info("Message 1 from CHILD process") | ||
logger.info("Message 2 from CHILD process") | ||
|
||
|
||
def test_logging_in_multiprocess(tmp_path): | ||
logger.setLevel(logging.INFO) | ||
emitter_log = tmp_path / "emitter_log.txt" | ||
filehandler_log = tmp_path / "filehandler_log.txt" | ||
|
||
filehandler = logging.FileHandler(filehandler_log) | ||
os.set_inheritable(filehandler.stream.fileno(), True) | ||
logger.addHandler(filehandler) | ||
emit.init(mode=EmitterMode.QUIET, appname="testapp", greeting="hi", log_filepath=emitter_log) | ||
|
||
logger.info("Message 1 from PARENT process") | ||
logger.info("Message 2 from PARENT process") | ||
|
||
child = multiprocessing.Process(target=callable_func) | ||
child.start() | ||
child.join() | ||
|
||
logger.info("Message 3 from PARENT process") | ||
|
||
emit.ended_ok() | ||
|
||
emitter_logged = strip_timestamps(emitter_log.read_text()) | ||
filehandler_logged = filehandler_log.read_text() | ||
|
||
if sys.platform == "linux": | ||
# Expect two messages from the parent process, then two from the child process, | ||
# then a final one from the parent again. | ||
expected_text = dedent( | ||
"""\ | ||
Message 1 from PARENT process | ||
Message 2 from PARENT process | ||
Message 1 from CHILD process | ||
Message 2 from CHILD process | ||
Message 3 from PARENT process | ||
""" | ||
) | ||
else: | ||
# Messages from the child process are NOT logged in non-Linux platforms. This | ||
# is not by design, but a record of the current expected behavior. | ||
expected_text = dedent( | ||
"""\ | ||
Message 1 from PARENT process | ||
Message 2 from PARENT process | ||
Message 3 from PARENT process | ||
""" | ||
) | ||
|
||
assert emitter_logged == "hi\n" + expected_text.rstrip() | ||
assert filehandler_logged == expected_text |