Skip to content

Reorganized machine.Timer tests#19197

Open
DvdGiessen wants to merge 2 commits into
micropython:masterfrom
DvdGiessen:reorganized-machine-timer-tests
Open

Reorganized machine.Timer tests#19197
DvdGiessen wants to merge 2 commits into
micropython:masterfrom
DvdGiessen:reorganized-machine-timer-tests

Conversation

@DvdGiessen
Copy link
Copy Markdown
Contributor

Summary

Split off from #19163 (comment).

This reorganizes the machine_timer tests, grouping them by virtual vs hardware and by softirq vs hardirq.

It also expands the tests in two ways:

  • It adds a loop that initialises a timer a large number of times; to test for bugs where a large number of inits in succession causes bugs.
  • It split the previously long 90ms sleep in a few shorter sleeps with the same total duration, printing in between, so we can check in the test output that callbacks are called within the expected window of time.

While running the updated tests on a bunch of platforms I also found the Zephyr port can hang when repeatedly initialising a timer, presumably because we're overwriting the timer struct of a timer that's already running. The last commit fixes that, ensuring that at least the unit tests on Zephyr don't hang the board.

Testing

The updated run without errors on esp8266, rp2, stm32 ports. The unix and nrf ports also run without errors, namely by skipping all these tests because they're not supported.

Two port have are broken due to not passing the tests:

  • esp32: Running on the original ESP32 breaks due to esp32/machine_timer: Repeatedly initialising hardware timer breaks clock. #19162.
  • zephyr (running on a STM32WB55RG): sleep_ms often takes a bit (up to 5-7ms) longer than it should, throwing off the timing and letting an additional iteration of the timer fire off. This probably wasn't a problem when we only did a single 90ms sleep; but the updated tests split that into a four smaller sleeps, and now each of those sleeps may overshoot by this amount of time making it drift enough that the test breaks.

Other ports were not tested.

Trade-offs and Alternatives

To fix the esp32 port, #19163 or any alternative fixing #19162 should be merged.

To fix the zephyr port, either the precision of the timer could be improved, or for the time being the timing requirements imposed by this test could be relaxed a bit.

Generative AI

I did not use generative AI tools when creating this PR.

@codecov
Copy link
Copy Markdown

codecov Bot commented May 7, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.47%. Comparing base (72222a6) to head (4292aab).
⚠️ Report is 25 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #19197   +/-   ##
=======================================
  Coverage   98.47%   98.47%           
=======================================
  Files         176      176           
  Lines       22831    22831           
=======================================
  Hits        22483    22483           
  Misses        348      348           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@dpgeorge dpgeorge added the tests Relates to tests/ directory in source label May 7, 2026
@dpgeorge
Copy link
Copy Markdown
Member

dpgeorge commented May 7, 2026

Thanks for doing this!

I tested on mimxrt TEENSY40. Both extmod/machine_timer_hardware_hardirq.py and extmod/machine_timer_hardware_softirq.py fail due to ValueError: Timer doesn't exist (on the Timer(0) line).

Since the old extmod/machine_hard_timer.py was skipped on mimxrt, I think these two hardware tests should continue to be skipped on that platform.

(The two virtual timer tests pass on mimxrt.)

@DvdGiessen DvdGiessen force-pushed the reorganized-machine-timer-tests branch from b228cb1 to 2f1529c Compare May 7, 2026 15:58
@DvdGiessen
Copy link
Copy Markdown
Contributor Author

Fixed. Previously the hardware timer test only ran on the esp8266 port. I saw some other ports seemed to support hardware timers as well; but looking closer I was wrong: They don't support them through the machine.Timer interface, but only through pyb.Timer.

Actually, I think that means there currently isn't a single port that supports hardware timers with a hard IRQ via machine.Timer. So that specific test always skips right now. I've left it in, because it is correct and should work for any port on which we eventually support this combination.

DvdGiessen added 2 commits May 7, 2026 18:16
Signed-off-by: Daniël van de Giessen <daniel@dvdgiessen.nl>
Signed-off-by: Daniël van de Giessen <daniel@dvdgiessen.nl>
@DvdGiessen DvdGiessen force-pushed the reorganized-machine-timer-tests branch from 2f1529c to 4292aab Compare May 7, 2026 16:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tests Relates to tests/ directory in source

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants