| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "testing" |
| 6 | "time" |
| 7 | |
| 8 | "github.com/carlosframework/platform/internal/release" |
| 9 | ) |
| 10 | |
| 11 | // Only CARLOS_CHANGE_INDEX=read arms the console's record view, and never on |
| 12 | // a host-claim console, whose sweeps are off. |
| 13 | func TestChangeIndexArming(t *testing.T) { |
| 14 | t.Parallel() |
| 15 | ds := release.NewDirStore(t.TempDir()) |
| 16 | ds.EnableChangeIndex() |
| 17 | env := func(v string) func(string) string { |
| 18 | return func(k string) string { |
| 19 | if k == "CARLOS_CHANGE_INDEX" { |
| 20 | return v |
| 21 | } |
| 22 | return "" |
| 23 | } |
| 24 | } |
| 25 | for _, c := range []struct { |
| 26 | val string |
| 27 | hostClaim bool |
| 28 | armed bool |
| 29 | }{ |
| 30 | {"", false, false}, |
| 31 | {"READ", false, false}, |
| 32 | {"1", false, false}, |
| 33 | {"read", true, false}, |
| 34 | {"read", false, true}, |
| 35 | } { |
| 36 | if got := armChangeIndex(env(c.val), c.hostClaim, ds) != nil; got != c.armed { |
| 37 | t.Errorf("CARLOS_CHANGE_INDEX=%q host claim %v: armed %v, want %v", c.val, c.hostClaim, got, c.armed) |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | // The backstop rebuilds the view as it starts, and stops with its context. |
| 43 | func TestRecordViewBackstopRebuildsAtStart(t *testing.T) { |
| 44 | t.Parallel() |
| 45 | ds := release.NewDirStore(t.TempDir()) |
| 46 | ds.EnableChangeIndex() |
| 47 | v := armChangeIndex(func(string) string { return "read" }, false, ds) |
| 48 | ctx, cancel := context.WithCancel(context.Background()) |
| 49 | done := make(chan struct{}) |
| 50 | go func() { recordViewBackstopLoop(ctx, v); close(done) }() |
| 51 | deadline := time.Now().Add(10 * time.Second) |
| 52 | for v.Snapshot() == nil { |
| 53 | if time.Now().After(deadline) { |
| 54 | t.Fatal("the backstop published nothing at start") |
| 55 | } |
| 56 | time.Sleep(10 * time.Millisecond) |
| 57 | } |
| 58 | if !v.Snapshot().Complete { |
| 59 | t.Fatal("the start rebuild of an empty bucket is not complete") |
| 60 | } |
| 61 | cancel() |
| 62 | select { |
| 63 | case <-done: |
| 64 | case <-time.After(10 * time.Second): |
| 65 | t.Fatal("the backstop did not stop with its context") |
| 66 | } |
| 67 | } |
| 68 | |