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