11import asyncio
22import logging
3- from unittest import IsolatedAsyncioTestCase
4- from unittest .mock import AsyncMock , MagicMock , call
3+ from unittest import TestCase
54
65from mqlog import MqttHandler
76
87
9- class TestMqttHandler (IsolatedAsyncioTestCase ):
8+ class FakeClient :
9+ """A fake MQTT client for testing purposes."""
10+
11+ def __init__ (self ):
12+ self .calls = []
13+
14+ # Store the call like a mock so we can check it later
15+ async def publish (self , topic , payload , qos = 0 ):
16+ self .calls .append ((topic , payload , qos ))
17+
18+
19+ class TestMqttHandler (TestCase ):
1020 def setUp (self ):
11- self .client = MagicMock ()
12- self .client .publish = AsyncMock ()
21+ self .client = FakeClient ()
1322 self .handler = MqttHandler (self .client , "test_topic" )
23+ self .handler .setFormatter (logging .Formatter ("%(message)s" ))
1424 self .logger = logging .getLogger ("test" )
1525 self .logger .addHandler (self .handler )
1626 self .logger .setLevel (logging .INFO )
1727
18- # Run for X seconds, then stop
19- async def run_with_timeout (self , timeout = 1 ):
20- async with asyncio .timeout (timeout ):
21- await self .handler .run ()
22- self .handler .close ()
23-
24- # Log a message at a given level after a delay of X seconds
25- async def log_message (self , msg , level , delay ):
26- await asyncio .sleep (delay )
27- self .logger .log (level , msg )
28-
2928 def test_no_flush (self ):
3029 """Buffer should not be flushed until capacity/level reached"""
3130 self .handler .flush_level = logging .WARNING
@@ -45,35 +44,50 @@ def test_flush_full_buffer(self):
4544 self .logger .info (f"Test message { i } " )
4645 self .assertTrue (self .handler .will_flush .is_set ())
4746
48- async def test_publish (self ):
47+ def test_publish (self ):
4948 """Flushing should publish messages to MQTT topic"""
5049 self .handler .flush_level = logging .ERROR
51- await asyncio .gather (
52- self .run_with_timeout (1 ),
53- self .log_message ("Test message 1" , logging .INFO , 0.1 ),
54- self .log_message ("Test message 2" , logging .ERROR , 0.2 ),
55- return_exceptions = True ,
56- )
57- self .client .publish .assert_awaited_with (
58- "test_topic" ,
59- "Test message 1\n Test message 2" ,
60- qos = 0 ,
50+
51+ async def do_test (handler , logger ):
52+ logger .info ("Test message 1" )
53+ await asyncio .sleep (0.1 )
54+ logger .error ("Test message 2" )
55+ await asyncio .sleep (0.1 )
56+ handler .stop ()
57+
58+ async def main (handler , logger ):
59+ await asyncio .gather (handler .run (), do_test (handler , logger ))
60+
61+ asyncio .run (main (self .handler , self .logger ))
62+
63+ self .assertEqual (
64+ self .client .calls , [("test_topic" , "Test message 1\n Test message 2" , 0 )]
6165 )
6266
63- async def test_flush_multiple (self ):
67+ def test_flush_multiple (self ):
6468 """Flushing multiple times should publish separate messages"""
6569 self .handler .capacity = 2
66- await asyncio .gather (
67- self .run_with_timeout (1 ),
68- self .log_message ("Test message 1" , logging .INFO , 0.1 ),
69- self .log_message ("Test message 2" , logging .INFO , 0.2 ),
70- self .log_message ("Test message 3" , logging .INFO , 0.3 ),
71- self .log_message ("Test message 4" , logging .INFO , 0.4 ),
72- return_exceptions = True ,
73- )
74- self .client .publish .assert_has_awaits (
70+
71+ async def do_test (handler , logger ):
72+ logger .info ("Test message 1" )
73+ await asyncio .sleep (0.1 )
74+ logger .info ("Test message 2" )
75+ await asyncio .sleep (0.1 )
76+ logger .info ("Test message 3" )
77+ await asyncio .sleep (0.1 )
78+ logger .info ("Test message 4" )
79+ await asyncio .sleep (0.1 )
80+ handler .stop ()
81+
82+ async def main (handler , logger ):
83+ await asyncio .gather (handler .run (), do_test (handler , logger ))
84+
85+ asyncio .run (main (self .handler , self .logger ))
86+
87+ self .assertEqual (
88+ self .client .calls ,
7589 [
76- call ("test_topic" , "Test message 1\n Test message 2" , qos = 0 ),
77- call ("test_topic" , "Test message 3\n Test message 4" , qos = 0 ),
78- ]
90+ ("test_topic" , "Test message 1\n Test message 2" , 0 ),
91+ ("test_topic" , "Test message 3\n Test message 4" , 0 ),
92+ ],
7993 )
0 commit comments