From d6d2540823f1032396ef725f1a828c5f8eef7768 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 7 Sep 2026 17:22:25 +0200 Subject: [PATCH] Test that work outside the scope still ratchets --- tests/components/core/test_blocking_scope.cpp | 30 +++++++++++++++---- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/tests/components/core/test_blocking_scope.cpp b/tests/components/core/test_blocking_scope.cpp index 69b2d37ade..8bdeec19ea 100644 --- a/tests/components/core/test_blocking_scope.cpp +++ b/tests/components/core/test_blocking_scope.cpp @@ -54,17 +54,18 @@ TEST(UnavoidableBlockingScope, NestedScopesExcuseTheOuterSpanOnce) { } namespace { -// Static: the guard publishes the component to App and nothing clears it +// Static: the guard publishes the component to App and nothing clears it. +// One instance per test, since a ratcheted threshold is permanent class DummyComponent : public Component {}; -DummyComponent &blocking_test_component() { - static DummyComponent component; - return component; +DummyComponent &blocking_test_component(size_t index) { + static DummyComponent components[2]; + return components[index]; } } // namespace // The excused stretch must neither warn nor ratchet the component's threshold TEST(UnavoidableBlockingScope, ExcusedStretchDoesNotRatchetTheThreshold) { - DummyComponent &component = blocking_test_component(); + DummyComponent &component = blocking_test_component(0); uint32_t threshold_before = 0; component.should_warn_of_blocking(0, threshold_before); { @@ -80,4 +81,23 @@ TEST(UnavoidableBlockingScope, ExcusedStretchDoesNotRatchetTheThreshold) { EXPECT_EQ(threshold_after, threshold_before); } +// Work outside the scope is still measured and still ratchets +TEST(UnavoidableBlockingScope, WorkOutsideTheScopeStillRatchetsTheThreshold) { + DummyComponent &component = blocking_test_component(1); + uint32_t threshold_before = 0; + component.should_warn_of_blocking(0, threshold_before); + { + LoopBlockingGuard guard(&component, nullptr, millis()); + { + UnavoidableBlockingScope scope; + delay(20); + } + delay(WARN_IF_BLOCKING_OVER_CS * 10U + 20); + guard.finish(); + } + uint32_t threshold_after = 0; + component.should_warn_of_blocking(0, threshold_after); + EXPECT_GT(threshold_after, threshold_before); +} + } // namespace esphome