2# +==== BEGIN rotary_logger =================+
4# ..........####...####..........
5# ......###.....#.#########......
6# ....##........#.###########....
7# ...#..........#.############...
8# ...#..........#.#####.######...
9# ..#.....##....#.###..#...####..
10# .#.....#.##...#.##..##########.
11# #.....##########....##...######
12# #.....#...##..#.##..####.######
13# .#...##....##.#.##..###..#####.
14# ..#.##......#.#.####...######..
15# ..#...........#.#############..
16# ..#...........#.#############..
17# ...##.........#.############...
18# ......#.......#.#########......
19# .......#......#.########.......
20# .........#####...#####.........
22# PROJECT: rotary_logger
23# FILE: test_control_functions.py
24# CREATION DATE: 02-11-2025
25# LAST Modified: 3:41:37 04-03-2026
27# A module that provides a universal python light on iops way of logging to files your program execution.
29# COPYRIGHT: (c) Asperguide
30# PURPOSE: File in charge of testing the control functions that provide status and information.
32# +==== END rotary_logger =================+
39from concurrent.futures
import ThreadPoolExecutor
40from pathlib
import Path
51 rl.start_logging(log_folder=tmp_path, merged=
False)
54 assert getattr(rl,
"_atexit_registered",
False)
in (
True,
False)
56 if rl._atexit_registered:
57 assert hasattr(rl,
"_registered_flushers")
58 assert len(rl._registered_flushers) >= 1
62 assert sys.stdout
is orig_out
63 assert getattr(rl,
"_atexit_registered",
False)
is False
68 rl.start_logging(log_folder=tmp_path, merged=
False)
71 assert rl.is_logging()
is True
74 paused = rl.pause_logging()
77 assert rl.is_logging()
is False
80 resumed = rl.resume_logging()
82 assert resumed
is False
83 assert rl.is_logging()
is True
90 while not stop_event.is_set():
97 rl.start_logging(log_folder=tmp_path, merged=
False)
99 stop_event = threading.Event()
104 t = threading.Thread(target=_toggle_pause_resume,
105 args=(rl, stop_event))
121 assert isinstance(rl.is_logging(), bool)
126 """pause_logging(toggle=False) should always pause, even if already paused."""
128 rl.start_logging(log_folder=tmp_path, merged=
False)
129 assert rl.is_logging()
132 result = rl.pause_logging(toggle=
False)
133 assert result
is True
136 result = rl.pause_logging(toggle=
False)
137 assert result
is True
143 """resume_logging(toggle=True) should pause when the logger is currently running."""
145 rl.start_logging(log_folder=tmp_path, merged=
False)
146 assert rl.is_logging()
149 result = rl.resume_logging(toggle=
True)
150 assert result
is True
151 assert not rl.is_logging()
157 """is_redirected() should return False before and after stop_logging, True during."""
158 from rotary_logger
import constants
as CONST
161 assert rl.is_redirected(CONST.StdMode.STDOUT)
is False
162 assert rl.is_redirected(CONST.StdMode.STDERR)
is False
164 rl.start_logging(log_folder=tmp_path, merged=
False)
165 assert rl.is_redirected(CONST.StdMode.STDOUT)
is True
166 assert rl.is_redirected(CONST.StdMode.STDERR)
is True
169 assert rl.is_redirected(CONST.StdMode.STDOUT)
is False
170 assert rl.is_redirected(CONST.StdMode.STDERR)
is False
174 """Calling the RotaryLogger instance like a function should start logging."""
176 orig_out = sys.stdout
179 assert rl.is_logging()
183 if sys.stdout
is not orig_out
and not hasattr(sys.stdout,
'original_stream'):
184 sys.stdout = orig_out
188 """stop_logging should restore sys.stdin and clear stdin_stream."""
191 rl.start_logging(log_folder=tmp_path, merged=
True)
192 assert rl.stdin_stream
is not None
195 assert rl.stdin_stream
is None
196 assert sys.stdin
is orig_in
_toggle_pause_resume(RotaryLogger rl, threading.Event stop_event)
test_resume_logging_toggle_true_pauses_when_active(Path tmp_path)
test_stop_logging_clears_stdin_stream(Path tmp_path)
test_is_redirected_reflects_logging_state(Path tmp_path)
test_pause_and_resume_toggle(Path tmp_path)
test_concurrent_pause_resume_stress(Path tmp_path)
test_start_stop_registers_and_unregisters_atexit(Path tmp_path)
test_pause_logging_toggle_false_always_pauses(Path tmp_path)
test_rotary_logger_callable_starts_logging(Path tmp_path)