Merge branch 'issue/5-immutable-publication'
This commit is contained in:
commit
f8eca20ef5
2 files changed
+442
No files matched your search
@@ -0,0 +1,210 @@
|
||||
#include <Void/Core/Publication.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <atomic>
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
struct Payload {
|
||||
std::uint64_t revision{0};
|
||||
std::string text;
|
||||
};
|
||||
|
||||
struct Tracked {
|
||||
Tracked() { ++live; }
|
||||
~Tracked() { --live; }
|
||||
Tracked(const Tracked&) = delete;
|
||||
Tracked& operator=(const Tracked&) = delete;
|
||||
|
||||
static inline std::atomic<int> live{0};
|
||||
};
|
||||
|
||||
[[nodiscard]] std::shared_ptr<const Payload>
|
||||
payload(std::uint64_t revision, std::string text = {})
|
||||
{
|
||||
return std::make_shared<const Payload>(
|
||||
Payload{.revision = revision, .text = std::move(text)});
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
Void::Testing::Test publication_retains_previous_snapshot()
|
||||
{
|
||||
Void::Core::Publication<Payload> publication{
|
||||
payload(1, "world"), {.epoch = 1, .revision = 1}};
|
||||
|
||||
auto held = publication.acquire();
|
||||
Void::Testing::require(held.get() != nullptr);
|
||||
|
||||
auto result = publication.publish(
|
||||
payload(2, "new world"), {.epoch = 1, .revision = 2});
|
||||
Void::Testing::expect(result.published());
|
||||
|
||||
// The retained handle still sees the old, complete snapshot.
|
||||
Void::Testing::expect_eq(held->revision, std::uint64_t{1});
|
||||
Void::Testing::expect_eq(held->text, std::string{"world"});
|
||||
Void::Testing::expect_eq(held.version().revision, std::uint64_t{1});
|
||||
|
||||
auto current = publication.acquire();
|
||||
Void::Testing::expect_eq(current->revision, std::uint64_t{2});
|
||||
Void::Testing::expect_eq(current->text, std::string{"new world"});
|
||||
Void::Testing::expect_eq(current.version().revision, std::uint64_t{2});
|
||||
}
|
||||
|
||||
Void::Testing::Test publication_compare_and_publish_rejects_obsolete_epoch()
|
||||
{
|
||||
Void::Core::Publication<Payload> publication{
|
||||
payload(1, "epoch one"), {.epoch = 1, .revision = 5}};
|
||||
|
||||
Void::Testing::require(
|
||||
publication
|
||||
.publish(payload(2, "epoch two"), {.epoch = 2, .revision = 0})
|
||||
.published());
|
||||
|
||||
// A producer that validated against the old epoch must be rejected.
|
||||
auto stale = publication.publish(payload(3, "late"),
|
||||
{.epoch = 2, .revision = 1},
|
||||
/*expected_epoch=*/1);
|
||||
Void::Testing::expect(stale.status ==
|
||||
Void::Core::Publication<Payload>::PublishStatus::
|
||||
stale_epoch);
|
||||
Void::Testing::expect_eq(publication.version().epoch, std::uint64_t{2});
|
||||
Void::Testing::expect_eq(publication.acquire()->revision,
|
||||
std::uint64_t{2});
|
||||
|
||||
// A lower epoch is rejected too.
|
||||
auto older = publication.publish(payload(4, "older"),
|
||||
{.epoch = 1, .revision = 99});
|
||||
Void::Testing::expect(older.status ==
|
||||
Void::Core::Publication<Payload>::PublishStatus::
|
||||
stale_epoch);
|
||||
Void::Testing::expect_eq(publication.acquire()->revision,
|
||||
std::uint64_t{2});
|
||||
}
|
||||
|
||||
Void::Testing::Test publication_rejects_non_advancing_revision()
|
||||
{
|
||||
Void::Core::Publication<Payload> publication{
|
||||
payload(1), {.epoch = 1, .revision = 3}};
|
||||
|
||||
auto same = publication.publish(payload(2), {.epoch = 1, .revision = 3});
|
||||
Void::Testing::expect(same.status ==
|
||||
Void::Core::Publication<Payload>::PublishStatus::
|
||||
stale_revision);
|
||||
auto lower = publication.publish(payload(3), {.epoch = 1, .revision = 2});
|
||||
Void::Testing::expect(lower.status ==
|
||||
Void::Core::Publication<Payload>::PublishStatus::
|
||||
stale_revision);
|
||||
Void::Testing::expect_eq(publication.acquire()->revision,
|
||||
std::uint64_t{1});
|
||||
}
|
||||
|
||||
Void::Testing::Test publication_new_epoch_resets_revision()
|
||||
{
|
||||
Void::Core::Publication<Payload> publication{
|
||||
payload(1), {.epoch = 1, .revision = 100}};
|
||||
auto result = publication.publish_epoch(payload(2), 2);
|
||||
Void::Testing::expect(result.published());
|
||||
Void::Testing::expect_eq(publication.version().epoch, std::uint64_t{2});
|
||||
Void::Testing::expect_eq(publication.version().revision,
|
||||
std::uint64_t{0});
|
||||
}
|
||||
|
||||
Void::Testing::Test publication_releases_value_after_last_reader()
|
||||
{
|
||||
std::weak_ptr<const Tracked> old;
|
||||
{
|
||||
auto initial = std::make_shared<const Tracked>();
|
||||
old = initial;
|
||||
Void::Core::Publication<Tracked> publication{
|
||||
std::move(initial), {.epoch = 1, .revision = 1}};
|
||||
auto held = publication.acquire();
|
||||
Void::Testing::expect(!old.expired());
|
||||
|
||||
Void::Testing::require(
|
||||
publication
|
||||
.publish(std::make_shared<const Tracked>(),
|
||||
{.epoch = 1, .revision = 2})
|
||||
.published());
|
||||
|
||||
// The old value is still referenced by the retained reader.
|
||||
Void::Testing::expect(!old.expired());
|
||||
held = publication.acquire();
|
||||
// Replacing the handle drops the last reference to the old value.
|
||||
Void::Testing::expect(old.expired());
|
||||
}
|
||||
Void::Testing::expect(old.expired());
|
||||
}
|
||||
|
||||
Void::Testing::Test publication_failed_preparation_preserves_last_good()
|
||||
{
|
||||
Void::Core::Publication<Payload> publication{
|
||||
payload(7, "good"), {.epoch = 1, .revision = 7}};
|
||||
|
||||
try {
|
||||
// A preparation that throws never reaches publish.
|
||||
throw std::runtime_error{"preparation failed"};
|
||||
} catch (const std::runtime_error&) {
|
||||
}
|
||||
|
||||
Void::Testing::expect_eq(publication.version().revision,
|
||||
std::uint64_t{7});
|
||||
Void::Testing::expect_eq(publication.acquire()->text, std::string{"good"});
|
||||
}
|
||||
|
||||
Void::Testing::Test publication_concurrent_readers_see_coherent_snapshots()
|
||||
{
|
||||
Void::Core::Publication<Payload> publication{
|
||||
payload(0, "0"), {.epoch = 1, .revision = 0}};
|
||||
|
||||
constexpr std::uint64_t kPublishes = 200;
|
||||
std::atomic<bool> done{false};
|
||||
std::atomic<int> inconsistent{0};
|
||||
std::atomic<std::uint64_t> observed_max{0};
|
||||
|
||||
std::vector<std::thread> readers;
|
||||
for (int reader = 0; reader < 3; ++reader) {
|
||||
readers.emplace_back([&] {
|
||||
while (!done.load(std::memory_order_acquire)) {
|
||||
auto snapshot = publication.acquire();
|
||||
if (snapshot->revision != snapshot.version().revision) {
|
||||
++inconsistent;
|
||||
}
|
||||
if (snapshot->text !=
|
||||
std::to_string(snapshot->revision)) {
|
||||
++inconsistent;
|
||||
}
|
||||
const auto revision = snapshot->revision;
|
||||
auto previous = observed_max.load();
|
||||
while (previous < revision &&
|
||||
!observed_max.compare_exchange_weak(previous,
|
||||
revision)) {
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
for (std::uint64_t revision = 1; revision <= kPublishes; ++revision) {
|
||||
const auto result = publication.publish(
|
||||
payload(revision, std::to_string(revision)),
|
||||
{.epoch = 1, .revision = revision});
|
||||
Void::Testing::expect(result.published());
|
||||
}
|
||||
done.store(true, std::memory_order_release);
|
||||
for (auto& reader : readers) {
|
||||
reader.join();
|
||||
}
|
||||
|
||||
Void::Testing::expect_eq(inconsistent.load(), 0);
|
||||
Void::Testing::expect_eq(publication.version().revision, kPublishes);
|
||||
Void::Testing::expect_eq(publication.acquire()->text,
|
||||
std::to_string(kPublishes));
|
||||
}
|
||||
Reference in new issue
Block a user