Skip to content

Commit 012ad62

Browse files
xndcnbizfsc
authored andcommitted
test: Add regression tests for notifier lifecycle
Add tests to verify that: - connect() does not recreate an existing notifier - disconnect() releases the notifier - disconnect() releases the notifier even when check() raises Co-authored-by: Frieder Schüler <frieder.schueler@bizerba.com>
1 parent 8f4363a commit 012ad62

1 file changed

Lines changed: 30 additions & 1 deletion

File tree

test/test_network.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ def test_network_check(self):
6969
def cleanup():
7070
# We must clear the fake exception installed below, since
7171
# .disconnect() implicitly calls .check() during test tear down.
72-
self.network.notifier.exception = None
72+
if self.network.notifier is not None:
73+
self.network.notifier.exception = None
7374
self.network.disconnect()
7475

7576
self.addCleanup(cleanup)
@@ -285,6 +286,34 @@ def wait_for_periodicity():
285286
if msg is not None:
286287
self.assertIsNone(bus.recv(PERIOD))
287288

289+
def test_network_connect_does_not_recreate_notifier(self):
290+
self.network.connect(interface="virtual")
291+
self.addCleanup(self.network.disconnect)
292+
notifier1 = self.network.notifier
293+
self.assertIsNotNone(notifier1)
294+
# Calling connect() again should reuse the existing notifier
295+
self.network.connect(interface="virtual")
296+
self.assertIs(self.network.notifier, notifier1)
297+
298+
def test_network_disconnect_releases_notifier(self):
299+
self.network.connect(interface="virtual")
300+
self.assertIsNotNone(self.network.notifier)
301+
self.network.disconnect()
302+
self.assertIsNone(self.network.notifier)
303+
304+
def test_network_disconnect_releases_notifier_on_exception(self):
305+
self.network.connect(interface="virtual")
306+
307+
class Custom(Exception):
308+
pass
309+
310+
self.network.notifier.exception = Custom("fake")
311+
with self.assertRaises(Custom):
312+
with self.assertLogs(level=logging.ERROR):
313+
self.network.disconnect()
314+
# Notifier must be released even when check() raises
315+
self.assertIsNone(self.network.notifier)
316+
288317

289318
class TestScanner(unittest.TestCase):
290319
TIMEOUT = 0.1

0 commit comments

Comments
 (0)