Austin Schuh | 59398d3 | 2023-05-03 08:10:55 -0700 | [diff] [blame] | 1 | #include <chrono> |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 2 | #include <csignal> |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 3 | #include <future> |
| 4 | #include <thread> |
| 5 | |
| 6 | #include "aos/events/ping_generated.h" |
| 7 | #include "aos/events/pong_generated.h" |
Austin Schuh | 59398d3 | 2023-05-03 08:10:55 -0700 | [diff] [blame] | 8 | #include "aos/ipc_lib/event.h" |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 9 | #include "aos/network/team_number.h" |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 10 | #include "aos/testing/path.h" |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 11 | #include "aos/testing/tmpdir.h" |
Austin Schuh | fc0bca4 | 2021-12-31 21:49:41 -0800 | [diff] [blame] | 12 | #include "aos/util/file.h" |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 13 | #include "gtest/gtest.h" |
| 14 | #include "starter_rpc_lib.h" |
| 15 | #include "starterd_lib.h" |
| 16 | |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 17 | using aos::testing::ArtifactPath; |
| 18 | |
Austin Schuh | 5f79a5a | 2021-10-12 17:46:50 -0700 | [diff] [blame] | 19 | namespace aos { |
| 20 | namespace starter { |
| 21 | |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 22 | class StarterdTest : public ::testing::Test { |
| 23 | public: |
Austin Schuh | 59398d3 | 2023-05-03 08:10:55 -0700 | [diff] [blame] | 24 | StarterdTest() { |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 25 | // Nuke the shm dir: |
Austin Schuh | 59398d3 | 2023-05-03 08:10:55 -0700 | [diff] [blame] | 26 | aos::util::UnlinkRecursive(FLAGS_shm_base); |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | protected: |
James Kuszmaul | 6b35e3a | 2022-04-06 15:00:39 -0700 | [diff] [blame] | 30 | void SetupStarterCleanup(aos::starter::Starter *starter) { |
| 31 | starter->event_loop() |
| 32 | ->AddTimer([this, starter]() { |
| 33 | if (test_done_) { |
| 34 | starter->Cleanup(); |
| 35 | } |
| 36 | }) |
| 37 | ->Setup(starter->event_loop()->monotonic_now(), |
Austin Schuh | 59398d3 | 2023-05-03 08:10:55 -0700 | [diff] [blame] | 38 | std::chrono::milliseconds(100)); |
James Kuszmaul | 6b35e3a | 2022-04-06 15:00:39 -0700 | [diff] [blame] | 39 | } |
| 40 | |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 41 | gflags::FlagSaver flag_saver_; |
James Kuszmaul | 6b35e3a | 2022-04-06 15:00:39 -0700 | [diff] [blame] | 42 | // Used to track when the test completes so that we can clean up the starter |
| 43 | // in its thread. |
| 44 | std::atomic<bool> test_done_{false}; |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | struct TestParams { |
| 48 | std::string config; |
| 49 | std::string hostname; |
| 50 | }; |
| 51 | |
| 52 | class StarterdConfigParamTest |
| 53 | : public StarterdTest, |
| 54 | public ::testing::WithParamInterface<TestParams> {}; |
| 55 | |
| 56 | TEST_P(StarterdConfigParamTest, MultiNodeStartStopTest) { |
| 57 | gflags::FlagSaver flag_saver; |
| 58 | FLAGS_override_hostname = GetParam().hostname; |
| 59 | const std::string config_file = ArtifactPath(GetParam().config); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 60 | |
| 61 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 62 | aos::configuration::ReadConfig(config_file); |
| 63 | |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 64 | auto new_config = aos::configuration::MergeWithConfig( |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 65 | &config.message(), |
| 66 | absl::StrFormat( |
| 67 | R"({"applications": [ |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 68 | { |
| 69 | "name": "ping", |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 70 | "executable_name": "%s", |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 71 | "nodes": ["pi1"], |
| 72 | "args": ["--shm_base", "%s", "--config", "%s", "--override_hostname", "%s"] |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 73 | }, |
| 74 | { |
| 75 | "name": "pong", |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 76 | "executable_name": "%s", |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 77 | "nodes": ["pi1"], |
| 78 | "args": ["--shm_base", "%s", "--config", "%s", "--override_hostname", "%s"] |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 79 | } |
| 80 | ]})", |
Austin Schuh | 59398d3 | 2023-05-03 08:10:55 -0700 | [diff] [blame] | 81 | ArtifactPath("aos/events/ping"), FLAGS_shm_base, config_file, |
| 82 | GetParam().hostname, ArtifactPath("aos/events/pong"), FLAGS_shm_base, |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 83 | config_file, GetParam().hostname)); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 84 | |
| 85 | const aos::Configuration *config_msg = &new_config.message(); |
| 86 | |
| 87 | // Set up starter with config file |
| 88 | aos::starter::Starter starter(config_msg); |
| 89 | |
| 90 | // Create an event loop to watch for ping messages, verifying it actually |
| 91 | // started. |
| 92 | aos::ShmEventLoop watcher_loop(config_msg); |
| 93 | watcher_loop.SkipAosLog(); |
| 94 | |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 95 | aos::ShmEventLoop client_loop(config_msg); |
| 96 | client_loop.SkipAosLog(); |
| 97 | StarterClient client(&client_loop); |
| 98 | client.SetTimeoutHandler( |
| 99 | []() { FAIL() << ": Command should not have timed out."; }); |
| 100 | bool success = false; |
| 101 | client.SetSuccessHandler([&success, &client_loop]() { |
| 102 | client_loop.Exit(); |
| 103 | success = true; |
| 104 | }); |
| 105 | |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 106 | watcher_loop |
| 107 | .AddTimer([&watcher_loop] { |
| 108 | watcher_loop.Exit(); |
| 109 | FAIL(); |
| 110 | }) |
| 111 | ->Setup(watcher_loop.monotonic_now() + std::chrono::seconds(7)); |
| 112 | |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 113 | std::atomic<int> test_stage = 0; |
| 114 | // Watch on the client loop since we need to interact with the StarterClient. |
| 115 | client_loop.MakeWatcher("/test", [&test_stage, &client, |
| 116 | &client_loop](const aos::examples::Ping &) { |
| 117 | switch (test_stage) { |
| 118 | case 1: { |
| 119 | test_stage = 2; |
| 120 | break; |
| 121 | } |
| 122 | case 2: { |
| 123 | { |
| 124 | client.SendCommands({{Command::STOP, "ping", {client_loop.node()}}}, |
| 125 | std::chrono::seconds(3)); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 126 | } |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 127 | test_stage = 3; |
| 128 | break; |
| 129 | } |
| 130 | } |
| 131 | }); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 132 | |
| 133 | watcher_loop.MakeWatcher( |
| 134 | "/aos", [&test_stage, &watcher_loop](const aos::starter::Status &status) { |
| 135 | const aos::starter::ApplicationStatus *app_status = |
| 136 | FindApplicationStatus(status, "ping"); |
| 137 | if (app_status == nullptr) { |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | switch (test_stage) { |
| 142 | case 0: { |
| 143 | if (app_status->has_state() && |
| 144 | app_status->state() == aos::starter::State::RUNNING) { |
| 145 | test_stage = 1; |
| 146 | } |
| 147 | break; |
| 148 | } |
| 149 | |
| 150 | case 3: { |
| 151 | if (app_status->has_state() && |
| 152 | app_status->state() == aos::starter::State::STOPPED) { |
| 153 | watcher_loop.Exit(); |
| 154 | SUCCEED(); |
| 155 | } |
| 156 | break; |
| 157 | } |
| 158 | } |
| 159 | }); |
| 160 | |
James Kuszmaul | 6b35e3a | 2022-04-06 15:00:39 -0700 | [diff] [blame] | 161 | SetupStarterCleanup(&starter); |
| 162 | |
Austin Schuh | 59398d3 | 2023-05-03 08:10:55 -0700 | [diff] [blame] | 163 | Event starter_started; |
| 164 | std::thread starterd_thread([&starter, &starter_started] { |
| 165 | starter.event_loop()->OnRun( |
| 166 | [&starter_started]() { starter_started.Set(); }); |
| 167 | starter.Run(); |
| 168 | }); |
| 169 | starter_started.Wait(); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 170 | |
Austin Schuh | 59398d3 | 2023-05-03 08:10:55 -0700 | [diff] [blame] | 171 | Event client_started; |
| 172 | std::thread client_thread([&client_loop, &client_started] { |
| 173 | client_loop.OnRun([&client_started]() { client_started.Set(); }); |
| 174 | client_loop.Run(); |
| 175 | }); |
| 176 | client_started.Wait(); |
| 177 | |
| 178 | watcher_loop.Run(); |
James Kuszmaul | 6b35e3a | 2022-04-06 15:00:39 -0700 | [diff] [blame] | 179 | test_done_ = true; |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 180 | client_thread.join(); |
Austin Schuh | cad2af9 | 2023-05-28 13:56:55 -0700 | [diff] [blame] | 181 | ASSERT_TRUE(success); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 182 | starterd_thread.join(); |
| 183 | } |
| 184 | |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 185 | INSTANTIATE_TEST_SUITE_P( |
| 186 | StarterdConfigParamTest, StarterdConfigParamTest, |
| 187 | ::testing::Values(TestParams{"aos/events/pingpong_config.json", ""}, |
| 188 | TestParams{"aos/starter/multinode_pingpong_config.json", |
| 189 | "pi1"})); |
| 190 | |
| 191 | TEST_F(StarterdTest, DeathTest) { |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 192 | const std::string config_file = |
| 193 | ArtifactPath("aos/events/pingpong_config.json"); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 194 | |
| 195 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 196 | aos::configuration::ReadConfig(config_file); |
| 197 | |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 198 | auto new_config = aos::configuration::MergeWithConfig( |
| 199 | &config.message(), absl::StrFormat( |
| 200 | R"({"applications": [ |
| 201 | { |
| 202 | "name": "ping", |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 203 | "executable_name": "%s", |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 204 | "args": ["--shm_base", "%s"] |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 205 | }, |
| 206 | { |
| 207 | "name": "pong", |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 208 | "executable_name": "%s", |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 209 | "args": ["--shm_base", "%s"] |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 210 | } |
| 211 | ]})", |
Austin Schuh | 59398d3 | 2023-05-03 08:10:55 -0700 | [diff] [blame] | 212 | ArtifactPath("aos/events/ping"), FLAGS_shm_base, |
| 213 | ArtifactPath("aos/events/pong"), FLAGS_shm_base)); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 214 | |
| 215 | const aos::Configuration *config_msg = &new_config.message(); |
| 216 | |
| 217 | // Set up starter with config file |
| 218 | aos::starter::Starter starter(config_msg); |
| 219 | |
| 220 | // Create an event loop to watch for ping messages, verifying it actually |
| 221 | // started. |
| 222 | aos::ShmEventLoop watcher_loop(config_msg); |
| 223 | watcher_loop.SkipAosLog(); |
| 224 | |
| 225 | watcher_loop |
| 226 | .AddTimer([&watcher_loop] { |
| 227 | watcher_loop.Exit(); |
| 228 | FAIL(); |
| 229 | }) |
Austin Schuh | a07b3ce | 2021-10-10 12:33:21 -0700 | [diff] [blame] | 230 | ->Setup(watcher_loop.monotonic_now() + std::chrono::seconds(11)); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 231 | |
| 232 | int test_stage = 0; |
| 233 | uint64_t id; |
| 234 | |
| 235 | watcher_loop.MakeWatcher("/aos", [&test_stage, &watcher_loop, |
| 236 | &id](const aos::starter::Status &status) { |
| 237 | const aos::starter::ApplicationStatus *app_status = |
| 238 | FindApplicationStatus(status, "ping"); |
| 239 | if (app_status == nullptr) { |
| 240 | return; |
| 241 | } |
| 242 | |
| 243 | switch (test_stage) { |
| 244 | case 0: { |
| 245 | if (app_status->has_state() && |
| 246 | app_status->state() == aos::starter::State::RUNNING) { |
Austin Schuh | a07b3ce | 2021-10-10 12:33:21 -0700 | [diff] [blame] | 247 | LOG(INFO) << "Ping is running"; |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 248 | test_stage = 1; |
| 249 | ASSERT_TRUE(app_status->has_pid()); |
| 250 | ASSERT_TRUE(kill(app_status->pid(), SIGINT) != -1); |
| 251 | ASSERT_TRUE(app_status->has_id()); |
| 252 | id = app_status->id(); |
| 253 | } |
| 254 | break; |
| 255 | } |
| 256 | |
| 257 | case 1: { |
| 258 | if (app_status->has_state() && |
| 259 | app_status->state() == aos::starter::State::RUNNING && |
| 260 | app_status->has_id() && app_status->id() != id) { |
Austin Schuh | a07b3ce | 2021-10-10 12:33:21 -0700 | [diff] [blame] | 261 | LOG(INFO) << "Ping restarted"; |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 262 | watcher_loop.Exit(); |
| 263 | SUCCEED(); |
| 264 | } |
| 265 | break; |
| 266 | } |
| 267 | } |
| 268 | }); |
| 269 | |
James Kuszmaul | 6b35e3a | 2022-04-06 15:00:39 -0700 | [diff] [blame] | 270 | SetupStarterCleanup(&starter); |
| 271 | |
Austin Schuh | 59398d3 | 2023-05-03 08:10:55 -0700 | [diff] [blame] | 272 | Event starter_started; |
| 273 | std::thread starterd_thread([&starter, &starter_started] { |
| 274 | starter.event_loop()->OnRun( |
| 275 | [&starter_started]() { starter_started.Set(); }); |
| 276 | starter.Run(); |
| 277 | }); |
| 278 | starter_started.Wait(); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 279 | watcher_loop.Run(); |
| 280 | |
James Kuszmaul | 6b35e3a | 2022-04-06 15:00:39 -0700 | [diff] [blame] | 281 | test_done_ = true; |
| 282 | |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 283 | starterd_thread.join(); |
| 284 | } |
Austin Schuh | 5f79a5a | 2021-10-12 17:46:50 -0700 | [diff] [blame] | 285 | |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 286 | TEST_F(StarterdTest, Autostart) { |
Austin Schuh | 5f79a5a | 2021-10-12 17:46:50 -0700 | [diff] [blame] | 287 | const std::string config_file = |
| 288 | ArtifactPath("aos/events/pingpong_config.json"); |
| 289 | |
| 290 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 291 | aos::configuration::ReadConfig(config_file); |
| 292 | |
Austin Schuh | 5f79a5a | 2021-10-12 17:46:50 -0700 | [diff] [blame] | 293 | auto new_config = aos::configuration::MergeWithConfig( |
| 294 | &config.message(), absl::StrFormat( |
| 295 | R"({"applications": [ |
| 296 | { |
| 297 | "name": "ping", |
| 298 | "executable_name": "%s", |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 299 | "args": ["--shm_base", "%s"], |
Austin Schuh | 5f79a5a | 2021-10-12 17:46:50 -0700 | [diff] [blame] | 300 | "autostart": false |
| 301 | }, |
| 302 | { |
| 303 | "name": "pong", |
| 304 | "executable_name": "%s", |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 305 | "args": ["--shm_base", "%s"] |
Austin Schuh | 5f79a5a | 2021-10-12 17:46:50 -0700 | [diff] [blame] | 306 | } |
| 307 | ]})", |
Austin Schuh | 59398d3 | 2023-05-03 08:10:55 -0700 | [diff] [blame] | 308 | ArtifactPath("aos/events/ping"), FLAGS_shm_base, |
| 309 | ArtifactPath("aos/events/pong"), FLAGS_shm_base)); |
Austin Schuh | 5f79a5a | 2021-10-12 17:46:50 -0700 | [diff] [blame] | 310 | |
| 311 | const aos::Configuration *config_msg = &new_config.message(); |
| 312 | |
| 313 | // Set up starter with config file |
| 314 | aos::starter::Starter starter(config_msg); |
| 315 | |
| 316 | // Create an event loop to watch for the application starting up. |
| 317 | aos::ShmEventLoop watcher_loop(config_msg); |
| 318 | watcher_loop.SkipAosLog(); |
| 319 | |
| 320 | watcher_loop |
| 321 | .AddTimer([&watcher_loop] { |
| 322 | watcher_loop.Exit(); |
| 323 | FAIL(); |
| 324 | }) |
| 325 | ->Setup(watcher_loop.monotonic_now() + std::chrono::seconds(7)); |
| 326 | |
James Kuszmaul | 6b35e3a | 2022-04-06 15:00:39 -0700 | [diff] [blame] | 327 | int pong_running_count = 0; |
| 328 | watcher_loop.MakeWatcher("/aos", [&watcher_loop, &pong_running_count]( |
James Kuszmaul | 6295a64 | 2022-03-22 15:23:59 -0700 | [diff] [blame] | 329 | const aos::starter::Status &status) { |
| 330 | const aos::starter::ApplicationStatus *ping_app_status = |
| 331 | FindApplicationStatus(status, "ping"); |
| 332 | const aos::starter::ApplicationStatus *pong_app_status = |
| 333 | FindApplicationStatus(status, "pong"); |
| 334 | if (ping_app_status == nullptr || pong_app_status == nullptr) { |
| 335 | return; |
| 336 | } |
Austin Schuh | 5f79a5a | 2021-10-12 17:46:50 -0700 | [diff] [blame] | 337 | |
James Kuszmaul | 6295a64 | 2022-03-22 15:23:59 -0700 | [diff] [blame] | 338 | if (ping_app_status->has_state() && |
| 339 | ping_app_status->state() != aos::starter::State::STOPPED) { |
| 340 | watcher_loop.Exit(); |
| 341 | FAIL(); |
| 342 | } |
| 343 | if (pong_app_status->has_state() && |
| 344 | pong_app_status->state() == aos::starter::State::RUNNING) { |
James Kuszmaul | 6b35e3a | 2022-04-06 15:00:39 -0700 | [diff] [blame] | 345 | ++pong_running_count; |
| 346 | // Sometimes if the timing for everything is *just* off, then the |
| 347 | // process_info will say that the process name is "starter_test" because |
| 348 | // it grabbed the name after the fork() but before the execvp(). To |
| 349 | // protect against that, wait an extra cycle. If things aren't fixed by |
| 350 | // the second cycle, then that is a problem. |
| 351 | if (pong_running_count < 2) { |
| 352 | return; |
| 353 | } |
James Kuszmaul | 6295a64 | 2022-03-22 15:23:59 -0700 | [diff] [blame] | 354 | ASSERT_TRUE(pong_app_status->has_process_info()); |
Austin Schuh | 6665e92 | 2022-04-01 10:06:25 -0700 | [diff] [blame] | 355 | ASSERT_EQ("pong", pong_app_status->process_info()->name()->string_view()) |
| 356 | << aos::FlatbufferToJson(&status); |
James Kuszmaul | 6295a64 | 2022-03-22 15:23:59 -0700 | [diff] [blame] | 357 | ASSERT_EQ(pong_app_status->pid(), pong_app_status->process_info()->pid()); |
James Kuszmaul | 6b35e3a | 2022-04-06 15:00:39 -0700 | [diff] [blame] | 358 | ASSERT_TRUE(pong_app_status->process_info()->has_cpu_usage()); |
| 359 | ASSERT_LE(0.0, pong_app_status->process_info()->cpu_usage()); |
James Kuszmaul | 6295a64 | 2022-03-22 15:23:59 -0700 | [diff] [blame] | 360 | watcher_loop.Exit(); |
| 361 | SUCCEED(); |
| 362 | } |
| 363 | }); |
Austin Schuh | 5f79a5a | 2021-10-12 17:46:50 -0700 | [diff] [blame] | 364 | |
James Kuszmaul | 6b35e3a | 2022-04-06 15:00:39 -0700 | [diff] [blame] | 365 | SetupStarterCleanup(&starter); |
| 366 | |
Austin Schuh | 59398d3 | 2023-05-03 08:10:55 -0700 | [diff] [blame] | 367 | Event starter_started; |
| 368 | std::thread starterd_thread([&starter, &starter_started] { |
| 369 | starter.event_loop()->OnRun( |
| 370 | [&starter_started]() { starter_started.Set(); }); |
| 371 | starter.Run(); |
| 372 | }); |
| 373 | starter_started.Wait(); |
Austin Schuh | 5f79a5a | 2021-10-12 17:46:50 -0700 | [diff] [blame] | 374 | watcher_loop.Run(); |
| 375 | |
James Kuszmaul | 6b35e3a | 2022-04-06 15:00:39 -0700 | [diff] [blame] | 376 | test_done_ = true; |
| 377 | |
Austin Schuh | 5f79a5a | 2021-10-12 17:46:50 -0700 | [diff] [blame] | 378 | starterd_thread.join(); |
| 379 | } |
| 380 | |
James Kuszmaul | e7c7e58 | 2022-01-07 18:50:01 -0800 | [diff] [blame] | 381 | // Tests that starterd respects autorestart. |
| 382 | TEST_F(StarterdTest, DeathNoRestartTest) { |
| 383 | const std::string config_file = |
| 384 | ArtifactPath("aos/events/pingpong_config.json"); |
| 385 | |
| 386 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 387 | aos::configuration::ReadConfig(config_file); |
| 388 | |
James Kuszmaul | e7c7e58 | 2022-01-07 18:50:01 -0800 | [diff] [blame] | 389 | auto new_config = aos::configuration::MergeWithConfig( |
| 390 | &config.message(), absl::StrFormat( |
| 391 | R"({"applications": [ |
| 392 | { |
| 393 | "name": "ping", |
| 394 | "executable_name": "%s", |
Austin Schuh | 59398d3 | 2023-05-03 08:10:55 -0700 | [diff] [blame] | 395 | "args": ["--shm_base", "%s"], |
James Kuszmaul | e7c7e58 | 2022-01-07 18:50:01 -0800 | [diff] [blame] | 396 | "autorestart": false |
| 397 | }, |
| 398 | { |
| 399 | "name": "pong", |
| 400 | "executable_name": "%s", |
Austin Schuh | 59398d3 | 2023-05-03 08:10:55 -0700 | [diff] [blame] | 401 | "args": ["--shm_base", "%s"] |
James Kuszmaul | e7c7e58 | 2022-01-07 18:50:01 -0800 | [diff] [blame] | 402 | } |
| 403 | ]})", |
Austin Schuh | 59398d3 | 2023-05-03 08:10:55 -0700 | [diff] [blame] | 404 | ArtifactPath("aos/events/ping"), FLAGS_shm_base, |
| 405 | ArtifactPath("aos/events/pong"), FLAGS_shm_base)); |
James Kuszmaul | e7c7e58 | 2022-01-07 18:50:01 -0800 | [diff] [blame] | 406 | |
| 407 | const aos::Configuration *config_msg = &new_config.message(); |
| 408 | |
| 409 | // Set up starter with config file |
| 410 | aos::starter::Starter starter(config_msg); |
| 411 | |
| 412 | // Create an event loop to watch for the Status message to watch the state |
| 413 | // transitions. |
| 414 | aos::ShmEventLoop watcher_loop(config_msg); |
| 415 | watcher_loop.SkipAosLog(); |
| 416 | |
| 417 | watcher_loop |
| 418 | .AddTimer([&watcher_loop] { |
| 419 | watcher_loop.Exit(); |
| 420 | SUCCEED(); |
| 421 | }) |
| 422 | ->Setup(watcher_loop.monotonic_now() + std::chrono::seconds(11)); |
| 423 | |
| 424 | int test_stage = 0; |
| 425 | uint64_t id; |
| 426 | |
| 427 | watcher_loop.MakeWatcher("/aos", [&test_stage, &watcher_loop, |
| 428 | &id](const aos::starter::Status &status) { |
| 429 | const aos::starter::ApplicationStatus *app_status = |
| 430 | FindApplicationStatus(status, "ping"); |
| 431 | if (app_status == nullptr) { |
| 432 | return; |
| 433 | } |
| 434 | |
| 435 | switch (test_stage) { |
| 436 | case 0: { |
| 437 | if (app_status->has_state() && |
| 438 | app_status->state() == aos::starter::State::RUNNING) { |
| 439 | LOG(INFO) << "Ping is running"; |
| 440 | test_stage = 1; |
| 441 | ASSERT_TRUE(app_status->has_pid()); |
| 442 | ASSERT_TRUE(kill(app_status->pid(), SIGINT) != -1); |
| 443 | ASSERT_TRUE(app_status->has_id()); |
| 444 | id = app_status->id(); |
| 445 | } |
| 446 | break; |
| 447 | } |
| 448 | |
| 449 | case 1: { |
| 450 | if (app_status->has_state() && |
| 451 | app_status->state() == aos::starter::State::RUNNING && |
| 452 | app_status->has_id() && app_status->id() != id) { |
| 453 | LOG(INFO) << "Ping restarted, it shouldn't..."; |
| 454 | watcher_loop.Exit(); |
| 455 | FAIL(); |
| 456 | } |
| 457 | break; |
| 458 | } |
| 459 | } |
| 460 | }); |
| 461 | |
James Kuszmaul | 6b35e3a | 2022-04-06 15:00:39 -0700 | [diff] [blame] | 462 | SetupStarterCleanup(&starter); |
| 463 | |
Austin Schuh | 59398d3 | 2023-05-03 08:10:55 -0700 | [diff] [blame] | 464 | Event starter_started; |
| 465 | std::thread starterd_thread([&starter, &starter_started] { |
| 466 | starter.event_loop()->OnRun( |
| 467 | [&starter_started]() { starter_started.Set(); }); |
| 468 | starter.Run(); |
| 469 | }); |
| 470 | starter_started.Wait(); |
James Kuszmaul | e7c7e58 | 2022-01-07 18:50:01 -0800 | [diff] [blame] | 471 | watcher_loop.Run(); |
| 472 | |
James Kuszmaul | 6b35e3a | 2022-04-06 15:00:39 -0700 | [diff] [blame] | 473 | test_done_ = true; |
| 474 | |
James Kuszmaul | e7c7e58 | 2022-01-07 18:50:01 -0800 | [diff] [blame] | 475 | starterd_thread.join(); |
| 476 | } |
| 477 | |
Austin Schuh | 59398d3 | 2023-05-03 08:10:55 -0700 | [diff] [blame] | 478 | TEST_F(StarterdTest, StarterChainTest) { |
| 479 | // This test was written in response to a bug that was found |
| 480 | // in StarterClient::Succeed. The bug caused the timeout handler |
| 481 | // to be reset after the success handler was called. |
| 482 | // the bug has been fixed, and this test will ensure it does |
| 483 | // not regress. |
| 484 | const std::string config_file = |
| 485 | ArtifactPath("aos/events/pingpong_config.json"); |
| 486 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 487 | aos::configuration::ReadConfig(config_file); |
| 488 | auto new_config = aos::configuration::MergeWithConfig( |
| 489 | &config.message(), absl::StrFormat( |
| 490 | R"({"applications": [ |
| 491 | { |
| 492 | "name": "ping", |
| 493 | "executable_name": "%s", |
| 494 | "args": ["--shm_base", "%s"], |
| 495 | "autorestart": false |
| 496 | }, |
| 497 | { |
| 498 | "name": "pong", |
| 499 | "executable_name": "%s", |
| 500 | "args": ["--shm_base", "%s"] |
| 501 | } |
| 502 | ]})", |
| 503 | ArtifactPath("aos/events/ping"), FLAGS_shm_base, |
| 504 | ArtifactPath("aos/events/pong"), FLAGS_shm_base)); |
| 505 | |
| 506 | const aos::Configuration *config_msg = &new_config.message(); |
| 507 | // Set up starter with config file |
| 508 | aos::starter::Starter starter(config_msg); |
| 509 | aos::ShmEventLoop client_loop(config_msg); |
| 510 | client_loop.SkipAosLog(); |
| 511 | StarterClient client(&client_loop); |
| 512 | bool success = false; |
| 513 | auto client_node = client_loop.node(); |
| 514 | |
| 515 | // limit the amount of time we will wait for the test to finish. |
| 516 | client_loop |
| 517 | .AddTimer([&client_loop] { |
| 518 | client_loop.Exit(); |
| 519 | FAIL() << "ERROR: The test has failed, the watcher has timed out. " |
| 520 | "The chain of stages defined below did not complete " |
| 521 | "within the time limit."; |
| 522 | }) |
| 523 | ->Setup(client_loop.monotonic_now() + std::chrono::seconds(20)); |
| 524 | |
| 525 | // variables have been defined, here we define the body of the test. |
| 526 | // We want stage1 to succeed, triggering stage2. |
| 527 | // We want stage2 to timeout, triggering stage3. |
| 528 | |
| 529 | auto stage3 = [&client_loop, &success]() { |
| 530 | LOG(INFO) << "Begin stage3."; |
| 531 | SUCCEED(); |
| 532 | success = true; |
| 533 | client_loop.Exit(); |
| 534 | LOG(INFO) << "End stage3."; |
| 535 | }; |
| 536 | auto stage2 = [this, &starter, &client, &client_node, &stage3] { |
| 537 | LOG(INFO) << "Begin stage2"; |
| 538 | test_done_ = true; // trigger `starter` to exit. |
| 539 | |
| 540 | // wait for the starter event loop to close, so we can |
| 541 | // intentionally trigger a timeout. |
| 542 | int attempts = 0; |
| 543 | while (starter.event_loop()->is_running()) { |
| 544 | ++attempts; |
| 545 | if (attempts > 5) { |
| 546 | LOG(INFO) << "Timeout while waiting for starter to exit"; |
| 547 | return; |
| 548 | } |
| 549 | LOG(INFO) << "Waiting for starter to close."; |
| 550 | std::this_thread::sleep_for(std::chrono::seconds(1)); |
| 551 | } |
| 552 | client.SetTimeoutHandler(stage3); |
| 553 | client.SetSuccessHandler([]() { |
| 554 | LOG(INFO) << "stage3 success handler called."; |
| 555 | FAIL() << ": Command should not have succeeded here."; |
| 556 | }); |
| 557 | // we want this command to timeout |
| 558 | client.SendCommands({{Command::START, "ping", {client_node}}}, |
| 559 | std::chrono::seconds(5)); |
| 560 | LOG(INFO) << "End stage2"; |
| 561 | }; |
| 562 | auto stage1 = [&client, &client_node, &stage2] { |
| 563 | LOG(INFO) << "Begin stage1"; |
| 564 | client.SetTimeoutHandler( |
| 565 | []() { FAIL() << ": Command should not have timed out."; }); |
| 566 | client.SetSuccessHandler(stage2); |
| 567 | client.SendCommands({{Command::STOP, "ping", {client_node}}}, |
| 568 | std::chrono::seconds(5)); |
| 569 | LOG(INFO) << "End stage1"; |
| 570 | }; |
| 571 | // start the test body |
| 572 | client_loop.AddTimer(stage1)->Setup(client_loop.monotonic_now() + |
| 573 | std::chrono::milliseconds(1)); |
| 574 | |
| 575 | // prepare the cleanup for starter. This will finish when we call |
| 576 | // `test_done_ = true;`. |
| 577 | SetupStarterCleanup(&starter); |
| 578 | |
| 579 | // run `starter.Run()` in a thread to simulate it running on |
| 580 | // another process. |
| 581 | Event started; |
| 582 | std::thread starterd_thread([&starter, &started] { |
| 583 | starter.event_loop()->OnRun([&started]() { started.Set(); }); |
| 584 | starter.Run(); |
| 585 | }); |
| 586 | |
| 587 | started.Wait(); |
| 588 | client_loop.Run(); |
| 589 | EXPECT_TRUE(success); |
| 590 | ASSERT_FALSE(starter.event_loop()->is_running()); |
| 591 | starterd_thread.join(); |
| 592 | } |
| 593 | |
Austin Schuh | 5f79a5a | 2021-10-12 17:46:50 -0700 | [diff] [blame] | 594 | } // namespace starter |
| 595 | } // namespace aos |