Stephan Pleines | f581a07 | 2024-05-23 20:59:27 -0700 | [diff] [blame] | 1 | #include <chrono> |
James Kuszmaul | 9a816a7 | 2023-03-23 15:10:34 -0700 | [diff] [blame] | 2 | #include <map> |
Stephan Pleines | f581a07 | 2024-05-23 20:59:27 -0700 | [diff] [blame] | 3 | #include <memory> |
| 4 | #include <string> |
| 5 | #include <vector> |
James Kuszmaul | 9a816a7 | 2023-03-23 15:10:34 -0700 | [diff] [blame] | 6 | |
Stephan Pleines | f581a07 | 2024-05-23 20:59:27 -0700 | [diff] [blame] | 7 | #include "aos/configuration_generated.h" |
James Kuszmaul | 9a816a7 | 2023-03-23 15:10:34 -0700 | [diff] [blame] | 8 | #include "aos/events/event_loop.h" |
| 9 | #include "aos/events/simulated_event_loop.h" |
| 10 | #include "aos/starter/starter_generated.h" |
Stephan Pleines | f581a07 | 2024-05-23 20:59:27 -0700 | [diff] [blame] | 11 | #include "aos/time/time.h" |
James Kuszmaul | 9a816a7 | 2023-03-23 15:10:34 -0700 | [diff] [blame] | 12 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 13 | namespace aos::starter { |
James Kuszmaul | 9a816a7 | 2023-03-23 15:10:34 -0700 | [diff] [blame] | 14 | |
| 15 | // Simple mock of starterd that updates the starter status message to act as |
| 16 | // though applications are started and stopped when requested. |
| 17 | // TODO(james.kuszmaul): Consider integrating with SimulatedEventLoopFactory. |
| 18 | class MockStarter { |
| 19 | public: |
| 20 | struct ApplicationStatus { |
| 21 | int id; |
| 22 | bool running; |
| 23 | aos::monotonic_clock::time_point start_time; |
| 24 | }; |
| 25 | |
| 26 | MockStarter(aos::EventLoop *event_loop); |
| 27 | |
| 28 | const aos::Node *node() const { return event_loop_->node(); } |
| 29 | |
| 30 | const std::map<std::string, ApplicationStatus> &statuses() const { |
| 31 | return statuses_; |
| 32 | } |
| 33 | |
| 34 | private: |
| 35 | void SendStatus(); |
| 36 | |
| 37 | aos::EventLoop *event_loop_; |
| 38 | aos::Sender<aos::starter::Status> status_sender_; |
| 39 | std::map<std::string, ApplicationStatus> statuses_; |
| 40 | int next_id_ = 0; |
| 41 | }; |
| 42 | |
| 43 | // Spins up MockStarter's for each node. |
| 44 | class MockStarters { |
| 45 | public: |
| 46 | MockStarters(aos::SimulatedEventLoopFactory *event_loop_factory); |
| 47 | const std::vector<std::unique_ptr<MockStarter>> &starters() const { |
| 48 | return mock_starters_; |
| 49 | } |
| 50 | |
| 51 | private: |
| 52 | std::vector<std::unique_ptr<aos::EventLoop>> event_loops_; |
| 53 | std::vector<std::unique_ptr<MockStarter>> mock_starters_; |
| 54 | }; |
| 55 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 56 | } // namespace aos::starter |