From 13cc75cf03c94dcb3e2fa5329e8a1ba0c7606acb Mon Sep 17 00:00:00 2001 From: Anze Kolar Date: Thu, 20 Jul 2017 15:28:01 +0200 Subject: [PATCH] Allow client to do sequential connect-disconnect With introduction of the kill switch it was impossible to restart the consumption of the OGN messages after a .disconnect() method has been called. This commit resets the kill flag after .connect() has been called. Minimal example: ``` client = AprsClient(aprs_user='testuser', aprs_filter='') client.connect() client.run(callback=lambda x: x, autoreconnect=True) ... client.disconnect() client.connect() client.run() ``` --- ogn/client/client.py | 2 ++ tests/client/test_AprsClient.py | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/ogn/client/client.py b/ogn/client/client.py index 55f0f79..c484e5e 100644 --- a/ogn/client/client.py +++ b/ogn/client/client.py @@ -38,6 +38,8 @@ class AprsClient: self.sock.send(login.encode()) self.sock_file = self.sock.makefile('rw') + self._kill = False + def disconnect(self): self.logger.info('Disconnect') try: diff --git a/tests/client/test_AprsClient.py b/tests/client/test_AprsClient.py index 04f2db9..8210adf 100644 --- a/tests/client/test_AprsClient.py +++ b/tests/client/test_AprsClient.py @@ -44,6 +44,28 @@ class OgnClientTest(unittest.TestCase): client.sock.close.assert_called_once_with() self.assertTrue(client._kill) + def test_reset_kill_reconnect(self): + client = AprsClient(aprs_user='testuser', aprs_filter='') + client.connect() + + # .run() should be allowed to execute after .connect() + mock_callback = mock.MagicMock( + side_effect=lambda raw_msg: client.disconnect()) + + self.assertFalse(client._kill) + client.run(callback=mock_callback, autoreconnect=True) + + # After .disconnect(), client._kill should be True + self.assertTrue(client._kill) + mock_callback.assert_called_once() + + # After we reconnect, .run() should be able to run again + mock_callback = mock.MagicMock( + side_effect=lambda raw_msg: client.disconnect()) + client.connect() + client.run(callback=mock_callback, autoreconnect=True) + mock_callback.assert_called_once() + def test_50_live_messages(self): print("Enter") self.remaining_messages = 50