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