Files
moloapp/tests/src/operability/test_lifecycle.cpp
T

95 lines
3.0 KiB
C++
Raw Normal View History

// Lifecycle tests: boot completion and the four phase drain.
#include <cstdio>
#include <string>
#include "operability/deploy/lifecycle.h"
#include "supervisor/common/clock.hpp"
#include "supervisor/common/observer.hpp"
namespace {
int g_fail = 0;
#define CHECK(cond) \
do { \
if (!(cond)) { \
++g_fail; \
std::fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
} \
} while (0)
void test_mark_serving() {
molo::common::FrozenClock clock(0);
molo::common::RecordingObserver obs;
molo::deploy::Lifecycle lc(clock, obs);
bool called = false;
lc.on_serving([&] { called = true; });
lc.mark_serving();
CHECK(called);
CHECK(lc.phase() == molo::deploy::Lifecycle::Phase::kServing);
}
void test_clean_drain() {
molo::common::FrozenClock clock(0);
molo::common::RecordingObserver obs;
molo::deploy::Lifecycle lc(clock, obs);
bool stopped = false;
bool done = false;
lc.on_stop_listeners([&] { stopped = true; });
lc.on_connections_drained([] { return true; });
lc.on_background_drained([] { return true; });
lc.on_complete([&] { done = true; });
CHECK(lc.run_drain(1000));
CHECK(stopped && done);
CHECK(lc.clean());
CHECK(lc.phase() == molo::deploy::Lifecycle::Phase::kDone);
}
void test_delayed_drain() {
molo::common::FrozenClock clock(0);
molo::common::RecordingObserver obs;
molo::deploy::Lifecycle lc(clock, obs);
// Probes flip true once the frozen clock passes their marks; the drain
// loop advances the clock via sleep_ms, so no real waiting happens.
lc.on_connections_drained([&] { return clock.steady_ms() >= 200; });
lc.on_background_drained([&] { return clock.steady_ms() >= 400; });
CHECK(lc.run_drain(5000, 50));
CHECK(lc.clean());
CHECK(clock.steady_ms() >= 400);
}
void test_drain_timeout_drops_background() {
molo::common::FrozenClock clock(0);
molo::common::RecordingObserver obs;
molo::deploy::Lifecycle lc(clock, obs);
bool dropped = false;
lc.on_connections_drained([] { return true; });
lc.on_background_drained([] { return false; });
lc.on_drop_background([&](const std::string&) { dropped = true; });
CHECK(!lc.run_drain(200, 25));
CHECK(!lc.clean());
CHECK(dropped);
CHECK(lc.phase() == molo::deploy::Lifecycle::Phase::kDone);
}
void test_drain_timeout_connections() {
molo::common::FrozenClock clock(0);
molo::common::RecordingObserver obs;
molo::deploy::Lifecycle lc(clock, obs);
lc.on_connections_drained([] { return false; });
lc.on_background_drained([] { return true; });
CHECK(!lc.run_drain(100, 25));
CHECK(!lc.clean());
}
} // namespace
int run_lifecycle_tests() {
test_mark_serving();
test_clean_drain();
test_delayed_drain();
test_drain_timeout_drops_background();
test_drain_timeout_connections();
return g_fail;
}