//go:build integration package main import ( "encoding/json" "testing" "time" "github.com/novox/mesh-controller/internal/inventory" ) func TestAPlanFollowedFromTheStoreAnswersWhenItMoves(t *testing.T) { open := aMesh(t) ctx := t.Context() // Read every 50ms rather than every second, so the wait answers as soon as the plan moves and the bound // below passes in a fraction of a second (novox/hq issue 591); the move still waits 1.5s, long enough // for the wait to have read the plan before it. was := planWaitEvery planWaitEvery = 50 * time.Millisecond t.Cleanup(func() { planWaitEvery = was }) now := time.Now().UTC() walk := inventory.Plan{ID: "plan-229", Repository: "novox/mesh-catalog", Branch: "main", Commit: "c229c229", Created: now, State: inventory.PlanBuilding, Tiers: [][]string{{"a"}, {"b"}}, Modules: map[string]*inventory.PlanModule{"a": {}, "b": {}}} if err := open.inventory.SavePlan(ctx, &walk); err != nil { t.Fatal(err) } moved := make(chan error, 1) go func() { time.Sleep(1500 * time.Millisecond) p, err := open.inventory.PlanByID(ctx, walk.ID) if err == nil { p.Tier, p.State = 1, inventory.PlanRolling err = open.inventory.SavePlan(ctx, &p) } moved <- err }() began := time.Now() var got planWaited out := printed(t, func() error { return plansCommand(ctx, []string{"wait", walk.ID, "--until", "tier", "--bound", "15s"}) }) if err := <-moved; err != nil { t.Fatal(err) } if err := json.Unmarshal([]byte(out), &got); err != nil { t.Fatalf("plans wait did not answer data: %v: %q", err, out) } if got.Came != "tier" || got.Plan.Tier != 1 || got.Plan.State != inventory.PlanRolling { t.Fatalf("the wait answered %+v", got) } if waited := time.Since(began); waited > 3*time.Second { t.Errorf("it answered after %s, not when the plan moved", waited) } out = printed(t, func() error { return plansCommand(ctx, []string{"wait", walk.ID, "--bound", "300ms"}) }) if err := json.Unmarshal([]byte(out), &got); err != nil || got.Came != "bound" || got.Until != "moved" { t.Fatalf("a plan that did not move answered %+v (%v)", got, err) } }