| 1 | package history |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "testing" |
| 6 | "time" |
| 7 | |
| 8 | "reasonix/internal/historycatalog" |
| 9 | ) |
| 10 | |
| 11 | func TestIndexedCatalogManagerRebuildPreservesRootsAndRestarts(t *testing.T) { |
| 12 | manager := &indexedCatalogManager{} |
| 13 | manager.open = func(ctx context.Context, opts historycatalog.Options) (*historycatalog.Catalog, error) { |
| 14 | opts.Path, opts.InMemory = "", true |
| 15 | return historycatalog.Open(ctx, opts) |
| 16 | } |
| 17 | var rebuilt []historycatalog.Root |
| 18 | manager.rebuild = func(_ context.Context, _ historycatalog.Options, roots []historycatalog.Root) (historycatalog.Status, error) { |
| 19 | rebuilt = append([]historycatalog.Root{}, roots...) |
| 20 | return historycatalog.Status{}, nil |
| 21 | } |
| 22 | firstRoot := historycatalog.Root{Path: t.TempDir(), Scope: "global"} |
| 23 | secondRoot := historycatalog.Root{Path: t.TempDir(), Scope: "global"} |
| 24 | manager.register([]historycatalog.Root{firstRoot}) |
| 25 | waitManagerOpen(t, manager) |
| 26 | if err := manager.rebuildShared(context.Background(), []historycatalog.Root{secondRoot}); err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | waitManagerOpen(t, manager) |
| 30 | if len(rebuilt) != 2 { |
| 31 | t.Fatalf("rebuilt roots = %#v, want both registered roots", rebuilt) |
| 32 | } |
| 33 | if manager.get() == nil { |
| 34 | t.Fatal("manager did not publish a fresh catalog after rebuild") |
| 35 | } |
| 36 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 37 | defer cancel() |
| 38 | if err := manager.close(ctx); err != nil { |
| 39 | t.Fatal(err) |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | func waitManagerOpen(t *testing.T, manager *indexedCatalogManager) { |
| 44 | t.Helper() |
| 45 | manager.mu.RLock() |
| 46 | var done chan struct{} |
| 47 | for _, candidate := range manager.opening { |
| 48 | done = candidate |
| 49 | } |
| 50 | manager.mu.RUnlock() |
| 51 | if done != nil { |
| 52 | <-done |
| 53 | } |
| 54 | } |
| 55 |