sincekit / tests /test_compact.cpp
pgg-engineering's picture
Release SinceKit 0.1.0 with native benchmarks and trace replay
ef8334d verified
Raw History Blame Contribute Delete
4.81 kB
#include "sincekit_compact.h"
#include <algorithm>
#include <cstdint>
#include <iostream>
#include <limits>
#include <random>
#include <stdexcept>
#include <tuple>
#include <type_traits>
#include <vector>
namespace {
using sincekit::CompactIndex;
using Change = std::tuple<std::uint32_t, std::uint64_t, bool>;
using Rows = std::vector<Change>;
void require(bool value) {
if (!value) throw std::runtime_error("compact journal check failed");
}
template<class Error, class Fn>
void expect(Fn&& fn) {
try {
fn();
} catch (const Error&) {
return;
}
throw std::runtime_error("expected exception was not thrown");
}
Rows read(const CompactIndex& index, std::uint64_t cursor) {
Rows rows;
const auto watermark = index.since(cursor, [&](auto id, auto rev, auto deleted) {
rows.emplace_back(id, rev, deleted);
});
require(watermark == index.revision());
return rows;
}
void boundaries() {
static_assert(!std::is_copy_constructible_v<CompactIndex>);
static_assert(!std::is_move_constructible_v<CompactIndex>);
if constexpr (std::numeric_limits<std::size_t>::digits > 32) {
expect<std::length_error>([] {
CompactIndex oversized(std::size_t{(std::numeric_limits<std::uint32_t>::max)()} + 1);
});
}
CompactIndex zero(0);
require(zero.capacity() == 0 && read(zero, 0).empty());
expect<std::out_of_range>([&] { zero.erase(0); });
expect<std::out_of_range>([&] { read(zero, 1); });
CompactIndex one(1);
require(one.erase(0) == 1);
require(one.erase(0) == 2);
require(one.touch(0) == 3);
require(read(one, 0) == Rows{{0, 3, false}});
require(read(one, 3).empty());
expect<std::out_of_range>([&] { one.touch(1); });
expect<std::out_of_range>([&] { read(one, (std::numeric_limits<std::uint64_t>::max)()); });
require(one.revision() == 3);
}
// Keep every event and rebuild results from scratch. The oracle has neither a
// compacted generation nor a latest-revision array and can check any old cursor.
Rows expected(const Rows& history, std::size_t capacity, std::size_t cursor) {
std::vector<unsigned char> seen(capacity);
Rows rows;
for (auto r = history.size(); r > cursor; --r) {
const auto& event = history[r - 1];
const auto id = std::get<0>(event);
if (!seen[id]) {
rows.push_back(event);
seen[id] = 1;
}
}
return rows;
}
void generations(std::uint32_t capacity) {
std::mt19937 random(67291 + capacity);
CompactIndex index(capacity);
Rows history;
for (std::size_t step = 0; step < 30 * capacity + 40; ++step) {
const auto offset = static_cast<std::uint32_t>(step % capacity);
std::uint32_t id = offset;
switch ((step / capacity) % 4) {
case 1: id = 0; break; // An entire tail of overwrites.
case 2: id = capacity - 1 - offset; break;
case 3: id = random() % capacity; break;
}
const bool deleted = (random() % 3 == 0);
const auto rev = deleted ? index.erase(id) : index.touch(id);
history.emplace_back(id, history.size() + 1, deleted);
require(index.capacity() == capacity && rev == history.size());
if (capacity <= 3 || offset == 0 || offset == capacity - 1) {
// Both sides of each compaction, including old cursors inside the
// generation that was just replaced.
for (std::size_t cursor = 0; cursor <= history.size(); ++cursor) {
require(read(index, cursor) == expected(history, capacity, cursor));
}
} else {
auto cursor = static_cast<std::size_t>(random()) % (history.size() + 1);
require(read(index, cursor) == expected(history, capacity, cursor));
}
}
}
struct Interrupted {};
void reentry() {
CompactIndex a(3), b(1);
a.touch(0);
a.erase(1);
a.touch(0); // A write during this traversal would trigger compaction.
const auto before = read(a, 0);
expect<Interrupted>([&] {
a.since(0, [&](auto, auto, auto) {
b.touch(0);
b.since(0, [&](auto, auto, auto) {
require(read(a, 0) == before);
expect<std::logic_error>([&] { a.erase(1); });
expect<std::logic_error>([&] { b.touch(0); });
throw Interrupted{};
});
});
});
require(a.revision() == 3 && read(a, 0) == before);
require(a.erase(2) == 4 && b.touch(0) == 2);
require(read(a, 0) == Rows{{2, 4, true}, {0, 3, false}, {1, 2, true}});
}
} // namespace
int main() {
boundaries();
reentry();
for (auto capacity : {1U, 2U, 3U, 17U, 65U}) generations(capacity);
std::cout << "compact journal tests passed\n";
}