|
5 | 5 |
|
6 | 6 | import asyncio |
7 | 7 | import logging |
8 | | -from collections.abc import Mapping |
9 | 8 | from typing import Any |
10 | 9 |
|
11 | 10 | import pytest |
@@ -77,78 +76,69 @@ async def test_logging_config_updating_actor( |
77 | 76 | # is not working anyway - python ignores it. |
78 | 77 | mocker.patch("frequenz.sdk.config._logging_actor.logging.basicConfig") |
79 | 78 |
|
80 | | - config_channel = Broadcast[Mapping[str, Any]](name="config") |
81 | | - config_sender = config_channel.new_sender() |
82 | | - async with LoggingConfigUpdatingActor( |
83 | | - config_recv=config_channel.new_receiver().map( |
84 | | - lambda app_config: app_config.get("logging", {}) |
85 | | - ) |
86 | | - ) as actor: |
| 79 | + # Mock ConfigManager |
| 80 | + mock_config_manager = mocker.Mock() |
| 81 | + mock_config_manager.config_channel = Broadcast[LoggingConfig | Exception | None]( |
| 82 | + name="config" |
| 83 | + ) |
| 84 | + mock_config_manager.new_receiver = mocker.Mock( |
| 85 | + return_value=mock_config_manager.config_channel.new_receiver() |
| 86 | + ) |
| 87 | + |
| 88 | + async with LoggingConfigUpdatingActor(mock_config_manager) as actor: |
87 | 89 | assert logging.getLogger("frequenz.sdk.actor").level == logging.NOTSET |
88 | 90 | assert logging.getLogger("frequenz.sdk.timeseries").level == logging.NOTSET |
89 | 91 |
|
90 | 92 | update_logging_spy = mocker.spy(actor, "_update_logging") |
91 | 93 |
|
92 | 94 | # Send first config |
93 | | - await config_sender.send( |
94 | | - { |
95 | | - "logging": { |
96 | | - "root_logger": {"level": "ERROR"}, |
97 | | - "loggers": { |
98 | | - "frequenz.sdk.actor": {"level": "DEBUG"}, |
99 | | - "frequenz.sdk.timeseries": {"level": "ERROR"}, |
100 | | - }, |
101 | | - } |
102 | | - } |
| 95 | + expected_config = LoggingConfig( |
| 96 | + root_logger=LoggerConfig(level="ERROR"), |
| 97 | + loggers={ |
| 98 | + "frequenz.sdk.actor": LoggerConfig(level="DEBUG"), |
| 99 | + "frequenz.sdk.timeseries": LoggerConfig(level="ERROR"), |
| 100 | + }, |
103 | 101 | ) |
| 102 | + await mock_config_manager.config_channel.new_sender().send(expected_config) |
104 | 103 | await asyncio.sleep(0.01) |
105 | | - update_logging_spy.assert_called_once_with( |
106 | | - LoggingConfig( |
107 | | - root_logger=LoggerConfig(level="ERROR"), |
108 | | - loggers={ |
109 | | - "frequenz.sdk.actor": LoggerConfig(level="DEBUG"), |
110 | | - "frequenz.sdk.timeseries": LoggerConfig(level="ERROR"), |
111 | | - }, |
112 | | - ) |
113 | | - ) |
| 104 | + update_logging_spy.assert_called_once_with(expected_config) |
114 | 105 | assert logging.getLogger("frequenz.sdk.actor").level == logging.DEBUG |
115 | 106 | assert logging.getLogger("frequenz.sdk.timeseries").level == logging.ERROR |
116 | 107 | update_logging_spy.reset_mock() |
117 | 108 |
|
118 | | - # Update config |
119 | | - await config_sender.send( |
120 | | - { |
121 | | - "logging": { |
122 | | - "root_logger": {"level": "WARNING"}, |
123 | | - "loggers": { |
124 | | - "frequenz.sdk.actor": {"level": "INFO"}, |
125 | | - }, |
126 | | - } |
127 | | - } |
| 109 | + # Send an exception and verify the previous config is maintained |
| 110 | + await mock_config_manager.config_channel.new_sender().send( |
| 111 | + ValueError("Test error") |
128 | 112 | ) |
129 | 113 | await asyncio.sleep(0.01) |
| 114 | + update_logging_spy.assert_not_called() # Should not try to update logging |
| 115 | + # Previous config should be maintained |
| 116 | + assert logging.getLogger("frequenz.sdk.actor").level == logging.DEBUG |
| 117 | + assert logging.getLogger("frequenz.sdk.timeseries").level == logging.ERROR |
| 118 | + assert ( |
| 119 | + actor._current_config == expected_config # pylint: disable=protected-access |
| 120 | + ) # pylint: disable=protected-access |
| 121 | + update_logging_spy.reset_mock() |
| 122 | + |
| 123 | + # Update config |
130 | 124 | expected_config = LoggingConfig( |
131 | 125 | root_logger=LoggerConfig(level="WARNING"), |
132 | 126 | loggers={ |
133 | 127 | "frequenz.sdk.actor": LoggerConfig(level="INFO"), |
134 | 128 | }, |
135 | 129 | ) |
| 130 | + await mock_config_manager.config_channel.new_sender().send(expected_config) |
| 131 | + await asyncio.sleep(0.01) |
136 | 132 | update_logging_spy.assert_called_once_with(expected_config) |
137 | 133 | assert logging.getLogger("frequenz.sdk.actor").level == logging.INFO |
138 | 134 | assert logging.getLogger("frequenz.sdk.timeseries").level == logging.NOTSET |
139 | 135 | update_logging_spy.reset_mock() |
140 | 136 |
|
141 | | - # Send invalid config to make sure actor doesn't crash and doesn't setup invalid config. |
142 | | - await config_sender.send({"logging": {"root_logger": {"level": "UNKNOWN"}}}) |
143 | | - await asyncio.sleep(0.01) |
144 | | - update_logging_spy.assert_not_called() |
145 | | - assert actor._current_config == expected_config |
146 | | - update_logging_spy.reset_mock() |
147 | | - |
148 | | - # Send empty config to reset logging to default |
149 | | - await config_sender.send({"other": {"var1": 1}}) |
| 137 | + # Send a None config to make sure actor doesn't crash and configures a default logging |
| 138 | + await mock_config_manager.config_channel.new_sender().send(None) |
150 | 139 | await asyncio.sleep(0.01) |
151 | 140 | update_logging_spy.assert_called_once_with(LoggingConfig()) |
152 | | - assert logging.getLogger("frequenz.sdk.actor").level == logging.NOTSET |
153 | | - assert logging.getLogger("frequenz.sdk.timeseries").level == logging.NOTSET |
| 141 | + assert ( |
| 142 | + actor._current_config == LoggingConfig() # pylint: disable=protected-access |
| 143 | + ) |
154 | 144 | update_logging_spy.reset_mock() |
0 commit comments