|
|
@@ -0,0 +1,104 @@
|
|
|
+#include "db_await.hpp"
|
|
|
+#include <gtest/gtest.h>
|
|
|
+
|
|
|
+// The startup wait, with an injected clock so the property that BROKE can be
|
|
|
+// asserted without a database and without really sleeping.
|
|
|
+//
|
|
|
+// What broke: the wait was bounded by an attempt count, and the cost of one
|
|
|
+// attempt was an unstated assumption about the database client. When client
|
|
|
+// 2.13.1 made connect() block up to 5 s instead of returning immediately, a
|
|
|
+// 30-attempt loop went from ~30 s to ~180 s - past systemd's 90 s
|
|
|
+// TimeoutStartSec, so the process was killed mid-loop and the honest
|
|
|
+// "database not reachable" diagnostic never printed.
|
|
|
+namespace {
|
|
|
+
|
|
|
+using namespace std::chrono_literals;
|
|
|
+using Clock = std::chrono::steady_clock;
|
|
|
+
|
|
|
+/// A fake clock the test advances itself. `sleepFor` advances it, and so does
|
|
|
+/// a connect attempt, which is how a slow attempt is simulated.
|
|
|
+struct FakeTime {
|
|
|
+ Clock::time_point t = Clock::time_point{};
|
|
|
+ Clock::time_point now() const { return t; }
|
|
|
+ void advance(std::chrono::milliseconds d) { t += d; }
|
|
|
+};
|
|
|
+
|
|
|
+TEST(DbAwait, AttemptsOnceEvenWithNoBudget) {
|
|
|
+ FakeTime c;
|
|
|
+ int attempts = 0;
|
|
|
+ const bool got = svapi::awaitConnectable([&] { ++attempts; return false; },
|
|
|
+ [&] { return c.now(); },
|
|
|
+ [&](std::chrono::milliseconds d) { c.advance(d); },
|
|
|
+ 0ms, 1s);
|
|
|
+ EXPECT_FALSE(got);
|
|
|
+ EXPECT_EQ(attempts, 1) << "a caller asking to connect wants one honest try";
|
|
|
+}
|
|
|
+
|
|
|
+TEST(DbAwait, ReturnsAsSoonAsItConnects) {
|
|
|
+ FakeTime c;
|
|
|
+ int attempts = 0;
|
|
|
+ const bool got = svapi::awaitConnectable([&] { return ++attempts == 3; },
|
|
|
+ [&] { return c.now(); },
|
|
|
+ [&](std::chrono::milliseconds d) { c.advance(d); },
|
|
|
+ 60s, 1s);
|
|
|
+ EXPECT_TRUE(got);
|
|
|
+ EXPECT_EQ(attempts, 3);
|
|
|
+}
|
|
|
+
|
|
|
+TEST(DbAwait, StaysInsideTheBudgetWhenEveryAttemptIsSLOW) {
|
|
|
+ // THE REGRESSION. Each attempt costs 5 s, as a 2.13.1 connect() does
|
|
|
+ // against an unreachable server. The old attempt-count loop would have run
|
|
|
+ // 30 x (5 + 1) = 180 s here. This must not exceed its 60 s budget, because
|
|
|
+ // 90 s is where systemd kills us.
|
|
|
+ FakeTime c;
|
|
|
+ int attempts = 0;
|
|
|
+ const auto start = c.now();
|
|
|
+ const bool got = svapi::awaitConnectable([&] { ++attempts; c.advance(5s); return false; },
|
|
|
+ [&] { return c.now(); },
|
|
|
+ [&](std::chrono::milliseconds d) { c.advance(d); },
|
|
|
+ 60s, 1s);
|
|
|
+ EXPECT_FALSE(got);
|
|
|
+ const auto elapsed = c.now() - start;
|
|
|
+ EXPECT_LE(elapsed, 65s) << "overshot its budget by more than one attempt";
|
|
|
+ EXPECT_LT(elapsed, 90s) << "would be SIGTERMed by systemd before reporting anything";
|
|
|
+ EXPECT_LE(attempts, 11) << "a 5 s attempt plus a 1 s pause fits ~10 times into 60 s";
|
|
|
+}
|
|
|
+
|
|
|
+TEST(DbAwait, AnInstantFailureStillRespectsTheBudgetRatherThanSpinning) {
|
|
|
+ // The OLD behaviour, where connect() returned immediately: the pause is
|
|
|
+ // what bounds the attempt rate, and the deadline is what ends it. Without
|
|
|
+ // the pause being charged against the deadline this would spin forever.
|
|
|
+ FakeTime c;
|
|
|
+ int attempts = 0;
|
|
|
+ const bool got = svapi::awaitConnectable([&] { ++attempts; return false; },
|
|
|
+ [&] { return c.now(); },
|
|
|
+ [&](std::chrono::milliseconds d) { c.advance(d); },
|
|
|
+ 10s, 1s);
|
|
|
+ EXPECT_FALSE(got);
|
|
|
+ EXPECT_LE(attempts, 11);
|
|
|
+ EXPECT_GE(attempts, 9) << "with instant failures it should use most of its budget";
|
|
|
+}
|
|
|
+
|
|
|
+TEST(DbAwait, NeverSleepsPastItsOwnDeadline) {
|
|
|
+ // If the remaining time is less than one pause, stop rather than sleep
|
|
|
+ // through the deadline - the overshoot is what systemd notices.
|
|
|
+ FakeTime c;
|
|
|
+ const auto start = c.now();
|
|
|
+ (void)svapi::awaitConnectable([&] { c.advance(900ms); return false; },
|
|
|
+ [&] { return c.now(); },
|
|
|
+ [&](std::chrono::milliseconds d) { c.advance(d); },
|
|
|
+ 5s, 1s);
|
|
|
+ EXPECT_LE(c.now() - start, 6s);
|
|
|
+}
|
|
|
+
|
|
|
+TEST(DbAwait, AZeroPauseDoesNotSleepAtAll) {
|
|
|
+ // Permitted, for a caller that wants to burn its budget on attempts.
|
|
|
+ FakeTime c;
|
|
|
+ int sleeps = 0;
|
|
|
+ (void)svapi::awaitConnectable([&] { c.advance(1s); return false; },
|
|
|
+ [&] { return c.now(); },
|
|
|
+ [&](std::chrono::milliseconds) { ++sleeps; }, 3s, 0ms);
|
|
|
+ EXPECT_EQ(sleeps, 0);
|
|
|
+}
|
|
|
+
|
|
|
+} // namespace
|