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