|
| 1 | +#include <stdexcept> |
| 2 | + |
| 3 | +#include <consolix/core.hpp> |
| 4 | +#include <consolix/components.hpp> |
| 5 | + |
| 6 | +#if CONSOLIX_USE_EVENT_HUB != 1 |
| 7 | +#error "test_module_hub_component requires CONSOLIX_USE_EVENT_HUB=1" |
| 8 | +#endif |
| 9 | + |
| 10 | +class TestModule final : public event_hub::Module { |
| 11 | +public: |
| 12 | + explicit TestModule(event_hub::EventBus& bus, int& initialized, int& processed, int& shutdown) : |
| 13 | + event_hub::Module(bus), |
| 14 | + m_initialized(initialized), |
| 15 | + m_processed(processed), |
| 16 | + m_shutdown(shutdown) { |
| 17 | + } |
| 18 | + |
| 19 | +protected: |
| 20 | + void on_initialize() override { |
| 21 | + ++m_initialized; |
| 22 | + tasks().post([this] { |
| 23 | + ++m_processed; |
| 24 | + }); |
| 25 | + } |
| 26 | + |
| 27 | + std::size_t on_process() override { |
| 28 | + ++m_processed; |
| 29 | + return 1; |
| 30 | + } |
| 31 | + |
| 32 | + void on_shutdown() noexcept override { |
| 33 | + ++m_shutdown; |
| 34 | + } |
| 35 | + |
| 36 | +private: |
| 37 | + int& m_initialized; |
| 38 | + int& m_processed; |
| 39 | + int& m_shutdown; |
| 40 | +}; |
| 41 | + |
| 42 | +int main() { |
| 43 | + int initialized = 0; |
| 44 | + int processed = 0; |
| 45 | + int shutdown = 0; |
| 46 | + |
| 47 | + event_hub::ModuleHub hub; |
| 48 | + hub.emplace_module<TestModule>(initialized, processed, shutdown); |
| 49 | + |
| 50 | + consolix::AppComponentManager manager; |
| 51 | + auto component = manager.add<consolix::ModuleHubComponent>(&hub); |
| 52 | + |
| 53 | + if (!manager.initialize()) { |
| 54 | + throw std::runtime_error("ModuleHubComponent failed to initialize"); |
| 55 | + } |
| 56 | + if (initialized != 1) { |
| 57 | + throw std::runtime_error("ModuleHubComponent did not initialize ModuleHub"); |
| 58 | + } |
| 59 | + |
| 60 | + manager.process(); |
| 61 | + |
| 62 | + if (processed < 2) { |
| 63 | + throw std::runtime_error("ModuleHubComponent did not process ModuleHub work"); |
| 64 | + } |
| 65 | + if (component->last_work_count() == 0) { |
| 66 | + throw std::runtime_error("ModuleHubComponent did not report processed work"); |
| 67 | + } |
| 68 | + |
| 69 | + manager.shutdown(0); |
| 70 | + |
| 71 | + if (shutdown != 1) { |
| 72 | + throw std::runtime_error("ModuleHubComponent did not shutdown ModuleHub exactly once"); |
| 73 | + } |
| 74 | + |
| 75 | + return 0; |
| 76 | +} |
0 commit comments