fix(testutil): wait for database visibility in SetupSharedTestDB (#5788)
* test(storage): be-s9d setup-shared-db visibility tests for dolt.New / initSharedSchema
Adds five tests pinning the behavioral contract that
testutil.SetupSharedTestDB must result in a database that is fully
visible to fresh MySQL sessions on the same Dolt server. The bug
under be-s9d (originally observed in the BEADS_TEST_EXTERNAL_DOLT_PORT
TestMain branch added in cfd38cda) is that CREATE DATABASE in
SetupSharedTestDB is left in the working set without an explicit
DOLT_COMMIT, so fresh connections that open their own session do not
see the new database. The reviewer's diagnosis (be-nx7 reconciliation
notes) calls out adding CALL DOLT_COMMIT after CREATE DATABASE, or
retry-with-USE in the external-port branch, as the fix surface.
State today (validator's failing tests, written before the fix):
PASS TestSetupSharedTestDB_FreshRawConnSeesDatabase
SHOW DATABASES on a fresh raw mysql conn does list the new db
(regression-guard for the existing-good behavior).
FAIL TestSetupSharedTestDB_NewStoreOpens
dolt.New() fails with "database not found" against a freshly
SetupSharedTestDB-created db, even though the prior test shows
SHOW DATABASES does see it. This is the literal failure mode
— the difference is dolt.New's initDB connection shape (empty
ServerUser, internal-built DSN) hits a stricter visibility
boundary than my User=root probe. Captures the bug.
PASS TestSetupSharedTestDB_FreshConnAfterSecondSetupCall
Idempotent CREATE-IF-NOT-EXISTS path stays visible across two
setup calls. Regression-guard for any DOLT_COMMIT side effect
the fix introduces.
PASS TestSetupSharedTestDB_RefusesProductionPort
FIREWALL guard against port 3307 (DefaultSQLPort) intact.
Defensive contract — must not regress through the be-s9d fix.
FAIL TestInitSharedSchema_AfterFreshDatabase
After initSharedSchema completes (all migrations apply,
DOLT_ADD + DOLT_COMMIT runs), a fresh connection's SHOW
TABLES returns zero rows. Schema commits on the
initSharedSchema session do not surface to other sessions —
the deeper invariant the fix must restore.
Both failing tests pass on the testcontainer path that masks the bug
in CreateIfMissing=true mode, which is why TestMain external-port is
the only place the suite-wide runtime hits it; my tests pin the
contract at the dolt.New / SHOW TABLES level so the fix is verifiable
without a second host-resident dolt sql-server. The benchstat-geomean
write-bench measurement that be-s9d also requests is a downstream
measurement task, not a test, and is not in this commit.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* fix(testutil): wait for database visibility in SetupSharedTestDB (be-s9d)
The BEADS_TEST_EXTERNAL_DOLT_PORT branch added in cfd38cda was supposed to
let CI/dev runs target a host-resident dolt sql-server, but the polecat
tests added in 295b4552 surfaced a startup race: SetupSharedTestDB returns
right after CREATE DATABASE, and a sibling pool opened by dolt.New() inside
initSharedSchema can outrace the Dolt server's catalog refresh and fail
with "Error 1049 (HY000): database not found: <name>" — Dolt's own wording
from go-mysql-server, distinct from MySQL's "Unknown database" handled in
isRetryableError since GH-1851.
Three load-bearing changes:
1. testutil.SetupSharedTestDB now polls USE <dbName> on the same handle
with bounded exponential backoff (50ms → 1s, 10s deadline) before
returning. The catalog race is what 295b4552's
TestSetupSharedTestDB_NewStoreOpens / TestInitSharedSchema_AfterFreshDatabase
were pinning; both flip from RED → GREEN with this poll.
2. dolt.isRetryableError now also retries "database not found", same
reason as the existing "unknown database" branch (GH-1851). Defensive
backstop in case any code path opens a fresh pool against the new
database without first going through SetupSharedTestDB's poll.
3. testmain_test.go now sets BOTH BEADS_DOLT_SERVER_PORT and
BEADS_DOLT_PORT (and unsets both at end). applyConfigDefaults reads
BEADS_DOLT_SERVER_PORT first and only falls through to the legacy
BEADS_DOLT_PORT, so a pre-existing BEADS_DOLT_SERVER_PORT in the
surrounding agent shell (gc-beads-bd exports the rig's gc.endpoint
port) silently won over the testmain-set BEADS_DOLT_PORT and routed
the bench to the wrong server. Both branches (external-port +
testcontainer) pin both vars.
Tests exercising these paths (added in 295b4552):
PASS TestSetupSharedTestDB_FreshRawConnSeesDatabase
PASS TestSetupSharedTestDB_NewStoreOpens (was FAIL at baseline)
PASS TestSetupSharedTestDB_FreshConnAfterSecondSetupCall
PASS TestSetupSharedTestDB_RefusesProductionPort
PASS TestInitSharedSchema_AfterFreshDatabase (was FAIL at baseline)
Run with TESTCONTAINERS_RYUK_DISABLED=true BEADS_TEST_EXTERNAL_DOLT_PORT=33400.
Unblocks the 10K write-bench measurement called for in be-eei §8 guardrail 5
(CreateIssue/UpdateIssue regression vs pre-D4v2). Bench results to follow
in a separate be-s9d note.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* chore: release gate PASS for shared-test-DB-visibility-waits theme (be-pow0)
* fix(testutil): narrow to test-only per review — drop retry-classifier change, reword visibility-wait comments
bee-ghosttrack's review (2026-09-03) on #5788 flagged that the
isRetryableError "database not found" case was a real production
behaviour change riding along in a PR described as test infrastructure:
it turns a genuinely-missing-database failure from immediate into a full
~30s backoff (serverRetryMaxElapsed) across six withRetryClassified call
sites. Dropping it entirely — SetupSharedTestDB's own visibility poll in
testdoltbranch.go already resolves the catalog race the classifier case
was meant to backstop, so it was never load-bearing for this fix.
Also rewords waitForDatabaseVisible's doc comment to describe what USE on
a pooled connection actually proves — that one connection observed the
catalog refresh, not a pool-wide guarantee, and that USE mutates that
connection's session default database as a side effect — and notes as a
comment (not a behaviour change) that the backoff cap can let delay reach
~1.6s before capping, which is harmless inside the 10s bound.
testmain_test.go's teardown also unset BEADS_DOLT_SERVER_PORT, the other
half of the same port-pin hunk the rebase already dropped in favour of
main's ambient-port FATAL guard (#5837); that half merged cleanly instead
of conflicting, so it survived silently until now. Removed — main never
sets that var, so unsetting it was dead code.
TestSetupSharedTestDB_NewStoreOpens and the rest of the visibility suite
still pass without the classifier change (podman testcontainer run: 5
PASS / 0 FAIL / 0 SKIP).
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
* chore: remove leaked internal release-gate doc from PR branch
release-gates/be-pow0-shared-test-db-visibility-gate.md
Tracked our internal gc-management/beads-rig release-gate checklist and
should never have been committed to this upstream branch -- it's process
residue from our deploy tooling, not part of the change itself.
Per maintainer bee-ghosttrack's request during review of PR #6571. The
root cause (our deploy tooling auto-committing this file) is already fixed
as of gm-qatxl1; this branch predates that fix, so this commit is cleanup
only -- no other content on this branch is touched.
Ref: be-lk7o3
* fix(testutil): poll SHOW DATABASES exact-match for visibility, not USE (be-2wf6u)
waitForDatabaseVisible was polling USE <dbName>, which Dolt can satisfy by
auto-attaching a database to the session before the server's catalog (what
SHOW DATABASES and dolt.New() actually check) has registered it — a no-op
wait in exactly the racy just-created case it exists to guard against
(bee-ghosttrack PR #5788 R2 item 1).
Switch to the same SHOW DATABASES exact-match iteration
databaseExistsOnServer (internal/storage/dolt/store.go) already uses, via a
small local helper (databaseVisibleNow) rather than a cross-package import,
to avoid coupling internal/testutil to internal/storage/dolt for one
10-line check. Preserves the existing backoff/deadline/ctx-cancellation
shape and the "not visible after" bounded error.
Update the call-site and function-doc comments to stop claiming the wait
proves visibility "to a fresh connection" — success only guarantees the
polling connection's own view is current, not a guarantee for every
connection in db's pool. R1 already corrected the function doc to this
wording; the call site had drifted from it (R2 item 2... no, R1 nit,
flagged again in R2 follow-up).
TestWaitForDatabaseVisible_ReturnsBoundedErrorWhenNeverCreated pins the
bounded-failure contract as a falsifiability/regression guard, not an
old-vs-new discriminator: a name that is never created is a permanent
absence the old USE-based poll's retry branch already handled correctly by
coincidence. R2 itself found the real race too flaky to assert on directly
("a mutation probe... did not discriminate either, because the
fresh-connection test is flaky against its own 45s deadline"), so this test
is exactly what R2 prescribed instead. Its doc comment is corrected to say
so accurately.
Verified against a real Dolt container (TESTCONTAINERS_RYUK_DISABLED=true —
without it, Ryuk's reaper handshake times out on this host and produces a
container-setup failure that looks like a test failure but isn't):
internal/testutil full package green (25.9s), internal/storage/dolt's 5/5
visibility suite green (17.1s).
refs be-2wf6u
* chore(bazel): sync gazelle srcs/deps for PR #5788 test files
Maintainer fixup during adoption. `make bazel-sync` output only:
- internal/storage/dolt: enumerate the PR's new untagged setup_shared_db_test.go
in go_test srcs (without it Bazel silently runs 0 of the PR's 5 new tests).
- internal/testutil: gazelle-canonical deps for the two non-stdlib imports the
PR adds to the already-enumerated testdoltbranch_test.go.
Base parity at 4e4ebdb44 is 0 untagged gaps / 100 of 100 packages, so the
omission is PR-introduced. Idempotent on re-run.
* fix(testutil): review fixes for PR #5788 — firewall pin + doc accuracy
Maintainer fixup applying the iteration-1 review synthesis (4 of its 5
findings; all minor/nit, no blockers or majors).
- setup_shared_db_test.go: narrow TestSetupSharedTestDB_RefusesProductionPort
to the one token only the firewall emits ("production") and add a "dial tcp"
negative control. The old containsAny(err, "refused", "production", "3307")
was vacuous: the test carries no skipIfNoServer guard, so it also runs where
nothing listens on 3307, and there a deleted firewall falls through to
CREATE DATABASE and fails with "dial tcp 127.0.0.1:3307: connect: connection
refused" — a string matching both "refused" and "3307". Measured: the old
assertion is true for the firewall message AND that dial error; the new one
is true only for the firewall message. Same single-specific-needle pattern
as the sibling TestSetupSharedTestDB_RefusesAmbientPortMismatch.
- setup_shared_db_test.go: mark the be-nx7 DOLT_COMMIT diagnosis in the file
header as superseded by the catalog-refresh-race diagnosis — the wait
succeeds before any DOLT_COMMIT runs (initSharedSchema commits only after
SetupSharedTestDB returns), so a redundant post-CREATE DOLT_COMMIT is not
the completion of this fix.
- testdoltbranch.go: state the real bound. The deadline is checked before the
sleep, so the final capped delay lands past it — probes at
0/50/150/350/750/1550/3150/4750/6350/7950/9550/11150ms, i.e. ~11.2s
worst-case return over 12 probes, not the "~10s" the doc claimed. The cap
comment no longer asserts the total stays inside 10s.
- store.go: comment-only back-reference marking databaseExistsOnServer and
internal/testutil.databaseVisibleNow as a coupled mirror, so a future edit
to the consumer's predicate cannot silently diverge the fixture's probe.
The synthesis prescribed this arm over hoisting into doltutil precisely to
keep the PR's upstream-requested test-only narrowing (eea022b6e) intact; it
adds no production behavior.
Synthesis finding 3 (deleting the visibility wait is invisible to the
deterministic suite) carries "Required fix: None" — disclosed in-code and not
fixable without an injectable probe; left as recorded residual.
Validation: gofmt clean; go build + go vet on both packages; focused tests
green with the ambient BEADS_DOLT_* env scrubbed — internal/storage/dolt
5 PASS / 0 FAIL / 0 SKIP, internal/testutil 2 PASS / 0 FAIL / 0 SKIP.
make bazel-sync is a no-op (the added import is stdlib).
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-authored-by: Gas City Adopt-PR <adopt-pr@gascity.local>
This commit is contained in:
parent
ab88b20517
commit
a1ac9f4dcb
6 changed files with 520 additions and 0 deletions
|
|
@ -320,6 +320,7 @@ go_test(
|
|||
"search_counts_parity_test.go",
|
||||
"search_parity_test.go",
|
||||
"settlement_commit_boundary_test.go",
|
||||
"setup_shared_db_test.go",
|
||||
"shared_server_predicate_test.go",
|
||||
"shared_store_migrate_gate_test.go",
|
||||
"slot_commit_boundary_test.go",
|
||||
|
|
|
|||
350
internal/storage/dolt/setup_shared_db_test.go
Normal file
350
internal/storage/dolt/setup_shared_db_test.go
Normal file
|
|
@ -0,0 +1,350 @@
|
|||
package dolt
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"database/sql"
|
||||
"encoding/hex"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
|
||||
"github.com/steveyegge/beads/internal/storage/doltutil"
|
||||
"github.com/steveyegge/beads/internal/testutil"
|
||||
)
|
||||
|
||||
// Tests for the SetupSharedTestDB / initSharedSchema interaction surfaced by
|
||||
// be-s9d. The bug observed in the BEADS_TEST_EXTERNAL_DOLT_PORT path is that
|
||||
// after testutil.SetupSharedTestDB runs CREATE DATABASE, a freshly-opened
|
||||
// connection from dolt.New() — which dials the same Dolt server with the new
|
||||
// database in the DSN — fails with "Error 1049 (HY000): database not found".
|
||||
// The original diagnosis (be-nx7 reviewer notes): the CREATE DATABASE is left
|
||||
// in the working set without an explicit DOLT_COMMIT, so a fresh connection
|
||||
// that opens its own session does not see it. That mechanism is superseded by
|
||||
// the catalog-refresh-race diagnosis: the visibility wait added for be-s9d
|
||||
// succeeds BEFORE any DOLT_COMMIT runs (initSharedSchema commits only after
|
||||
// SetupSharedTestDB returns), so the invisibility is a transient window in
|
||||
// which Dolt has not yet registered the database in the server catalog — what
|
||||
// SHOW DATABASES and dolt.New() actually check — not a permanent working-set
|
||||
// state. Do not "complete" the fix with a redundant post-CREATE DOLT_COMMIT.
|
||||
//
|
||||
// These tests pin the behavioral contract: after SetupSharedTestDB returns,
|
||||
// the new database MUST be visible to a brand-new *sql.DB connection — both a
|
||||
// raw mysql driver connection (matching the dolt.New() shape) and a full
|
||||
// dolt.New() store. Both code paths exercise the same race; if the fix
|
||||
// regresses, these tests fail.
|
||||
|
||||
// uniqueSetupSharedDBName returns a per-test database name with a random
|
||||
// suffix so each test gets a fresh CREATE DATABASE without colliding with
|
||||
// the package-shared dolt_pkg_shared or with concurrent subtests.
|
||||
func uniqueSetupSharedDBName(t *testing.T) string {
|
||||
t.Helper()
|
||||
buf := make([]byte, 4)
|
||||
if _, err := rand.Read(buf); err != nil {
|
||||
t.Fatalf("rand.Read: %v", err)
|
||||
}
|
||||
return "test_setupshared_" + hex.EncodeToString(buf)
|
||||
}
|
||||
|
||||
// TestSetupSharedTestDB_FreshRawConnSeesDatabase verifies that a brand-new
|
||||
// *sql.DB connection sees the new database in SHOW DATABASES immediately
|
||||
// after SetupSharedTestDB returns.
|
||||
//
|
||||
// SHOW DATABASES (not USE) is the canonical visibility check: dolt.New()
|
||||
// guards CREATE-vs-open with a SHOW DATABASES probe (databaseExistsOnServer
|
||||
// in store.go), and the be-s9d failure mode is that SHOW DATABASES on a
|
||||
// fresh connection does NOT list the just-created database, so dolt.New()
|
||||
// returns "database not found".
|
||||
//
|
||||
// USE <db> on a fresh connection appears to succeed via the mysql driver
|
||||
// (Dolt may auto-attach the database in a session even when the catalog
|
||||
// hasn't registered it), so a Ping-based test is not strict enough — we
|
||||
// hit SHOW DATABASES directly.
|
||||
func TestSetupSharedTestDB_FreshRawConnSeesDatabase(t *testing.T) {
|
||||
skipIfNoServer(t)
|
||||
|
||||
dbName := uniqueSetupSharedDBName(t)
|
||||
|
||||
setupConn, err := testutil.SetupSharedTestDB(testServerPort, dbName)
|
||||
if err != nil {
|
||||
t.Fatalf("SetupSharedTestDB(%q): %v", dbName, err)
|
||||
}
|
||||
t.Cleanup(func() { _ = setupConn.Close() })
|
||||
t.Cleanup(func() { dropTestDatabase(t, testServerPort, dbName) })
|
||||
|
||||
// Open a fresh *sql.DB WITHOUT the database in the DSN — this is the
|
||||
// shape of dolt.New()'s initDB connection that runs SHOW DATABASES.
|
||||
dsn := doltutil.ServerDSN{
|
||||
Host: "127.0.0.1",
|
||||
Port: testServerPort,
|
||||
User: "root",
|
||||
Timeout: 5 * time.Second,
|
||||
}.String()
|
||||
freshDB, err := sql.Open("mysql", dsn)
|
||||
if err != nil {
|
||||
t.Fatalf("sql.Open: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = freshDB.Close() })
|
||||
freshDB.SetMaxOpenConns(1)
|
||||
|
||||
rows, err := freshDB.QueryContext(context.Background(), "SHOW DATABASES")
|
||||
if err != nil {
|
||||
t.Fatalf("SHOW DATABASES on fresh connection: %v", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var found bool
|
||||
var seen []string
|
||||
for rows.Next() {
|
||||
var n string
|
||||
if err := rows.Scan(&n); err != nil {
|
||||
t.Fatalf("scan: %v", err)
|
||||
}
|
||||
seen = append(seen, n)
|
||||
if n == dbName {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
t.Fatalf("rows.Err: %v", err)
|
||||
}
|
||||
|
||||
if !found {
|
||||
t.Fatalf("SHOW DATABASES on fresh connection did not list %q\n databases visible: %v\n SetupSharedTestDB created the database but it is not in a fresh session's catalog — this is the be-s9d bug",
|
||||
dbName, seen)
|
||||
}
|
||||
}
|
||||
|
||||
// TestSetupSharedTestDB_NewStoreOpens verifies that the canonical
|
||||
// initSharedSchema flow — call SetupSharedTestDB, then dolt.New() against
|
||||
// the freshly-created database — succeeds. This is exactly the sequence
|
||||
// testmain_test.go runs in the BEADS_TEST_EXTERNAL_DOLT_PORT branch where
|
||||
// the bug surfaces.
|
||||
func TestSetupSharedTestDB_NewStoreOpens(t *testing.T) {
|
||||
skipIfNoServer(t)
|
||||
|
||||
dbName := uniqueSetupSharedDBName(t)
|
||||
|
||||
setupConn, err := testutil.SetupSharedTestDB(testServerPort, dbName)
|
||||
if err != nil {
|
||||
t.Fatalf("SetupSharedTestDB(%q): %v", dbName, err)
|
||||
}
|
||||
t.Cleanup(func() { _ = setupConn.Close() })
|
||||
t.Cleanup(func() { dropTestDatabase(t, testServerPort, dbName) })
|
||||
|
||||
ctx, cancel := testContext(t)
|
||||
defer cancel()
|
||||
|
||||
cfg := &Config{
|
||||
Path: t.TempDir(),
|
||||
ServerHost: "127.0.0.1",
|
||||
ServerPort: testServerPort,
|
||||
Database: dbName,
|
||||
MaxOpenConns: 1,
|
||||
// CreateIfMissing intentionally false: SetupSharedTestDB already
|
||||
// created the database. If New errors with "database not found"
|
||||
// here, the be-s9d race reproduced.
|
||||
}
|
||||
|
||||
store, err := New(ctx, cfg)
|
||||
if err != nil {
|
||||
t.Fatalf("New on fresh database %q: %v\n SetupSharedTestDB created the database but dolt.New cannot see it (be-s9d)",
|
||||
dbName, err)
|
||||
}
|
||||
t.Cleanup(func() { _ = store.Close() })
|
||||
|
||||
// SetConfig exercises an actual write through the store, which forces
|
||||
// the new connection to USE <db> and execute. If the database is
|
||||
// "found" but the working set is malformed, this will fail.
|
||||
if err := store.SetConfig(ctx, "issue_prefix", "setupshared_test"); err != nil {
|
||||
t.Fatalf("SetConfig on fresh store: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestSetupSharedTestDB_FreshConnAfterSecondSetupCall verifies that a second
|
||||
// SetupSharedTestDB call against the SAME database name (idempotent path)
|
||||
// does not break visibility for fresh connections. SetupSharedTestDB already
|
||||
// uses CREATE DATABASE IF NOT EXISTS and tolerates Dolt's error 1007, but if
|
||||
// the fix introduces side effects (e.g., DOLT_COMMIT on every call), they
|
||||
// must not regress the second-call shape.
|
||||
func TestSetupSharedTestDB_FreshConnAfterSecondSetupCall(t *testing.T) {
|
||||
skipIfNoServer(t)
|
||||
|
||||
dbName := uniqueSetupSharedDBName(t)
|
||||
|
||||
first, err := testutil.SetupSharedTestDB(testServerPort, dbName)
|
||||
if err != nil {
|
||||
t.Fatalf("first SetupSharedTestDB: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = first.Close() })
|
||||
t.Cleanup(func() { dropTestDatabase(t, testServerPort, dbName) })
|
||||
|
||||
second, err := testutil.SetupSharedTestDB(testServerPort, dbName)
|
||||
if err != nil {
|
||||
t.Fatalf("second SetupSharedTestDB on existing %q: %v", dbName, err)
|
||||
}
|
||||
t.Cleanup(func() { _ = second.Close() })
|
||||
|
||||
dsn := doltutil.ServerDSN{
|
||||
Host: "127.0.0.1",
|
||||
Port: testServerPort,
|
||||
User: "root",
|
||||
Timeout: 5 * time.Second,
|
||||
}.String()
|
||||
freshDB, err := sql.Open("mysql", dsn)
|
||||
if err != nil {
|
||||
t.Fatalf("sql.Open: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = freshDB.Close() })
|
||||
freshDB.SetMaxOpenConns(1)
|
||||
|
||||
rows, err := freshDB.QueryContext(context.Background(), "SHOW DATABASES")
|
||||
if err != nil {
|
||||
t.Fatalf("SHOW DATABASES after second SetupSharedTestDB: %v", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var found bool
|
||||
for rows.Next() {
|
||||
var n string
|
||||
if err := rows.Scan(&n); err != nil {
|
||||
t.Fatalf("scan: %v", err)
|
||||
}
|
||||
if n == dbName {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
t.Fatalf("rows.Err: %v", err)
|
||||
}
|
||||
if !found {
|
||||
t.Fatalf("SHOW DATABASES did not list %q after second SetupSharedTestDB call; idempotency must not regress visibility",
|
||||
dbName)
|
||||
}
|
||||
}
|
||||
|
||||
// TestSetupSharedTestDB_RefusesProductionPort verifies the FIREWALL guard
|
||||
// in SetupSharedTestDB: passing the production port (DefaultSQLPort) must
|
||||
// fail with a clear refusal message and must not open a connection. This is
|
||||
// a defensive contract — the fix in be-s9d touches SetupSharedTestDB and
|
||||
// the firewall must remain intact through any refactor.
|
||||
//
|
||||
// The assertion deliberately matches only "production", the one token the
|
||||
// firewall itself emits. Accepting "refused" or "3307" as well would make
|
||||
// this test vacuous: it carries no skipIfNoServer guard, so it also runs on
|
||||
// hosts with nothing listening on 3307, and there a deleted firewall falls
|
||||
// through to CREATE DATABASE and fails with
|
||||
// "dial tcp 127.0.0.1:3307: connect: connection refused" — a string that
|
||||
// contains both of those needles. Same single-specific-needle pattern as
|
||||
// TestSetupSharedTestDB_RefusesAmbientPortMismatch in
|
||||
// internal/testutil/testdoltbranch_test.go.
|
||||
func TestSetupSharedTestDB_RefusesProductionPort(t *testing.T) {
|
||||
conn, err := testutil.SetupSharedTestDB(DefaultSQLPort, "should_never_be_created")
|
||||
if err == nil {
|
||||
if conn != nil {
|
||||
_ = conn.Close()
|
||||
}
|
||||
t.Fatal("SetupSharedTestDB on production port returned no error; the firewall is broken")
|
||||
}
|
||||
if conn != nil {
|
||||
_ = conn.Close()
|
||||
t.Errorf("SetupSharedTestDB on production port returned a non-nil *sql.DB; firewall must close any handle before returning the error")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "production") {
|
||||
t.Errorf("error message should clearly refuse the production port, got: %v", err)
|
||||
}
|
||||
if strings.Contains(err.Error(), "dial tcp") {
|
||||
t.Errorf("refusal must come from the firewall, not from dialing the port, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestInitSharedSchema_AfterFreshDatabase verifies the initSharedSchema flow
|
||||
// against a freshly-created database. This is the higher-level integration
|
||||
// test: it exercises the same TestMain-time call sequence that fails in the
|
||||
// BEADS_TEST_EXTERNAL_DOLT_PORT branch, but driven from a regular test so
|
||||
// the failure is captured by `go test` rather than a fatal-out-of-TestMain
|
||||
// stderr message.
|
||||
//
|
||||
// Note: the production initSharedSchema closes over the package-level
|
||||
// testSharedDB. This test temporarily redirects testSharedDB to a unique
|
||||
// per-test name so it does not collide with the suite-wide shared DB.
|
||||
func TestInitSharedSchema_AfterFreshDatabase(t *testing.T) {
|
||||
skipIfNoServer(t)
|
||||
|
||||
dbName := uniqueSetupSharedDBName(t)
|
||||
|
||||
// SetupSharedTestDB does the CREATE DATABASE.
|
||||
setupConn, err := testutil.SetupSharedTestDB(testServerPort, dbName)
|
||||
if err != nil {
|
||||
t.Fatalf("SetupSharedTestDB: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = setupConn.Close() })
|
||||
t.Cleanup(func() { dropTestDatabase(t, testServerPort, dbName) })
|
||||
|
||||
// initSharedSchema reads the package-level testSharedDB, so we must
|
||||
// redirect it for the duration of this test. No goroutines from
|
||||
// previous tests should still be calling initSharedSchema, but we
|
||||
// restore the original value to be safe.
|
||||
prevDB := testSharedDB
|
||||
testSharedDB = dbName
|
||||
t.Cleanup(func() { testSharedDB = prevDB })
|
||||
|
||||
if err := initSharedSchema(testServerPort); err != nil {
|
||||
t.Fatalf("initSharedSchema on fresh database %q: %v\n this is the literal failure mode reported in be-s9d",
|
||||
dbName, err)
|
||||
}
|
||||
|
||||
// After initSharedSchema succeeds, the schema must be visible from
|
||||
// yet another fresh connection — captures the case where DOLT_COMMIT
|
||||
// in initSharedSchema fixed up visibility for that connection only,
|
||||
// not for subsequent ones. Use a quick SHOW TABLES probe.
|
||||
dsn := doltutil.ServerDSN{
|
||||
Host: "127.0.0.1",
|
||||
Port: testServerPort,
|
||||
User: "root",
|
||||
Database: dbName,
|
||||
Timeout: 5 * time.Second,
|
||||
}.String()
|
||||
freshDB, err := sql.Open("mysql", dsn)
|
||||
if err != nil {
|
||||
t.Fatalf("sql.Open: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = freshDB.Close() })
|
||||
|
||||
rows, err := freshDB.QueryContext(context.Background(), "SHOW TABLES")
|
||||
if err != nil {
|
||||
t.Fatalf("SHOW TABLES on fresh connection after initSharedSchema: %v", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var names []string
|
||||
for rows.Next() {
|
||||
var n string
|
||||
if err := rows.Scan(&n); err != nil {
|
||||
t.Fatalf("scan: %v", err)
|
||||
}
|
||||
names = append(names, n)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
t.Fatalf("rows.Err: %v", err)
|
||||
}
|
||||
if len(names) == 0 {
|
||||
t.Fatalf("SHOW TABLES returned no tables on fresh connection; initSharedSchema did not commit the schema visibly\n (got %d rows; expected at least the issues table)",
|
||||
len(names))
|
||||
}
|
||||
|
||||
// issues is the canonical table the schema migrations create; its
|
||||
// presence proves the schema commit landed for fresh sessions.
|
||||
hasIssues := false
|
||||
for _, n := range names {
|
||||
if n == "issues" {
|
||||
hasIssues = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !hasIssues {
|
||||
t.Fatalf("SHOW TABLES did not include 'issues': %v", names)
|
||||
}
|
||||
}
|
||||
|
|
@ -2922,6 +2922,14 @@ func serverEndpointIdentity(cfg *Config) string {
|
|||
// databaseExistsOnServer checks if a database with the exact given name exists
|
||||
// on the Dolt server. Uses SHOW DATABASES + iterate instead of SHOW DATABASES LIKE
|
||||
// to avoid LIKE wildcard issues with underscores in database names.
|
||||
//
|
||||
// COUPLED MIRROR: internal/testutil.databaseVisibleNow is a deliberate copy of
|
||||
// this predicate (testutil cannot import this package without a cycle), and
|
||||
// SetupSharedTestDB's visibility wait polls that copy so that once the fixture
|
||||
// observes the database, this check — the one dolt.New() depends on — observes
|
||||
// it too. Changing the predicate here (information_schema, name normalization,
|
||||
// …) without updating the mirror silently reintroduces the be-s9d race, and no
|
||||
// test pins the equivalence.
|
||||
func databaseExistsOnServer(ctx context.Context, db *sql.DB, name string) (bool, error) {
|
||||
rows, err := db.QueryContext(ctx, "SHOW DATABASES")
|
||||
if err != nil {
|
||||
|
|
|
|||
|
|
@ -116,6 +116,10 @@ go_test(
|
|||
],
|
||||
data = glob(["testdata/**"]),
|
||||
embed = [":testutil"],
|
||||
deps = [
|
||||
"//internal/storage/doltutil",
|
||||
"@com_github_go_sql_driver_mysql//:mysql",
|
||||
],
|
||||
)
|
||||
|
||||
# Dolt image pin read as text by //test/docsync and the dolt pin tests in
|
||||
|
|
|
|||
|
|
@ -178,6 +178,17 @@ func SetupSharedTestDB(port int, dbName string) (*sql.DB, error) {
|
|||
}
|
||||
}
|
||||
|
||||
// Wait for the new database to become visible on this connection before
|
||||
// returning — not a guarantee for every connection in db's pool (see
|
||||
// waitForDatabaseVisible's doc). Without this, a sibling pool from
|
||||
// dolt.New() — opened inside initSharedSchema — can race the Dolt
|
||||
// server's catalog refresh and fail with "Error 1049 (HY000): database
|
||||
// not found: <dbName>" (be-nx7 external-port path).
|
||||
if err := waitForDatabaseVisible(ctx, db, dbName); err != nil {
|
||||
_ = db.Close()
|
||||
return nil, fmt.Errorf("SetupSharedTestDB: wait for visibility: %w", err)
|
||||
}
|
||||
|
||||
// Switch to the database and clean stale branches
|
||||
CleanTestBranches(db, dbName)
|
||||
|
||||
|
|
@ -319,3 +330,68 @@ func commitAllowEmpty(ctx context.Context, db doltBranchSQL, message string) err
|
|||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// waitForDatabaseVisible polls SHOW DATABASES on db with exponential backoff
|
||||
// until dbName appears via exact-match iteration — the same check
|
||||
// databaseExistsOnServer (internal/storage/dolt/store.go) uses, and the one
|
||||
// dolt.New() actually depends on. Success means the connection that ran the
|
||||
// query has observed the server's catalog refresh for dbName — not a
|
||||
// guarantee for every connection in db's pool. Polling is bounded to ~10s —
|
||||
// far longer than any catalog-refresh window observed in practice — and the
|
||||
// deadline is checked before the sleep, so the call returns within ~11.2s
|
||||
// worst case (12 probes: the last capped delay lands past the bound). Either
|
||||
// way a real failure surfaces a clear error instead of hanging the test
|
||||
// binary.
|
||||
func waitForDatabaseVisible(ctx context.Context, db *sql.DB, dbName string) error {
|
||||
const maxElapsed = 10 * time.Second
|
||||
deadline := time.Now().Add(maxElapsed)
|
||||
delay := 50 * time.Millisecond
|
||||
for {
|
||||
found, err := databaseVisibleNow(ctx, db, dbName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if found {
|
||||
return nil
|
||||
}
|
||||
if time.Now().After(deadline) {
|
||||
return fmt.Errorf("database %q not visible after %s", dbName, maxElapsed)
|
||||
}
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
case <-time.After(delay):
|
||||
}
|
||||
// Cap is checked before doubling, so delay can reach ~1.6s (not 1s)
|
||||
// on the iteration that crosses the threshold. The deadline is
|
||||
// checked before this sleep, so that last delay runs past the ~10s
|
||||
// polling bound rather than inside it — harmless, and it is why the
|
||||
// doc above states a ~11.2s worst-case return.
|
||||
if delay < time.Second {
|
||||
delay *= 2
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// databaseVisibleNow reports whether dbName appears in SHOW DATABASES, via
|
||||
// exact-match iteration — mirrors databaseExistsOnServer in
|
||||
// internal/storage/dolt/store.go. SHOW DATABASES LIKE has wildcard issues
|
||||
// with underscores in database names, which test database names contain.
|
||||
func databaseVisibleNow(ctx context.Context, db *sql.DB, dbName string) (bool, error) {
|
||||
rows, err := db.QueryContext(ctx, "SHOW DATABASES")
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
var name string
|
||||
if err := rows.Scan(&name); err != nil {
|
||||
return false, err
|
||||
}
|
||||
if name == dbName {
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
return false, rows.Err()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,16 @@
|
|||
package testutil
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"database/sql"
|
||||
"encoding/hex"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
_ "github.com/go-sql-driver/mysql" // MySQL driver for direct DB connections
|
||||
"github.com/steveyegge/beads/internal/storage/doltutil"
|
||||
)
|
||||
|
||||
// TestSetupSharedTestDB_RefusesAmbientPortMismatch guards the same class of
|
||||
|
|
@ -27,3 +35,76 @@ func TestSetupSharedTestDB_RefusesAmbientPortMismatch(t *testing.T) {
|
|||
t.Errorf("SetupSharedTestDB error = %q, want it to name the ambient BEADS_DOLT_SERVER_PORT disagreement", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// neverCreatedDBName returns a per-test database name with a random suffix
|
||||
// that is guaranteed not to collide with any database CREATEd elsewhere in
|
||||
// the suite — mirrors uniqueSetupSharedDBName in
|
||||
// internal/storage/dolt/setup_shared_db_test.go (a different package, so not
|
||||
// directly reusable here).
|
||||
func neverCreatedDBName(t *testing.T) string {
|
||||
t.Helper()
|
||||
buf := make([]byte, 4)
|
||||
if _, err := rand.Read(buf); err != nil {
|
||||
t.Fatalf("rand.Read: %v", err)
|
||||
}
|
||||
return "test_never_created_" + hex.EncodeToString(buf)
|
||||
}
|
||||
|
||||
// TestWaitForDatabaseVisible_ReturnsBoundedErrorWhenNeverCreated pins the
|
||||
// bounded-failure contract that makes the visibility-probe fix falsifiable
|
||||
// (bee-ghosttrack R2 item 1): for a database name that is never CREATEd,
|
||||
// waitForDatabaseVisible must return the "not visible after" error within a
|
||||
// bound close to its documented ~10s deadline — it must neither hang nor
|
||||
// silently report success.
|
||||
//
|
||||
// This is a falsifiability guard, not an old-vs-new discriminator for the
|
||||
// bug it replaces: a name that is never created is a *permanent* absence,
|
||||
// and the old USE-based poll's "unknown database" retry branch already
|
||||
// handled that correctly by coincidence (measured ~13s, correct error). The
|
||||
// actual bug only showed up in the *racy* just-created case — USE can
|
||||
// return success on attempt 1 because Dolt sometimes auto-attaches a
|
||||
// database to a session before the server catalog (what SHOW DATABASES and
|
||||
// dolt.New() actually check) has registered it — and R2 found that race too
|
||||
// flaky to assert on directly (a mutation-probe experiment against the
|
||||
// fresh-connection test was non-deterministic, not reliably red). What this
|
||||
// test does guard is a future regression: an implementation that
|
||||
// unconditionally reports success would fail it, even though the USE-based
|
||||
// poll it replaces does not.
|
||||
func TestWaitForDatabaseVisible_ReturnsBoundedErrorWhenNeverCreated(t *testing.T) {
|
||||
RequireDoltContainer(t)
|
||||
|
||||
dsn := doltutil.ServerDSN{Host: "127.0.0.1", Port: DoltContainerPortInt(), User: "root", Timeout: 10 * time.Second}.String()
|
||||
db, err := sql.Open("mysql", dsn)
|
||||
if err != nil {
|
||||
t.Fatalf("sql.Open: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = db.Close() })
|
||||
|
||||
dbName := neverCreatedDBName(t)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
||||
defer cancel()
|
||||
|
||||
const hangBound = 15 * time.Second
|
||||
start := time.Now()
|
||||
done := make(chan error, 1)
|
||||
go func() {
|
||||
done <- waitForDatabaseVisible(ctx, db, dbName)
|
||||
}()
|
||||
|
||||
select {
|
||||
case err := <-done:
|
||||
elapsed := time.Since(start)
|
||||
if err == nil {
|
||||
t.Fatalf("waitForDatabaseVisible(%q): got nil error after %s for a database that was never created — visibility check is not strict enough (silent success)", dbName, elapsed)
|
||||
}
|
||||
if !strings.Contains(err.Error(), "not visible after") {
|
||||
t.Errorf("waitForDatabaseVisible(%q) error = %q, want it to contain %q (the bounded-deadline error)", dbName, err.Error(), "not visible after")
|
||||
}
|
||||
if elapsed > hangBound {
|
||||
t.Errorf("waitForDatabaseVisible(%q) took %s to return, want it bounded near the documented ~10s deadline", dbName, elapsed)
|
||||
}
|
||||
case <-time.After(hangBound):
|
||||
t.Fatalf("waitForDatabaseVisible(%q): did not return within %s — appears to hang instead of returning a bounded error", dbName, hangBound)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue