Files
Library/tests/standalone/Async/EventBusAsync.cpp
faumarayandClaude Opus 4.8 80293ee56f Add async/resilience/reflection primitives + FSM v2 rewrite
A round of new universal capabilities (all generic — templated/reflection
based, no app coupling), verified by the test suite (1103 passing):

Data reflection:
- Reflect.hpp now (de)serializes chrono::duration, chrono::time_point,
  std::variant (tagged), std::array, and non-string-keyed std::map;
  Meta.hpp gains is_time_point_v et al.

Async:
- EventBus gains Mode::Async (worker-thread dispatch + flush()).
- ThreadPool gains a bounded queue, try_submit(), and rejection stats.

New modules / containers:
- Resilience: TokenBucket, KeyedRateLimiter<Key>, CircuitBreaker,
  ResourceMonitor.
- Containers: BufferPool<T> (size-class buffer reuse) and Cache<K,V>
  (per-entry TTL, LRU/LFU/FIFO eviction, hit/miss stats).
- System: process/CPU/memory metrics (Metrics.hpp) and a streaming
  Process API.

Plus FSM v2 (typed Success/Failure, sub-machines, parallel regions,
history states, deferred events), Configuration/Logging reflection
helpers, and the accompanying test suites.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-22 15:45:16 +03:00

72 lines
1.9 KiB
C++

#include <Void/Async/EventBus.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Suite.hpp>
#include <atomic>
#include <thread>
namespace
{
using Void::EventBus::EventBus;
using Void::EventBus::Mode;
using Void::Testing::Test;
struct Tick {
int n = 0;
};
struct EventBusAsync : Void::Testing::Suite<EventBusAsync> {
// Async handlers run on the bus worker, not the publisher thread.
Test handler_runs_off_publisher_thread()
{
EventBus bus{Mode::Async};
std::atomic<std::thread::id> handler_thread{};
bus.subscribe<Tick>([&](const Tick&) {
handler_thread.store(std::this_thread::get_id());
});
bus.publish(Tick{1});
bus.flush();
Void::Testing::expect(handler_thread.load() !=
std::this_thread::get_id());
}
// flush() guarantees every published event has been delivered.
Test flush_delivers_all_events()
{
EventBus bus{Mode::Async};
std::atomic<int> total{0};
bus.subscribe<Tick>([&](const Tick& t) {
total.fetch_add(t.n, std::memory_order_relaxed);
});
for (int i = 1; i <= 50; ++i) {
bus.publish(Tick{i});
}
bus.flush();
Void::Testing::expect_eq(total.load(), 50 * 51 / 2);
}
// Unsubscribing before publish means the handler never fires.
Test unsubscribe_before_publish_not_called()
{
EventBus bus{Mode::Async};
std::atomic<int> count{0};
const auto id = bus.subscribe<Tick>(
[&](const Tick&) { count.fetch_add(1); });
bus.unsubscribe(id);
bus.publish(Tick{1});
bus.flush();
Void::Testing::expect_eq(count.load(), 0);
}
// Sync mode (the default) still delivers inline within publish().
Test sync_mode_delivers_inline()
{
EventBus bus; // Mode::Sync
int seen = 0;
bus.subscribe<Tick>([&](const Tick& t) { seen = t.n; });
bus.publish(Tick{7});
Void::Testing::expect_eq(seen, 7); // no flush needed
}
};
} // namespace