66
77using BT ::NodeStatus;
88
9+ // RUNNING until halted; throws on its second tick if `throw_on_running` is set.
10+ class AsyncMain : public BT ::StatefulActionNode
11+ {
12+ public:
13+ AsyncMain (const std::string& name, const BT ::NodeConfig& config, int * halted,
14+ bool throw_on_running)
15+ : StatefulActionNode(name, config), halted_(halted), throw_(throw_on_running)
16+ {}
17+ static BT ::PortsList providedPorts ()
18+ {
19+ return {};
20+ }
21+ NodeStatus onStart () override
22+ {
23+ return NodeStatus::RUNNING ;
24+ }
25+ NodeStatus onRunning () override
26+ {
27+ if (throw_)
28+ {
29+ throw std::runtime_error (" boom" );
30+ }
31+ return NodeStatus::RUNNING ;
32+ }
33+ void onHalted () override
34+ {
35+ (*halted_)++;
36+ }
37+
38+ private:
39+ int * halted_;
40+ bool throw_;
41+ };
42+
943class FinallyTest : public testing ::Test
1044{
1145protected:
1246 BT ::BehaviorTreeFactory factory;
1347 int cleanup_count = 0 ;
1448 int main_ticks = 0 ;
49+ int main_halted = 0 ;
50+ int cleanup_ticks = 0 ;
1551
1652 void SetUp () override
1753 {
@@ -25,6 +61,12 @@ class FinallyTest : public testing::Test
2561 factory.registerSimpleCondition (" RunTwice" , [this ](BT ::TreeNode&) {
2662 return ++main_ticks < 3 ? NodeStatus::RUNNING : NodeStatus::SUCCESS ;
2763 });
64+ factory.registerNodeType <AsyncMain>(" AsyncMain" , &main_halted, false );
65+ factory.registerNodeType <AsyncMain>(" AsyncThrow" , &main_halted, true );
66+ // Cleanup that is RUNNING on its first tick, then SUCCESS
67+ factory.registerSimpleCondition (" AsyncCleanup" , [this ](BT ::TreeNode&) {
68+ return ++cleanup_ticks < 2 ? NodeStatus::RUNNING : NodeStatus::SUCCESS ;
69+ });
2870 factory.registerSimpleCondition (" AlwaysRunning" , [this ](BT ::TreeNode&) {
2971 main_ticks++;
3072 return NodeStatus::RUNNING ;
@@ -130,3 +172,78 @@ TEST_F(FinallyTest, WrongChildCount_RejectedAtLoad)
130172 </BehaviorTree></root>)" ),
131173 BT ::RuntimeError);
132174}
175+
176+ TEST_F (FinallyTest, MainSkipped_CleanupRunsAndReturnsSkipped)
177+ {
178+ auto tree = factory.createTreeFromText (R"(
179+ <root BTCPP_format="4"><BehaviorTree>
180+ <Finally><AlwaysSuccess _skipIf="true"/><Cleanup/></Finally>
181+ </BehaviorTree></root>)" );
182+
183+ EXPECT_EQ (tree.tickWhileRunning (), NodeStatus::SKIPPED );
184+ tree.haltTree ();
185+ EXPECT_EQ (cleanup_count, 1 );
186+ }
187+
188+ TEST_F (FinallyTest, AsyncCleanup_ReturnsMainStatusWhenCleanupCompletes)
189+ {
190+ EXPECT_EQ (run (" <AlwaysFailure/>" , " <AsyncCleanup/>" ), NodeStatus::FAILURE );
191+ EXPECT_EQ (cleanup_ticks, 2 );
192+ }
193+
194+ TEST_F (FinallyTest, AsyncMainThrows_MainHaltedAndCleanupRuns)
195+ {
196+ EXPECT_EQ (run (" <AsyncThrow/>" ), NodeStatus::FAILURE );
197+ EXPECT_EQ (main_halted, 1 );
198+ EXPECT_EQ (cleanup_count, 1 );
199+ }
200+
201+ TEST_F (FinallyTest, HaltWhileMainRunning_MainHalted)
202+ {
203+ auto tree = factory.createTreeFromText (R"(
204+ <root BTCPP_format="4"><BehaviorTree>
205+ <Finally><AsyncMain/><Cleanup/></Finally>
206+ </BehaviorTree></root>)" );
207+
208+ EXPECT_EQ (tree.tickOnce (), NodeStatus::RUNNING );
209+ tree.haltTree ();
210+ EXPECT_EQ (main_halted, 1 );
211+ EXPECT_EQ (cleanup_count, 1 );
212+ }
213+
214+ TEST_F (FinallyTest, HaltWhileCleanupRunning_CleanupNotRestarted)
215+ {
216+ auto tree = factory.createTreeFromText (R"(
217+ <root BTCPP_format="4"><BehaviorTree>
218+ <Finally><AlwaysSuccess/><AsyncCleanup/></Finally>
219+ </BehaviorTree></root>)" );
220+
221+ EXPECT_EQ (tree.tickOnce (), NodeStatus::RUNNING );
222+ tree.haltTree ();
223+ EXPECT_EQ (cleanup_ticks, 1 );
224+ }
225+
226+ TEST_F (FinallyTest, HaltCleanupRunning_CleanupHalted)
227+ {
228+ auto tree = factory.createTreeFromText (R"(
229+ <root BTCPP_format="4"><BehaviorTree>
230+ <Finally><AlwaysRunning/><AsyncCleanup/></Finally>
231+ </BehaviorTree></root>)" );
232+
233+ EXPECT_EQ (tree.tickOnce (), NodeStatus::RUNNING );
234+ tree.haltTree ();
235+ EXPECT_EQ (cleanup_ticks, 1 );
236+ EXPECT_EQ (tree.rootNode ()->status (), NodeStatus::IDLE );
237+ }
238+
239+ TEST_F (FinallyTest, CleanupThrowsDuringHalt_DoesNotPropagate)
240+ {
241+ auto tree = factory.createTreeFromText (R"(
242+ <root BTCPP_format="4"><BehaviorTree>
243+ <Finally><AlwaysRunning/><Throw/></Finally>
244+ </BehaviorTree></root>)" );
245+
246+ EXPECT_EQ (tree.tickOnce (), NodeStatus::RUNNING );
247+ EXPECT_NO_THROW (tree.haltTree ());
248+ EXPECT_EQ (tree.rootNode ()->status (), NodeStatus::IDLE );
249+ }
0 commit comments