Repository navigation
test: add opt-in failover drills (just test-dr) - #84
Merged
Merged
Conversation
This was referenced Oct 28, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds
just test-dr, an opt-in suite that runsbuild_connection_factorythrough a real failover. It followsfaststream-outbox/tests_drandfaststream-redis-timers/tests_dr. Every failover test intests/monkeypatchesasyncpg.connect, so this is the first run of the multi-host path against real servers.docker-compose.dr.ymlstarts a primary, a streaming replica, and a second standby, and gives thedrcontainer one multi-host DSN covering all three. The drill builds a write engine frombuild_db_dsn(...)and a read engine frombuild_db_dsn(..., use_replica=True), both withbuild_connection_factoryasasync_creator, and writes insideTransactionunderpostgres_retry. It then kills or pauses the primary, promotes the replica withpg_promote(), and repeats the write until it lands. It checks that:use_replica=Truesucceed before and after the promotion;The justfile starts a fresh cluster for each scenario.
tests_dr/is outsidetestpathsand omitted from coverage, and the ruff test ignores now include it. CI does not run it.Results
Three runs of
just test-dron Docker Desktop (macOS, arm64):postgres_retryattemptsTimeoutErroreach runWith the primary killed, the first attempt fails on the pooled connection to the dead host. SQLAlchemy marks the connection invalidated, and
postgres_retryreruns the body on a new connection that reaches the promoted replica.With the primary paused, the first statement on the pooled connection hits
command_timeout=3and raisesTimeoutError.postgres_retrydoes not retry it, as CONTEXT.md says, since the server may have committed. The drill's own loop calls again and the next call connects. A host-by-host fallback happens whenever the shuffled bulk attempt puts the frozen host first and spends the whole connect timeout on it.Found by the drill, not fixed here
command_timeoutin the DSN, the pause drill hangs. db-retry bounds the connect but not statements, so the first statement on a pooled connection to a frozen primary waits until the test budget cancels it (in production, until TCP keepalive gives up, about two hours by default). To reproduce, changedsn = f"{DB_DSN}&command_timeout={COMMAND_TIMEOUT_SECONDS}"todsn = DB_DSNintests_dr/test_failover.pyand rundocker compose -f docker-compose.dr.yml run --rm dr uv run pytest tests_dr -s -k pause. Whether db-retry should set a default or document the requirement belongs in its own issue, and a fix would start with a failing test.wal_keep_size=256MB. Without it, the two standbys' concurrentpg_basebackupruns sometimes failed with "requested WAL segment ... has already been removed" (1 in 5 cluster starts). faststream-outbox's DR compose has the same layout.Test plan
just test-dr: three runs, both scenarios passed each timejust test --cov=.: 63 passed, 100% coveragejust lint-ci: clean