Repository navigation
Description
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.hsrc/platforms/shared/mock/esp/32/drivers/spi_peripheral_mock.hsrc/platforms/esp/32/drivers/i2s/i2s_lcd_cam_peripheral_mock.hsrc/platforms/esp/32/drivers/parlio/parlio_peripheral_mock.hsrc/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 ISRhandler(arg)once; the engine'shandleInterrupt()will then callonThresholdInterrupt(channel)→fillNextBuffer().simulateTxDoneInterrupt(channel)— same but for the TX-done bit. Setsstate.transmissionComplete = truefrom inside the ISR.setInstallDriverFailure(bool),setConfigureChannelFailure(bool),setIsrInstallFailure(bool)— error-injection knobs for the negative paths inChannelEngineRMT4Impl::{configureChannel, ChannelEngineRMT4Impl::ChannelEngineRMT4Impl}.getChannelState(channel) -> RecordedChannelState— read-only inspection so assertions can check thatsetGpio()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:
- Peripheral lifecycle round-trip.
configureChannel→installDriver→setTxThresholdIntrEnable→setGpiorecords exactly what the engine passed.uninstallDriverclears the installed flag. - 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. - Driver acquireChannel — Strategy 1 (reuse pin match). Construct
ChannelEngineRMT4Impl(mock). Enqueue twoChannelDatafor the same pin sequentially; verify only oneconfigureChannel+installDrivercall (the second acquire reuses the channel slot). - Driver acquireChannel — Strategy 2 (reconfigure idle channel for a new pin). Verify a second
configureChannelon a different pin lands on the same hardware channel slot. - Driver acquireChannel — Strategy 3 (create new slot). Enqueue distinct pins up to
FASTLED_RMT_MAX_CHANNELSand verify each gets its own channel andinstallDrivercall. - Channel exhaustion. Enqueue one more than
FASTLED_RMT_MAX_CHANNELS; verify the extra one is deferred viamPendingChannels, then released into a freed slot aftersimulateTxDoneInterrupt()+poll(). - Negative path —
installDriverfailure.mock.setInstallDriverFailure(true); show; verifyacquireChannelreturnsnullptrand the channel is not markedinUse. - Negative path —
setGpiofailure. Verify the engine callsuninstallDriverto clean up aftersetGpiofails (this is the cleanup branch inconfigureChannel). - Threshold-interrupt → buffer refill plumbing. Start a transmission, call
simulateThresholdInterrupt(channel); verify the engine advancedpixelDataPos(i.e. confirms the ISR dispatch wiring works through the mock'sinstallIsrcookie). - TX-done flow ends in
poll() == READY. Start a transmission,simulateTxDoneInterrupt(channel), then callpoll()repeatedly; verify it goes BUSY → READY and thatsourceData->isInUse()returns tofalse.
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-sidetests/runner. The mock's.cpp.hppis included from the test TU, not from any_build.cpp.hppundersrc/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 undersrc/platforms/shared/mock/esp/32/drivers/and implement everyIRMT4Peripheralmethod. - Mock is single-threaded — no
std::thread, nostd::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.cppand pass underbash test --cpp. - No changes to the production link targets.
Plan drafted with assistance from Claude Code.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
test(rmt4): Rmt4PeripheralMock + 22 host unit tests, no threads (#3462)…