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