Skip to content

Commit

Permalink
Tests: harden test_emcy_consumer_wait (#532)
Browse files Browse the repository at this point in the history
* Tests: harden test_emcy_consumer_wait

Wait a little bit longer before dispatching the EMCY packet, and start
the one-shot timer just before invoking the wait() call.

* Better resource management
  • Loading branch information
erlend-aasland authored Aug 12, 2024
1 parent 83594ac commit 4301c4b
Showing 1 changed file with 24 additions and 15 deletions.
39 changes: 24 additions & 15 deletions test/test_emcy.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import threading
import unittest
from contextlib import contextmanager

import can
import canopen
Expand Down Expand Up @@ -72,7 +73,7 @@ def test_emcy_consumer_reset(self):
self.assertEqual(len(self.emcy.active), 0)

def test_emcy_consumer_wait(self):
PAUSE = TIMEOUT / 4
PAUSE = TIMEOUT / 2

def push_err():
self.emcy.on_emcy(0x81, b'\x01\x20\x01\x01\x02\x03\x04\x05', 100)
Expand All @@ -84,34 +85,42 @@ def check_err(err):
data=bytes([1, 2, 3, 4, 5]), ts=100,
)

@contextmanager
def timer(func):
t = threading.Timer(PAUSE, func)
try:
yield t
finally:
t.join(TIMEOUT)

# Check unfiltered wait, on timeout.
self.assertIsNone(self.emcy.wait(timeout=TIMEOUT))

# Check unfiltered wait, on success.
timer = threading.Timer(PAUSE, push_err)
timer.start()
with self.assertLogs(level=logging.INFO):
err = self.emcy.wait(timeout=TIMEOUT)
with timer(push_err) as t:
with self.assertLogs(level=logging.INFO):
t.start()
err = self.emcy.wait(timeout=TIMEOUT)
check_err(err)

# Check filtered wait, on success.
timer = threading.Timer(PAUSE, push_err)
timer.start()
with self.assertLogs(level=logging.INFO):
err = self.emcy.wait(0x2001, TIMEOUT)
with timer(push_err) as t:
with self.assertLogs(level=logging.INFO):
t.start()
err = self.emcy.wait(0x2001, TIMEOUT)
check_err(err)

# Check filtered wait, on timeout.
timer = threading.Timer(PAUSE, push_err)
timer.start()
self.assertIsNone(self.emcy.wait(0x9000, TIMEOUT))
with timer(push_err) as t:
t.start()
self.assertIsNone(self.emcy.wait(0x9000, TIMEOUT))

def push_reset():
self.emcy.on_emcy(0x81, b'\x00\x00\x00\x00\x00\x00\x00\x00', 100)

timer = threading.Timer(PAUSE, push_reset)
timer.start()
self.assertIsNone(self.emcy.wait(0x9000, TIMEOUT))
with timer(push_reset) as t:
t.start()
self.assertIsNone(self.emcy.wait(0x9000, TIMEOUT))


class TestEmcyError(unittest.TestCase):
Expand Down

0 comments on commit 4301c4b

Please sign in to comment.