From 1ce2a56140566b1f1fccc7eff3f0b84b45c01578 Mon Sep 17 00:00:00 2001 From: Hemna Date: Mon, 11 Jan 2021 11:03:41 -0500 Subject: [PATCH] Updated MsgTrack restart_delayed This patch updates the restart_delayed method to accept the count of messages to restart as well as the most_recent flag that sorts the messages based on most recent first. If you want the oldest first, then pass in False --- aprsd/messaging.py | 27 ++++++-- tests/test_messaging.py | 150 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 171 insertions(+), 6 deletions(-) create mode 100644 tests/test_messaging.py diff --git a/aprsd/messaging.py b/aprsd/messaging.py index 3f1f36d..491ba11 100644 --- a/aprsd/messaging.py +++ b/aprsd/messaging.py @@ -112,13 +112,28 @@ class MsgTrack: if msg.last_send_attempt < msg.retry_count: msg.send() - def restart_delayed(self): + def _resend(self, msg): + msg.last_send_attempt = 0 + msg.send() + + def restart_delayed(self, count=None, most_recent=True): """Walk the list of delayed messages and restart them if any.""" - for key in self.track.keys(): - msg = self.track[key] - if msg.last_send_attempt == msg.retry_count: - msg.last_send_attempt = 0 - msg.send() + if not count: + # Send all the delayed messages + for key in self.track.keys(): + msg = self.track[key] + if msg.last_send_attempt == msg.retry_count: + self._resend(msg) + else: + # They want to resend delayed messages + tmp = sorted( + self.track.items(), + reverse=most_recent, + key=lambda x: x[1].last_send_time, + ) + msg_list = tmp[:count] + for (_key, msg) in msg_list: + self._resend(msg) def flush(self): """Nuke the old pickle file that stored the old results from last aprsd run.""" diff --git a/tests/test_messaging.py b/tests/test_messaging.py new file mode 100644 index 0000000..d7a9110 --- /dev/null +++ b/tests/test_messaging.py @@ -0,0 +1,150 @@ +import datetime +import unittest +from unittest import mock + +from aprsd import messaging + + +class TestMessageTrack(unittest.TestCase): + def _clean_track(self): + track = messaging.MsgTrack() + track.track = {} + track.total_messages_tracked = 0 + return track + + def test_create(self): + track1 = messaging.MsgTrack() + track2 = messaging.MsgTrack() + + self.assertEqual(track1, track2) + + def test_add(self): + track = self._clean_track() + fromcall = "KFART" + tocall = "KHELP" + message = "somthing" + msg = messaging.TextMessage(fromcall, tocall, message) + + track.add(msg) + self.assertEqual(msg, track.get(msg.id)) + + def test_remove(self): + track = self._clean_track() + fromcall = "KFART" + tocall = "KHELP" + message = "somthing" + msg = messaging.TextMessage(fromcall, tocall, message) + track.add(msg) + + track.remove(msg.id) + self.assertEqual(None, track.get(msg.id)) + + def test_len(self): + """Test getting length of tracked messages.""" + track = self._clean_track() + fromcall = "KFART" + tocall = "KHELP" + message = "somthing" + msg = messaging.TextMessage(fromcall, tocall, message) + track.add(msg) + self.assertEqual(1, len(track)) + msg2 = messaging.TextMessage(tocall, fromcall, message) + track.add(msg2) + self.assertEqual(2, len(track)) + + track.remove(msg.id) + self.assertEqual(1, len(track)) + + @mock.patch("aprsd.messaging.TextMessage.send") + def test__resend(self, mock_send): + """Test the _resend method.""" + track = self._clean_track() + fromcall = "KFART" + tocall = "KHELP" + message = "somthing" + msg = messaging.TextMessage(fromcall, tocall, message) + msg.last_send_attempt = 3 + track.add(msg) + + track._resend(msg) + msg.send.assert_called_with() + self.assertEqual(0, msg.last_send_attempt) + + @mock.patch("aprsd.messaging.TextMessage.send") + def test_restart_delayed(self, mock_send): + """Test the _resend method.""" + track = self._clean_track() + fromcall = "KFART" + tocall = "KHELP" + message1 = "something" + message2 = "something another" + message3 = "something another again" + + mock1_send = mock.MagicMock() + mock2_send = mock.MagicMock() + mock3_send = mock.MagicMock() + + msg1 = messaging.TextMessage(fromcall, tocall, message1) + msg1.last_send_attempt = 3 + msg1.last_send_time = datetime.datetime.now() + msg1.send = mock1_send + track.add(msg1) + + msg2 = messaging.TextMessage(tocall, fromcall, message2) + msg2.last_send_attempt = 3 + msg2.last_send_time = datetime.datetime.now() + msg2.send = mock2_send + track.add(msg2) + + track.restart_delayed(count=None) + msg1.send.assert_called_once() + self.assertEqual(0, msg1.last_send_attempt) + msg2.send.assert_called_once() + self.assertEqual(0, msg2.last_send_attempt) + + msg1.last_send_attempt = 3 + msg1.send.reset_mock() + msg2.last_send_attempt = 3 + msg2.send.reset_mock() + + track.restart_delayed(count=1) + msg1.send.assert_not_called() + msg2.send.assert_called_once() + self.assertEqual(3, msg1.last_send_attempt) + self.assertEqual(0, msg2.last_send_attempt) + + msg3 = messaging.TextMessage(tocall, fromcall, message3) + msg3.last_send_attempt = 3 + msg3.last_send_time = datetime.datetime.now() + msg3.send = mock3_send + track.add(msg3) + + msg1.last_send_attempt = 3 + msg1.send.reset_mock() + msg2.last_send_attempt = 3 + msg2.send.reset_mock() + msg3.last_send_attempt = 3 + msg3.send.reset_mock() + + track.restart_delayed(count=2) + msg1.send.assert_not_called() + msg2.send.assert_called_once() + msg3.send.assert_called_once() + self.assertEqual(3, msg1.last_send_attempt) + self.assertEqual(0, msg2.last_send_attempt) + self.assertEqual(0, msg3.last_send_attempt) + + msg1.last_send_attempt = 3 + msg1.send.reset_mock() + msg2.last_send_attempt = 3 + msg2.send.reset_mock() + msg3.last_send_attempt = 3 + msg3.send.reset_mock() + + track.restart_delayed(count=2, most_recent=False) + msg1.send.assert_called_once() + msg2.send.assert_called_once() + msg3.send.assert_not_called() + self.assertEqual(0, msg1.last_send_attempt) + self.assertEqual(0, msg2.last_send_attempt) + self.assertEqual(3, msg3.last_send_attempt)