From bb740c580415c0358e1eef482d63b6c534226d0f Mon Sep 17 00:00:00 2001 From: Jose Velazquez Date: Fri, 24 Feb 2023 17:08:56 -0600 Subject: [PATCH] Adding some timestamp checks to unit tests --- tests/test_protocol_sniffer.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/test_protocol_sniffer.py b/tests/test_protocol_sniffer.py index 746d40bc1..ad282af86 100644 --- a/tests/test_protocol_sniffer.py +++ b/tests/test_protocol_sniffer.py @@ -65,6 +65,12 @@ def test_protocol_sniffer(self): for d in data: packages.append(modulator.modulate(list(map(int, d)), pause)) + next_msg_timestamp = ( + time.time() + ) # Due to simulated transmission method used in testing, + # we won't be quite precise on the first timestamp on which data will be received + # But at least we'll validate it is received near this time + # verify modulation was correct pa = ProtocolAnalyzer(None) signal = Signal("", "", sample_rate=sample_rate) @@ -91,3 +97,20 @@ def test_protocol_sniffer(self): sniffer.stop() self.assertEqual(sniffer.plain_bits_str, data) + # Validate timestamps: + for i in range(len(sniffer.messages)): + msg = sniffer.messages[i] + if i == 0: + # For the first message, we can't have much accuracy due to the simulated mechanism used to deliver data. + # Let's just verify the timestamp makes sense with following condition: + # (next_msg_timestamp < msg.timestamp && msg.timestamp < next_msg_timestamp + SLEEP_TIME_DURING_PROCESS) + self.assertLess(next_msg_timestamp, msg.timestamp) + self.assertLess(msg.timestamp, next_msg_timestamp + 2) + else: + # For each message, verify the timestamp according to the theoretical calculation: + # (next_msg_timestamp - 0.0001 < msg.timestamp && msg.timestamp < next_msg_timestamp + 0.0001) + self.assertLess(next_msg_timestamp - 0.0001, msg.timestamp) + self.assertLess(msg.timestamp, next_msg_timestamp + 0.0001) + next_msg_timestamp = ( + msg.timestamp + len(packages[i]) / modulator.sample_rate + )