129 lines
3.3 KiB
C++
129 lines
3.3 KiB
C++
#include "reloadable_host/reload_transaction.hpp"
|
|
|
|
#include <cassert>
|
|
#include <chrono>
|
|
#include <cstdio>
|
|
#include <string>
|
|
|
|
namespace
|
|
{
|
|
using namespace ReloadableHost;
|
|
|
|
Void::Result::Result<void> accept_all(const ReleaseCandidate&)
|
|
{
|
|
return {};
|
|
}
|
|
|
|
Void::Result::Result<void> reject_all(const ReleaseCandidate&)
|
|
{
|
|
return std::unexpected{
|
|
Void::Result::Error{Void::Result::ErrorKind::invalid_argument,
|
|
"release failed validation"}};
|
|
}
|
|
|
|
void happy_path_stays_published()
|
|
{
|
|
ReloadTransaction tx{1};
|
|
const ReleaseCandidate candidate{"rel-a", "/releases/a", 2};
|
|
assert(tx.prepare(candidate, accept_all).has_value());
|
|
Quiescence quiescence;
|
|
assert(tx.drain(quiescence).has_value());
|
|
assert(tx.publish().has_value());
|
|
tx.commit(true);
|
|
|
|
const auto report = tx.report();
|
|
assert(report.id == 1);
|
|
assert(report.phase == ReloadPhase::Completed);
|
|
assert(report.outcome == ReloadOutcome::Published);
|
|
assert(report.candidate == "rel-a");
|
|
quiescence.resume();
|
|
}
|
|
|
|
void cancel_before_publish_rolls_back()
|
|
{
|
|
ReloadTransaction tx{2};
|
|
assert(tx.prepare({"rel-b", "/releases/b", 1}, accept_all)
|
|
.has_value());
|
|
tx.cancel();
|
|
assert(tx.report().outcome == ReloadOutcome::RolledBack);
|
|
assert(tx.report().phase == ReloadPhase::CancelledBeforePublish);
|
|
|
|
Quiescence quiescence;
|
|
assert(!tx.drain(quiescence).has_value());
|
|
}
|
|
|
|
void cancel_after_publish_is_a_noop()
|
|
{
|
|
ReloadTransaction tx{3};
|
|
assert(tx.prepare({"rel-c", "/releases/c", 1}, accept_all)
|
|
.has_value());
|
|
Quiescence quiescence;
|
|
assert(tx.drain(quiescence).has_value());
|
|
assert(tx.publish().has_value());
|
|
|
|
tx.cancel();
|
|
assert(tx.report().phase == ReloadPhase::Committing);
|
|
|
|
// A lost durable acknowledgement is Unknown, never RolledBack.
|
|
tx.commit(false);
|
|
assert(tx.report().phase == ReloadPhase::UnknownOutcome);
|
|
assert(tx.report().outcome == ReloadOutcome::UnknownOutcome);
|
|
quiescence.resume();
|
|
}
|
|
|
|
void invalid_candidate_fails_closed()
|
|
{
|
|
ReloadTransaction tx{4};
|
|
assert(!tx.prepare({"rel-d", "/releases/d", 1}, reject_all)
|
|
.has_value());
|
|
assert(tx.report().phase == ReloadPhase::Failed);
|
|
assert(tx.report().outcome == ReloadOutcome::Failed);
|
|
}
|
|
|
|
void quiescence_blocks_admission_and_drains()
|
|
{
|
|
Quiescence quiescence{ReloadLimits{
|
|
.drain_deadline = std::chrono::milliseconds{10},
|
|
.max_pins = 2}};
|
|
|
|
assert(quiescence.admit().has_value());
|
|
assert(quiescence.admit().has_value());
|
|
// Pin cap.
|
|
assert(!quiescence.admit().has_value());
|
|
|
|
// Pinned calls hold the drain until the deadline.
|
|
assert(!quiescence.drain());
|
|
assert(quiescence.draining());
|
|
|
|
quiescence.release();
|
|
quiescence.release();
|
|
assert(quiescence.drain());
|
|
// Admissions stay closed while draining.
|
|
assert(!quiescence.admit().has_value());
|
|
|
|
quiescence.resume();
|
|
assert(quiescence.admit().has_value());
|
|
quiescence.release();
|
|
}
|
|
|
|
void invalid_transitions_are_rejected()
|
|
{
|
|
ReloadTransaction tx{5};
|
|
Quiescence quiescence;
|
|
assert(!tx.publish().has_value());
|
|
assert(!tx.drain(quiescence).has_value());
|
|
}
|
|
} // namespace
|
|
|
|
int main()
|
|
{
|
|
happy_path_stays_published();
|
|
cancel_before_publish_rolls_back();
|
|
cancel_after_publish_is_a_noop();
|
|
invalid_candidate_fails_closed();
|
|
quiescence_blocks_admission_and_drains();
|
|
invalid_transitions_are_rejected();
|
|
std::puts("reloadable-host transaction ok");
|
|
return 0;
|
|
}
|