summaryrefslogtreecommitdiff
path: root/tests/unit/test_wxui_radiothread.py
diff options
context:
space:
mode:
authorChristoph Berg <christoph.berg@credativ.de>2022-11-08 11:40:45 +0100
committerChristoph Berg <christoph.berg@credativ.de>2022-11-08 11:40:45 +0100
commit42b1373eff59af2aebb218ded3d2d55cc96dc630 (patch)
treed6e7e435fa41c65fff7a73dd7dc99bb4a12da5c8 /tests/unit/test_wxui_radiothread.py
parent176c8e29d64892072697c5d30513204773545a9b (diff)
parent6f19923a918f7fb63a444b0c99950fdbe9d2f74c (diff)
Update upstream source from tag 'upstream/20221106+py3'
Update to upstream version '20221106+py3' with Debian dir b54298e303932808cc08745c5f5133ad7b12628e
Diffstat (limited to 'tests/unit/test_wxui_radiothread.py')
-rw-r--r--tests/unit/test_wxui_radiothread.py105
1 files changed, 105 insertions, 0 deletions
diff --git a/tests/unit/test_wxui_radiothread.py b/tests/unit/test_wxui_radiothread.py
new file mode 100644
index 0000000..c60da8c
--- /dev/null
+++ b/tests/unit/test_wxui_radiothread.py
@@ -0,0 +1,105 @@
+import sys
+import time
+from unittest import mock
+
+sys.modules['wx'] = wx = mock.MagicMock()
+
+from tests.unit import base
+from chirp.wxui import radiothread
+
+
+class TestRadioThread(base.BaseTest):
+ def setUp(self):
+ super().setUp()
+
+ def test_radiojob(self):
+ radio = mock.MagicMock()
+ editor = mock.MagicMock()
+ job = radiothread.RadioJob(editor, 'get_memory', [12], {})
+ self.assertIsNone(job.dispatch(radio))
+ radio.get_memory.assert_called_once_with(12)
+ self.assertEqual(job.result, radio.get_memory.return_value)
+
+ def test_radiojob_exception(self):
+ radio = mock.MagicMock()
+ radio.get_memory.side_effect = ValueError('some error')
+ editor = mock.MagicMock()
+ job = radiothread.RadioJob(editor, 'get_memory', [12], {})
+ self.assertIsNone(job.dispatch(radio))
+ radio.get_memory.assert_called_once_with(12)
+ self.assertIsInstance(job.result, ValueError)
+
+ def test_thread(self):
+ radio = mock.MagicMock()
+ radio.get_features.side_effect = ValueError('some error')
+ editor = mock.MagicMock()
+ # Simulate an edit conflict with the first event by returning
+ # False for "delivered" to force us to queue an event.
+ editor.radio_thread_event.side_effect = [False, True, True, True]
+ thread = radiothread.RadioThread(radio)
+ mem = mock.MagicMock()
+ job1id = thread.submit(editor, 'get_memory', 12)
+ job2id = thread.submit(editor, 'set_memory', mem)
+ job3id = thread.submit(editor, 'get_features')
+ # We have to start the thread after we submit the main jobs so
+ # the order is stable for comparison.
+ thread.start()
+
+ # Wait for the main jobs to be processed before we signal exit
+ while not all([radio.get_memory.called,
+ radio.set_memory.called,
+ radio.get_features.called]):
+ time.sleep(0.1)
+
+ thread.end()
+ thread.join(5)
+ self.assertFalse(thread.is_alive())
+ radio.get_memory.assert_called_once_with(12)
+ radio.set_memory.assert_called_once_with(mem)
+ radio.get_features.assert_called_once_with()
+ self.assertEqual(4, editor.radio_thread_event.call_count)
+
+ # We expect the jobs to be delivered in order of
+ # priority. Since we return False for the first call to
+ # radio_thread_event(), job2 should be queued and then
+ # delivered first on the next cycle.
+ expected_order = [job2id, job2id, job3id, job1id]
+ for i, (jobid, call) in enumerate(
+ zip(expected_order,
+ editor.radio_thread_event.call_args_list)):
+ job = call[0][0]
+ self.assertEqual(jobid, job.id)
+
+ # We should call non-blocking for every call except the last
+ # one, when the queue is empty
+ editor.radio_thread_event.assert_has_calls([
+ mock.call(mock.ANY, block=False),
+ mock.call(mock.ANY, block=False),
+ mock.call(mock.ANY, block=False),
+ mock.call(mock.ANY, block=True),
+ ])
+
+ def test_thread_abort_priority(self):
+ radio = mock.MagicMock()
+ radio.get_features.side_effect = ValueError('some error')
+ editor = mock.MagicMock()
+ thread = radiothread.RadioThread(radio)
+ mem = mock.MagicMock()
+ job1id = thread.submit(editor, 'get_memory', 12)
+ job2id = thread.submit(editor, 'set_memory', mem)
+ job3id = thread.submit(editor, 'get_features')
+ thread.end()
+ # We have to start the thread after we submit the main jobs so
+ # the order is stable for comparison.
+ thread.start()
+
+ thread.join(5)
+ self.assertFalse(thread.is_alive())
+
+ # Our end sentinel should have gone to the head of the queue
+ # so that exiting the application does not leave a thread
+ # running in the background fetching hundreds of memories.
+ radio.get_memory.assert_not_called()
+ radio.set_memory.assert_not_called()
+ radio.get_features.assert_not_called()
+ wx.PostEvent.assert_not_called()