Skip to content

Commit

Permalink
Adding some timestamp checks to unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
PepsConti committed Nov 15, 2023
1 parent 0ca2cbd commit bb740c5
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions tests/test_protocol_sniffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
)

0 comments on commit bb740c5

Please sign in to comment.