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