5 #include <gtest/gtest.h> 10 using Timepoint = std::chrono::time_point<std::chrono::steady_clock>;
16 const std::string &node_name,
18 :
BT::CoroActionNode(node_name, config)
19 , will_fail_(will_fail)
25 virtual void halt()
override 27 std::cout <<
"Action was halted. doing cleanup here" << std::endl;
28 start_time_ = Timepoint::min();
46 std::cout <<
"Starting action " << std::endl;
49 if (start_time_ == Timepoint::min())
51 start_time_ = std::chrono::steady_clock::now();
54 while (std::chrono::steady_clock::now() < (start_time_ + timeout_))
56 setStatusRunningAndYield();
61 std::cout <<
"Done" << std::endl;
62 start_time_ = Timepoint::min();
91 BT::assignDefaultRemapping<SimpleCoroAction>(node_config_);
95 EXPECT_FALSE( node.wasHalted() );
98 EXPECT_FALSE( node.wasHalted() );
100 node.will_fail_ =
true;
102 <<
"Should execute again and retun failure";
103 EXPECT_FALSE( node.wasHalted() );
107 <<
"Shoudln't fail because we set status to idle";
108 EXPECT_FALSE( node.wasHalted() );
112 TEST(CoroTest, do_action_timeout)
116 BT::assignDefaultRemapping<SimpleCoroAction>(node_config_);
124 EXPECT_TRUE( node.wasHalted() );
129 EXPECT_FALSE( node.wasHalted() );
136 BT::assignDefaultRemapping<SimpleCoroAction>(node_config_);
138 SimpleCoroAction actionA( milliseconds(200),
false,
"action_A", node_config_);
139 SimpleCoroAction actionB( milliseconds(200),
false,
"action_B", node_config_);
148 EXPECT_FALSE( actionA.wasHalted() );
152 TEST(CoroTest, OtherThreadHalt)
156 BT::assignDefaultRemapping<SimpleCoroAction>(node_config_);
158 SimpleCoroAction actionA( milliseconds(200),
false,
"action_A", node_config_);
159 actionA.executeTick();
161 std::cout <<
"----- 1 ------ " << std::endl;
162 auto handle = std::async(std::launch::async, [&](){
166 std::cout <<
"----- 2 ------ " << std::endl;
167 EXPECT_TRUE( actionA.wasHalted() );
169 std::cout <<
"----- 3------ " << std::endl;
170 handle = std::async(std::launch::async, [&](){
171 actionA.executeTick();
174 std::cout <<
"----- 4 ------ " << std::endl;
virtual void halt() override
TEST(CoroTest, do_action)
std::chrono::time_point< std::chrono::steady_clock > Timepoint
static Blackboard::Ptr create(Blackboard::Ptr parent={})
BT::NodeStatus executeWhileRunning(BT::TreeNode &node)
The TimeoutNode will halt() a running child if the latter has been RUNNING for more than a give time...
std::chrono::milliseconds Millisecond
Blackboard::Ptr blackboard
virtual BT::NodeStatus tick() override
Method to be implemented by the user.
The CoroActionNode class is an ideal candidate for asynchronous actions which need to communicate wit...
void setChild(TreeNode *child)
Abstract base class for Behavior Tree Nodes.
SimpleCoroAction(milliseconds timeout, bool will_fail, const std::string &node_name, const BT::NodeConfiguration &config)
void addChild(TreeNode *child)
The method used to add nodes to the children vector.
virtual BT::NodeStatus executeTick()
The method that should be used to invoke tick() and setStatus();.
void setRequiredTime(Millisecond ms)
The SequenceNode is used to tick children in an ordered sequence. If any child returns RUNNING...
std::chrono::milliseconds timeout_