1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-30 06:33:51 +00:00

Fix scheduler race conditions and add comprehensive test suite (#9348)

This commit is contained in:
J. Nick Koston
2025-07-07 14:57:55 -05:00
committed by GitHub
parent 138ff749f3
commit 3ef392d433
45 changed files with 2686 additions and 102 deletions

View File

@@ -0,0 +1,21 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.const import CONF_ID
scheduler_recursive_timeout_component_ns = cg.esphome_ns.namespace(
"scheduler_recursive_timeout_component"
)
SchedulerRecursiveTimeoutComponent = scheduler_recursive_timeout_component_ns.class_(
"SchedulerRecursiveTimeoutComponent", cg.Component
)
CONFIG_SCHEMA = cv.Schema(
{
cv.GenerateID(): cv.declare_id(SchedulerRecursiveTimeoutComponent),
}
).extend(cv.COMPONENT_SCHEMA)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)

View File

@@ -0,0 +1,40 @@
#include "recursive_timeout_component.h"
#include "esphome/core/log.h"
namespace esphome {
namespace scheduler_recursive_timeout_component {
static const char *const TAG = "scheduler_recursive_timeout";
void SchedulerRecursiveTimeoutComponent::setup() { ESP_LOGCONFIG(TAG, "SchedulerRecursiveTimeoutComponent setup"); }
void SchedulerRecursiveTimeoutComponent::run_recursive_timeout_test() {
ESP_LOGI(TAG, "Starting recursive timeout test - scheduling timeout from within timeout");
// Reset state
this->nested_level_ = 0;
// Schedule the initial timeout with 1ms delay
this->set_timeout(1, [this]() {
ESP_LOGI(TAG, "Executing initial timeout");
this->nested_level_ = 1;
// From within this timeout, schedule another timeout with 1ms delay
this->set_timeout(1, [this]() {
ESP_LOGI(TAG, "Executing nested timeout 1");
this->nested_level_ = 2;
// From within this nested timeout, schedule yet another timeout with 1ms delay
this->set_timeout(1, [this]() {
ESP_LOGI(TAG, "Executing nested timeout 2");
this->nested_level_ = 3;
// Test complete
ESP_LOGI(TAG, "Recursive timeout test complete - all %d levels executed", this->nested_level_);
});
});
});
}
} // namespace scheduler_recursive_timeout_component
} // namespace esphome

View File

@@ -0,0 +1,20 @@
#pragma once
#include "esphome/core/component.h"
namespace esphome {
namespace scheduler_recursive_timeout_component {
class SchedulerRecursiveTimeoutComponent : public Component {
public:
void setup() override;
float get_setup_priority() const override { return setup_priority::LATE; }
void run_recursive_timeout_test();
private:
int nested_level_{0};
};
} // namespace scheduler_recursive_timeout_component
} // namespace esphome