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