-
Notifications
You must be signed in to change notification settings - Fork 825
Expand file tree
/
Copy pathgtest_substitution.cpp
More file actions
348 lines (289 loc) · 10.5 KB
/
gtest_substitution.cpp
File metadata and controls
348 lines (289 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#include "behaviortree_cpp/bt_factory.h"
#include <future>
#include <gtest/gtest.h>
using namespace BT;
static const char* json_text = R"(
{
"TestNodeConfigs": {
"TestA": {
"async_delay": 2000,
"return_status": "SUCCESS",
"post_script": "msg ='message SUBSTITUED'"
},
"TestB": {
"return_status": "FAILURE"
}
},
"SubstitutionRules": {
"actionA": "TestA",
"actionB": "TestB",
"actionC": "NotAConfig"
}
}
)";
TEST(Substitution, Parser)
{
BehaviorTreeFactory factory;
factory.loadSubstitutionRuleFromJSON(json_text);
const auto& rules = factory.substitutionRules();
ASSERT_EQ(rules.size(), 3);
ASSERT_EQ(rules.count("actionA"), 1);
ASSERT_EQ(rules.count("actionB"), 1);
ASSERT_EQ(rules.count("actionC"), 1);
auto configA = std::get_if<TestNodeConfig>(&rules.at("actionA"));
ASSERT_EQ(configA->return_status, NodeStatus::SUCCESS);
ASSERT_EQ(configA->async_delay, std::chrono::milliseconds(2000));
ASSERT_EQ(configA->post_script, "msg ='message SUBSTITUED'");
auto configB = std::get_if<TestNodeConfig>(&rules.at("actionB"));
ASSERT_EQ(configB->return_status, NodeStatus::FAILURE);
ASSERT_EQ(configB->async_delay, std::chrono::milliseconds(0));
ASSERT_TRUE(configB->post_script.empty());
ASSERT_EQ(*std::get_if<std::string>(&rules.at("actionC")), "NotAConfig");
}
// Regression test for issue #934: segfault when substituting a SubTree node
TEST(Substitution, SubTreeNodeSubstitution)
{
static const char* parent_xml = R"(
<root BTCPP_format="4">
<BehaviorTree ID="Parent">
<SubTree ID="Child" name="child" />
</BehaviorTree>
</root>
)";
static const char* child_xml = R"(
<root BTCPP_format="4">
<BehaviorTree ID="Child">
<AlwaysSuccess />
</BehaviorTree>
</root>
)";
BehaviorTreeFactory factory;
factory.registerBehaviorTreeFromText(parent_xml);
factory.registerBehaviorTreeFromText(child_xml);
BT::TestNodeConfig config;
config.return_status = BT::NodeStatus::SUCCESS;
factory.addSubstitutionRule("child", config);
// This should not crash (was a segfault before the fix)
Tree tree;
ASSERT_NO_THROW(tree = factory.createTree("Parent"));
// The substituted tree should tick successfully
auto status = tree.tickWhileRunning();
ASSERT_EQ(status, BT::NodeStatus::SUCCESS);
}
// Test for issue #930: Mock substitution with registerSimpleAction
// should not hang when using string-based substitution from JSON.
TEST(Substitution, StringSubstitutionWithSimpleAction_Issue930)
{
// XML tree: Sequence with a Delay, then an action to be substituted
static const char* xml_text = R"(
<root BTCPP_format="4">
<BehaviorTree ID="MainTree">
<Sequence>
<Delay delay_msec="100">
<AlwaysSuccess/>
</Delay>
<SaySomething name="action_to_replace" message="hello"/>
</Sequence>
</BehaviorTree>
</root>
)";
BehaviorTreeFactory factory;
// Register original action
factory.registerSimpleAction("SaySomething",
[](TreeNode& node) { return NodeStatus::SUCCESS; },
{ InputPort<std::string>("message") });
// Register substitute action
factory.registerSimpleAction("MyTestAction",
[](TreeNode&) { return NodeStatus::SUCCESS; });
// Use string-based substitution: replace action_to_replace with
// MyTestAction
factory.addSubstitutionRule("action_to_replace", "MyTestAction");
factory.registerBehaviorTreeFromText(xml_text);
auto tree = factory.createTree("MainTree");
// This should NOT hang. Use a future with timeout to detect hangs.
auto future =
std::async(std::launch::async, [&tree]() { return tree.tickWhileRunning(); });
auto status = future.wait_for(std::chrono::seconds(5));
ASSERT_NE(status, std::future_status::timeout) << "Tree hung! tickWhileRunning did not "
"complete within 5 seconds";
ASSERT_EQ(future.get(), NodeStatus::SUCCESS);
}
// Test for issue #930: TestNodeConfig-based substitution with
// async_delay should also not hang on single-threaded executor.
TEST(Substitution, TestNodeConfigAsyncSubstitution_Issue930)
{
static const char* xml_text = R"(
<root BTCPP_format="4">
<BehaviorTree ID="MainTree">
<Sequence>
<AlwaysSuccess name="action_A"/>
<AlwaysSuccess name="action_B"/>
</Sequence>
</BehaviorTree>
</root>
)";
BehaviorTreeFactory factory;
factory.registerBehaviorTreeFromText(xml_text);
// Substitute action_B with a TestNodeConfig that has async_delay
TestNodeConfig test_config;
test_config.return_status = NodeStatus::SUCCESS;
test_config.async_delay = std::chrono::milliseconds(100);
factory.addSubstitutionRule("action_B", test_config);
auto tree = factory.createTree("MainTree");
// This should NOT hang -- the TestNode should complete after the
// async_delay and emit a wake-up signal.
auto future =
std::async(std::launch::async, [&tree]() { return tree.tickWhileRunning(); });
auto status = future.wait_for(std::chrono::seconds(5));
ASSERT_NE(status, std::future_status::timeout) << "Tree hung! tickWhileRunning did not "
"complete within 5 seconds";
ASSERT_EQ(future.get(), NodeStatus::SUCCESS);
}
// Test for issue #930: JSON-based substitution mapping to a
// registered SimpleAction (string rule) should work correctly.
TEST(Substitution, JsonStringSubstitution_Issue930)
{
static const char* xml_text = R"(
<root BTCPP_format="4">
<BehaviorTree ID="MainTree">
<Sequence>
<AlwaysSuccess name="action_A"/>
<AlwaysSuccess name="action_B"/>
</Sequence>
</BehaviorTree>
</root>
)";
// JSON that maps action_B to a registered SimpleAction
// (not a TestNodeConfig name)
static const char* json_rules = R"(
{
"TestNodeConfigs": {},
"SubstitutionRules": {
"action_B": "MyReplacement"
}
}
)";
BehaviorTreeFactory factory;
// Register the replacement action
factory.registerSimpleAction("MyReplacement",
[](TreeNode&) { return NodeStatus::SUCCESS; });
factory.loadSubstitutionRuleFromJSON(json_rules);
factory.registerBehaviorTreeFromText(xml_text);
auto tree = factory.createTree("MainTree");
// This should NOT hang
auto future =
std::async(std::launch::async, [&tree]() { return tree.tickWhileRunning(); });
auto status = future.wait_for(std::chrono::seconds(5));
ASSERT_NE(status, std::future_status::timeout) << "Tree hung! tickWhileRunning did not "
"complete within 5 seconds";
ASSERT_EQ(future.get(), NodeStatus::SUCCESS);
}
// Test for issue #930: loadSubstitutionRuleFromJSON should work
// when TestNodeConfigs is empty (only string rules).
TEST(Substitution, JsonWithEmptyTestNodeConfigs_Issue930)
{
static const char* json_rules = R"(
{
"TestNodeConfigs": {},
"SubstitutionRules": {
"node_A": "ReplacementNode"
}
}
)";
BehaviorTreeFactory factory;
factory.registerSimpleAction("ReplacementNode",
[](TreeNode&) { return NodeStatus::SUCCESS; });
// This should not throw
ASSERT_NO_THROW(factory.loadSubstitutionRuleFromJSON(json_rules));
const auto& rules = factory.substitutionRules();
ASSERT_EQ(rules.size(), 1);
ASSERT_EQ(rules.count("node_A"), 1);
auto* rule_str = std::get_if<std::string>(&rules.at("node_A"));
ASSERT_NE(rule_str, nullptr);
ASSERT_EQ(*rule_str, "ReplacementNode");
}
// Test for issue #930: loadSubstitutionRuleFromJSON should handle
// missing TestNodeConfigs gracefully.
TEST(Substitution, JsonWithoutTestNodeConfigs_Issue930)
{
static const char* json_rules = R"(
{
"SubstitutionRules": {
"node_A": "ReplacementNode"
}
}
)";
BehaviorTreeFactory factory;
factory.registerSimpleAction("ReplacementNode",
[](TreeNode&) { return NodeStatus::SUCCESS; });
// TestNodeConfigs is optional: string-only substitution rules
// don't need TestNodeConfigs.
ASSERT_NO_THROW(factory.loadSubstitutionRuleFromJSON(json_rules));
const auto& rules = factory.substitutionRules();
ASSERT_EQ(rules.size(), 1);
auto* rule_str = std::get_if<std::string>(&rules.at("node_A"));
ASSERT_NE(rule_str, nullptr);
ASSERT_EQ(*rule_str, "ReplacementNode");
}
// Test for issue #930: End-to-end test combining JSON-based
// string substitution with tree execution involving async nodes.
// This closely matches the issue reporter's scenario.
TEST(Substitution, JsonStringSubstitutionWithDelay_Issue930)
{
static const char* xml_text = R"(
<root BTCPP_format="4">
<BehaviorTree ID="MainTree">
<Sequence>
<Delay delay_msec="50">
<AlwaysSuccess/>
</Delay>
<Script name="script_2" code=" val:=1 "/>
</Sequence>
</BehaviorTree>
</root>
)";
static const char* json_rules = R"(
{
"SubstitutionRules": {
"script_2": "MyTest"
}
}
)";
BehaviorTreeFactory factory;
bool action_executed = false;
factory.registerSimpleAction("MyTest", [&](TreeNode&) {
action_executed = true;
return NodeStatus::SUCCESS;
});
factory.loadSubstitutionRuleFromJSON(json_rules);
factory.registerBehaviorTreeFromText(xml_text);
auto tree = factory.createTree("MainTree");
auto future =
std::async(std::launch::async, [&tree]() { return tree.tickWhileRunning(); });
auto status = future.wait_for(std::chrono::seconds(5));
ASSERT_NE(status, std::future_status::timeout) << "Tree hung! tickWhileRunning did not "
"complete within "
"5 seconds";
ASSERT_EQ(future.get(), NodeStatus::SUCCESS);
ASSERT_TRUE(action_executed);
}
// Test for issue #930: Verify that substituted node's registration
// ID is preserved correctly for string-based substitution.
TEST(Substitution, StringSubstitutionRegistrationID_Issue930)
{
static const char* xml_text = R"(
<root BTCPP_format="4">
<BehaviorTree ID="MainTree">
<AlwaysSuccess name="target_node"/>
</BehaviorTree>
</root>
)";
BehaviorTreeFactory factory;
factory.registerSimpleAction("MyReplacement",
[](TreeNode&) { return NodeStatus::SUCCESS; });
factory.addSubstitutionRule("target_node", "MyReplacement");
factory.registerBehaviorTreeFromText(xml_text);
auto tree = factory.createTree("MainTree");
// The substituted node should still work correctly
ASSERT_EQ(tree.tickWhileRunning(), NodeStatus::SUCCESS);
}