package meta import ( "path/filepath" "testing" ) func TestAcceptSnapshotReplayRevertAndGC(t *testing.T) { db := open(t) if err := db.CreateShare("proj", "device", 1); err != nil { t.Fatal(err) } accept := func(id, parent string, conflict, advanced bool, head string) { t.Helper() got, err := db.AcceptSnapshot(Snapshot{ID: id, ManifestHash: id, Share: "proj", ParentID: parent}, []ChunkRef{{Hash: id}, {Hash: id}}) if err != nil || got != (SnapshotAcceptance{Head: head, Conflict: conflict, Advanced: advanced}) { t.Fatalf("accept(%s,%s)=%+v, %v", id, parent, got, err) } } accept("a", "", false, true, "a") accept("a", "old-parent", false, false, "a") // replay is independent of its original parent accept("b", "a", false, true, "b") candidates, err := db.DeletableSnapshots("proj", 0) if err != nil || len(candidates) != 1 || candidates[0] != "a" { t.Fatalf("GC candidates=%v, %v", candidates, err) } accept("a", "", true, false, "b") // existing snapshot cannot bypass the parent check accept("a", "b", false, true, "a") // valid revert preserves original row and refs // GC's previously selected candidate is now head; deletion must recheck. if err := db.DeleteSnapshot("proj", "a", []string{"a"}); err != nil { t.Fatal(err) } for _, hash := range []string{"a", "b"} { if rc, err := db.ChunkRefcount(hash); err != nil || rc != 1 { t.Fatalf("%s refcount=%d, %v; want 1", hash, rc, err) } } snaps, err := db.SnapshotIDs("proj") if err != nil || len(snaps) != 2 { t.Fatalf("snapshots=%v, %v", snaps, err) } // If GC prunes an old snapshot before a revert, accepting it must restore refs. if err := db.DeleteSnapshot("proj", "b", []string{"b"}); err != nil { t.Fatal(err) } accept("b", "a", false, true, "b") if rc, err := db.ChunkRefcount("b"); err != nil || rc != 1 { t.Fatalf("restored b refcount=%d, %v", rc, err) } } func TestAcceptSnapshotAcrossConnections(t *testing.T) { path := filepath.Join(t.TempDir(), "meta.db") a, err := Open(path) if err != nil { t.Fatal(err) } defer a.Close() b, err := Open(path) if err != nil { t.Fatal(err) } defer b.Close() if err := a.CreateShare("proj", "device", 1); err != nil { t.Fatal(err) } type outcome struct { id string result SnapshotAcceptance err error } start := make(chan struct{}) done := make(chan outcome, 2) for i, db := range []*DB{a, b} { id := []string{"a", "b"}[i] go func() { <-start got, err := db.AcceptSnapshot(Snapshot{ID: id, Share: "proj"}, []ChunkRef{{Hash: id}}) done <- outcome{id, got, err} }() } close(start) first, second := <-done, <-done if first.err != nil || second.err != nil { t.Fatalf("errors: %v, %v", first.err, second.err) } if first.result.Conflict { first, second = second, first } if first.result.Conflict || !first.result.Advanced || !second.result.Conflict || second.result.Advanced || second.result.Head != first.id { t.Fatalf("outcomes: %+v, %+v", first, second) } ids, err := a.SnapshotIDs("proj") if err != nil || len(ids) != 1 || ids[0] != first.id { t.Fatalf("snapshots=%v, %v", ids, err) } if rc, err := a.ChunkRefcount(second.id); err != nil || rc != 0 { t.Fatalf("loser refcount=%d, %v", rc, err) } }