mirror of
https://github.com/esphome/esphome.git
synced 2026-09-11 15:27:33 +00:00
Some work has no shorter form: bringing up a radio, the first connect of a network stack, a key generation whose cost is the algorithm. Wrapping it in this scope moves the loop pass start forward by its duration, so the blocking warning keeps reporting everything else in the pass and the component's threshold does not ratchet over it. The comment says what it is for and that it must never hide code that could be made faster.
35 lines
1.0 KiB
C++
35 lines
1.0 KiB
C++
#include <gtest/gtest.h>
|
|
|
|
#include "esphome/core/application.h"
|
|
#include "esphome/core/hal.h"
|
|
|
|
namespace esphome {
|
|
|
|
// The scope must push the pass start forward by the time it covers and by
|
|
// nothing else, so the blocking guard sees only the work outside it
|
|
TEST(UnavoidableBlockingScope, ExcludesItsDurationFromThePass) {
|
|
const uint32_t pass_start = millis();
|
|
LoopBlockingGuard guard(nullptr, nullptr, pass_start);
|
|
ASSERT_EQ(App.get_loop_component_start_time(), pass_start);
|
|
|
|
const uint32_t before = millis();
|
|
{
|
|
UnavoidableBlockingScope scope;
|
|
delay(30);
|
|
}
|
|
const uint32_t excused = millis() - before;
|
|
|
|
const uint32_t moved = App.get_loop_component_start_time() - pass_start;
|
|
EXPECT_GE(moved, 30u);
|
|
EXPECT_LE(moved, excused);
|
|
}
|
|
|
|
TEST(UnavoidableBlockingScope, ZeroLengthScopeLeavesTheStartAlone) {
|
|
const uint32_t pass_start = millis();
|
|
LoopBlockingGuard guard(nullptr, nullptr, pass_start);
|
|
{ UnavoidableBlockingScope scope; }
|
|
EXPECT_LE(App.get_loop_component_start_time() - pass_start, 1u);
|
|
}
|
|
|
|
} // namespace esphome
|