15
15
// The mocked version of the base.
16
16
struct MockedAsyncActionNode : public BT ::AsyncActionNode
17
17
{
18
- using BT::AsyncActionNode::AsyncActionNode;
19
- MOCK_METHOD0 (tick, BT::NodeStatus());
18
+ using BT::AsyncActionNode::AsyncActionNode;
19
+ MOCK_METHOD0 (tick, BT::NodeStatus());
20
20
21
- // Tick while the node is running.
22
- BT::NodeStatus spinUntilDone ()
21
+ // Tick while the node is running.
22
+ BT::NodeStatus spinUntilDone ()
23
+ {
24
+ do
23
25
{
24
- do
25
- {
26
- executeTick ();
27
- } while (status () == BT::NodeStatus::RUNNING);
28
- return status ();
29
- }
30
-
31
- // Expose the setStatus method.
32
- using BT::AsyncActionNode::setStatus;
26
+ executeTick ();
27
+ } while (status () == BT::NodeStatus::RUNNING);
28
+ return status ();
29
+ }
30
+
31
+ // Expose the setStatus method.
32
+ using BT::AsyncActionNode::setStatus;
33
33
};
34
34
35
35
// The fixture taking care of the node-setup.
36
36
struct MockedAsyncActionFixture : public testing ::Test
37
37
{
38
- BT::NodeConfiguration config;
39
- MockedAsyncActionNode sn;
40
- MockedAsyncActionFixture () : sn(" node" , config)
41
- {
42
- }
38
+ BT::NodeConfiguration config;
39
+ MockedAsyncActionNode sn;
40
+ MockedAsyncActionFixture () : sn(" node" , config)
41
+ {}
43
42
};
44
43
45
44
// Parameters for the terminal node states.
@@ -49,94 +48,95 @@ struct NodeStatusFixture : public testing::WithParamInterface<BT::NodeStatus>,
49
48
};
50
49
51
50
INSTANTIATE_TEST_CASE_P (/* */ , NodeStatusFixture,
52
- testing::Values (BT::NodeStatus::SUCCESS, BT::NodeStatus::FAILURE));
51
+ testing::Values (BT::NodeStatus::SUCCESS,
52
+ BT::NodeStatus::FAILURE));
53
53
54
54
TEST_P (NodeStatusFixture, normal_routine)
55
55
{
56
- // Test verifies the "normal" operation: We correctly propagate the result
57
- // from the tick to the caller.
58
- const BT::NodeStatus state = GetParam ();
59
-
60
- // Setup the mock-expectations.
61
- EXPECT_CALL (sn, tick ()).WillOnce (testing::Invoke ([&]() {
62
- std::this_thread::sleep_for (std::chrono::milliseconds (10 ));
63
- return state;
64
- }));
65
-
66
- // Spin the node and check the final status.
67
- ASSERT_EQ (sn.spinUntilDone (), state);
56
+ // Test verifies the "normal" operation: We correctly propagate the result
57
+ // from the tick to the caller.
58
+ const BT::NodeStatus state = GetParam ();
59
+
60
+ // Setup the mock-expectations.
61
+ EXPECT_CALL (sn, tick ()).WillOnce (testing::Invoke ([&]() {
62
+ std::this_thread::sleep_for (std::chrono::milliseconds (10 ));
63
+ return state;
64
+ }));
65
+
66
+ // Spin the node and check the final status.
67
+ ASSERT_EQ (sn.spinUntilDone (), state);
68
68
}
69
69
70
70
TEST_F (MockedAsyncActionFixture, no_halt)
71
71
{
72
- // Test verifies that halt returns immediately, if the node is idle. It
73
- // further checks if the halt-flag is resetted correctly.
74
- sn.halt ();
75
- ASSERT_TRUE (sn.isHaltRequested ());
76
-
77
- // Below we further verify that the halt flag is cleaned up properly.
78
- const BT::NodeStatus state{BT::NodeStatus::SUCCESS};
79
- EXPECT_CALL (sn, tick ()).WillOnce (testing::Return (state));
80
-
81
- // Spin the node and check.
82
- ASSERT_EQ (sn.spinUntilDone (), state);
83
- ASSERT_FALSE (sn.isHaltRequested ());
72
+ // Test verifies that halt returns immediately, if the node is idle. It
73
+ // further checks if the halt-flag is resetted correctly.
74
+ sn.halt ();
75
+ ASSERT_TRUE (sn.isHaltRequested ());
76
+
77
+ // Below we further verify that the halt flag is cleaned up properly.
78
+ const BT::NodeStatus state{BT::NodeStatus::SUCCESS};
79
+ EXPECT_CALL (sn, tick ()).WillOnce (testing::Return (state));
80
+
81
+ // Spin the node and check.
82
+ ASSERT_EQ (sn.spinUntilDone (), state);
83
+ ASSERT_FALSE (sn.isHaltRequested ());
84
84
}
85
85
86
86
TEST_F (MockedAsyncActionFixture, halt)
87
87
{
88
- // Test verifies that calling halt() is blocking.
89
- bool release = false ;
90
- std::mutex m;
91
- std::condition_variable cv;
92
-
93
- const BT::NodeStatus state{BT::NodeStatus::SUCCESS};
94
- EXPECT_CALL (sn, tick ()).WillOnce (testing::Invoke ([&]() {
95
- // Sleep until we send the release signal.
96
- std::unique_lock<std::mutex> l (m);
97
- while (!release)
98
- cv.wait (l);
99
-
100
- return state;
101
- }));
102
-
103
- // Start the execution.
104
- sn.executeTick ();
105
-
106
- // Try to halt the node (cv will block it...)
107
- std::future<void > halted = std::async (std::launch::async, [&]() { sn.halt (); });
108
- ASSERT_EQ (halted.wait_for (std::chrono::milliseconds (10 )), std::future_status::timeout);
109
- ASSERT_EQ (sn.status (), BT::NodeStatus::RUNNING);
110
-
111
- // Release the method.
112
- {
113
- std::unique_lock<std::mutex> l (m);
114
- release = true ;
115
- cv.notify_one ();
116
- }
117
-
118
- // Wait for the future to return.
119
- halted.wait ();
120
- ASSERT_EQ (sn.status (), state);
88
+ // Test verifies that calling halt() is blocking.
89
+ bool release = false ;
90
+ std::mutex m;
91
+ std::condition_variable cv;
92
+
93
+ const BT::NodeStatus state{BT::NodeStatus::SUCCESS};
94
+ EXPECT_CALL (sn, tick ()).WillOnce (testing::Invoke ([&]() {
95
+ // Sleep until we send the release signal.
96
+ std::unique_lock<std::mutex> l (m);
97
+ while (!release)
98
+ cv.wait (l);
99
+
100
+ return state;
101
+ }));
102
+
103
+ // Start the execution.
104
+ sn.executeTick ();
105
+
106
+ // Try to halt the node (cv will block it...)
107
+ std::future<void > halted = std::async (std::launch::async, [&]() { sn.halt (); });
108
+ ASSERT_EQ (halted.wait_for (std::chrono::milliseconds (10 )), std::future_status::timeout);
109
+ ASSERT_EQ (sn.status (), BT::NodeStatus::RUNNING);
110
+
111
+ // Release the method.
112
+ {
113
+ std::unique_lock<std::mutex> l (m);
114
+ release = true ;
115
+ cv.notify_one ();
116
+ }
117
+
118
+ // Wait for the future to return.
119
+ halted.wait ();
120
+ ASSERT_EQ (sn.status (), state);
121
121
}
122
122
123
123
TEST_F (MockedAsyncActionFixture, exception)
124
124
{
125
- // Verifies that we can recover from the exceptions in the tick method:
126
- // 1) catch the exception, 2) re-raise it in the caller thread.
127
-
128
- // Setup the mock.
129
- EXPECT_CALL (sn, tick ()).WillOnce (testing::Invoke ([&]() {
130
- throw std::runtime_error (" This is not good!" );
131
- return BT::NodeStatus::SUCCESS;
132
- }));
133
-
134
- ASSERT_ANY_THROW (sn.spinUntilDone ());
135
- testing::Mock::VerifyAndClearExpectations (&sn);
136
-
137
- // Now verify that the exception is cleared up (we succeed).
138
- sn.setStatus (BT::NodeStatus::IDLE);
139
- const BT::NodeStatus state{BT::NodeStatus::SUCCESS};
140
- EXPECT_CALL (sn, tick ()).WillOnce (testing::Return (state));
141
- ASSERT_EQ (sn.spinUntilDone (), state);
125
+ // Verifies that we can recover from the exceptions in the tick method:
126
+ // 1) catch the exception, 2) re-raise it in the caller thread.
127
+
128
+ // Setup the mock.
129
+ EXPECT_CALL (sn, tick ()).WillOnce (testing::Invoke ([&]() {
130
+ throw std::runtime_error (" This is not good!" );
131
+ return BT::NodeStatus::SUCCESS;
132
+ }));
133
+
134
+ ASSERT_ANY_THROW (sn.spinUntilDone ());
135
+ testing::Mock::VerifyAndClearExpectations (&sn);
136
+
137
+ // Now verify that the exception is cleared up (we succeed).
138
+ sn.setStatus (BT::NodeStatus::IDLE);
139
+ const BT::NodeStatus state{BT::NodeStatus::SUCCESS};
140
+ EXPECT_CALL (sn, tick ()).WillOnce (testing::Return (state));
141
+ ASSERT_EQ (sn.spinUntilDone (), state);
142
142
}
0 commit comments