Skip to content

emcy: catch callback exceptions and expand test coverage#659

Open
friederschueler wants to merge 1 commit intocanopen-python:masterfrom
friederschueler:fix/emcy-tests-pr604
Open

emcy: catch callback exceptions and expand test coverage#659
friederschueler wants to merge 1 commit intocanopen-python:masterfrom
friederschueler:fix/emcy-tests-pr604

Conversation

@friederschueler
Copy link
Copy Markdown
Collaborator

What type of PR is this?

/kind bugfix test
/area emcy.py


What this PR does / why we need it

This PR incorporates the review feedback from #604 (by @yuvraj-kolkar17) as suggested by @acolomb, since the original contributor has been unresponsive for several months.

Bug fix in canopen/emcy.py:
An exception raised in one EMCY callback would silently abort all subsequent callbacks in the list. The on_emcy() loop now wraps each callback in try/except and logs the exception, so remaining callbacks are always called.

Test improvements in test/test_emcy.py:

  • Convert leading comment to docstring in test_emcy_consumer_on_emcy
  • Remove superfluous inline # Dispatch … comments
  • test_emcy_consumer_initialization: verify initial state (without testing threading.Condition type as an implementation detail)
  • test_emcy_consumer_multiple_callbacks: simplified using lambdas
  • test_emcy_consumer_callback_exception_handling: rewritten with lambdas; adds assertion assertEqual(['success1', 'success2']) that verifies the fix
  • test_emcy_consumer_error_reset_variants: various reset code patterns
  • test_emcy_consumer_wait_timeout_edge_cases: zero/near-zero timeouts
  • test_emcy_consumer_wait_concurrent_errors: multiple concurrent errors
  • TestEmcyIntegration: producer-consumer roundtrip tests using network.subscribe() instead of manual rxbus.recv() calls
  • Removed tests that only exercised internal implementation details or Python's own exception mechanism

Which issue(s) this PR fixes

Closes #604


Does this PR introduce a user-facing change?

NONE

- Fix bug where an exception in one EMCY callback would abort all
  subsequent callbacks; now logs the exception and continues
- Add test for callback exception handling (verifies fix)
- Add tests for EmcyConsumer initialization, multiple callback order,
  error reset variants, wait timeout edge cases, concurrent errors
- Add tests for EmcyError initialization types and str representation
- Add tests for EmcyProducer initialization, send/reset edge cases
- Add TestEmcyIntegration class using network.subscribe() for
  producer-consumer roundtrip tests
- Remove superfluous inline comments per reviewer feedback (acolomb)
- Convert leading comment to docstring in test_emcy_consumer_on_emcy

Addresses review feedback from PR canopen-python#604 by yuvraj-kolkar17.
@codecov
Copy link
Copy Markdown

codecov Bot commented May 6, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@acolomb
Copy link
Copy Markdown
Member

acolomb commented May 7, 2026

It would really help the review process if you based your version on top of the commits in #604. Is that feasible? It will be squashed anyway when merging.

@friederschueler
Copy link
Copy Markdown
Collaborator Author

friederschueler commented May 7, 2026

I really tried to rebase this commit to #604 and then apply the changes, but even with AI this didn't work.

Instead here is a summary of how #659 addresses your specific review comments on #604:

Your review comment on #604 Addressed in #659
Convert comment to docstring ✅ Done
Superfluous # Dispatch … comments ✅ Removed
emcy_received type is an implementation detail ✅ Removed
Simplify callbacks with lambdas ✅ Done
len(callbacks) is not public API ✅ Removed
successful_callbacks unused — check actual behavior assertEqual(['success1', 'success2'])
"Avoid artificial delays" (time_expiry_during_execution) ✅ Removed
Human-readable string checks are fragile ✅ Only assertIn(f"0x{code:04X}", s)
Why not use network.subscribe()? ✅ Integration tests use rx_net.subscribe()
Redundant tests (struct_packing, network_assignment, emcy_error_inheritance) ✅ Removed

Additionally, #659 includes the actual bug fix in emcy.py (try/except around each callback) which #604 does not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants