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