package main import ( "context" "testing" "time" "github.com/carlosframework/platform/internal/release" ) // Only CARLOS_CHANGE_INDEX=read arms the console's record view, and never on // a host-claim console, whose sweeps are off. func TestChangeIndexArming(t *testing.T) { t.Parallel() ds := release.NewDirStore(t.TempDir()) ds.EnableChangeIndex() env := func(v string) func(string) string { return func(k string) string { if k == "CARLOS_CHANGE_INDEX" { return v } return "" } } for _, c := range []struct { val string hostClaim bool armed bool }{ {"", false, false}, {"READ", false, false}, {"1", false, false}, {"read", true, false}, {"read", false, true}, } { if got := armChangeIndex(env(c.val), c.hostClaim, ds) != nil; got != c.armed { t.Errorf("CARLOS_CHANGE_INDEX=%q host claim %v: armed %v, want %v", c.val, c.hostClaim, got, c.armed) } } } // The backstop rebuilds the view as it starts, and stops with its context. func TestRecordViewBackstopRebuildsAtStart(t *testing.T) { t.Parallel() ds := release.NewDirStore(t.TempDir()) ds.EnableChangeIndex() v := armChangeIndex(func(string) string { return "read" }, false, ds) ctx, cancel := context.WithCancel(context.Background()) done := make(chan struct{}) go func() { recordViewBackstopLoop(ctx, v); close(done) }() deadline := time.Now().Add(10 * time.Second) for v.Snapshot() == nil { if time.Now().After(deadline) { t.Fatal("the backstop published nothing at start") } time.Sleep(10 * time.Millisecond) } if !v.Snapshot().Complete { t.Fatal("the start rebuild of an empty bucket is not complete") } cancel() select { case <-done: case <-time.After(10 * time.Second): t.Fatal("the backstop did not stop with its context") } }