138 lines
3.4 KiB
C++
138 lines
3.4 KiB
C++
#include <Void/Testing/CompletionObserver.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
#include <chrono>
|
|
#include <future>
|
|
#include <memory>
|
|
#include <mutex>
|
|
#include <stdexcept>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
namespace
|
|
{
|
|
|
|
using namespace std::chrono_literals;
|
|
|
|
[[nodiscard]] std::vector<char> completion_order(char first, char second)
|
|
{
|
|
Void::Testing::CompletionGate gate;
|
|
std::mutex mutex;
|
|
std::vector<char> order;
|
|
|
|
const auto task = [&](char id) {
|
|
return Void::Testing::observe_completion(
|
|
[id] { return id; },
|
|
[&, id] {
|
|
{
|
|
std::lock_guard lock{mutex};
|
|
order.push_back(id);
|
|
}
|
|
gate.arrive();
|
|
(void)gate.wait_release(5s);
|
|
});
|
|
};
|
|
|
|
auto a = task(first);
|
|
auto b = task(second);
|
|
auto first_future = std::async(std::launch::async, std::move(a));
|
|
if (!gate.wait_for(1, 5s)) {
|
|
return {};
|
|
}
|
|
auto second_future = std::async(std::launch::async, std::move(b));
|
|
const bool both = gate.wait_for(2, 5s);
|
|
gate.release();
|
|
Void::Testing::expect_eq(first_future.get(), first);
|
|
Void::Testing::expect_eq(second_future.get(), second);
|
|
if (!both) {
|
|
return {};
|
|
}
|
|
|
|
std::lock_guard lock{mutex};
|
|
return order;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
Void::Testing::Test completion_observer_runs_before_future_ready()
|
|
{
|
|
Void::Testing::CompletionGate gate;
|
|
std::promise<void> observed;
|
|
auto wrapped = Void::Testing::observe_completion(
|
|
[] { return 42; },
|
|
[&] {
|
|
observed.set_value();
|
|
(void)gate.wait_release(5s);
|
|
});
|
|
|
|
auto future = std::async(std::launch::async, std::move(wrapped));
|
|
observed.get_future().wait();
|
|
// The observer already ran, but the callable has not returned yet.
|
|
Void::Testing::expect(future.wait_for(0ms) ==
|
|
std::future_status::timeout);
|
|
gate.release();
|
|
Void::Testing::expect_eq(future.get(), 42);
|
|
}
|
|
|
|
Void::Testing::Test completion_observer_preserves_result_categories()
|
|
{
|
|
int observed = 0;
|
|
|
|
auto void_callable =
|
|
Void::Testing::observe_completion([] {}, [&] { ++observed; });
|
|
void_callable();
|
|
Void::Testing::expect_eq(observed, 1);
|
|
|
|
auto move_only = Void::Testing::observe_completion(
|
|
[] { return std::make_unique<int>(7); },
|
|
[&] { ++observed; });
|
|
auto value = move_only();
|
|
Void::Testing::require(value != nullptr);
|
|
Void::Testing::expect_eq(*value, 7);
|
|
Void::Testing::expect_eq(observed, 2);
|
|
|
|
int storage = 5;
|
|
auto reference = Void::Testing::observe_completion(
|
|
[&]() -> int& { return storage; }, [] {});
|
|
int& ref = reference();
|
|
ref = 9;
|
|
Void::Testing::expect_eq(storage, 9);
|
|
}
|
|
|
|
Void::Testing::Test completion_observer_forwards_exceptions()
|
|
{
|
|
int observed = 0;
|
|
auto throwing = Void::Testing::observe_completion(
|
|
[]() -> int { throw std::runtime_error{"boom"}; },
|
|
[&] { ++observed; });
|
|
|
|
bool caught = false;
|
|
try {
|
|
(void)throwing();
|
|
} catch (const std::runtime_error&) {
|
|
caught = true;
|
|
}
|
|
Void::Testing::expect(caught);
|
|
Void::Testing::expect_eq(observed, 0);
|
|
}
|
|
|
|
Void::Testing::Test completion_gate_release_and_timeout()
|
|
{
|
|
Void::Testing::CompletionGate gate;
|
|
Void::Testing::expect(!gate.wait_release(5ms));
|
|
gate.arrive();
|
|
Void::Testing::expect(gate.wait_for(1, 5ms));
|
|
Void::Testing::expect(!gate.wait_for(2, 5ms));
|
|
gate.release();
|
|
Void::Testing::expect(gate.wait_release(5ms));
|
|
}
|
|
|
|
Void::Testing::Test completion_order_is_deterministic()
|
|
{
|
|
Void::Testing::expect(completion_order('A', 'B') ==
|
|
std::vector<char>{'A', 'B'});
|
|
Void::Testing::expect(completion_order('B', 'A') ==
|
|
std::vector<char>{'B', 'A'});
|
|
}
|