Fault injection reference
roboticks.fault_injection ships four context managers that manipulate the rclpy DDS layer for the duration of a with block. For the task-oriented introduction with worked examples, see Testing → Fault injection; this page is the API reference.
drop_messages
Probabilistically drop messages on a topic.
Signature
Behaviour
- Installs an interceptor on the topic at context entry; uninstalls at exit.
- Drops are evaluated per message, not per second. A rate of 0.5 drops roughly half the messages.
- If multiple
drop_messagesblocks nest on the same topic, the product of rates applies (composition is multiplicative). - Out-of-range
rateraisesValueErrorat entry.
Example
delay_messages
Add a fixed latency to every message on a topic.
Signature
Behaviour
- Each intercepted message is held for
ms ± jitter_ms/2before being released to subscribers. - Holding is implemented as a
threading.Timerper message. High-rate topics with highmswill hold many in-flight messages; budget memory accordingly. - Order is preserved (per-topic FIFO).
Example
kill_node
SIGTERM a named node and assert it stays down for the duration of the block.
Signature
Returns (as context value)
ANodeKilledHandle with:
Raises
LookupError— no node with that name visible on the ROS graph at entry.RuntimeError— process refused to exit withinwait_for_exit.
Example
corrupt_topic
Apply a mutator function to every message on a topic.
Signature
Behaviour
- The mutator runs between the publisher and subscribers — every subscriber sees the mutated value.
- Mutator exceptions propagate up to the publishing thread and abort the test. Catch inside the mutator if you want resilient behaviour.
- The mutator may mutate in place and return the same object, or return a fresh one.
Example
Composition
All four context managers compose with each other and withmcap_capture:
DDS implementation notes
- Implemented via rclpy’s middleware plug-points — no DDS vendor extensions required.
- Per-test isolated: the interceptor installs into the current process only; other processes in a
launch_testinggraph see the un-faulted topic. - For across-process fault injection (e.g. fault a topic between two separately-launched nodes), use
launch_testingintegration and inject from inside the test process that owns the topic.
Limits
Next
Fault injection tutorial
Task-oriented examples and decision tree.
Assertions
The natural pair with fault injection.
MCAP capture
Record the degraded signal for forensics.
Launch testing
Fault injection across a multi-process graph.