Skip to content

Commit 1cc9e3b

Browse files
committed
test(logmanager): fix assert to wait log creation
1 parent 8af465c commit 1cc9e3b

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

app/managers/log_manager.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ def __init__(self):
3939
self._setup_handler(LogFormat.JSON)
4040
self.current_format = LogFormat.JSON
4141

42+
# Store references to automatic log tasks
43+
self._automatic_log_tasks = []
44+
4245
# Start automatic logging if enabled
4346
if os.getenv("ENABLE_AUTOMATIC_LOGS", "false").lower() == "true":
4447
self._start_automatic_logs()
@@ -89,17 +92,19 @@ def _start_automatic_logs(self):
8992
)
9093

9194
# Create single log immediately
92-
asyncio.create_task(
95+
task_single = asyncio.create_task(
9396
self._create_single_log(log_data, getattr(logging, log_data.level.upper()))
9497
)
98+
self._automatic_log_tasks.append(task_single)
9599

96100
# Setup recurring logs if needed
97101
if log_data.interval and log_data.duration:
98-
asyncio.create_task(
102+
task_recurring = asyncio.create_task(
99103
self._create_recurring_logs(
100104
log_data, log_data.interval, log_data.duration
101105
)
102106
)
107+
self._automatic_log_tasks.append(task_recurring)
103108

104109
async def create_log(self, log_data: LogRequest) -> dict | str:
105110
"""Create log"""

tests/unit/test_log_manager.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,19 +199,19 @@ async def test_automatic_logs_initialization(self, monkeypatch, caplog):
199199
# Create manager with captured logs
200200
with caplog.at_level(logging.INFO, logger="app.managers.log_manager"):
201201
manager = LogManager()
202-
# Wait a bit for the log to be created
203-
await asyncio.sleep(0.1)
202+
# Attendre explicitement la fin des tâches automatiques
203+
if manager._automatic_log_tasks:
204+
await asyncio.gather(*manager._automatic_log_tasks)
204205

205-
# Verify logs were created with correct configuration
206206
assert any(
207207
"Test auto message" in record.message for record in caplog.records
208208
), "Expected log message not found in records"
209209
assert all(
210-
record.levelname == "INFO" for record in caplog.records
210+
record.levelname == "INFO" for record in caplog.records if "Test auto message" in record.message
211211
), "Not all logs are at INFO level"
212212
assert all(
213213
hasattr(record, "service") and record.service == "test-service"
214-
for record in caplog.records
214+
for record in caplog.records if "Test auto message" in record.message
215215
), "Service attribute missing or incorrect"
216216
assert isinstance(
217217
manager.logger.handlers[0].formatter, JsonFormatter

0 commit comments

Comments
 (0)