| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include "IEventBus.h" | ||
| 4 | #include <queue> | ||
| 5 | #include <map> | ||
| 6 | #include <vector> | ||
| 7 | |||
| 8 | /** | ||
| 9 | * @brief Mock event bus for synchronous testing | ||
| 10 | * | ||
| 11 | * Provides deterministic event processing for unit tests. | ||
| 12 | */ | ||
| 13 | class MockEventBus : public IEventBus { | ||
| 14 | public: | ||
| 15 |
1/2✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
|
10 | MockEventBus() = default; |
| 16 | |||
| 17 | 30 | void subscribe(EventType type, std::function<void(const Event&)> handler) override { | |
| 18 | 30 | m_subscribers[type].push_back(std::move(handler)); | |
| 19 | 30 | } | |
| 20 | |||
| 21 | 30 | void publish(const Event& event) override { | |
| 22 | 30 | m_queue.push(event); | |
| 23 | 30 | m_history.push_back(event); | |
| 24 | 30 | } | |
| 25 | |||
| 26 | 15 | void processAllPending() override { | |
| 27 |
2/2✓ Branch 1 taken 28 times.
✓ Branch 2 taken 15 times.
|
43 | while (!m_queue.empty()) { |
| 28 | 28 | Event event = m_queue.front(); | |
| 29 | 28 | m_queue.pop(); | |
| 30 |
1/2✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
|
28 | dispatchEvent(event); |
| 31 | } | ||
| 32 | 15 | } | |
| 33 | |||
| 34 | ✗ | [[nodiscard]] bool waitForEvent(Event& outEvent, uint32_t timeoutMs) override { | |
| 35 | (void) timeoutMs; | ||
| 36 | ✗ | if (m_queue.empty()) { | |
| 37 | ✗ | return false; | |
| 38 | } | ||
| 39 | |||
| 40 | ✗ | outEvent = m_queue.front(); | |
| 41 | ✗ | m_queue.pop(); | |
| 42 | ✗ | dispatchEvent(outEvent); | |
| 43 | ✗ | return true; | |
| 44 | } | ||
| 45 | |||
| 46 | // Test helpers | ||
| 47 | [[nodiscard]] size_t getPendingEventCount() const { | ||
| 48 | return m_queue.size(); | ||
| 49 | } | ||
| 50 | |||
| 51 | void clear() { | ||
| 52 | while (!m_queue.empty()) { | ||
| 53 | m_queue.pop(); | ||
| 54 | } | ||
| 55 | m_history.clear(); | ||
| 56 | } | ||
| 57 | |||
| 58 | // Inspect published events history | ||
| 59 | 1 | const std::vector<Event>& history() const { return m_history; } | |
| 60 | size_t historyCount() const { return m_history.size(); } | ||
| 61 | |||
| 62 | private: | ||
| 63 | 28 | void dispatchEvent(const Event& event) { | |
| 64 |
1/2✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
|
28 | auto it = m_subscribers.find(event.type); |
| 65 |
2/2✓ Branch 2 taken 11 times.
✓ Branch 3 taken 17 times.
|
28 | if (it != m_subscribers.end()) { |
| 66 |
2/2✓ Branch 6 taken 11 times.
✓ Branch 7 taken 11 times.
|
22 | for (const auto& handler : it->second) { |
| 67 |
1/2✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
|
11 | if (handler) { |
| 68 |
1/2✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
|
11 | handler(event); |
| 69 | } | ||
| 70 | } | ||
| 71 | } | ||
| 72 | 28 | } | |
| 73 | |||
| 74 | std::queue<Event> m_queue; | ||
| 75 | std::map<EventType, std::vector<std::function<void(const Event&)>>> m_subscribers; | ||
| 76 | std::vector<Event> m_history; | ||
| 77 | }; | ||
| 78 |