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 | |
| 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 | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 14 | #include "aos/testing/path.h" |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 15 | |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 16 | namespace aos { |
| 17 | namespace common { |
| 18 | namespace actions { |
| 19 | namespace testing { |
| 20 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 21 | namespace chrono = ::std::chrono; |
| 22 | |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame] | 23 | class TestActorIndex |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 24 | : public aos::common::actions::ActorBase<actions::TestActionGoal> { |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame] | 25 | public: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 26 | typedef TypedActionFactory<actions::TestActionGoal> Factory; |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 27 | |
| 28 | explicit TestActorIndex(::aos::EventLoop *event_loop) |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 29 | : aos::common::actions::ActorBase<actions::TestActionGoal>( |
| 30 | event_loop, "/test_action") {} |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 31 | |
| 32 | static Factory MakeFactory(::aos::EventLoop *event_loop) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 33 | return Factory(event_loop, "/test_action"); |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 34 | } |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame] | 35 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 36 | bool RunAction(const UInt *new_index) override { |
| 37 | VLOG(1) << "New index " << FlatbufferToJson(new_index); |
| 38 | index = new_index->val(); |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame] | 39 | return true; |
| 40 | } |
| 41 | |
| 42 | uint32_t index = 0; |
| 43 | }; |
| 44 | |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 45 | class TestActorNOP |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 46 | : public aos::common::actions::ActorBase<actions::TestActionGoal> { |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 47 | public: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 48 | typedef TypedActionFactory<actions::TestActionGoal> Factory; |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 49 | |
| 50 | explicit TestActorNOP(::aos::EventLoop *event_loop) |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame^] | 51 | : actions::ActorBase<actions::TestActionGoal>(event_loop, |
| 52 | "/test_action") {} |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 53 | |
| 54 | static Factory MakeFactory(::aos::EventLoop *event_loop) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 55 | return Factory(event_loop, "/test_action"); |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 56 | } |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 57 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 58 | bool RunAction(const UInt *) override { return true; } |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 59 | }; |
| 60 | |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 61 | class TestActorShouldCancel |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 62 | : public aos::common::actions::ActorBase<actions::TestActionGoal> { |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 63 | public: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 64 | typedef TypedActionFactory<actions::TestActionGoal> Factory; |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 65 | |
| 66 | explicit TestActorShouldCancel(::aos::EventLoop *event_loop) |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 67 | : aos::common::actions::ActorBase<actions::TestActionGoal>( |
| 68 | event_loop, "/test_action") {} |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 69 | |
| 70 | static Factory MakeFactory(::aos::EventLoop *event_loop) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 71 | return Factory(event_loop, "/test_action"); |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 72 | } |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 73 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 74 | bool RunAction(const UInt *) override { |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 75 | while (!ShouldCancel()) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 76 | AOS_LOG(FATAL, "NOT CANCELED!!\n"); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 77 | } |
Daniel Petti | 3b1e48f | 2015-02-15 15:57:53 -0800 | [diff] [blame] | 78 | return true; |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 79 | } |
| 80 | }; |
| 81 | |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame] | 82 | class TestActor2Nop |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 83 | : public aos::common::actions::ActorBase<actions::TestAction2Goal> { |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame] | 84 | public: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 85 | typedef TypedActionFactory<actions::TestAction2Goal> Factory; |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 86 | |
| 87 | explicit TestActor2Nop(::aos::EventLoop *event_loop) |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame^] | 88 | : actions::ActorBase<actions::TestAction2Goal>(event_loop, |
| 89 | "/test_action2") {} |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 90 | |
| 91 | static Factory MakeFactory(::aos::EventLoop *event_loop) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 92 | return Factory(event_loop, "/test_action2"); |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 93 | } |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame] | 94 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 95 | bool RunAction(const actions::MyParams *) { return true; } |
Ben Fredrickson | 9fb2ab1 | 2015-02-16 16:42:08 -0800 | [diff] [blame] | 96 | }; |
| 97 | |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 98 | class ActionTest : public ::testing::Test { |
| 99 | protected: |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 100 | ActionTest() |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 101 | : configuration_(configuration::ReadConfig( |
| 102 | aos::testing::ArtifactPath("aos/actions/action_test_config.json"))), |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 103 | event_loop_factory_(&configuration_.message()), |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 104 | actor1_event_loop_(event_loop_factory_.MakeEventLoop("actor1")), |
| 105 | actor2_event_loop_(event_loop_factory_.MakeEventLoop("actor2")), |
Brian Silverman | 13065ed | 2020-12-16 15:15:27 -0800 | [diff] [blame] | 106 | test_event_loop_(event_loop_factory_.MakeEventLoop("test")) {} |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 107 | |
Austin Schuh | 02e2aeb | 2020-02-10 21:51:30 -0800 | [diff] [blame] | 108 | void RunAt(monotonic_clock::time_point exec_time, std::function<void()> fn) { |
| 109 | TimerHandler *timer = test_event_loop_->AddTimer(fn); |
| 110 | test_event_loop_->OnRun([timer, exec_time]() { timer->Setup(exec_time); }); |
| 111 | } |
| 112 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 113 | FlatbufferDetachedBuffer<Configuration> configuration_; |
| 114 | |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 115 | // Bring up and down Core. |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 116 | ::aos::SimulatedEventLoopFactory event_loop_factory_; |
| 117 | |
| 118 | ::std::unique_ptr<::aos::EventLoop> actor1_event_loop_; |
| 119 | ::std::unique_ptr<::aos::EventLoop> actor2_event_loop_; |
| 120 | ::std::unique_ptr<::aos::EventLoop> test_event_loop_; |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 121 | }; |
| 122 | |
| 123 | // Tests that the the actions exist in a safe state at startup. |
| 124 | TEST_F(ActionTest, DoesNothing) { |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 125 | ActionQueue action_queue; |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 126 | // Tick an empty queue and make sure it was not running. |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 127 | EXPECT_FALSE(action_queue.Running()); |
| 128 | action_queue.Tick(); |
| 129 | EXPECT_FALSE(action_queue.Running()); |
Ben Fredrickson | d69f38b | 2015-01-28 20:06:15 -0800 | [diff] [blame] | 130 | } |
| 131 | |
Brian Silverman | a2ae62d | 2015-03-15 15:55:22 -0700 | [diff] [blame] | 132 | // Tests that starting with an old run message in the goal queue actually works. |
| 133 | // This used to result in the client hanging, waiting for a response to its |
| 134 | // cancel message. |
| 135 | TEST_F(ActionTest, StartWithOldGoal) { |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 136 | ::std::unique_ptr<::aos::EventLoop> test2_event_loop = |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 137 | event_loop_factory_.MakeEventLoop("test2"); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 138 | ::aos::Sender<TestActionGoal> goal_sender = |
| 139 | test2_event_loop->MakeSender<TestActionGoal>("/test_action"); |
| 140 | ::aos::Fetcher<Status> status_fetcher = |
| 141 | test2_event_loop->MakeFetcher<Status>("/test_action"); |
Brian Silverman | a2ae62d | 2015-03-15 15:55:22 -0700 | [diff] [blame] | 142 | |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 143 | TestActorIndex::Factory nop_actor_factory = |
| 144 | TestActorNOP::MakeFactory(test_event_loop_.get()); |
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 | ActionQueue action_queue; |
Brian Silverman | a2ae62d | 2015-03-15 15:55:22 -0700 | [diff] [blame] | 147 | |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 148 | { |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame^] | 149 | ::aos::Sender<TestActionGoal>::Builder builder = goal_sender.MakeBuilder(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 150 | |
| 151 | TestActionGoal::Builder goal_builder = |
| 152 | builder.MakeBuilder<TestActionGoal>(); |
| 153 | |
| 154 | goal_builder.add_run(971); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame^] | 155 | builder.CheckOk(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 |