Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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