Skip to main content

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_messages blocks nest on the same topic, the product of rates applies (composition is multiplicative).
  • Out-of-range rate raises ValueError at 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/2 before being released to subscribers.
  • Holding is implemented as a threading.Timer per message. High-rate topics with high ms will 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)

A NodeKilledHandle with:

Raises

  • LookupError — no node with that name visible on the ROS graph at entry.
  • RuntimeError — process refused to exit within wait_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 with mcap_capture:
The order of nesting doesn’t change the observable result; the DDS-layer interceptor multiplexes per-topic state.

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_testing graph see the un-faulted topic.
  • For across-process fault injection (e.g. fault a topic between two separately-launched nodes), use launch_testing integration 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.