network: Avoid notifier re-creating in connect function#621
network: Avoid notifier re-creating in connect function#621xndcn wants to merge 1 commit intocanopen-python:masterfrom
Conversation
|
ping, thanks |
|
If network connect again without disconnect, current implementation will not re-create bus, but notifier will always be re-created. I think it is better to check the notifier in connect() and release it in disconnect, as well as the bus object. |
|
The fix is correct – without this patch, calling Two suggestions: 1. Simplify The try:
self.check()
finally:
self.notifier = None2. Add regression tests Consider adding tests to def test_network_connect_does_not_recreate_notifier(self):
self.network.connect(interface="virtual")
self.addCleanup(self.network.disconnect)
notifier1 = self.network.notifier
self.assertIsNotNone(notifier1)
# Calling connect() again should reuse the existing notifier
self.network.connect(interface="virtual")
self.assertIs(self.network.notifier, notifier1)
def test_network_disconnect_releases_notifier(self):
self.network.connect(interface="virtual")
self.assertIsNotNone(self.network.notifier)
self.network.disconnect()
self.assertIsNone(self.network.notifier)
def test_network_disconnect_releases_notifier_on_exception(self):
self.network.connect(interface="virtual")
class Custom(Exception):
pass
self.network.notifier.exception = Custom("fake")
with self.assertRaises(Custom):
with self.assertLogs(level=logging.ERROR):
self.network.disconnect()
# Notifier must be released even when check() raises
self.assertIsNone(self.network.notifier)I verified locally that all three tests pass with the patched code. |
No description provided.