11import pytest
22import logging
33
4- from typing import Optional
4+ from typing import Optional , override
55from monero import (
66 MoneroConnectionManager , MoneroRpcConnection , MoneroConnectionPollType
77)
88from utils import (
99 ConnectionChangeCollector , TestUtils as Utils ,
10- AssertUtils , RpcConnectionUtils
10+ AssertUtils , RpcConnectionUtils , BaseTestClass
1111)
1212
1313logger : logging .Logger = logging .getLogger ("TestMoneroConnectionManager" )
1414
1515
1616@pytest .mark .integration
17- class TestMoneroConnectionManager :
17+ class TestMoneroConnectionManager ( BaseTestClass ) :
1818 """Connection manager integration tests"""
1919
2020 OFFLINE_PROXY_URI : str = "127.0.0.1:9050"
2121 """Proxy used to simulate offline servers"""
2222
2323 _cm : MoneroConnectionManager | None = None
24+ """Connection manager test instance."""
2425
2526 #region Fixtures
2627
27- # Setup and teardown of test class
28- @pytest .fixture (scope = "class" , autouse = True )
29- def global_setup_and_teardown (self ):
30- """Executed once before all tests"""
31- self .before_all ()
32- yield
33- self .after_all ()
34-
3528 # Before all tests
29+ @override
3630 def before_all (self ) -> None :
37- """Executed once before all tests"""
38- logger .info (f"Setup test class { type (self ).__name__ } " )
31+ super ().before_all ()
3932 self ._cm = MoneroConnectionManager ()
4033
4134 # After all tests
35+ @override
4236 def after_all (self ) -> None :
43- """Executed once after all tests"""
44- logger .info (f"Teardown test class { type (self ).__name__ } " )
37+ super ().after_all ()
4538 if self ._cm :
4639 self ._cm .reset ()
4740 logger .debug ("Resetted connection manager" )
48- else :
49- logger .warning ("Test connection manager is not set!" )
5041
5142 Utils .RPC_WALLET_MANAGER .clear ()
5243
53- # setup and teardown of each test
54- @pytest .fixture (autouse = True )
55- def setup_and_teardown (self , request : pytest .FixtureRequest ):
56- logger .info (f"Before { request .node .name } " ) # type: ignore
57- yield
58- logger .info (f"After { request .node .name } " ) # type: ignore
59-
6044 # test connnections fixture
6145 @pytest .fixture (scope = "class" )
6246 def connections (self ) -> list [MoneroRpcConnection ]:
@@ -122,7 +106,7 @@ def test_connection_manager(self, connection_manager: MoneroConnectionManager, c
122106
123107 # auto connect to the best available connection
124108 connection_manager .start_polling (Utils .SYNC_PERIOD_IN_MS )
125- listener .wait_for_change (Utils .SYNC_PERIOD_IN_MS , "Waiting for auto connect to best available connection" )
109+ listener .wait_for_change (num_expected_changes + 1 , Utils .SYNC_PERIOD_IN_MS , "Waiting for auto connect to best available connection" )
126110 assert connection_manager .is_connected ()
127111 connection = connection_manager .get_connection ()
128112 assert connection is not None
@@ -166,7 +150,7 @@ def test_connection_manager(self, connection_manager: MoneroConnectionManager, c
166150 continue
167151 conn .proxy_uri = self .OFFLINE_PROXY_URI
168152
169- listener .wait_for_change (Utils .SYNC_PERIOD_IN_MS , "Simulating priotizized servers shut down" )
153+ listener .wait_for_change (num_expected_changes + 1 , Utils .SYNC_PERIOD_IN_MS , "Simulating priotizized servers shut down" )
170154 assert connection_manager .is_connected () is False , f"{ connection_manager .get_connection ().serialize ()} "
171155 connection = connection_manager .get_connection ()
172156
@@ -330,7 +314,7 @@ def test_connection_manager(self, connection_manager: MoneroConnectionManager, c
330314 poll_type = MoneroConnectionPollType .CURRENT
331315 )
332316
333- listener .wait_for_change (Utils .SYNC_PERIOD_IN_MS , "Polling current connection" )
317+ listener .wait_for_change (num_expected_changes + 1 , Utils .SYNC_PERIOD_IN_MS , "Polling current connection" )
334318 assert connection_manager .is_connected () is True
335319 num_expected_changes += 1
336320 assert num_expected_changes == listener .num_changed_connections
@@ -340,7 +324,7 @@ def test_connection_manager(self, connection_manager: MoneroConnectionManager, c
340324 num_expected_changes += 1
341325 assert num_expected_changes == listener .num_changed_connections
342326 connection_manager .start_polling (period_ms = Utils .SYNC_PERIOD_IN_MS , poll_type = MoneroConnectionPollType .ALL )
343- listener .wait_for_change (Utils .SYNC_PERIOD_IN_MS , "Polling all connections" )
327+ listener .wait_for_change (num_expected_changes + 1 , Utils .SYNC_PERIOD_IN_MS , "Polling all connections" )
344328 assert connection_manager .is_connected () is True
345329 num_expected_changes += 1
346330 assert num_expected_changes == listener .num_changed_connections
@@ -351,7 +335,7 @@ def test_connection_manager(self, connection_manager: MoneroConnectionManager, c
351335 for con in ordered_connections :
352336 con .proxy_uri = self .OFFLINE_PROXY_URI
353337
354- listener .wait_for_change (Utils .SYNC_PERIOD_IN_MS , "Simulating total shut down" )
338+ listener .wait_for_change (num_expected_changes + 1 , Utils .SYNC_PERIOD_IN_MS , "Simulating total shut down" )
355339 assert connection .is_online () is False , f"Expected offline connection: { connection .serialize ()} "
356340 num_expected_changes += 1
357341 assert num_expected_changes == listener .num_changed_connections
0 commit comments