-
Notifications
You must be signed in to change notification settings - Fork 825
Expand file tree
/
Copy pathgtest_groot2_publisher.cpp
More file actions
106 lines (93 loc) · 3.09 KB
/
gtest_groot2_publisher.cpp
File metadata and controls
106 lines (93 loc) · 3.09 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
#include <chrono>
#include <cstdlib>
#include <future>
#include <behaviortree_cpp/loggers/groot2_protocol.h>
#include <behaviortree_cpp/loggers/groot2_publisher.h>
#include <gtest/gtest.h>
using namespace std::chrono_literals;
namespace
{
static const char* xml_text = R"(
<root BTCPP_format="4">
<BehaviorTree ID="MainTree">
<ThrowRuntimeError/>
</BehaviorTree>
</root>
)";
static const char* xml_text_sequence = R"(
<root BTCPP_format="4">
<BehaviorTree ID="MainTree">
<Sequence>
<AlwaysSuccess/>
<ThrowRuntimeError/>
</Sequence>
</BehaviorTree>
</root>
)";
void throwRuntimeError()
{
BT::BehaviorTreeFactory factory;
factory.registerSimpleAction("ThrowRuntimeError", [](BT::TreeNode&) -> BT::NodeStatus {
throw BT::RuntimeError("Oops!");
});
auto tree = factory.createTreeFromText(xml_text);
BT::Groot2Publisher publisher(tree);
EXPECT_THROW(tree.tickExactlyOnce(), BT::RuntimeError);
}
} // namespace
TEST(Groot2PublisherTest, EnsureNoInfiniteLoopOnThrow)
{
EXPECT_EXIT(
{
auto fut = std::async(std::launch::async, throwRuntimeError);
auto status = fut.wait_for(1s); // shouldn't run for more than 1 second
if(status != std::future_status::ready)
{
std::cerr << "Test took too long. Possible infinite loop.\n";
std::exit(EXIT_FAILURE);
}
std::exit(EXIT_SUCCESS);
},
::testing::ExitedWithCode(EXIT_SUCCESS), ".*");
}
// Test that destructor completes quickly even after exception
// This test runs multiple times to catch race conditions
TEST(Groot2PublisherTest, DestructorCompletesAfterException)
{
for(int i = 0; i < 5; i++)
{
BT::BehaviorTreeFactory factory;
factory.registerSimpleAction("ThrowRuntimeError",
[](BT::TreeNode&) -> BT::NodeStatus {
throw BT::RuntimeError("Test exception");
});
auto tree = factory.createTreeFromText(xml_text);
BT::Groot2Publisher publisher(tree, 1667 + i * 2);
EXPECT_THROW(tree.tickExactlyOnce(), BT::RuntimeError);
}
}
// Test that destructor completes quickly when tree has multiple nodes
TEST(Groot2PublisherTest, DestructorCompletesWithMultipleNodes)
{
BT::BehaviorTreeFactory factory;
factory.registerSimpleAction("ThrowRuntimeError", [](BT::TreeNode&) -> BT::NodeStatus {
throw BT::RuntimeError("Test exception in sequence");
});
auto tree = factory.createTreeFromText(xml_text_sequence);
BT::Groot2Publisher publisher(tree, 1677);
EXPECT_THROW(tree.tickExactlyOnce(), BT::RuntimeError);
}
// Test rapid creation and destruction of publishers
TEST(Groot2PublisherTest, RapidCreateDestroy)
{
for(int i = 0; i < 3; i++)
{
BT::BehaviorTreeFactory factory;
factory.registerSimpleAction(
"ThrowRuntimeError",
[](BT::TreeNode&) -> BT::NodeStatus { throw BT::RuntimeError("Rapid test"); });
auto tree = factory.createTreeFromText(xml_text);
BT::Groot2Publisher publisher(tree, 1687 + i * 2);
EXPECT_THROW(tree.tickExactlyOnce(), BT::RuntimeError);
}
}