Skip to content

Add Rmt4PeripheralMock + unit tests (no threads) — follow-up to #3458 #3462

Closed

Description

@zackees

Goal

Follow up to #3458 (merged in #3461) — add a host-side mock for IRMT4Peripheral plus unit tests that exercise ChannelEngineRMT4Impl's channel-management state machine without touching real ESP32 hardware.

This is the test seam the wrapper was built for. The interface header (irmt4_peripheral.h) explicitly calls out "(future) Rmt4PeripheralMock — host-side simulation for unit tests" and the engine factory exposes ChannelEngineRMT4::create(peripheral) precisely so tests can pass a mock.

Reference template

Use the existing RMT5 mock as the shape model — same peripheral family, same call-flow conventions, and the engine architecture is the closest cousin:

  • src/platforms/shared/mock/esp/32/drivers/rmt5_peripheral_mock.h (interface + sim API)
  • src/platforms/shared/mock/esp/32/drivers/rmt5_peripheral_mock.cpp.hpp (concrete impl)
  • tests/platforms/shared/mock/esp/32/drivers/rmt5_peripheral_mock.cpp (the unit tests)

The RMT5 mock is single-threaded by design — no std::thread, no async callbacks, no condition variables. Tests drive simulation manually via simulateTxDone(handle) to fire the registered ISR callback synchronously. The RMT4 mock must follow the same constraint. Threads in a peripheral mock create races between the test thread and the simulation thread, defeat deterministic ordering, and force the engine's polling state machine into timing-coupled assertions. None of that is necessary or desirable.

Other mocks that follow the same single-threaded pattern (look at any of these for confirmation):

  • src/platforms/shared/mock/esp/32/drivers/uart_peripheral_mock.h
  • src/platforms/shared/mock/esp/32/drivers/spi_peripheral_mock.h
  • src/platforms/esp/32/drivers/i2s/i2s_lcd_cam_peripheral_mock.h
  • src/platforms/esp/32/drivers/parlio/parlio_peripheral_mock.h
  • src/platforms/shared/mock/arm/teensy4/drivers/flexio/flexio_peripheral_mock.h

Files to add

src/platforms/shared/mock/esp/32/drivers/rmt4_peripheral_mock.h
src/platforms/shared/mock/esp/32/drivers/rmt4_peripheral_mock.cpp.hpp
tests/platforms/shared/mock/esp/32/drivers/rmt4_peripheral_mock.cpp

Mock surface

The mock must implement every method of IRMT4Peripheral. From src/platforms/esp/32/drivers/rmt/rmt_4/irmt4_peripheral.h:

Method Mock behavior
configureChannel(cfg) Record cfg in a per-channel table; return true unless error-injected.
installDriver(channel) Mark channel installed=true; return true unless error-injected.
uninstallDriver(channel) Mark channel installed=false.
setTxThresholdIntrEnable(channel, en, thr) Record latest (en, thr).
setTxIntrEnable(channel, en) Record latest en.
setGpio(channel, pin, invert) Record (pin, invert).
installIsr(handler, arg, out_handle) Store (handler, arg); hand back a unique non-null void* cookie via out_handle.
freeIsr(handle) Match cookie, clear the stored (handler, arg).

Mock-only simulation API (no threads)

Add a thin extension surface for tests to drive the engine synchronously, mirroring Rmt5PeripheralMock's simulateTransmitDone():

  • simulateThresholdInterrupt(channel) — invoke the stored ISR handler(arg) once; the engine's handleInterrupt() will then call onThresholdInterrupt(channel) → fillNextBuffer().
  • simulateTxDoneInterrupt(channel) — same but for the TX-done bit. Sets state.transmissionComplete = true from inside the ISR.
  • setInstallDriverFailure(bool), setConfigureChannelFailure(bool), setIsrInstallFailure(bool) — error-injection knobs for the negative paths in ChannelEngineRMT4Impl::{configureChannel, ChannelEngineRMT4Impl::ChannelEngineRMT4Impl}.
  • getChannelState(channel) -> RecordedChannelState — read-only inspection so assertions can check that setGpio() etc. fired with the expected arguments.

