Files
minstrel/internal/db/wait_test.go
T
bvandeusenandClaude Opus 5.5 040b90898b
release / web (push) Successful in 1m12s
release / go (push) Failing after 1m12s
release / govulncheck (push) Successful in 16s
release / integration (push) Successful in 3m49s
release / android (push) Successful in 4m28s
release / Build signed APK (push) Successful in 5m54s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Skipped
release / Verify release artifacts (tag releases only) (push) Skipped
fix: Minstrel waits for its database at startup instead of exiting
A first docker compose up starts Minstrel and a new Postgres together, and
the database spends several seconds initialising before it takes
connections. Minstrel tried once and exited, and with no restart policy
in the quick-start compose it stayed down. It now retries every two
seconds for up to two minutes, logging each attempt, and only then exits
with the last connection error.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 09:46:19 -05:00

61 lines
1.6 KiB
Go

package db
import (
"context"
"errors"
"io"
"log/slog"
"testing"
"time"
)
var quietLogger = slog.New(slog.NewTextHandler(io.Discard, nil))
func TestWaitFor_RetriesUntilTheDatabaseAnswers(t *testing.T) {
calls := 0
err := waitFor(context.Background(), quietLogger, time.Millisecond, time.Second, func(context.Context) error {
calls++
if calls < 3 {
return errors.New("connection refused")
}
return nil
})
if err != nil {
t.Fatalf("waitFor = %v, want nil once the third attempt connects", err)
}
if calls != 3 {
t.Errorf("attempts = %d, want 3 (two refusals, then success)", calls)
}
}
func TestWaitFor_GivesUpWithTheLastError(t *testing.T) {
refused := errors.New("password authentication failed")
err := waitFor(context.Background(), quietLogger, time.Millisecond, 20*time.Millisecond, func(context.Context) error {
return refused
})
if !errors.Is(err, refused) {
t.Fatalf("waitFor = %v, want it to carry the last connection error", err)
}
}
func TestWaitFor_StopsWhenShutdownBegins(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
start := time.Now()
err := waitFor(ctx, quietLogger, time.Hour, time.Hour, func(context.Context) error {
return errors.New("connection refused")
})
if !errors.Is(err, context.Canceled) {
t.Fatalf("waitFor = %v, want context.Canceled", err)
}
if time.Since(start) > time.Second {
t.Errorf("waitFor took %s after cancel, want an immediate return", time.Since(start))
}
}
func TestWaitReady_EmptyURLIsAnErrorNotAWait(t *testing.T) {
if err := WaitReady(context.Background(), "", quietLogger); err == nil {
t.Fatal("WaitReady(\"\") = nil, want an error")
}
}