Files
Library/tests/standalone/Async/ObservableReentrancy.cpp

237 lines
7.2 KiB
C++

#include <Void/Async/Observable.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <cstdint>
#include <memory>
#include <stdexcept>
#include <utility>
#include <vector>
namespace
{
struct MoveOnlyVal {
int id{0};
MoveOnlyVal() = default;
explicit MoveOnlyVal(int v) noexcept : id{v} {}
MoveOnlyVal(const MoveOnlyVal&) = delete;
MoveOnlyVal& operator=(const MoveOnlyVal&) = delete;
MoveOnlyVal(MoveOnlyVal&&) noexcept = default;
MoveOnlyVal& operator=(MoveOnlyVal&&) noexcept = default;
[[nodiscard]] bool operator==(const MoveOnlyVal& other) const noexcept
{
return id == other.id;
}
};
} // namespace
Void::Testing::Test observable_nested_set_fifo()
{
using namespace Void::Observable;
Observable<int> obs{0};
std::vector<std::pair<int, int>> seen;
obs.on_change([&](const int& old_v, const int& new_v) {
seen.emplace_back(old_v, new_v);
if (new_v == 1) {
obs.set(2);
Void::Testing::expect_eq(obs.get(), 2);
obs.set(3);
Void::Testing::expect_eq(obs.get(), 3);
}
});
obs.set(1);
Void::Testing::expect_eq(obs.get(), 3);
Void::Testing::require(seen.size() == 3);
Void::Testing::expect_eq(seen[0].first, 0);
Void::Testing::expect_eq(seen[0].second, 1);
Void::Testing::expect_eq(seen[1].first, 1);
Void::Testing::expect_eq(seen[1].second, 2);
Void::Testing::expect_eq(seen[2].first, 2);
Void::Testing::expect_eq(seen[2].second, 3);
}
Void::Testing::Test observable_unsubscribe_in_listener_skips_rest()
{
using namespace Void::Observable;
Observable<int> obs{0};
int second_hits = 0;
Handle second_h{};
obs.on_change([&](const int&, const int&) { obs.unsubscribe(second_h); });
second_h = obs.on_change([&](const int&, const int&) { ++second_hits; });
obs.set(1);
Void::Testing::expect_eq(second_hits, 0);
obs.set(2);
Void::Testing::expect_eq(second_hits, 0);
}
Void::Testing::Test observable_subscribe_in_listener_not_current_pass()
{
using namespace Void::Observable;
Observable<int> obs{0};
int late_hits = 0;
bool added = false;
obs.on_change([&](const int&, const int&) {
if (!added) {
added = true;
obs.on_change([&](const int&, const int&) { ++late_hits; });
}
});
obs.set(1);
Void::Testing::expect_eq(late_hits, 0);
obs.set(2);
Void::Testing::expect_eq(late_hits, 1);
}
Void::Testing::Test observable_try_set_overloaded_when_pending_full()
{
using namespace Void::Observable;
Observable<int> obs{0};
Void::Result::Result<void> overflow_res{};
bool overflow_checked = false;
obs.on_change([&](const int&, const int& new_v) {
if (new_v == 1) {
for (std::size_t i = 0; i < kMaxPendingNotifications; ++i) {
auto r = obs.try_set(static_cast<int>(i + 2));
if (!r.has_value()) {
overflow_checked = false;
return;
}
}
const int val_before_overload = obs.get();
overflow_res = obs.try_set(999999);
Void::Testing::expect_eq(obs.get(), val_before_overload);
// Overflow via set() updates value and increments dropped_notifications.
obs.set(888888);
Void::Testing::expect_eq(obs.get(), 888888);
overflow_checked = true;
}
});
obs.set(1);
Void::Testing::expect(overflow_checked);
Void::Testing::require(!overflow_res.has_value());
Void::Testing::expect_eq(
overflow_res.error().code().value,
static_cast<std::int32_t>(Void::Async::AsyncErrorCode::overloaded));
Void::Testing::expect_eq(obs.get(), 888888);
Void::Testing::expect_eq(obs.dropped_notifications(), std::size_t{1});
}
Void::Testing::Test observable_listener_throw_restores_state()
{
using namespace Void::Observable;
Observable<int> obs{0};
int normal_hits = 0;
obs.on_change([&](const int&, const int& new_v) {
if (new_v == 1) {
obs.set(2);
obs.set(3);
throw std::runtime_error("listener_boom");
}
++normal_hits;
});
bool caught = false;
try {
obs.set(1);
} catch (const std::runtime_error&) {
caught = true;
}
Void::Testing::expect(caught);
Void::Testing::expect_eq(obs.get(), 3);
Void::Testing::expect_eq(obs.dropped_notifications(), std::size_t{2});
// Subsequent set operates cleanly with depth restored to 0.
obs.set(4);
Void::Testing::expect_eq(obs.get(), 4);
Void::Testing::expect_eq(normal_hits, 1);
}
Void::Testing::Test observable_self_unsubscribe_releases_capture()
{
using namespace Void::Observable;
Observable<int> obs{0};
auto sentinel = std::make_shared<int>(55);
Handle h{};
h = obs.on_change([&, sentinel](const int&, const int&) {
(void)sentinel;
Void::Testing::expect(obs.unsubscribe(h));
});
Void::Testing::expect_eq(sentinel.use_count(), 2L);
obs.set(1);
Void::Testing::expect_eq(sentinel.use_count(), 1L);
Void::Testing::expect_eq(obs.listener_count(), std::size_t{0});
}
Void::Testing::Test observable_move_only_immediate_notification_compat()
{
using namespace Void::Observable;
Observable<MoveOnlyVal> obs{MoveOnlyVal{0}};
std::vector<std::pair<int, int>> trace;
obs.on_change([&](const MoveOnlyVal& ov, const MoveOnlyVal& nv) {
trace.emplace_back(ov.id, nv.id);
if (nv.id == 1) {
obs.set(MoveOnlyVal{2});
}
});
obs.on_change([&](const MoveOnlyVal& ov, const MoveOnlyVal& nv) {
trace.emplace_back(ov.id * 10, nv.id * 10);
});
obs.set(MoveOnlyVal{1});
// Immediate recursive delivery matches 1.x notify(old, value_) aliasing.
Void::Testing::require(trace.size() == 4);
Void::Testing::expect_eq(trace[0].first, 0);
Void::Testing::expect_eq(trace[0].second, 1);
Void::Testing::expect_eq(trace[1].first, 1);
Void::Testing::expect_eq(trace[1].second, 2);
Void::Testing::expect_eq(trace[2].first, 10);
Void::Testing::expect_eq(trace[2].second, 20);
Void::Testing::expect_eq(trace[3].first, 0);
Void::Testing::expect_eq(trace[3].second, 20);
Void::Testing::expect_eq(obs.dropped_notifications(), std::size_t{0});
}
Void::Testing::Test signal_reentrant_connect_disconnect_during_emit()
{
using namespace Void::Observable;
Signal<int> sig;
std::vector<int> log;
Handle second_h{};
Handle third_h{};
sig.connect([&](const int& v) {
log.push_back(v);
if (v == 1) {
Void::Testing::expect(sig.disconnect(second_h));
third_h = sig.connect([&](const int& x) { log.push_back(x * 100); });
sig.emit(2);
}
});
second_h = sig.connect([&](const int& v) { log.push_back(v * 10); });
sig.emit(1);
// First emit skips disconnected second_h and skips newly added third_h;
// queued emit(2) runs first slot and third_h FIFO.
Void::Testing::require(log.size() == 3);
Void::Testing::expect_eq(log[0], 1);
Void::Testing::expect_eq(log[1], 2);
Void::Testing::expect_eq(log[2], 200);
Void::Testing::expect(third_h.id != 0);
}