Files
Library/tests/standalone/Async/ThreadPoolBackpressure.cpp
T
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

71 lines
2.0 KiB
C++

#include <Void/Async/ThreadPool.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Suite.hpp>
#include <atomic>
#include <thread>
namespace
{
using Void::Concurrency::ThreadPool;
using Void::Testing::Test;
struct ThreadPoolBackpressure
: Void::Testing::Suite<ThreadPoolBackpressure> {
// Unbounded pool (the default) accepts everything and never
// rejects.
Test unbounded_default_accepts_all()
{
ThreadPool pool{2};
std::atomic<int> sum{0};
for (int i = 0; i < 100; ++i) {
auto f = pool.try_submit([&sum] {
sum.fetch_add(1, std::memory_order_relaxed);
});
Void::Testing::expect(f.has_value());
f->wait();
}
const auto s = pool.stats();
Void::Testing::expect_eq(s.submitted, std::uint64_t{100});
Void::Testing::expect_eq(s.completed, std::uint64_t{100});
Void::Testing::expect_eq(s.rejected, std::uint64_t{0});
Void::Testing::expect_eq(sum.load(), 100);
}
// A bounded pool rejects try_submit once the queue is full.
Test bounded_pool_rejects_when_full()
{
ThreadPool pool{1, /*max_queue=*/2};
std::atomic<bool> started{false};
std::atomic<bool> release{false};
// Occupy the single worker so the queue can fill up.
auto blocker = pool.submit([&] {
started.store(true, std::memory_order_release);
while (!release.load(std::memory_order_acquire)) {
std::this_thread::yield();
}
});
while (!started.load(std::memory_order_acquire)) {
std::this_thread::yield();
}
auto a = pool.try_submit([] {}); // queue size 1
auto b = pool.try_submit([] {}); // queue size 2 (== max)
auto c = pool.try_submit([] {}); // full -> rejected
Void::Testing::expect(a.has_value());
Void::Testing::expect(b.has_value());
Void::Testing::expect(!c.has_value());
release.store(true, std::memory_order_release);
blocker.wait();
a->wait();
b->wait();
const auto s = pool.stats();
Void::Testing::expect(s.rejected >= 1);
Void::Testing::expect_eq(s.submitted, std::uint64_t{3});
}
};
} // namespace