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()), |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 105 | actor1_event_loop_(event_loop_factory_.MakeEventLoop("actor1")), |
| 106 | actor2_event_loop_(event_loop_factory_.MakeEventLoop("actor2")), |
| 107 | test_event_loop_(event_loop_factory_.MakeEventLoop("test")) { |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 108 | ::aos::testing::EnableTestLogging(); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Austin Schuh | 02e2aeb | 2020-02-10 21:51:30 -0800 | [diff] [blame] | 111 | void RunAt(monotonic_clock::time_point exec_time, std::function<void()> fn) { |
| 112 | TimerHandler *timer = test_event_loop_->AddTimer(fn); |
| 113 | test_event_loop_->OnRun([timer, exec_time]() { timer->Setup(exec_time); }); |
| 114 | } |
| 115 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 116 | FlatbufferDetachedBuffer<Configuration> configuration_; |
| 117 | |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 118 | // Bring up and down Core. |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 119 | ::aos::SimulatedEventLoopFactory event_loop_factory_; |
| 120 | |
| 121 | ::std::unique_ptr<::aos::EventLoop> actor1_event_loop_; |
| 122 | ::std::unique_ptr<::aos::EventLoop> actor2_event_loop_; |
| 123 | ::std::unique_ptr<::aos::EventLoop> test_event_loop_; |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 124 | }; |
| 125 | |
| 126 | // Tests that the the actions exist in a safe state at startup. |
| 127 | TEST_F(ActionTest, DoesNothing) { |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 128 | ActionQueue action_queue; |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 129 | // Tick an empty queue and make sure it was not running. |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 130 | EXPECT_FALSE(action_queue.Running()); |
| 131 | action_queue.Tick(); |
| 132 | EXPECT_FALSE(action_queue.Running()); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Brian Silverman | a2ae62d | 2015-03-15 15:55:22 -0700 | [diff] [blame] | 135 | // Tests that starting with an old run message in the goal queue actually works. |
| 136 | // This used to result in the client hanging, waiting for a response to its |
| 137 | // cancel message. |
| 138 | TEST_F(ActionTest, StartWithOldGoal) { |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 139 | ::std::unique_ptr<::aos::EventLoop> test2_event_loop = |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 140 | event_loop_factory_.MakeEventLoop("test2"); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 141 | ::aos::Sender<TestActionGoal> goal_sender = |
| 142 | test2_event_loop->MakeSender<TestActionGoal>("/test_action"); |
| 143 | ::aos::Fetcher<Status> status_fetcher = |
| 144 | test2_event_loop->MakeFetcher<Status>("/test_action"); |
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 | TestActorIndex::Factory nop_actor_factory = |
| 147 | TestActorNOP::MakeFactory(test_event_loop_.get()); |
Brian Silverman | a2ae62d | 2015-03-15 15:55:22 -0700 | [diff] [blame] | 148 | |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 149 | ActionQueue action_queue; |
Brian Silverman | a2ae62d | 2015-03-15 15:55:22 -0700 | [diff] [blame] | 150 | |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 151 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 152 | ::aos::Sender<TestActionGoal>::Builder builder = |
| 153 | goal_sender.MakeBuilder(); |
| 154 | |
| 155 | TestActionGoal::Builder goal_builder = |
| 156 | builder.MakeBuilder<TestActionGoal>(); |
| 157 | |
| 158 | goal_builder.add_run(971); |
| 159 | ASSERT_TRUE(builder.Send(goal_builder.Finish())); |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | TestActorNOP nop_act(actor1_event_loop_.get()); |
| 163 | |
| 164 | ASSERT_FALSE(status_fetcher.Fetch()); |
| 165 | |
Austin Schuh | 02e2aeb | 2020-02-10 21:51:30 -0800 | [diff] [blame] | 166 | RunAt(monotonic_clock::time_point(chrono::seconds(1)), [&]() { |
| 167 | ASSERT_TRUE(status_fetcher.Fetch()); |
| 168 | EXPECT_EQ(0u, status_fetcher->running()); |
| 169 | EXPECT_EQ(0u, status_fetcher->last_running()); |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 170 | |
Austin Schuh | 02e2aeb | 2020-02-10 21:51:30 -0800 | [diff] [blame] | 171 | { |
| 172 | UIntT uint; |
| 173 | uint.val = 0; |
| 174 | action_queue.EnqueueAction(nop_actor_factory.Make(uint)); |
| 175 | } |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 176 | |
Austin Schuh | 02e2aeb | 2020-02-10 21:51:30 -0800 | [diff] [blame] | 177 | // We started an action and it should be running. |
| 178 | EXPECT_TRUE(action_queue.Running()); |
Brian Silverman | a2ae62d | 2015-03-15 15:55:22 -0700 | [diff] [blame] | 179 | |
Austin Schuh | 02e2aeb | 2020-02-10 21:51:30 -0800 | [diff] [blame] | 180 | action_queue.CancelAllActions(); |
| 181 | action_queue.Tick(); |
Brian Silverman | a2ae62d | 2015-03-15 15:55:22 -0700 | [diff] [blame] | 182 | |
Austin Schuh | 02e2aeb | 2020-02-10 21:51:30 -0800 | [diff] [blame] | 183 | EXPECT_TRUE(action_queue.Running()); |
| 184 | }); |
Brian Silverman | a2ae62d | 2015-03-15 15:55:22 -0700 | [diff] [blame] | 185 | |
| 186 | // Run the action so it can signal completion. |
Austin Schuh | 02e2aeb | 2020-02-10 21:51:30 -0800 | [diff] [blame] | 187 | RunAt(monotonic_clock::time_point(chrono::seconds(2)), [&]() { |
| 188 | action_queue.Tick(); |
Brian Silverman | a2ae62d | 2015-03-15 15:55:22 -0700 | [diff] [blame] | 189 | |
Austin Schuh | 02e2aeb | 2020-02-10 21:51:30 -0800 | [diff] [blame] | 190 | // Make sure it stopped. |
| 191 | EXPECT_FALSE(action_queue.Running()); |
| 192 | }); |
| 193 | |
| 194 | event_loop_factory_.RunFor(chrono::seconds(3)); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | // Tests that an action starts and stops. |
| 198 | TEST_F(ActionTest, ActionQueueWasRunning) { |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 199 | TestActorNOP nop_act(actor1_event_loop_.get()); |
| 200 | |
| 201 | TestActorIndex::Factory nop_actor_factory = |
| 202 | TestActorNOP::MakeFactory(test_event_loop_.get()); |
| 203 | |
| 204 | ActionQueue action_queue; |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 205 | |
| 206 | // Tick an empty queue and make sure it was not running. |
Austin Schuh | 02e2aeb | 2020-02-10 21:51:30 -0800 | [diff] [blame] | 207 | RunAt(monotonic_clock::time_point(chrono::seconds(1)), [&]() { |
| 208 | action_queue.Tick(); |
| 209 | EXPECT_FALSE(action_queue.Running()); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 210 | |
Austin Schuh | 02e2aeb | 2020-02-10 21:51:30 -0800 | [diff] [blame] | 211 | { |
| 212 | UIntT uint; |
| 213 | uint.val = 0; |
| 214 | action_queue.EnqueueAction(nop_actor_factory.Make(uint)); |
| 215 | } |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 216 | |
Austin Schuh | 02e2aeb | 2020-02-10 21:51:30 -0800 | [diff] [blame] | 217 | // We started an action and it should be running. |
| 218 | EXPECT_TRUE(action_queue.Running()); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 219 | |
Austin Schuh | 02e2aeb | 2020-02-10 21:51:30 -0800 | [diff] [blame] | 220 | // Tick it and make sure it is still running. |
| 221 | action_queue.Tick(); |
| 222 | EXPECT_TRUE(action_queue.Running()); |
| 223 | }); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 224 | |
| 225 | // Run the action so it can signal completion. |
Austin Schuh | 02e2aeb | 2020-02-10 21:51:30 -0800 | [diff] [blame] | 226 | RunAt(monotonic_clock::time_point(chrono::seconds(2)), [&]() { |
| 227 | action_queue.Tick(); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 228 | |
Austin Schuh | 02e2aeb | 2020-02-10 21:51:30 -0800 | [diff] [blame] | 229 | // Make sure it stopped. |
| 230 | EXPECT_FALSE(action_queue.Running()); |
| 231 | }); |
| 232 | |
| 233 | event_loop_factory_.RunFor(chrono::seconds(3)); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | // Tests that we can cancel two actions and have them both stop. |
| 237 | TEST_F(ActionTest, ActionQueueCancelAll) { |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 238 | TestActorNOP nop_act(actor1_event_loop_.get()); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 239 | |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 240 | TestActorIndex::Factory nop_actor_factory = |
| 241 | TestActorNOP::MakeFactory(test_event_loop_.get()); |
| 242 | |
| 243 | ActionQueue action_queue; |
| 244 | |
| 245 | // Let the actor and action queue start up and confirm nothing is running. |
Austin Schuh | 02e2aeb | 2020-02-10 21:51:30 -0800 | [diff] [blame] | 246 | RunAt(monotonic_clock::time_point(chrono::seconds(1)), [&]() { |
| 247 | action_queue.Tick(); |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 248 | |
Austin Schuh | 02e2aeb | 2020-02-10 21:51:30 -0800 | [diff] [blame] | 249 | EXPECT_FALSE(action_queue.Running()); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 250 | |
Austin Schuh | 02e2aeb | 2020-02-10 21:51:30 -0800 | [diff] [blame] | 251 | // Enqueue two actions to test both cancel. We can have an action and a next |
| 252 | // action so we want to test that. |
| 253 | { |
| 254 | UIntT uint; |
| 255 | uint.val = 0; |
| 256 | action_queue.EnqueueAction(nop_actor_factory.Make(uint)); |
| 257 | action_queue.EnqueueAction(nop_actor_factory.Make(uint)); |
| 258 | } |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 259 | |
Austin Schuh | 02e2aeb | 2020-02-10 21:51:30 -0800 | [diff] [blame] | 260 | action_queue.Tick(); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 261 | |
Austin Schuh | 02e2aeb | 2020-02-10 21:51:30 -0800 | [diff] [blame] | 262 | // Check that current and next exist. |
| 263 | EXPECT_TRUE(action_queue.GetCurrentActionState(nullptr, nullptr, nullptr, |
| 264 | nullptr, nullptr, nullptr)); |
| 265 | EXPECT_TRUE(action_queue.GetNextActionState(nullptr, nullptr, nullptr, |
| 266 | nullptr, nullptr, nullptr)); |
| 267 | |
| 268 | action_queue.CancelAllActions(); |
| 269 | action_queue.Tick(); |
| 270 | |
| 271 | // It should still be running as the actor could not have signaled. |
| 272 | EXPECT_TRUE(action_queue.Running()); |
| 273 | |
| 274 | bool sent_started, sent_cancel, interrupted; |
| 275 | EXPECT_TRUE(action_queue.GetCurrentActionState( |
| 276 | nullptr, &sent_started, &sent_cancel, &interrupted, nullptr, nullptr)); |
| 277 | EXPECT_TRUE(sent_started); |
| 278 | EXPECT_TRUE(sent_cancel); |
| 279 | EXPECT_FALSE(interrupted); |
| 280 | |
| 281 | EXPECT_FALSE(action_queue.GetNextActionState(nullptr, nullptr, nullptr, |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 282 | nullptr, nullptr, nullptr)); |
Austin Schuh | 02e2aeb | 2020-02-10 21:51:30 -0800 | [diff] [blame] | 283 | }); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 284 | |
| 285 | // Run the action so it can signal completion. |
Austin Schuh | 02e2aeb | 2020-02-10 21:51:30 -0800 | [diff] [blame] | 286 | RunAt(monotonic_clock::time_point(chrono::seconds(2)), [&]() { |
| 287 | action_queue.Tick(); |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 288 | |
Austin Schuh | 02e2aeb | 2020-02-10 21:51:30 -0800 | [diff] [blame] | 289 | // Make sure it stopped. |
| 290 | EXPECT_FALSE(action_queue.Running()); |
| 291 | EXPECT_EQ(1, nop_act.running_count()); |
| 292 | }); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 293 | |
Austin Schuh | 02e2aeb | 2020-02-10 21:51:30 -0800 | [diff] [blame] | 294 | event_loop_factory_.RunFor(chrono::seconds(3)); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | // Tests that an action that would block forever stops when canceled. |
| 298 | TEST_F(ActionTest, ActionQueueCancelOne) { |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 299 | TestActorShouldCancel cancel_act(actor1_event_loop_.get()); |
| 300 | |
| 301 | TestActorShouldCancel::Factory cancel_action_factory = |
| 302 | TestActorShouldCancel::MakeFactory(test_event_loop_.get()); |
| 303 | |
| 304 | ActionQueue action_queue; |
| 305 | |
| 306 | // Let the actor and action queue start up. |
Austin Schuh | 02e2aeb | 2020-02-10 21:51:30 -0800 | [diff] [blame] | 307 | RunAt(monotonic_clock::time_point(chrono::seconds(1)), [&]() { |
| 308 | action_queue.Tick(); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 309 | |
Austin Schuh | 02e2aeb | 2020-02-10 21:51:30 -0800 | [diff] [blame] | 310 | // Enqueue blocking action. |
| 311 | { |
| 312 | UIntT uint; |
| 313 | uint.val = 0; |
| 314 | action_queue.EnqueueAction(cancel_action_factory.Make(uint)); |
| 315 | } |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 316 | |
Austin Schuh | 02e2aeb | 2020-02-10 21:51:30 -0800 | [diff] [blame] | 317 | action_queue.Tick(); |
| 318 | EXPECT_TRUE(action_queue.Running()); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 319 | |
Austin Schuh | 02e2aeb | 2020-02-10 21:51:30 -0800 | [diff] [blame] | 320 | // Tell action to cancel. |
| 321 | action_queue.CancelCurrentAction(); |
| 322 | action_queue.Tick(); |
| 323 | }); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 324 | |
| 325 | // This will block forever on failure. |
| 326 | // TODO(ben): prolly a bad way to fail |
Austin Schuh | 02e2aeb | 2020-02-10 21:51:30 -0800 | [diff] [blame] | 327 | RunAt(monotonic_clock::time_point(chrono::seconds(2)), [&]() { |
| 328 | action_queue.Tick(); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 329 | |
Austin Schuh | 02e2aeb | 2020-02-10 21:51:30 -0800 | [diff] [blame] | 330 | // It should still be running as the actor could not have signalled. |
| 331 | EXPECT_FALSE(action_queue.Running()); |
| 332 | }); |
| 333 | |
| 334 | event_loop_factory_.RunFor(chrono::seconds(3)); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 335 | } |
| 336 | |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 337 | // 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] | 338 | TEST_F(ActionTest, ActionQueueTwoActions) { |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 339 | TestActorNOP nop_actor(actor1_event_loop_.get()); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 340 | |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 341 | TestActorIndex::Factory nop_actor_factory = |
| 342 | TestActorNOP::MakeFactory(test_event_loop_.get()); |
| 343 | |
| 344 | ActionQueue action_queue; |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 345 | |
| 346 | // id for the first time run. |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 347 | uint32_t nop_actor_id = 0; |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 348 | // Check the internal state and write down id for later use. |
| 349 | bool sent_started, sent_cancel, interrupted; |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 350 | // id for the second run. |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 351 | uint32_t nop_actor2_id = 0; |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 352 | |
Austin Schuh | 02e2aeb | 2020-02-10 21:51:30 -0800 | [diff] [blame] | 353 | // Tick an empty queue and make sure it was not running. |
| 354 | RunAt(monotonic_clock::time_point(chrono::seconds(1)), [&]() { |
| 355 | action_queue.Tick(); |
| 356 | EXPECT_FALSE(action_queue.Running()); |
| 357 | |
| 358 | // Enqueue action to be canceled. |
| 359 | { |
| 360 | UIntT uint; |
| 361 | uint.val = 0; |
| 362 | action_queue.EnqueueAction(nop_actor_factory.Make(uint)); |
| 363 | } |
| 364 | action_queue.Tick(); |
| 365 | |
| 366 | // Should still be running as the actor could not have signalled. |
| 367 | EXPECT_TRUE(action_queue.Running()); |
| 368 | |
| 369 | EXPECT_TRUE(action_queue.GetCurrentActionState(nullptr, &sent_started, |
| 370 | &sent_cancel, &interrupted, |
| 371 | &nop_actor_id, nullptr)); |
| 372 | EXPECT_TRUE(sent_started); |
| 373 | EXPECT_FALSE(sent_cancel); |
| 374 | EXPECT_FALSE(interrupted); |
| 375 | ASSERT_NE(0u, nop_actor_id); |
| 376 | |
| 377 | // Add the next action which should ensure the first stopped. |
| 378 | { |
| 379 | UIntT uint; |
| 380 | uint.val = 0; |
| 381 | action_queue.EnqueueAction(nop_actor_factory.Make(uint)); |
| 382 | } |
| 383 | |
| 384 | // Check the internal state and write down id for later use. |
| 385 | EXPECT_TRUE(action_queue.GetNextActionState(nullptr, &sent_started, |
| 386 | &sent_cancel, &interrupted, |
| 387 | &nop_actor2_id, nullptr)); |
| 388 | EXPECT_NE(nop_actor_id, nop_actor2_id); |
| 389 | EXPECT_FALSE(sent_started); |
| 390 | EXPECT_FALSE(sent_cancel); |
| 391 | EXPECT_FALSE(interrupted); |
| 392 | ASSERT_NE(0u, nop_actor2_id); |
| 393 | |
| 394 | action_queue.Tick(); |
| 395 | }); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 396 | |
| 397 | // Run the action so it can signal completion. |
Austin Schuh | 02e2aeb | 2020-02-10 21:51:30 -0800 | [diff] [blame] | 398 | RunAt(monotonic_clock::time_point(chrono::seconds(2)), [&]() { |
| 399 | action_queue.Tick(); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 400 | |
Austin Schuh | 02e2aeb | 2020-02-10 21:51:30 -0800 | [diff] [blame] | 401 | // Check the new action is the right one. |
| 402 | uint32_t test_id = 0; |
| 403 | EXPECT_TRUE(action_queue.GetCurrentActionState( |
| 404 | nullptr, &sent_started, &sent_cancel, &interrupted, &test_id, nullptr)); |
| 405 | EXPECT_TRUE(sent_started); |
| 406 | EXPECT_FALSE(sent_cancel); |
| 407 | EXPECT_FALSE(interrupted); |
| 408 | EXPECT_EQ(nop_actor2_id, test_id); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 409 | |
Austin Schuh | 02e2aeb | 2020-02-10 21:51:30 -0800 | [diff] [blame] | 410 | // Make sure it is still going. |
| 411 | EXPECT_TRUE(action_queue.Running()); |
| 412 | }); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 413 | |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 414 | // Now let everything finish. |
Austin Schuh | 02e2aeb | 2020-02-10 21:51:30 -0800 | [diff] [blame] | 415 | RunAt(monotonic_clock::time_point(chrono::seconds(3)), [&]() { |
| 416 | action_queue.Tick(); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 417 | |
Austin Schuh | 02e2aeb | 2020-02-10 21:51:30 -0800 | [diff] [blame] | 418 | // Make sure it stopped. |
| 419 | EXPECT_FALSE(action_queue.Running()); |
| 420 | }); |
| 421 | |
| 422 | event_loop_factory_.RunFor(chrono::seconds(4)); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 423 | } |
| 424 | |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame] | 425 | // Tests that we do get an index with our goal |
| 426 | TEST_F(ActionTest, ActionIndex) { |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 427 | TestActorIndex idx_actor(actor1_event_loop_.get()); |
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 | TestActorIndex::Factory test_actor_index_factory = |
| 430 | TestActorIndex::MakeFactory(test_event_loop_.get()); |
Austin Schuh | 02e2aeb | 2020-02-10 21:51:30 -0800 | [diff] [blame] | 431 | ::aos::Fetcher<actions::TestActionGoal> goal_fetcher_ = |
| 432 | test_event_loop_->MakeFetcher<actions::TestActionGoal>("/test_action"); |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 433 | |
| 434 | ActionQueue action_queue; |
| 435 | // Tick an empty queue and make sure it was not running. Also tick the |
| 436 | // factory to allow it to send out the initial cancel message. |
Austin Schuh | 02e2aeb | 2020-02-10 21:51:30 -0800 | [diff] [blame] | 437 | RunAt(monotonic_clock::time_point(chrono::seconds(1)), [&]() { |
| 438 | action_queue.Tick(); |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 439 | |
Austin Schuh | 02e2aeb | 2020-02-10 21:51:30 -0800 | [diff] [blame] | 440 | EXPECT_FALSE(action_queue.Running()); |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame] | 441 | |
Austin Schuh | 02e2aeb | 2020-02-10 21:51:30 -0800 | [diff] [blame] | 442 | // Enqueue action to post index. |
| 443 | { |
| 444 | UIntT uint; |
| 445 | uint.val = 5; |
| 446 | action_queue.EnqueueAction(test_actor_index_factory.Make(uint)); |
| 447 | } |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame] | 448 | |
Austin Schuh | 02e2aeb | 2020-02-10 21:51:30 -0800 | [diff] [blame] | 449 | ASSERT_TRUE(goal_fetcher_.Fetch()); |
| 450 | EXPECT_EQ(5u, goal_fetcher_->params()->val()); |
| 451 | EXPECT_EQ(0u, idx_actor.index); |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame] | 452 | |
Austin Schuh | 02e2aeb | 2020-02-10 21:51:30 -0800 | [diff] [blame] | 453 | action_queue.Tick(); |
| 454 | }); |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame] | 455 | |
| 456 | // Run the next action so it can accomplish signal completion. |
Austin Schuh | 02e2aeb | 2020-02-10 21:51:30 -0800 | [diff] [blame] | 457 | RunAt(monotonic_clock::time_point(chrono::seconds(2)), [&]() { |
| 458 | action_queue.Tick(); |
| 459 | EXPECT_EQ(5u, idx_actor.index); |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 460 | |
Austin Schuh | 02e2aeb | 2020-02-10 21:51:30 -0800 | [diff] [blame] | 461 | // Enqueue action to post index. |
| 462 | { |
| 463 | UIntT uint; |
| 464 | uint.val = 3; |
| 465 | action_queue.EnqueueAction(test_actor_index_factory.Make(uint)); |
| 466 | } |
| 467 | ASSERT_TRUE(goal_fetcher_.Fetch()); |
| 468 | EXPECT_EQ(3u, goal_fetcher_->params()->val()); |
| 469 | }); |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame] | 470 | |
| 471 | // Run the next action so it can accomplish signal completion. |
Austin Schuh | 02e2aeb | 2020-02-10 21:51:30 -0800 | [diff] [blame] | 472 | RunAt(monotonic_clock::time_point(chrono::seconds(3)), [&]() { |
| 473 | action_queue.Tick(); |
| 474 | EXPECT_EQ(3u, idx_actor.index); |
| 475 | }); |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 476 | |
Austin Schuh | 02e2aeb | 2020-02-10 21:51:30 -0800 | [diff] [blame] | 477 | event_loop_factory_.RunFor(chrono::seconds(3)); |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | // Tests that an action with a structure params works. |
| 481 | TEST_F(ActionTest, StructParamType) { |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 482 | TestActor2Nop nop_actor(actor2_event_loop_.get()); |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame] | 483 | |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 484 | TestActor2Nop::Factory test_action_2_nop_factory = |
| 485 | TestActor2Nop::MakeFactory(test_event_loop_.get()); |
| 486 | |
| 487 | ActionQueue action_queue; |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame] | 488 | |
Austin Schuh | 02e2aeb | 2020-02-10 21:51:30 -0800 | [diff] [blame] | 489 | RunAt(monotonic_clock::time_point(chrono::seconds(0)), [&]() { |
| 490 | // Tick an empty queue and make sure it was not running. |
| 491 | action_queue.Tick(); |
| 492 | EXPECT_FALSE(action_queue.Running()); |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame] | 493 | |
Austin Schuh | 02e2aeb | 2020-02-10 21:51:30 -0800 | [diff] [blame] | 494 | actions::MyParamsT p; |
| 495 | p.param1 = 5.0; |
| 496 | p.param2 = 7; |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame] | 497 | |
Austin Schuh | 02e2aeb | 2020-02-10 21:51:30 -0800 | [diff] [blame] | 498 | action_queue.EnqueueAction(test_action_2_nop_factory.Make(p)); |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame] | 499 | |
Austin Schuh | 02e2aeb | 2020-02-10 21:51:30 -0800 | [diff] [blame] | 500 | // We started an action and it should be running. |
| 501 | EXPECT_TRUE(action_queue.Running()); |
| 502 | |
| 503 | // Tick it and make sure it is still running. |
| 504 | action_queue.Tick(); |
| 505 | EXPECT_TRUE(action_queue.Running()); |
| 506 | }); |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame] | 507 | |
| 508 | // Run the action so it can signal completion. |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 509 | // The actor takes no time, but running for a second is the best way to get it |
| 510 | // to go. |
Austin Schuh | 02e2aeb | 2020-02-10 21:51:30 -0800 | [diff] [blame] | 511 | RunAt(monotonic_clock::time_point(chrono::seconds(1)), [&]() { |
| 512 | action_queue.Tick(); |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 513 | |
Austin Schuh | 02e2aeb | 2020-02-10 21:51:30 -0800 | [diff] [blame] | 514 | // Make sure it stopped. |
| 515 | EXPECT_FALSE(action_queue.Running()); |
| 516 | }); |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame] | 517 | |
Austin Schuh | 02e2aeb | 2020-02-10 21:51:30 -0800 | [diff] [blame] | 518 | event_loop_factory_.RunFor(chrono::seconds(2)); |
Brian Silverman | 237a554 | 2015-03-29 17:59:29 -0400 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | } // namespace testing |
| 522 | } // namespace actions |
| 523 | } // namespace common |
| 524 | } // namespace aos |