Skip to content
Open
Show file tree
Hide file tree
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
15 changes: 9 additions & 6 deletions canopen/emcy.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,19 @@ def wait(
while True:
with self.emcy_received:
prev_log_size = len(self.log)
self.emcy_received.wait(timeout)
remaining = end_time - time.time()
if remaining <= 0:
return None # Actual timeout reached
self.emcy_received.wait(remaining)
if len(self.log) == prev_log_size:
# Resumed due to timeout
return None
if time.time() >= end_time:
# Resumed due to timeout
return None
else:
continue
# Get last logged EMCY
emcy = self.log[-1]
logger.info("Got %s", emcy)
if time.time() > end_time:
# No valid EMCY received on time
return None
if emcy_code is None or emcy.code == emcy_code:
# This is the one we're interested in
return emcy
Expand Down
8 changes: 8 additions & 0 deletions test/test_emcy.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ def test_emcy_consumer_wait(self):
def push_err():
emcy.on_emcy(0x81, b'\x01\x20\x01\x01\x02\x03\x04\x05', 100)

def no_err():
with emcy.emcy_received:
emcy.emcy_received.notify_all()

def check_err(err):
self.assertIsNotNone(err)
self.check_error(
Expand All @@ -103,6 +107,10 @@ def check_err(err):
):
check_err(emcy.wait(timeout=TIMEOUT))

# Check unfiltered wait, spurious condition wake-up.
with mock_rx_thread(emcy, no_err):
self.assertIsNone(emcy.wait(timeout=TIMEOUT))

# Check filtered wait, on success.
with (
self.assertLogs(level=logging.INFO),
Expand Down
Loading