Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit da1cb44

Browse files
author
Davide Faconti
committed
fix issue BehaviorTree#228 . Retry and Repeat node need to halt the child
1 parent 5d752e1 commit da1cb44

File tree

6 files changed

+75
-23
lines changed

6 files changed

+75
-23
lines changed

CMakeLists.txt

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,7 @@ elseif( CATKIN_DEVEL_PREFIX OR CATKIN_BUILD_BINARY_PACKAGE)
102102
set(BUILD_TOOL_INCLUDE_DIRS ${catkin_INCLUDE_DIRS})
103103

104104
elseif(BUILD_UNIT_TESTS)
105-
find_package(GTest)
106-
107-
if(NOT GTEST_FOUND)
108-
message(WARNING " GTest missing! You may want to follow these instructions:")
109-
message(WARNING " https://gist.github.com/Cartexius/4c437c084d6e388288201aadf9c8cdd5")
110-
endif()
111-
105+
find_package(GTest REQUIRED)
112106
endif()
113107

114108

src/decorators/repeat_node.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,14 @@ NodeStatus RepeatNode::tick()
5656
case NodeStatus::SUCCESS:
5757
{
5858
try_index_++;
59+
haltChild();
5960
}
6061
break;
6162

6263
case NodeStatus::FAILURE:
6364
{
6465
try_index_ = 0;
66+
haltChild();
6567
return (NodeStatus::FAILURE);
6668
}
6769

src/decorators/retry_node.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,14 @@ NodeStatus RetryNode::tick()
6060
case NodeStatus::SUCCESS:
6161
{
6262
try_index_ = 0;
63+
haltChild();
6364
return (NodeStatus::SUCCESS);
6465
}
6566

6667
case NodeStatus::FAILURE:
6768
{
6869
try_index_++;
70+
haltChild();
6971
}
7072
break;
7173

tests/gtest_decorator.cpp

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,7 @@ struct DeadlineTest : testing::Test
2828
{
2929
root.setChild(&action);
3030
}
31-
~DeadlineTest()
32-
{
33-
34-
}
31+
~DeadlineTest() = default;
3532
};
3633

3734
struct RepeatTest : testing::Test
@@ -43,10 +40,19 @@ struct RepeatTest : testing::Test
4340
{
4441
root.setChild(&action);
4542
}
46-
~RepeatTest()
43+
~RepeatTest() = default;
44+
};
45+
46+
struct RepeatTestAsync : testing::Test
47+
{
48+
BT::RepeatNode root;
49+
BT::AsyncActionTest action;
50+
51+
RepeatTestAsync() : root("repeat", 3), action("action", milliseconds(100))
4752
{
48-
53+
root.setChild(&action);
4954
}
55+
~RepeatTestAsync() = default;
5056
};
5157

5258
struct RetryTest : testing::Test
@@ -58,10 +64,7 @@ struct RetryTest : testing::Test
5864
{
5965
root.setChild(&action);
6066
}
61-
~RetryTest()
62-
{
63-
64-
}
67+
~RetryTest() = default;
6568
};
6669

6770
struct TimeoutAndRetry : testing::Test
@@ -128,6 +131,38 @@ TEST_F(RetryTest, RetryTestA)
128131
ASSERT_EQ(1, action.tickCount() );
129132
}
130133

134+
135+
TEST_F(RepeatTestAsync, RepeatTestAsync)
136+
{
137+
action.setExpectedResult(NodeStatus::SUCCESS);
138+
139+
auto res = root.executeTick();
140+
141+
while(res == NodeStatus::RUNNING){
142+
std::this_thread::sleep_for(std::chrono::milliseconds(20));
143+
res = root.executeTick();
144+
}
145+
146+
ASSERT_EQ(NodeStatus::SUCCESS, root.status());
147+
ASSERT_EQ(3, action.successCount() );
148+
ASSERT_EQ(0, action.failureCount() );
149+
150+
//-------------------
151+
action.setExpectedResult(NodeStatus::FAILURE);
152+
action.resetCounters();
153+
154+
res = root.executeTick();
155+
while(res == NodeStatus::RUNNING){
156+
std::this_thread::sleep_for(std::chrono::milliseconds(20));
157+
res = root.executeTick();
158+
}
159+
160+
ASSERT_EQ(NodeStatus::FAILURE, root.status());
161+
ASSERT_EQ(0, action.successCount() );
162+
ASSERT_EQ(1, action.failureCount() );
163+
164+
}
165+
131166
TEST_F(RepeatTest, RepeatTestA)
132167
{
133168
action.setExpectedResult(NodeStatus::FAILURE);

tests/include/action_test_node.h

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,21 @@ class AsyncActionTest : public AsyncActionNode
4949

5050
void setExpectedResult(NodeStatus res);
5151

52-
int tickCount() const
53-
{
52+
int tickCount() const {
5453
return tick_count_;
5554
}
5655

57-
void resetTicks()
58-
{
56+
int successCount() const {
57+
return success_count_;
58+
}
59+
60+
int failureCount() const {
61+
return failure_count_;
62+
}
63+
64+
void resetCounters() {
65+
success_count_ = 0;
66+
failure_count_ = 0;
5967
tick_count_ = 0;
6068
}
6169

@@ -64,6 +72,8 @@ class AsyncActionTest : public AsyncActionNode
6472
BT::Duration time_;
6573
std::atomic<NodeStatus> expected_result_;
6674
std::atomic<int> tick_count_;
75+
int success_count_;
76+
int failure_count_;
6777

6878
};
6979
}

tests/src/action_test_node.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
#include <string>
1616

1717
BT::AsyncActionTest::AsyncActionTest(const std::string& name, BT::Duration deadline_ms) :
18-
AsyncActionNode(name, {})
18+
AsyncActionNode(name, {}),
19+
success_count_(0),
20+
failure_count_(0)
1921
{
2022
expected_result_ = NodeStatus::SUCCESS;
2123
time_ = deadline_ms;
@@ -40,12 +42,19 @@ BT::NodeStatus BT::AsyncActionTest::tick()
4042
return NodeStatus::IDLE;
4143
}
4244

45+
if( expected_result_ == NodeStatus::SUCCESS){
46+
success_count_++;
47+
}
48+
else if( expected_result_ == NodeStatus::FAILURE){
49+
failure_count_++;
50+
}
51+
4352
return expected_result_;
4453
}
4554

4655
void BT::AsyncActionTest::halt()
4756
{
48-
// do more cleanup here is necessary
57+
// do more cleanup here if necessary
4958
AsyncActionNode::halt();
5059
}
5160

0 commit comments

Comments
 (0)