Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/decorators/delay_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ DelayNode::DelayNode(const std::string& name, unsigned milliseconds)
}

DelayNode::DelayNode(const std::string& name, const NodeConfig& config)
: DecoratorNode(name, config), timer_id_(0), msec_(0)
: DecoratorNode(name, config), timer_id_(0), msec_(0), read_parameter_from_ports_(true)
{}

void DelayNode::halt()
Expand Down
43 changes: 43 additions & 0 deletions tests/gtest_decorator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,46 @@ TEST(Decorator, RunOnce)
// counters[1] contains the number of times TestB was ticked
ASSERT_EQ(counters[1], 5);
}

// Test for DelayNode with XML: delay_msec port should be honored
TEST(Decorator, DelayWithXML)
{
BT::BehaviorTreeFactory factory;

const std::string xml_text = R"(
<root BTCPP_format="4" >
<BehaviorTree>
<Delay delay_msec="200">
Comment thread
facontidavide marked this conversation as resolved.
Outdated
<AlwaysSuccess/>
</Delay>
</BehaviorTree>
</root>)";

auto tree = factory.createTreeFromText(xml_text);

// First tick should return RUNNING (delay not complete)
auto start = std::chrono::steady_clock::now();
NodeStatus status = tree.tickOnce();
ASSERT_EQ(status, NodeStatus::RUNNING);

// Wait for a short time, still should be RUNNING
std::this_thread::sleep_for(std::chrono::milliseconds(50));
status = tree.tickOnce();
ASSERT_EQ(status, NodeStatus::RUNNING);

// Poll until the delay completes (with timeout for safety)
while(status == NodeStatus::RUNNING)
{
std::this_thread::sleep_for(std::chrono::milliseconds(10));
Comment thread
facontidavide marked this conversation as resolved.
Outdated
status = tree.tickOnce();
}
auto end = std::chrono::steady_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);

// The child (AlwaysSuccess) should have been executed after the delay
ASSERT_EQ(status, NodeStatus::SUCCESS);
// Verify that at least ~200ms have passed (with small tolerance for timing jitter)
ASSERT_GE(elapsed.count(), 180);
// Ensure the test didn't take too long (sanity check)
ASSERT_LE(elapsed.count(), 500);
Comment thread
facontidavide marked this conversation as resolved.
Outdated
}