carlos / platform Public

tests: serialise local-replica writes in the continuity tests

CI caught TestHAReplicateAcrossGapRestoresWhole restoring a corrupt
snapshot: the start's snapshot and the store's own first snapshot wrote
the same file together, and litestream's file client shares one
temporary name (S3 takes each PUT whole). The continuity tests' local
replicas now take writes one at a time. 5/5 runs of the eleven tests.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 3c712c387e396dc7c6425059992115d8b1d33e8c parent 1218431
4 files changed, +51 −13
  • internal/lease/continuity_round3_test.go +4 −5
  • internal/lease/continuity_test.go +24 −3
  • internal/lease/ha_continuity_test.go +22 −2
  • internal/lease/protect_test.go +1 −3

CI failed — run details

diff --git a/internal/lease/continuity_round3_test.go b/internal/lease/continuity_round3_test.go
index 2569008f9..76bcfaefb 100644
--- a/internal/lease/continuity_round3_test.go
+++ b/internal/lease/continuity_round3_test.go
@@ -12,7 +12,6 @@ import (
"time"
"github.com/benbjohnson/litestream"
- lsfile "github.com/benbjohnson/litestream/file"
"github.com/superfly/ltx"
)
@@ -71,7 +70,7 @@ func TestFullImageBudgetScalesWithDatabaseSize(t *testing.T) {
if fi, err := os.Stat(g.path); err != nil || fi.Size() < 4<<20 {
t.Fatalf("the database is not large enough to need its size's time: %v %v", fi, err)
}
- slow := &slowSnapshots{ReplicaClient: lsfile.NewReplicaClient(g.replica), delay: 500 * time.Millisecond}
+ slow := &slowSnapshots{ReplicaClient: serialFile(g.replica), delay: 500 * time.Millisecond}
if err := g.replicateAndStop(g.held(slow), app, "slow store wake"); err != nil {
t.Fatal(err)
}
@@ -201,7 +200,7 @@ func TestFullImageUploadIsConfirmedBeforeServing(t *testing.T) {
t.Run("first start", func(t *testing.T) {
g := newGapRig(t)
app := openWakeApp(t, g.path)
- bad := refusingUploads{lsfile.NewReplicaClient(g.replica)}
+ bad := refusingUploads{serialFile(g.replica)}
if err := g.replicateAndStop(g.held(bad), app, "unuploaded"); err == nil {
t.Fatal("Replicate returned before its first image was uploaded")
}
@@ -216,7 +215,7 @@ func TestFullImageUploadIsConfirmedBeforeServing(t *testing.T) {
t.Fatalf("the WAL is still there: %v", err)
}
app = openWakeApp(t, g.path)
- bad := refusingUploads{lsfile.NewReplicaClient(g.replica)}
+ bad := refusingUploads{serialFile(g.replica)}
if err := g.replicateAndStop(g.held(bad), app, "unuploaded"); err == nil {
t.Fatal("Replicate returned before the full image was uploaded")
}
@@ -252,7 +251,7 @@ func TestPinnedReplicateRetriesATransientReplicaRead(t *testing.T) {
t.Fatal("not continuous after a clean close")
}
app := openWakeApp(t, g.path)
- flaky := &flakyListing{ReplicaClient: lsfile.NewReplicaClient(g.replica)}
+ flaky := &flakyListing{ReplicaClient: serialFile(g.replica)}
flaky.n.Store(2)
if err := g.replicateAndStop(g.held(flaky), app, "retried"); err != nil {
t.Fatalf("a pinned start failed on a transient listing error: %v", err)
diff --git a/internal/lease/continuity_test.go b/internal/lease/continuity_test.go
index 21581da76..36afa2d97 100644
--- a/internal/lease/continuity_test.go
+++ b/internal/lease/continuity_test.go
@@ -9,6 +9,7 @@ import (
"os"
"path/filepath"
"strings"
+ "sync"
"sync/atomic"
"testing"
"time"
@@ -60,7 +61,7 @@ func (g *gapRig) held(client litestream.ReplicaClient) *Held {
g.t.Helper()
h := localHeld(g.t, g.backend, 10*time.Second)
if client == nil {
- client = lsfile.NewReplicaClient(g.replica)
+ client = serialFile(g.replica)
}
h.replicaClient = client
return h
@@ -174,6 +175,26 @@ func assertRestoresWhole(t *testing.T, replica string, rows int, last string) {
}
}
+// serialWrites is a local replica whose writes go one at a time. A
+// start's snapshot and the store's own first snapshot can write the same
+// file at the same moment; S3 takes each PUT whole, but litestream's file
+// client shares one temporary name, and two writers interleave in it.
+type serialWrites struct {
+ litestream.ReplicaClient
+ mu sync.Mutex
+}
+
+func (c *serialWrites) WriteLTXFile(ctx context.Context, level int, lo, hi ltx.TXID, r io.Reader) (*ltx.FileInfo, error) {
+ c.mu.Lock()
+ defer c.mu.Unlock()
+ return c.ReplicaClient.WriteLTXFile(ctx, level, lo, hi, r)
+}
+
+// serialFile is a local replica in dir with serialWrites.
+func serialFile(dir string) litestream.ReplicaClient {
+ return &serialWrites{ReplicaClient: lsfile.NewReplicaClient(dir)}
+}
+
// bigMigration trips SQLite's 1000-frame autocheckpoint.
const bigMigration = 20000
@@ -222,7 +243,7 @@ func TestGapRepairFailureIsRetried(t *testing.T) {
g := newGapRig(t)
g.wake("first wake")
app := g.migrate(bigMigration)
- bad := &failingSnapshots{ReplicaClient: lsfile.NewReplicaClient(g.replica)}
+ bad := &failingSnapshots{ReplicaClient: serialFile(g.replica)}
bad.fail.Store(true)
if err := g.replicateAndStop(g.held(bad), app, "refused wake"); err == nil {
t.Fatal("Replicate served across a gap whose snapshot failed")
@@ -261,7 +282,7 @@ func TestGapRepairTimeoutFailsReplicateAndIsRetried(t *testing.T) {
g := newGapRig(t)
g.wake("first wake")
app := g.migrate(bigMigration)
- slow := &failingSnapshots{ReplicaClient: lsfile.NewReplicaClient(g.replica)}
+ slow := &failingSnapshots{ReplicaClient: serialFile(g.replica)}
slow.hang.Store(true)
start := time.Now()
err := g.replicateAndStop(g.held(slow), app, "timed out wake")
diff --git a/internal/lease/ha_continuity_test.go b/internal/lease/ha_continuity_test.go
index 99c557316..fbca64840 100644
--- a/internal/lease/ha_continuity_test.go
+++ b/internal/lease/ha_continuity_test.go
@@ -1,14 +1,18 @@
package lease_test
import (
+ "context"
"database/sql"
"fmt"
+ "io"
"path/filepath"
"strings"
+ "sync"
"testing"
"github.com/benbjohnson/litestream"
lsfile "github.com/benbjohnson/litestream/file"
+ "github.com/superfly/ltx"
"github.com/carlosframework/platform/internal/hafresh"
"github.com/carlosframework/platform/internal/lease"
@@ -23,13 +27,17 @@ func TestHAReplicateAcrossGapRestoresWhole(t *testing.T) {
fx := leasetest.NewLockFixture(t)
replica := t.TempDir()
path := tempDB(t)
+ // One client for both wakes, its writes one at a time: the start's
+ // snapshot and the store's own first snapshot can write the same file
+ // together, which S3 takes whole and litestream's file client does not.
+ client := &serialClient{ReplicaClient: lsfile.NewReplicaClient(replica)}
// A first wake under an ordinary lease, closed cleanly.
h, err := lease.Acquire(ctx, fx.Bucket, "instances/first", "")
if err != nil {
t.Fatal(err)
}
- lease.SetReplicaClientForTest(h, lsfile.NewReplicaClient(replica))
+ lease.SetReplicaClientForTest(h, client)
app := openApp(t, path)
r, err := h.Replicate(ctx, path, 0)
if err != nil {
@@ -68,7 +76,7 @@ func TestHAReplicateAcrossGapRestoresWhole(t *testing.T) {
if err != nil {
t.Fatal(err)
}
- lease.SetReplicaClientForTest(ha, lsfile.NewReplicaClient(replica))
+ lease.SetReplicaClientForTest(ha, client)
hr, err := ha.Replicate(ctx, path, 0)
if err != nil {
t.Fatal(err)
@@ -125,3 +133,15 @@ func execOn(t *testing.T, d *sql.DB, q string) {
t.Fatalf("%s: %v", q, err)
}
}
+
+// serialClient is a replica client whose writes go one at a time.
+type serialClient struct {
+ litestream.ReplicaClient
+ mu sync.Mutex
+}
+
+func (c *serialClient) WriteLTXFile(ctx context.Context, level int, lo, hi ltx.TXID, r io.Reader) (*ltx.FileInfo, error) {
+ c.mu.Lock()
+ defer c.mu.Unlock()
+ return c.ReplicaClient.WriteLTXFile(ctx, level, lo, hi, r)
+}
diff --git a/internal/lease/protect_test.go b/internal/lease/protect_test.go
index 59e3f9c5d..27ec08d22 100644
--- a/internal/lease/protect_test.go
+++ b/internal/lease/protect_test.go
@@ -2,8 +2,6 @@ package lease
import (
"testing"
-
- lsfile "github.com/benbjohnson/litestream/file"
)
// The start pin: taken before the process starts, it keeps every frame the
@@ -20,7 +18,7 @@ func TestProtectStartKeepsBootWritesWithoutSnapshot(t *testing.T) {
t.Fatal("a database its clean close left untouched is not continuous")
}
app := g.migrate(bigMigration)
- refuse := &failingSnapshots{ReplicaClient: lsfile.NewReplicaClient(g.replica)}
+ refuse := &failingSnapshots{ReplicaClient: serialFile(g.replica)}
refuse.fail.Store(true)
if err := g.replicateAndStop(g.held(refuse), app, "pinned wake"); err != nil {
t.Fatalf("a pinned wake needed a snapshot: %v", err)