Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 1 | #include <unistd.h> |
| 2 | |
| 3 | #include <memory> |
Brian Silverman | a2ae62d | 2015-03-15 15:55:22 -0700 | [diff] [blame] | 4 | #include <thread> |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 5 | #include <chrono> |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 6 | |
| 7 | #include "gtest/gtest.h" |
Brian Silverman | f5f8d8e | 2015-12-06 18:39:12 -0500 | [diff] [blame] | 8 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 9 | #include "aos/actions/actions.h" |
| 10 | #include "aos/actions/actions.q.h" |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 11 | #include "aos/actions/actor.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 12 | #include "aos/actions/test_action.q.h" |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 13 | #include "aos/events/simulated-event-loop.h" |
| 14 | #include "aos/queue.h" |
| 15 | #include "aos/testing/test_logging.h" |
Brian Silverman | f5f8d8e | 2015-12-06 18:39:12 -0500 | [diff] [blame] | 16 | #include "aos/testing/test_shm.h" |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 17 | |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 18 | namespace aos { |
| 19 | namespace common { |
| 20 | namespace actions { |
| 21 | namespace testing { |
| 22 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 23 | |
| 24 | namespace chrono = ::std::chrono; |
| 25 | |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame] | 26 | class TestActorIndex |
| 27 | : public aos::common::actions::ActorBase<actions::TestActionQueueGroup> { |
| 28 | public: |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 29 | typedef TypedActionFactory<actions::TestActionQueueGroup> Factory; |
| 30 | |
| 31 | explicit TestActorIndex(::aos::EventLoop *event_loop) |
| 32 | : aos::common::actions::ActorBase<actions::TestActionQueueGroup>( |
| 33 | event_loop, ".aos.common.actions.test_action") {} |
| 34 | |
| 35 | static Factory MakeFactory(::aos::EventLoop *event_loop) { |
| 36 | return Factory(event_loop, ".aos.common.actions.test_action"); |
| 37 | } |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame] | 38 | |
| 39 | bool RunAction(const uint32_t &new_index) override { |
| 40 | index = new_index; |
| 41 | return true; |
| 42 | } |
| 43 | |
| 44 | uint32_t index = 0; |
| 45 | }; |
| 46 | |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 47 | class TestActorNOP |
| 48 | : public aos::common::actions::ActorBase<actions::TestActionQueueGroup> { |
| 49 | public: |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 50 | typedef TypedActionFactory<actions::TestActionQueueGroup> Factory; |
| 51 | |
| 52 | explicit TestActorNOP(::aos::EventLoop *event_loop) |
| 53 | : actions::ActorBase<actions::TestActionQueueGroup>( |
| 54 | event_loop, ".aos.common.actions.test_action") {} |
| 55 | |
| 56 | static Factory MakeFactory(::aos::EventLoop *event_loop) { |
| 57 | return Factory(event_loop, ".aos.common.actions.test_action"); |
| 58 | } |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 59 | |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame] | 60 | bool RunAction(const uint32_t &) override { return true; } |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 61 | }; |
| 62 | |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 63 | class TestActorShouldCancel |
| 64 | : public aos::common::actions::ActorBase<actions::TestActionQueueGroup> { |
| 65 | public: |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 66 | typedef TypedActionFactory<actions::TestActionQueueGroup> Factory; |
| 67 | |
| 68 | explicit TestActorShouldCancel(::aos::EventLoop *event_loop) |
| 69 | : aos::common::actions::ActorBase<actions::TestActionQueueGroup>( |
| 70 | event_loop, ".aos.common.actions.test_action") {} |
| 71 | |
| 72 | static Factory MakeFactory(::aos::EventLoop *event_loop) { |
| 73 | return Factory(event_loop, ".aos.common.actions.test_action"); |
| 74 | } |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 75 | |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame] | 76 | bool RunAction(const uint32_t &) override { |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 77 | while (!ShouldCancel()) { |
| 78 | LOG(FATAL, "NOT CANCELED!!\n"); |
| 79 | } |
Daniel Petti | 3b1e48f | 2015-02-15 15:57:53 -0800 | [diff] [blame] | 80 | return true; |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 81 | } |
| 82 | }; |
| 83 | |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame] | 84 | class TestActor2Nop |
| 85 | : public aos::common::actions::ActorBase<actions::TestAction2QueueGroup> { |
| 86 | public: |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 87 | typedef TypedActionFactory<actions::TestAction2QueueGroup> Factory; |
| 88 | |
| 89 | explicit TestActor2Nop(::aos::EventLoop *event_loop) |
| 90 | : actions::ActorBase<actions::TestAction2QueueGroup>( |
| 91 | event_loop, ".aos.common.actions.test_action2") {} |
| 92 | |
| 93 | static Factory MakeFactory(::aos::EventLoop *event_loop) { |
| 94 | return Factory(event_loop, ".aos.common.actions.test_action2"); |
| 95 | } |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame] | 96 | |
| 97 | bool RunAction(const actions::MyParams &) { return true; } |
| 98 | }; |
| 99 | |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 100 | class ActionTest : public ::testing::Test { |
| 101 | protected: |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 102 | ActionTest() |
| 103 | : actor1_event_loop_(event_loop_factory_.MakeEventLoop()), |
| 104 | actor2_event_loop_(event_loop_factory_.MakeEventLoop()), |
| 105 | test_event_loop_(event_loop_factory_.MakeEventLoop()) { |
| 106 | ::aos::testing::EnableTestLogging(); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | // Bring up and down Core. |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 110 | ::aos::SimulatedEventLoopFactory event_loop_factory_; |
| 111 | |
| 112 | ::std::unique_ptr<::aos::EventLoop> actor1_event_loop_; |
| 113 | ::std::unique_ptr<::aos::EventLoop> actor2_event_loop_; |
| 114 | ::std::unique_ptr<::aos::EventLoop> test_event_loop_; |
| 115 | |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | // Tests that the the actions exist in a safe state at startup. |
| 119 | TEST_F(ActionTest, DoesNothing) { |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 120 | ActionQueue action_queue; |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 121 | // Tick an empty queue and make sure it was not running. |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 122 | EXPECT_FALSE(action_queue.Running()); |
| 123 | action_queue.Tick(); |
| 124 | EXPECT_FALSE(action_queue.Running()); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 125 | } |
| 126 | |
Brian Silverman | a2ae62d | 2015-03-15 15:55:22 -0700 | [diff] [blame] | 127 | // Tests that starting with an old run message in the goal queue actually works. |
| 128 | // This used to result in the client hanging, waiting for a response to its |
| 129 | // cancel message. |
| 130 | TEST_F(ActionTest, StartWithOldGoal) { |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 131 | ::std::unique_ptr<::aos::EventLoop> test2_event_loop = |
| 132 | event_loop_factory_.MakeEventLoop(); |
| 133 | ::aos::Sender<TestActionQueueGroup::Goal> goal_sender = |
| 134 | test2_event_loop->MakeSender<TestActionQueueGroup::Goal>( |
| 135 | ".aos.common.actions.test_action.goal"); |
| 136 | ::aos::Fetcher<Status> status_fetcher = test2_event_loop->MakeFetcher<Status>( |
| 137 | ".aos.common.actions.test_action.status"); |
Brian Silverman | a2ae62d | 2015-03-15 15:55:22 -0700 | [diff] [blame] | 138 | |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 139 | TestActorIndex::Factory nop_actor_factory = |
| 140 | TestActorNOP::MakeFactory(test_event_loop_.get()); |
Brian Silverman | a2ae62d | 2015-03-15 15:55:22 -0700 | [diff] [blame] | 141 | |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 142 | ActionQueue action_queue; |
Brian Silverman | a2ae62d | 2015-03-15 15:55:22 -0700 | [diff] [blame] | 143 | |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 144 | { |
| 145 | auto goal_message = goal_sender.MakeMessage(); |
| 146 | goal_message->run = 971; |
| 147 | ASSERT_TRUE(goal_message.Send()); |
| 148 | } |
| 149 | |
| 150 | TestActorNOP nop_act(actor1_event_loop_.get()); |
| 151 | |
| 152 | ASSERT_FALSE(status_fetcher.Fetch()); |
| 153 | |
| 154 | event_loop_factory_.RunFor(chrono::seconds(1)); |
| 155 | |
| 156 | ASSERT_TRUE(status_fetcher.Fetch()); |
| 157 | EXPECT_EQ(0u, status_fetcher->running); |
| 158 | EXPECT_EQ(0u, status_fetcher->last_running); |
| 159 | |
| 160 | action_queue.EnqueueAction(nop_actor_factory.Make(0)); |
Brian Silverman | a2ae62d | 2015-03-15 15:55:22 -0700 | [diff] [blame] | 161 | |
| 162 | // We started an action and it should be running. |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 163 | EXPECT_TRUE(action_queue.Running()); |
Brian Silverman | a2ae62d | 2015-03-15 15:55:22 -0700 | [diff] [blame] | 164 | |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 165 | action_queue.CancelAllActions(); |
| 166 | action_queue.Tick(); |
Brian Silverman | a2ae62d | 2015-03-15 15:55:22 -0700 | [diff] [blame] | 167 | |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 168 | EXPECT_TRUE(action_queue.Running()); |
Brian Silverman | a2ae62d | 2015-03-15 15:55:22 -0700 | [diff] [blame] | 169 | |
| 170 | // Run the action so it can signal completion. |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 171 | event_loop_factory_.RunFor(chrono::seconds(1)); |
| 172 | action_queue.Tick(); |
Brian Silverman | a2ae62d | 2015-03-15 15:55:22 -0700 | [diff] [blame] | 173 | |
| 174 | // Make sure it stopped. |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 175 | EXPECT_FALSE(action_queue.Running()); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | // Tests that an action starts and stops. |
| 179 | TEST_F(ActionTest, ActionQueueWasRunning) { |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 180 | TestActorNOP nop_act(actor1_event_loop_.get()); |
| 181 | |
| 182 | TestActorIndex::Factory nop_actor_factory = |
| 183 | TestActorNOP::MakeFactory(test_event_loop_.get()); |
| 184 | |
| 185 | ActionQueue action_queue; |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 186 | |
| 187 | // Tick an empty queue and make sure it was not running. |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 188 | event_loop_factory_.RunFor(chrono::seconds(1)); |
| 189 | action_queue.Tick(); |
| 190 | EXPECT_FALSE(action_queue.Running()); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 191 | |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 192 | action_queue.EnqueueAction(nop_actor_factory.Make(0)); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 193 | |
| 194 | // We started an action and it should be running. |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 195 | EXPECT_TRUE(action_queue.Running()); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 196 | |
| 197 | // Tick it and make sure it is still running. |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 198 | action_queue.Tick(); |
| 199 | EXPECT_TRUE(action_queue.Running()); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 200 | |
| 201 | // Run the action so it can signal completion. |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 202 | event_loop_factory_.RunFor(chrono::seconds(1)); |
| 203 | action_queue.Tick(); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 204 | |
| 205 | // Make sure it stopped. |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 206 | EXPECT_FALSE(action_queue.Running()); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | // Tests that we can cancel two actions and have them both stop. |
| 210 | TEST_F(ActionTest, ActionQueueCancelAll) { |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 211 | TestActorNOP nop_act(actor1_event_loop_.get()); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 212 | |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 213 | TestActorIndex::Factory nop_actor_factory = |
| 214 | TestActorNOP::MakeFactory(test_event_loop_.get()); |
| 215 | |
| 216 | ActionQueue action_queue; |
| 217 | |
| 218 | // Let the actor and action queue start up and confirm nothing is running. |
| 219 | event_loop_factory_.RunFor(chrono::seconds(1)); |
| 220 | action_queue.Tick(); |
| 221 | |
| 222 | EXPECT_FALSE(action_queue.Running()); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 223 | |
| 224 | // Enqueue two actions to test both cancel. We can have an action and a next |
| 225 | // action so we want to test that. |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 226 | action_queue.EnqueueAction(nop_actor_factory.Make(0)); |
| 227 | action_queue.EnqueueAction(nop_actor_factory.Make(0)); |
| 228 | |
| 229 | action_queue.Tick(); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 230 | |
| 231 | // Check that current and next exist. |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 232 | EXPECT_TRUE(action_queue.GetCurrentActionState(nullptr, nullptr, nullptr, |
| 233 | nullptr, nullptr, nullptr)); |
| 234 | EXPECT_TRUE(action_queue.GetNextActionState(nullptr, nullptr, nullptr, |
| 235 | nullptr, nullptr, nullptr)); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 236 | |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 237 | action_queue.CancelAllActions(); |
| 238 | action_queue.Tick(); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 239 | |
| 240 | // It should still be running as the actor could not have signaled. |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 241 | EXPECT_TRUE(action_queue.Running()); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 242 | |
| 243 | bool sent_started, sent_cancel, interrupted; |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 244 | EXPECT_TRUE(action_queue.GetCurrentActionState( |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 245 | nullptr, &sent_started, &sent_cancel, &interrupted, nullptr, nullptr)); |
| 246 | EXPECT_TRUE(sent_started); |
| 247 | EXPECT_TRUE(sent_cancel); |
| 248 | EXPECT_FALSE(interrupted); |
| 249 | |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 250 | EXPECT_FALSE(action_queue.GetNextActionState(nullptr, nullptr, nullptr, |
| 251 | nullptr, nullptr, nullptr)); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 252 | |
| 253 | // Run the action so it can signal completion. |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 254 | event_loop_factory_.RunFor(chrono::seconds(1)); |
| 255 | |
| 256 | action_queue.Tick(); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 257 | |
| 258 | // Make sure it stopped. |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 259 | EXPECT_FALSE(action_queue.Running()); |
| 260 | EXPECT_EQ(1, nop_act.running_count()); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | // Tests that an action that would block forever stops when canceled. |
| 264 | TEST_F(ActionTest, ActionQueueCancelOne) { |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 265 | TestActorShouldCancel cancel_act(actor1_event_loop_.get()); |
| 266 | |
| 267 | TestActorShouldCancel::Factory cancel_action_factory = |
| 268 | TestActorShouldCancel::MakeFactory(test_event_loop_.get()); |
| 269 | |
| 270 | ActionQueue action_queue; |
| 271 | |
| 272 | // Let the actor and action queue start up. |
| 273 | event_loop_factory_.RunFor(chrono::seconds(1)); |
| 274 | action_queue.Tick(); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 275 | |
| 276 | // Enqueue blocking action. |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 277 | action_queue.EnqueueAction(cancel_action_factory.Make(0)); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 278 | |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 279 | action_queue.Tick(); |
| 280 | EXPECT_TRUE(action_queue.Running()); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 281 | |
| 282 | // Tell action to cancel. |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 283 | action_queue.CancelCurrentAction(); |
| 284 | action_queue.Tick(); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 285 | |
| 286 | // This will block forever on failure. |
| 287 | // TODO(ben): prolly a bad way to fail |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 288 | event_loop_factory_.RunFor(chrono::seconds(1)); |
| 289 | action_queue.Tick(); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 290 | |
| 291 | // It should still be running as the actor could not have signalled. |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 292 | EXPECT_FALSE(action_queue.Running()); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 293 | } |
| 294 | |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 295 | // Tests that 2 actions in a row causes the second one to cancel the first one. |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 296 | TEST_F(ActionTest, ActionQueueTwoActions) { |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 297 | TestActorNOP nop_actor(actor1_event_loop_.get()); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 298 | |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 299 | TestActorIndex::Factory nop_actor_factory = |
| 300 | TestActorNOP::MakeFactory(test_event_loop_.get()); |
| 301 | |
| 302 | ActionQueue action_queue; |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 303 | // Tick an empty queue and make sure it was not running. |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 304 | event_loop_factory_.RunFor(chrono::seconds(1)); |
| 305 | action_queue.Tick(); |
| 306 | EXPECT_FALSE(action_queue.Running()); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 307 | |
| 308 | // Enqueue action to be canceled. |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 309 | action_queue.EnqueueAction(nop_actor_factory.Make(0)); |
| 310 | action_queue.Tick(); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 311 | |
| 312 | // Should still be running as the actor could not have signalled. |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 313 | EXPECT_TRUE(action_queue.Running()); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 314 | |
| 315 | // id for the first time run. |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 316 | uint32_t nop_actor_id = 0; |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 317 | // Check the internal state and write down id for later use. |
| 318 | bool sent_started, sent_cancel, interrupted; |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 319 | EXPECT_TRUE(action_queue.GetCurrentActionState(nullptr, &sent_started, |
| 320 | &sent_cancel, &interrupted, |
| 321 | &nop_actor_id, nullptr)); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 322 | EXPECT_TRUE(sent_started); |
| 323 | EXPECT_FALSE(sent_cancel); |
| 324 | EXPECT_FALSE(interrupted); |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 325 | ASSERT_NE(0u, nop_actor_id); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 326 | |
| 327 | // Add the next action which should ensure the first stopped. |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 328 | action_queue.EnqueueAction(nop_actor_factory.Make(0)); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 329 | |
| 330 | // id for the second run. |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 331 | uint32_t nop_actor2_id = 0; |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 332 | // Check the internal state and write down id for later use. |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 333 | EXPECT_TRUE(action_queue.GetNextActionState(nullptr, &sent_started, |
| 334 | &sent_cancel, &interrupted, |
| 335 | &nop_actor2_id, nullptr)); |
| 336 | EXPECT_NE(nop_actor_id, nop_actor2_id); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 337 | EXPECT_FALSE(sent_started); |
| 338 | EXPECT_FALSE(sent_cancel); |
| 339 | EXPECT_FALSE(interrupted); |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 340 | ASSERT_NE(0u, nop_actor2_id); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 341 | |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 342 | action_queue.Tick(); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 343 | |
| 344 | // Run the action so it can signal completion. |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 345 | event_loop_factory_.RunFor(chrono::seconds(1)); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 346 | |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 347 | action_queue.Tick(); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 348 | |
| 349 | // Check the new action is the right one. |
| 350 | uint32_t test_id = 0; |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 351 | EXPECT_TRUE(action_queue.GetCurrentActionState( |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 352 | nullptr, &sent_started, &sent_cancel, &interrupted, &test_id, nullptr)); |
| 353 | EXPECT_TRUE(sent_started); |
| 354 | EXPECT_FALSE(sent_cancel); |
| 355 | EXPECT_FALSE(interrupted); |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 356 | EXPECT_EQ(nop_actor2_id, test_id); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 357 | |
| 358 | // Make sure it is still going. |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 359 | EXPECT_TRUE(action_queue.Running()); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 360 | |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 361 | // Now let everything finish. |
| 362 | event_loop_factory_.RunFor(chrono::seconds(1)); |
| 363 | action_queue.Tick(); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 364 | |
| 365 | // Make sure it stopped. |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 366 | EXPECT_FALSE(action_queue.Running()); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 367 | } |
| 368 | |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame] | 369 | // Tests that we do get an index with our goal |
| 370 | TEST_F(ActionTest, ActionIndex) { |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 371 | TestActorIndex idx_actor(actor1_event_loop_.get()); |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame] | 372 | |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 373 | TestActorIndex::Factory test_actor_index_factory = |
| 374 | TestActorIndex::MakeFactory(test_event_loop_.get()); |
| 375 | |
| 376 | ActionQueue action_queue; |
| 377 | // Tick an empty queue and make sure it was not running. Also tick the |
| 378 | // factory to allow it to send out the initial cancel message. |
| 379 | event_loop_factory_.RunFor(chrono::seconds(1)); |
| 380 | action_queue.Tick(); |
| 381 | |
| 382 | EXPECT_FALSE(action_queue.Running()); |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame] | 383 | |
| 384 | // Enqueue action to post index. |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 385 | action_queue.EnqueueAction(test_actor_index_factory.Make(5)); |
| 386 | ::aos::Fetcher<actions::TestActionQueueGroup::Goal> goal_fetcher_ = |
| 387 | test_event_loop_->MakeFetcher<actions::TestActionQueueGroup::Goal>( |
| 388 | ".aos.common.actions.test_action.goal"); |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame] | 389 | |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 390 | ASSERT_TRUE(goal_fetcher_.Fetch()); |
| 391 | EXPECT_EQ(5u, goal_fetcher_->params); |
| 392 | EXPECT_EQ(0u, idx_actor.index); |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame] | 393 | |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 394 | action_queue.Tick(); |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame] | 395 | |
| 396 | // Run the next action so it can accomplish signal completion. |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 397 | event_loop_factory_.RunFor(chrono::seconds(1)); |
| 398 | |
| 399 | action_queue.Tick(); |
| 400 | EXPECT_EQ(5u, idx_actor.index); |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame] | 401 | |
| 402 | // Enqueue action to post index. |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 403 | action_queue.EnqueueAction(test_actor_index_factory.Make(3)); |
| 404 | ASSERT_TRUE(goal_fetcher_.Fetch()); |
| 405 | EXPECT_EQ(3u, goal_fetcher_->params); |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame] | 406 | |
| 407 | // Run the next action so it can accomplish signal completion. |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 408 | event_loop_factory_.RunFor(chrono::seconds(1)); |
| 409 | |
| 410 | action_queue.Tick(); |
| 411 | EXPECT_EQ(3u, idx_actor.index); |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | // Tests that an action with a structure params works. |
| 415 | TEST_F(ActionTest, StructParamType) { |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 416 | TestActor2Nop nop_actor(actor2_event_loop_.get()); |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame] | 417 | |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 418 | TestActor2Nop::Factory test_action_2_nop_factory = |
| 419 | TestActor2Nop::MakeFactory(test_event_loop_.get()); |
| 420 | |
| 421 | ActionQueue action_queue; |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame] | 422 | // Tick an empty queue and make sure it was not running. |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 423 | action_queue.Tick(); |
| 424 | EXPECT_FALSE(action_queue.Running()); |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame] | 425 | |
| 426 | actions::MyParams p; |
| 427 | p.param1 = 5.0; |
| 428 | p.param2 = 7; |
| 429 | |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 430 | action_queue.EnqueueAction(test_action_2_nop_factory.Make(p)); |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame] | 431 | |
| 432 | // We started an action and it should be running. |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 433 | EXPECT_TRUE(action_queue.Running()); |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame] | 434 | |
| 435 | // Tick it and make sure it is still running. |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 436 | action_queue.Tick(); |
| 437 | EXPECT_TRUE(action_queue.Running()); |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame] | 438 | |
| 439 | // Run the action so it can signal completion. |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 440 | // The actor takes no time, but running for a second is the best way to get it |
| 441 | // to go. |
| 442 | event_loop_factory_.RunFor(chrono::seconds(1)); |
| 443 | |
| 444 | action_queue.Tick(); |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame] | 445 | |
| 446 | // Make sure it stopped. |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame^] | 447 | EXPECT_FALSE(action_queue.Running()); |
Brian Silverman | 237a554 | 2015-03-29 17:59:29 -0400 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | } // namespace testing |
| 451 | } // namespace actions |
| 452 | } // namespace common |
| 453 | } // namespace aos |