All of these run on the test thread. No locks. No std::thread. No sleeping.

Tests (minimum set)

In tests/platforms/shared/mock/esp/32/drivers/rmt4_peripheral_mock.cpp:

  1. Peripheral lifecycle round-trip. configureChannel → installDriver → setTxThresholdIntrEnable → setGpio records exactly what the engine passed. uninstallDriver clears the installed flag.
  2. ISR install/free cookie symmetry. installIsr(handler, arg, out) returns a unique non-null cookie. freeIsr(cookie) clears the registration. Free-then-install returns a new cookie.
  3. Driver acquireChannel — Strategy 1 (reuse pin match). Construct ChannelEngineRMT4Impl(mock). Enqueue two ChannelData for the same pin sequentially; verify only one configureChannel + installDriver call (the second acquire reuses the channel slot).
  4. Driver acquireChannel — Strategy 2 (reconfigure idle channel for a new pin). Verify a second configureChannel on a different pin lands on the same hardware channel slot.
  5. Driver acquireChannel — Strategy 3 (create new slot). Enqueue distinct pins up to FASTLED_RMT_MAX_CHANNELS and verify each gets its own channel and installDriver call.
  6. Channel exhaustion. Enqueue one more than FASTLED_RMT_MAX_CHANNELS; verify the extra one is deferred via mPendingChannels, then released into a freed slot after simulateTxDoneInterrupt() + poll().
  7. Negative path — installDriver failure. mock.setInstallDriverFailure(true); show; verify acquireChannel returns nullptr and the channel is not marked inUse.
  8. Negative path — setGpio failure. Verify the engine calls uninstallDriver to clean up after setGpio fails (this is the cleanup branch in configureChannel).
  9. Threshold-interrupt → buffer refill plumbing. Start a transmission, call simulateThresholdInterrupt(channel); verify the engine advanced pixelDataPos (i.e. confirms the ISR dispatch wiring works through the mock's installIsr cookie).
  10. TX-done flow ends in poll() == READY. Start a transmission, simulateTxDoneInterrupt(channel), then call poll() repeatedly; verify it goes BUSY → READY and that sourceData->isInUse() returns to false.

Use the FastLED unit-test framework (FL_TEST_CASE, FL_SUBCASE, FL_CHECK, FL_REQUIRE) — same idioms as tests/platforms/shared/mock/esp/32/drivers/rmt5_peripheral_mock.cpp.

Validation

  • bash test --cpp — new tests included; existing tests stay green.
  • The mock should NOT be reachable from any production link target — it lives under src/platforms/shared/mock/ and is only pulled in by the host-side tests/ runner. The mock's .cpp.hpp is included from the test TU, not from any _build.cpp.hpp under src/platforms/esp/32/drivers/rmt/rmt_4/.

Out of scope

  • Real ISR hot-path coverage (register writes, RMTMEM access). Those stay inline IRAM on real silicon; the mock doesn't try to fake them — the test seam is the ISR callback dispatch, not the IRAM internals.
  • Any changes to Rmt4PeripheralESP (the real hardware impl). It already matches the same interface.
  • Mock for rmt_rx — separate driver, separate issue if and when needed.

Acceptance

  • rmt4_peripheral_mock.{h,cpp.hpp} exist under src/platforms/shared/mock/esp/32/drivers/ and implement every IRMT4Peripheral method.
  • Mock is single-threaded — no std::thread, no std::condition_variable, no async callbacks. ISR simulation runs on the test thread.
  • All 10 test cases above (or equivalents) land in tests/platforms/shared/mock/esp/32/drivers/rmt4_peripheral_mock.cpp and pass under bash test --cpp.
  • No changes to the production link targets.

Plan drafted with assistance from Claude Code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions