|
@@ -54,3 +54,68 @@ TEST(Errors, GatewayTimeoutIsFiveOhFour) {
|
|
|
// an upstream that did not answer, not a slow client (which would be 408).
|
|
// an upstream that did not answer, not a slow client (which would be 408).
|
|
|
EXPECT_EQ(httpStatus(ErrCode::GatewayTimeout), 504);
|
|
EXPECT_EQ(httpStatus(ErrCode::GatewayTimeout), 504);
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+// ---------------------------------------------------------------------------
|
|
|
|
|
+// Retry-After is keyed on the CAUSE, and the raise site is what knows it.
|
|
|
|
|
+//
|
|
|
|
|
+// The point of these is the PAIRING. A test that only asserts "a transient
|
|
|
|
|
+// 503 advises 5 seconds" would pass equally against a handler that stamped
|
|
|
|
|
+// every error with a header, which is the defect being avoided rather than
|
|
|
|
|
+// the behaviour being added - most of this service's 503s are "did not
|
|
|
|
|
+// apply" results whose remedy has no duration, and a number there would be
|
|
|
|
|
+// honoured by a caller and would be a lie.
|
|
|
|
|
+// ---------------------------------------------------------------------------
|
|
|
|
|
+
|
|
|
|
|
+TEST(RetryAfter, ARateLimitKeepsItsHistoricalSixtySeconds) {
|
|
|
|
|
+ EXPECT_EQ(svapi::retryAfterSeconds(
|
|
|
|
|
+ svapi::ApiError(svapi::ErrCode::TooManyRequests, "rate_limited", "slow down")),
|
|
|
|
|
+ svapi::kRetryAfterRateLimitedSec);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(RetryAfter, AnExplicitHintFromTheRaiseSiteBeatsTheStatusDefault) {
|
|
|
|
|
+ // So a future limiter with a real budget can say so rather than inherit 60.
|
|
|
|
|
+ svapi::ApiError e(svapi::ErrCode::TooManyRequests, "rate_limited", "slow down",
|
|
|
|
|
+ nlohmann::json::object(), 7);
|
|
|
|
|
+ EXPECT_EQ(svapi::retryAfterSeconds(e), 7);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(RetryAfter, AnUnreachableDatabaseAdvisesTheShortTransientValue) {
|
|
|
|
|
+ svapi::ApiError e(svapi::ErrCode::Unavailable, "db_unavailable", "deadline exceeded",
|
|
|
|
|
+ nlohmann::json::object(), svapi::kRetryAfterDbUnreachableSec);
|
|
|
|
|
+ EXPECT_EQ(svapi::retryAfterSeconds(e), svapi::kRetryAfterDbUnreachableSec);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(RetryAfter, ReadOnlyRecoveryAdvisesLongerThanAMereOutage) {
|
|
|
|
|
+ svapi::ApiError e(svapi::ErrCode::Unavailable, "read_only", "read-only mode",
|
|
|
|
|
+ nlohmann::json::object(), svapi::kRetryAfterReadOnlySec);
|
|
|
|
|
+ EXPECT_EQ(svapi::retryAfterSeconds(e), svapi::kRetryAfterReadOnlySec);
|
|
|
|
|
+ // The ORDERING is the decision, not the two numbers: whatever they are,
|
|
|
|
|
+ // recovery must never advise a shorter wait than a transient blip.
|
|
|
|
|
+ EXPECT_GT(svapi::kRetryAfterReadOnlySec, svapi::kRetryAfterDbUnreachableSec);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// --- the falsifiability pairing: the header is SELECTIVE, not blanket -------
|
|
|
|
|
+
|
|
|
|
|
+TEST(RetryAfter, AnOrdinary503SaysNOTHING) {
|
|
|
|
|
+ // "insert failed", "drop failed", "failed to create relation": a write
|
|
|
|
|
+ // that did not apply. No duration makes it apply, so advising one would
|
|
|
|
|
+ // be inventing a remedy.
|
|
|
|
|
+ EXPECT_EQ(svapi::retryAfterSeconds(
|
|
|
|
|
+ svapi::ApiError(svapi::ErrCode::Unavailable, "db_error", "insert failed")),
|
|
|
|
|
+ 0);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(RetryAfter, NoRunnerConfiguredSaysNOTHING) {
|
|
|
|
|
+ // An operator has to act. Retrying never helps, so the header must not
|
|
|
|
|
+ // suggest it does.
|
|
|
|
|
+ EXPECT_EQ(svapi::retryAfterSeconds(
|
|
|
|
|
+ svapi::ApiError(svapi::ErrCode::Unavailable, "no_runner", "no runner")),
|
|
|
|
|
+ 0);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(RetryAfter, ARefusalTheCallerMustFixSaysNOTHING) {
|
|
|
|
|
+ for (auto code : {svapi::ErrCode::Unprocessable, svapi::ErrCode::Forbidden,
|
|
|
|
|
+ svapi::ErrCode::NotFound, svapi::ErrCode::Conflict}) {
|
|
|
|
|
+ EXPECT_EQ(svapi::retryAfterSeconds(svapi::ApiError(code, "x", "y")), 0);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|