| new file mode 100644 |
| index 000000000..ab9ac6d2f |
| --- /dev/null |
| +++ b/internal/console/backups_test.go |
| @@ -0,0 +1,464 @@ |
| +package console |
| + |
| +import ( |
| + "bytes" |
| + "context" |
| + "encoding/json" |
| + "errors" |
| + "io" |
| + "path/filepath" |
| + "sync" |
| + "sync/atomic" |
| + "testing" |
| + "time" |
| + |
| + lsfile "github.com/benbjohnson/litestream/file" |
| + "github.com/superfly/ltx" |
| + |
| + "github.com/carlosframework/platform/internal/dbdoor" |
| + "github.com/carlosframework/platform/internal/demo" |
| + "github.com/carlosframework/platform/internal/provision" |
| + "github.com/carlosframework/platform/internal/regionlabel" |
| +) |
| + |
| +// backupsRegion is the console's region in most rows of backupsRows. |
| +const backupsRegion = "ie" |
| + |
| +// backupsRow is one record and the console region it is seen from. |
| +type backupsRow struct { |
| + name string |
| + rec provision.Record |
| + console string // the console's CARLOS_REGION |
| + class backupClass |
| + reason string |
| +} |
| + |
| +func backupsYes() *bool { v := true; return &v } |
| +func backupsNo() *bool { v := false; return &v } |
| + |
| +// backupsRows is one row per reason classifyBackups gives, plus the rows |
| +// where two facts meet, so the first one wins in the stated order. |
| +func backupsRows() []backupsRow { |
| + rec := func(slug string, mut func(*provision.Record)) provision.Record { |
| + r := provision.Record{Host: slug + ".go.tito.io", Region: backupsRegion, Hibernate: backupsYes()} |
| + if mut != nil { |
| + mut(&r) |
| + } |
| + return r |
| + } |
| + demoOf := func(r *provision.Record) { r.Demo = &demo.Descriptor{ID: "d1", Host: r.Host} } |
| + return []backupsRow{ |
| + {"replicated", rec("rep", nil), backupsRegion, backupReplicated, ""}, |
| + {"kind instance", rec("kin", func(r *provision.Record) { r.Kind = "instance" }), backupsRegion, backupReplicated, ""}, |
| + {"unit only", rec("unit", func(r *provision.Record) { r.Unit = "shop.service" }), backupsRegion, backupReplicated, ""}, |
| + {"single region", rec("single", func(r *provision.Record) { r.Region = "" }), "", backupReplicated, ""}, |
| + {"service route", rec("svc", func(r *provision.Record) { r.Kind = "service" }), backupsRegion, backupNoCopy, "not_instance"}, |
| + {"static route", rec("stat", func(r *provision.Record) { r.Kind = "static" }), backupsRegion, backupNoCopy, "not_instance"}, |
| + {"own address", rec("addr", func(r *provision.Record) { r.Addr = "127.0.0.1:9000" }), backupsRegion, backupNoCopy, "own_address"}, |
| + {"own addresses", rec("addrs", func(r *provision.Record) { r.Addrs = []string{"127.0.0.1:9000", "127.0.0.1:9001"} }), backupsRegion, backupNoCopy, "own_address"}, |
| + {"no sleep", rec("awake", func(r *provision.Record) { r.Hibernate = backupsNo() }), backupsRegion, backupNoCopy, "no_sleep"}, |
| + {"demo", rec("demo", demoOf), backupsRegion, backupNoCopy, "demo"}, |
| + {"pooled", rec("pool", func(r *provision.Record) { r.Pool = "shared" }), backupsRegion, backupNotShown, "pooled"}, |
| + {"fleet", rec("fleet", func(r *provision.Record) { r.Region = "fleet/acme" }), backupsRegion, backupNotShown, "fleet"}, |
| + {"fleet in the console's region", rec("fleetin", func(r *provision.Record) { r.Region = "fleet/acme" }), "fleet/acme", backupNotShown, "fleet"}, |
| + {"other region", rec("far", func(r *provision.Record) { r.Region = "uk" }), backupsRegion, backupNotShown, "other_region"}, |
| + {"home record on a regional console", rec("home", func(r *provision.Record) { r.Region = "" }), backupsRegion, backupNotShown, "other_region"}, |
| + {"pooled and no sleep", rec("poolawake", func(r *provision.Record) { r.Pool = "shared"; r.Hibernate = backupsNo() }), backupsRegion, backupNoCopy, "no_sleep"}, |
| + {"demo and no sleep", rec("demoawake", func(r *provision.Record) { demoOf(r); r.Hibernate = backupsNo() }), backupsRegion, backupNoCopy, "no_sleep"}, |
| + {"demo and pooled", rec("demopool", func(r *provision.Record) { demoOf(r); r.Pool = "shared" }), backupsRegion, backupNoCopy, "demo"}, |
| + {"pooled in another region", rec("poolfar", func(r *provision.Record) { r.Pool = "shared"; r.Region = "uk" }), backupsRegion, backupNotShown, "pooled"}, |
| + {"fleet that does not sleep", rec("fleetawake", func(r *provision.Record) { r.Region = "fleet/acme"; r.Hibernate = backupsNo() }), backupsRegion, backupNoCopy, "no_sleep"}, |
| + } |
| +} |
| + |
| +func TestClassifyBackupsTable(t *testing.T) { |
| + for _, row := range backupsRows() { |
| + t.Run(row.name, func(t *testing.T) { |
| + class, reason := classifyBackups(row.rec, row.console) |
| + if class != row.class || reason != row.reason { |
| + t.Fatalf("classifyBackups = %q/%q, want %q/%q", class, reason, row.class, row.reason) |
| + } |
| + // Region is never an intrinsic fact: replicaIntrinsic says |
| + // replicated wherever classifyBackups refuses only on region. |
| + ic, ir := replicaIntrinsic(row.rec) |
| + if row.reason == "other_region" { |
| + if ic != backupReplicated || ir != "" { |
| + t.Fatalf("replicaIntrinsic = %q/%q for an other-region row, want replicated", ic, ir) |
| + } |
| + } else if ic != row.class || ir != row.reason { |
| + t.Fatalf("replicaIntrinsic = %q/%q, want %q/%q", ic, ir, row.class, row.reason) |
| + } |
| + }) |
| + } |
| +} |
| + |
| +// The oracle that keeps the door and `db pull` in agreement (spec §1 |
| +// R2-2): classifyBackups says replicated exactly when resolveDBSource |
| +// admits the record, and an other-region refusal is db pull's residency |
| +// refusal, which it forwards. |
| +func TestClassifyBackupsAgreesWithResolveDBSource(t *testing.T) { |
| + fx := dbFixture(t) |
| + enableInstances(t, fx.consoleFixture, "titogo", titoManifest()) |
| + acct, err := fx.st.AccountBySqid("bab") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + for _, row := range backupsRows() { |
| + t.Run(row.name, func(t *testing.T) { |
| + putDBRecord(t, fx.consoleFixture, row.rec) |
| + fx.srv.cfg.Region = row.console |
| + class, reason := classifyBackups(row.rec, row.console) |
| + _, err := fx.srv.resolveDBSource(t.Context(), acct, "alice@keymail.dev", "titogo", row.rec.Host) |
| + if (class == backupReplicated) != (err == nil) { |
| + t.Fatalf("classifyBackups = %q/%q but resolveDBSource = %v", class, reason, err) |
| + } |
| + if reason == "other_region" && !errors.Is(err, ErrResidency) { |
| + t.Fatalf("other_region but resolveDBSource = %v, want ErrResidency (which db pull forwards)", err) |
| + } |
| + }) |
| + } |
| +} |
| + |
| +// resolveReplica and dbClassRefusal answer every row exactly as they did |
| +// before they called the shared classifier. The goldens were captured by |
| +// running the code at 25cedabc over backupsRows. |
| +func TestResolveReplicaUsesIntrinsic(t *testing.T) { |
| + const demoMsg = "Only a verified claim can move a demo's data into a permanent app. Generic move, copy and import tools cannot do this. Demos have no backup." |
| + golden := map[string]struct { |
| + prefix, err, refusal string |
| + is error |
| + }{ |
| + "replicated": {"instances/rep.go.tito.io", "", "", nil}, |
| + "kind instance": {"instances/kin.go.tito.io", "", "", nil}, |
| + "unit only": {"instances/unit.go.tito.io", "", "", nil}, |
| + "single region": {"instances/single.go.tito.io", "", "", nil}, |
| + "service route": {"instances/svc.go.tito.io", "", "svc.go.tito.io is a service route. The platform keeps a database copy only for instances.", nil}, |
| + "static route": {"instances/stat.go.tito.io", "", "stat.go.tito.io is a static route. The platform keeps a database copy only for instances.", nil}, |
| + "own address": {"instances/addr.go.tito.io", "", "addr.go.tito.io is served from its own address, so the platform does not manage its database.", nil}, |
| + "own addresses": {"instances/addrs.go.tito.io", "", "addrs.go.tito.io is served from its own address, so the platform does not manage its database.", nil}, |
| + "no sleep": {"instances/awake.go.tito.io", "", "awake.go.tito.io does not sleep, so the platform does not keep a copy of its database.", nil}, |
| + "demo": {"instances/demo.go.tito.io", "", demoMsg, nil}, |
| + "pooled": {"", `db: pooled instances have no replica of their own: pool.go.tito.io is in pool "shared"`, "", ErrPooled}, |
| + "fleet": {"", `db: the instance's data is outside this region: fleet.go.tito.io is placed on fleet "fleet/acme"`, "", ErrResidency}, |
| + "fleet in the console's region": {"", `db: the instance's data is outside this region: fleetin.go.tito.io is placed on fleet "fleet/acme"`, "", ErrResidency}, |
| + "other region": {"", `db: the instance's data is outside this region: far.go.tito.io is homed in "uk", this console serves "ie"`, "", ErrResidency}, |
| + "home record on a regional console": {"", `db: the instance's data is outside this region: home.go.tito.io is homed in "", this console serves "ie"`, "", ErrResidency}, |
| + "pooled and no sleep": {"", `db: pooled instances have no replica of their own: poolawake.go.tito.io is in pool "shared"`, "poolawake.go.tito.io does not sleep, so the platform does not keep a copy of its database.", ErrPooled}, |
| + "demo and no sleep": {"instances/demoawake.go.tito.io", "", demoMsg, nil}, |
| + "demo and pooled": {"", `db: pooled instances have no replica of their own: demopool.go.tito.io is in pool "shared"`, demoMsg, ErrPooled}, |
| + "pooled in another region": {"", `db: pooled instances have no replica of their own: poolfar.go.tito.io is in pool "shared"`, "", ErrPooled}, |
| + "fleet that does not sleep": {"", `db: the instance's data is outside this region: fleetawake.go.tito.io is placed on fleet "fleet/acme"`, "fleetawake.go.tito.io does not sleep, so the platform does not keep a copy of its database.", ErrResidency}, |
| + } |
| + rows := backupsRows() |
| + if len(golden) != len(rows) { |
| + t.Fatalf("%d goldens for %d rows", len(golden), len(rows)) |
| + } |
| + for _, row := range rows { |
| + t.Run(row.name, func(t *testing.T) { |
| + want, ok := golden[row.name] |
| + if !ok { |
| + t.Fatalf("no golden for %q", row.name) |
| + } |
| + s := &Server{} |
| + s.cfg.Region = row.console |
| + s.cfg.HibernatePrefix = "instances" |
| + prefix, err := s.resolveReplica(row.rec, row.rec.Host) |
| + got := "" |
| + if err != nil { |
| + got = err.Error() |
| + } |
| + if prefix != want.prefix || got != want.err { |
| + t.Fatalf("resolveReplica = %q, %q; want %q, %q", prefix, got, want.prefix, want.err) |
| + } |
| + if want.is != nil && !errors.Is(err, want.is) { |
| + t.Fatalf("resolveReplica error %v is not %v", err, want.is) |
| + } |
| + if msg := dbClassRefusal(row.rec); msg != want.refusal { |
| + t.Fatalf("dbClassRefusal = %q, want %q", msg, want.refusal) |
| + } |
| + }) |
| + } |
| +} |
| + |
| +func TestBackupsForReadsPositionAndHistory(t *testing.T) { |
| + fx := dbFixture(t) |
| + fx.replicate(t, dbTestHost, 3) |
| + rd := lsfile.NewReplicaClient(filepath.Join(fx.root, "instances", dbTestHost)) |
| + plan, from, known, err := dbdoor.PositionAndHistory(t.Context(), rd, 20000) |
| + if err != nil || !known { |
| + t.Fatalf("PositionAndHistory = %v, known %v", err, known) |
| + } |
| + |
| + rec := provision.Record{Host: dbTestHost, Region: fx.srv.cfg.Region, Hibernate: backupsYes()} |
| + v := fx.srv.backupsFor(t.Context(), rec) |
| + if v.Class != backupReplicated || v.Reason != "" || v.Region != "" { |
| + t.Fatalf("view = %+v, want replicated", v) |
| + } |
| + if v.CopiedTo == nil || v.CopiedTo.Transaction != plan.TXID || !v.CopiedTo.WrittenAt.Equal(plan.At) { |
| + t.Fatalf("copied_to = %+v, want %d at %v", v.CopiedTo, plan.TXID, plan.At) |
| + } |
| + if v.HistoryFrom == nil || !v.HistoryFrom.Equal(from) { |
| + t.Fatalf("history_from = %v, want %v", v.HistoryFrom, from) |
| + } |
| + if v.HABorn || v.Files != "not_copied" { |
| + t.Fatalf("HABorn %v files %q, want false and not_copied", v.HABorn, v.Files) |
| + } |
| + |
| + // The same host born HA: the files line is not_shown. The position is |
| + // the memoised one; HABorn is the record's own. |
| + rec.HA = true |
| + v = fx.srv.backupsFor(t.Context(), rec) |
| + if !v.HABorn || v.Files != "not_shown" || v.CopiedTo == nil { |
| + t.Fatalf("born HA view = %+v, want HABorn and files not_shown", v) |
| + } |
| + |
| + b, err := json.Marshal(v) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + var keys map[string]json.RawMessage |
| + if err := json.Unmarshal(b, &keys); err != nil { |
| + t.Fatal(err) |
| + } |
| + for _, k := range []string{"class", "copied_to", "history_from", "files"} { |
| + if _, ok := keys[k]; !ok { |
| + t.Errorf("%s missing from %s", k, b) |
| + } |
| + } |
| + for _, k := range []string{"reason", "region", "HABorn", "ha_born"} { |
| + if _, ok := keys[k]; ok { |
| + t.Errorf("%s present in %s", k, b) |
| + } |
| + } |
| + var pos map[string]json.RawMessage |
| + if err := json.Unmarshal(keys["copied_to"], &pos); err != nil || len(pos) != 2 || pos["transaction"] == nil || pos["written_at"] == nil { |
| + t.Errorf("copied_to = %s, want exactly transaction and written_at", keys["copied_to"]) |
| + } |
| +} |
| + |
| +func TestBackupsForNotShownNeverReadsBucket(t *testing.T) { |
| + fx := dbFixture(t) |
| + fx.srv.dbReplica = func(context.Context, string) (replicaReader, error) { |
| + t.Error("dbReplica called for a record the door does not read") |
| + return nil, errors.New("no") |
| + } |
| + for _, row := range backupsRows() { |
| + if row.class == backupReplicated { |
| + continue |
| + } |
| + t.Run(row.name, func(t *testing.T) { |
| + fx.srv.cfg.Region = row.console |
| + v := fx.srv.backupsFor(t.Context(), row.rec) |
| + if v.Class != row.class || v.Reason != row.reason { |
| + t.Fatalf("view = %q/%q, want %q/%q", v.Class, v.Reason, row.class, row.reason) |
| + } |
| + if v.CopiedTo != nil || v.HistoryFrom != nil { |
| + t.Fatalf("view = %+v carries a position", v) |
| + } |
| + if v.Files != "not_copied" { |
| + t.Fatalf("files = %q, want not_copied", v.Files) |
| + } |
| + wantRegion := "" |
| + if row.reason == "other_region" { |
| + wantRegion = regionlabel.ForCode(row.rec.Region, fx.srv.regionHome(), fx.srv.regionNames()).Text |
| + if wantRegion == "" { |
| + t.Fatal("no region label to name") |
| + } |
| + } |
| + if v.Region != wantRegion { |
| + t.Fatalf("region = %q, want %q", v.Region, wantRegion) |
| + } |
| + }) |
| + } |
| +} |
| + |
| +func TestBackupsForNothingReplicated(t *testing.T) { |
| + fx := dbFixture(t) |
| + v := fx.srv.backupsFor(t.Context(), provision.Record{Host: dbTestHost, Region: fx.srv.cfg.Region, Hibernate: backupsYes()}) |
| + if v.Class != backupNotShown || v.Reason != "nothing_replicated" || v.CopiedTo != nil || v.HistoryFrom != nil { |
| + t.Fatalf("view = %+v, want not_shown/nothing_replicated", v) |
| + } |
| + if fx.opens.Load() != 1 { |
| + t.Fatalf("dbReplica opened %d times, want 1", fx.opens.Load()) |
| + } |
| +} |
| + |
| +// listedReplica lists n level-0 files, transactions 1..n one each, and |
| +// serves a header for any of them. gate, when set, holds every listing |
| +// until it is closed; entered is signalled on each listing. |
| +type listedReplica struct { |
| + n int |
| + at time.Time |
| + gate chan struct{} |
| + entered chan struct{} |
| +} |
| + |
| +func (r *listedReplica) LTXFiles(ctx context.Context, level int, _ ltx.TXID, _ bool) (ltx.FileIterator, error) { |
| + if r.entered != nil { |
| + select { |
| + case r.entered <- struct{}{}: |
| + default: |
| + } |
| + } |
| + if r.gate != nil { |
| + select { |
| + case <-r.gate: |
| + case <-ctx.Done(): |
| + return nil, ctx.Err() |
| + } |
| + } |
| + if level != 0 { |
| + return ltx.NewFileInfoSliceIterator(nil), nil |
| + } |
| + fs := make([]*ltx.FileInfo, r.n) |
| + for i := range fs { |
| + tx := ltx.TXID(i + 1) |
| + fs[i] = <x.FileInfo{Level: 0, MinTXID: tx, MaxTXID: tx, Size: 100, CreatedAt: r.at.Add(time.Duration(i) * time.Second)} |
| + } |
| + return ltx.NewFileInfoSliceIterator(fs), nil |
| +} |
| + |
| +func (r *listedReplica) OpenLTXFile(_ context.Context, _ int, minTXID, maxTXID ltx.TXID, offset, size int64) (io.ReadCloser, error) { |
| + h := ltx.Header{Version: ltx.Version, PageSize: 4096, Commit: 2, MinTXID: minTXID, MaxTXID: maxTXID, Timestamp: r.at.UnixMilli()} |
| + b, err := h.MarshalBinary() |
| + if err != nil { |
| + return nil, err |
| + } |
| + end := offset + size |
| + if size <= 0 || end > int64(len(b)) { |
| + end = int64(len(b)) |
| + } |
| + return io.NopCloser(bytes.NewReader(b[offset:end])), nil |
| +} |
| + |
| +// blockingReplica lists nothing until its context ends. |
| +type blockingReplica struct{} |
| + |
| +func (blockingReplica) LTXFiles(ctx context.Context, _ int, _ ltx.TXID, _ bool) (ltx.FileIterator, error) { |
| + <-ctx.Done() |
| + return nil, ctx.Err() |
| +} |
| + |
| +func (blockingReplica) OpenLTXFile(context.Context, int, ltx.TXID, ltx.TXID, int64, int64) (io.ReadCloser, error) { |
| + return nil, errors.New("blockingReplica opens nothing") |
| +} |
| + |
| +func TestBackupsForBounds(t *testing.T) { |
| + at := time.Date(2026, 11, 11, 9, 0, 0, 0, time.UTC) |
| + rec := func(slug string) provision.Record { |
| + return provision.Record{Host: slug + ".go.tito.io", Hibernate: backupsYes()} |
| + } |
| + serve := func(fx *dbFx, m map[string]replicaReader, opens *atomic.Int32) { |
| + fx.srv.dbReplica = func(_ context.Context, prefix string) (replicaReader, error) { |
| + opens.Add(1) |
| + rd, ok := m[prefix] |
| + if !ok { |
| + t.Errorf("unexpected prefix %q", prefix) |
| + return nil, errors.New("unexpected prefix") |
| + } |
| + return rd, nil |
| + } |
| + } |
| + |
| + t.Run("file cap", func(t *testing.T) { |
| + fx := dbFixture(t) |
| + opens := new(atomic.Int32) |
| + serve(fx, map[string]replicaReader{ |
| + "instances/cap.go.tito.io": &listedReplica{n: 20000, at: at}, |
| + "instances/over.go.tito.io": &listedReplica{n: 20001, at: at}, |
| + }, opens) |
| + v := fx.srv.backupsFor(t.Context(), rec("cap")) |
| + if v.Class != backupReplicated || v.CopiedTo == nil || v.CopiedTo.Transaction != 20000 { |
| + t.Fatalf("20,000 files = %+v, want replicated at 20000", v) |
| + } |
| + v = fx.srv.backupsFor(t.Context(), rec("over")) |
| + if v.Class != backupNotShown || v.Reason != "too_large_to_plan" || v.CopiedTo != nil { |
| + t.Fatalf("20,001 files = %+v, want not_shown/too_large_to_plan", v) |
| + } |
| + }) |
| + |
| + t.Run("deadline", func(t *testing.T) { |
| + fx := dbFixture(t) |
| + fx.srv.backups.deadline = 50 * time.Millisecond |
| + opens := new(atomic.Int32) |
| + serve(fx, map[string]replicaReader{"instances/slow.go.tito.io": blockingReplica{}}, opens) |
| + start := time.Now() |
| + v := fx.srv.backupsFor(t.Context(), rec("slow")) |
| + if v.Class != backupNotShown || v.Reason != "too_large_to_plan" { |
| + t.Fatalf("blocked read = %+v, want not_shown/too_large_to_plan", v) |
| + } |
| + if d := time.Since(start); d > 5*time.Second { |
| + t.Fatalf("blocked read took %v", d) |
| + } |
| + }) |
| + |
| + t.Run("one read per host, memoised", func(t *testing.T) { |
| + fx := dbFixture(t) |
| + now := at |
| + var nowMu sync.Mutex |
| + fx.srv.backups.now = func() time.Time { nowMu.Lock(); defer nowMu.Unlock(); return now } |
| + gated := &listedReplica{n: 3, at: at, gate: make(chan struct{}), entered: make(chan struct{}, 1)} |
| + opens := new(atomic.Int32) |
| + serve(fx, map[string]replicaReader{"instances/busy.go.tito.io": gated}, opens) |
| + |
| + views := make([]backupsView, 2) |
| + var wg sync.WaitGroup |
| + wg.Add(1) |
| + go func() { defer wg.Done(); views[0] = fx.srv.backupsFor(context.Background(), rec("busy")) }() |
| + select { |
| + case <-gated.entered: |
| + case <-time.After(5 * time.Second): |
| + t.Fatal("the first read never listed") |
| + } |
| + wg.Add(1) |
| + go func() { defer wg.Done(); views[1] = fx.srv.backupsFor(context.Background(), rec("busy")) }() |
| + deadline := time.Now().Add(5 * time.Second) |
| + for fx.srv.backups.waiting("busy.go.tito.io") != 1 { |
| + if time.Now().After(deadline) { |
| + t.Fatal("the second call never joined the read in flight") |
| + } |
| + time.Sleep(time.Millisecond) |
| + } |
| + close(gated.gate) |
| + wg.Wait() |
| + if n := opens.Load(); n != 1 { |
| + t.Fatalf("two concurrent calls opened the replica %d times, want 1", n) |
| + } |
| + for i, v := range views { |
| + if v.Class != backupReplicated || v.CopiedTo == nil || v.CopiedTo.Transaction != 3 { |
| + t.Fatalf("view %d = %+v", i, v) |
| + } |
| + } |
| + |
| + nowMu.Lock() |
| + now = at.Add(5*time.Minute - time.Second) |
| + nowMu.Unlock() |
| + if v := fx.srv.backupsFor(t.Context(), rec("busy")); v.Class != backupReplicated || opens.Load() != 1 { |
| + t.Fatalf("a call within 5 minutes = %+v after %d opens, want the memo and 1", v, opens.Load()) |
| + } |
| + nowMu.Lock() |
| + now = at.Add(5 * time.Minute) |
| + nowMu.Unlock() |
| + if v := fx.srv.backupsFor(t.Context(), rec("busy")); v.Class != backupReplicated || opens.Load() != 2 { |
| + t.Fatalf("a call at 5 minutes = %+v after %d opens, want a fresh read", v, opens.Load()) |
| + } |
| + }) |
| + |
| + t.Run("a failed read is unavailable and not kept", func(t *testing.T) { |
| + fx := dbFixture(t) |
| + opens := new(atomic.Int32) |
| + fx.srv.dbReplica = func(context.Context, string) (replicaReader, error) { |
| + opens.Add(1) |
| + return nil, errors.New("s3: something broke") |
| + } |
| + for i := 1; i <= 2; i++ { |
| + v := fx.srv.backupsFor(t.Context(), rec("broken")) |
| + if v.Class != backupNotShown || v.Reason != "unavailable" { |
| + t.Fatalf("failed read = %+v, want not_shown/unavailable", v) |
| + } |
| + if opens.Load() != int32(i) { |
| + t.Fatalf("after %d calls opened %d times", i, opens.Load()) |
| + } |
| + } |
| + }) |
| +} |