| 1 | package sessioncatalog |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "path/filepath" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | func TestMetadataProgressSharesWriterBoundary(t *testing.T) { |
| 11 | c, err := Open(t.Context(), Options{Path: filepath.Join(t.TempDir(), "catalog.sqlite"), MetadataOnly: true, StartPaused: true}) |
| 12 | if err != nil { |
| 13 | t.Fatal(err) |
| 14 | } |
| 15 | t.Cleanup(func() { _ = c.Close(context.Background()) }) |
| 16 | target := DirectoryTarget{Path: t.TempDir(), Scope: "global"} |
| 17 | generation, _, err := c.beginDirectoryScan(t.Context(), target, "first", 1) |
| 18 | if err != nil { |
| 19 | t.Fatal(err) |
| 20 | } |
| 21 | calls := 0 |
| 22 | c.testScanProgressWriteHook = func() { |
| 23 | calls++ |
| 24 | if c.mutationMu.TryLock() { |
| 25 | c.mutationMu.Unlock() |
| 26 | t.Error("scan progress bypassed the catalog writer boundary") |
| 27 | } |
| 28 | } |
| 29 | if err := c.updateDirectoryScanProgress(t.Context(), target.Path, generation, 128); err != nil { |
| 30 | t.Fatal(err) |
| 31 | } |
| 32 | if calls != 1 { |
| 33 | t.Fatalf("progress writes = %d", calls) |
| 34 | } |
| 35 | var count int |
| 36 | if err := c.db.QueryRow(`SELECT indexed FROM catalog_directories`).Scan(&count); err != nil || count != 128 { |
| 37 | t.Fatalf("progress = %d, error = %v", count, err) |
| 38 | } |
| 39 | // A stale worker cannot publish progress into a newer directory generation. |
| 40 | if _, _, err := c.beginDirectoryScan(t.Context(), target, "second", 2); err != nil { |
| 41 | t.Fatal(err) |
| 42 | } |
| 43 | if err := c.updateDirectoryScanProgress(t.Context(), target.Path, generation, 256); err != nil { |
| 44 | t.Fatal(err) |
| 45 | } |
| 46 | if err := c.db.QueryRow(`SELECT indexed FROM catalog_directories`).Scan(&count); err != nil || count != 0 { |
| 47 | t.Fatalf("stale progress = %d, error = %v", count, err) |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | func TestMetadataProgressCancellationDoesNotCompleteDiscovery(t *testing.T) { |
| 52 | c, err := Open(t.Context(), Options{InMemory: true, MetadataOnly: true, StartPaused: true}) |
| 53 | if err != nil { |
| 54 | t.Fatal(err) |
| 55 | } |
| 56 | t.Cleanup(func() { _ = c.Close(context.Background()) }) |
| 57 | target := DirectoryTarget{Path: t.TempDir(), Scope: "global"} |
| 58 | generation, _, err := c.beginDirectoryScan(t.Context(), target, "first", 1) |
| 59 | if err != nil { |
| 60 | t.Fatal(err) |
| 61 | } |
| 62 | ctx, cancel := context.WithCancel(t.Context()) |
| 63 | c.testScanProgressWriteHook = cancel |
| 64 | if err := c.updateDirectoryScanProgress(ctx, target.Path, generation, 128); !errors.Is(err, context.Canceled) { |
| 65 | t.Fatalf("canceled progress: %v", err) |
| 66 | } |
| 67 | if state := c.DirectoryStatus(t.Context(), target.Path); state.State != "scanning" { |
| 68 | t.Fatalf("canceled progress completed discovery: %+v", state) |
| 69 | } |
| 70 | var count int |
| 71 | if err := c.db.QueryRow(`SELECT indexed FROM catalog_directories`).Scan(&count); err != nil || count != 0 { |
| 72 | t.Fatalf("canceled progress = %d, error = %v", count, err) |
| 73 | } |
| 74 | } |
| 75 |