Headers move, CMake targets merge, C++ namespaces stay unchanged: - Core: Scope, Lazy, Counted, Flags, Hash, Bits, Uuid, TypeList, TypeMap, Variant - Functional: Pipeline, Either, Result, Retry - Containers: Ring, Pool, PriorityQueue, LRU, Trie, Graph, DisjointSet, Bitset, Interval - Math: +Matrix, Numeric, Random, Histogram - Text: Str, Format, Tokenizer, Diff, Encoding, Semver - Data: Json, Csv, Ini - Async: Coro, Concurrency, EventBus, Observable - Time: +Bench - Log: Log facade + Logging framework (facade now header-only, breaking the Stacktrace<->Log link cycle the merge would have created) - System: FileSystem + Process - Configuration: +Argparse - Unchanged: Stacktrace, Plugin, StateMachine, Testing, TestingMock, Detail Module system fixes: - OPTIONAL_DEPS resolution deferred to void_finalize_modules() so it no longer depends on the alphabetical discovery order of src/ subdirs (Testing -> Text would have been silently dropped). - Every header-only module now has a tracked module.cmake; previously the empty src/<Name> marker dirs were untracked, so a fresh clone would have silently skipped those modules. - VOID_HAS_LOGGING/CONCURRENCY/STR consumers renamed to LOG/ASYNC/TEXT. Tests move to tests/standalone/<Umbrella>/<Feature>.cpp (Result suite files prefixed: ResultBasic etc.). Test count verified identical (831) before and after. README/CLAUDE.md module tables updated. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
143 lines
3.2 KiB
C++
143 lines
3.2 KiB
C++
#include <Void/Async/Channel.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
#include <atomic>
|
|
#include <string>
|
|
#include <thread>
|
|
#include <vector>
|
|
|
|
using Void::Concurrency::Channel;
|
|
|
|
Void::Testing::Test capacity_is_preserved()
|
|
{
|
|
Channel<int> ch{8};
|
|
Void::Testing::expect_eq(ch.capacity(), std::size_t{8});
|
|
}
|
|
|
|
Void::Testing::Test try_send_and_try_receive_roundtrip()
|
|
{
|
|
Channel<int> ch{4};
|
|
Void::Testing::expect(ch.try_send(10));
|
|
Void::Testing::expect(ch.try_send(20));
|
|
Void::Testing::expect_eq(ch.size(), std::size_t{2});
|
|
auto v1 = ch.try_receive();
|
|
Void::Testing::expect(v1.has_value());
|
|
Void::Testing::expect_eq(*v1, 10);
|
|
auto v2 = ch.try_receive();
|
|
Void::Testing::expect(v2.has_value());
|
|
Void::Testing::expect_eq(*v2, 20);
|
|
auto empty = ch.try_receive();
|
|
Void::Testing::expect(!empty.has_value());
|
|
}
|
|
|
|
Void::Testing::Test try_send_fails_when_full()
|
|
{
|
|
Channel<int> ch{2};
|
|
Void::Testing::expect(ch.try_send(1));
|
|
Void::Testing::expect(ch.try_send(2));
|
|
Void::Testing::expect(!ch.try_send(3));
|
|
}
|
|
|
|
Void::Testing::Test close_unblocks_receiver()
|
|
{
|
|
Channel<int> ch{4};
|
|
std::thread producer{[&] { ch.close(); }};
|
|
auto val = ch.receive();
|
|
producer.join();
|
|
Void::Testing::expect(!val.has_value());
|
|
Void::Testing::expect(ch.is_closed());
|
|
}
|
|
|
|
Void::Testing::Test producer_consumer_ordering()
|
|
{
|
|
Channel<int> ch{16};
|
|
constexpr int N = 10;
|
|
std::thread producer{[&] {
|
|
for (int i = 0; i < N; ++i) {
|
|
ch.send(i);
|
|
}
|
|
ch.close();
|
|
}};
|
|
std::vector<int> received;
|
|
while (auto v = ch.receive()) {
|
|
received.push_back(*v);
|
|
}
|
|
producer.join();
|
|
Void::Testing::expect_eq(static_cast<int>(received.size()), N);
|
|
for (int i = 0; i < N; ++i) {
|
|
Void::Testing::expect_eq(received[i], i);
|
|
}
|
|
}
|
|
|
|
Void::Testing::Test try_send_fails_after_close()
|
|
{
|
|
Channel<int> ch{4};
|
|
ch.close();
|
|
Void::Testing::expect(!ch.try_send(1));
|
|
}
|
|
|
|
Void::Testing::Test send_returns_false_when_closed()
|
|
{
|
|
Channel<int> ch{1};
|
|
ch.close();
|
|
bool ok = ch.send(42);
|
|
Void::Testing::expect(!ok);
|
|
}
|
|
|
|
Void::Testing::Test size_and_empty()
|
|
{
|
|
Channel<int> ch{4};
|
|
Void::Testing::expect(ch.empty());
|
|
Void::Testing::expect_eq(ch.size(), std::size_t{0});
|
|
ch.try_send(1);
|
|
ch.try_send(2);
|
|
Void::Testing::expect_eq(ch.size(), std::size_t{2});
|
|
Void::Testing::expect(!ch.empty());
|
|
}
|
|
|
|
Void::Testing::Test receive_drains_then_nullopt_after_close()
|
|
{
|
|
Channel<int> ch{4};
|
|
ch.try_send(10);
|
|
ch.try_send(20);
|
|
ch.close();
|
|
Void::Testing::expect_eq(*ch.try_receive(), 10);
|
|
Void::Testing::expect_eq(*ch.try_receive(), 20);
|
|
Void::Testing::expect(!ch.try_receive().has_value());
|
|
}
|
|
|
|
Void::Testing::Test multiple_producers_sum()
|
|
{
|
|
Channel<int> ch{64};
|
|
std::atomic<int> total{0};
|
|
|
|
auto producer = [&](int n) {
|
|
for (int i = 0; i < n; ++i) {
|
|
ch.send(i);
|
|
}
|
|
};
|
|
|
|
std::thread t1(producer, 5);
|
|
std::thread t2(producer, 5);
|
|
t1.join();
|
|
t2.join();
|
|
ch.close();
|
|
|
|
while (auto v = ch.receive()) {
|
|
total += *v;
|
|
}
|
|
|
|
// Sum of 0+1+2+3+4 = 10, two producers → 20
|
|
Void::Testing::expect_eq(total.load(), 20);
|
|
}
|
|
|
|
Void::Testing::Test string_values_roundtrip()
|
|
{
|
|
Channel<std::string> ch{4};
|
|
ch.try_send("hello");
|
|
ch.try_send("world");
|
|
Void::Testing::expect_eq(*ch.try_receive(), std::string{"hello"});
|
|
Void::Testing::expect_eq(*ch.try_receive(), std::string{"world"});
|
|
}
|