package session_test import ( "fmt" "testing" "testing/synctest" "time" "tangled.org/guid.foo/simmer/api/v1alpha1" "tangled.org/guid.foo/simmer/cluster" "tangled.org/guid.foo/simmer/dataplane" "tangled.org/guid.foo/simmer/internal/world" "tangled.org/guid.foo/simmer/session" ) // backoffs are the waits after the first and the second failed turn in // a row. var backoffs = []time.Duration{time.Minute, 4 * time.Minute} // failTurns runs n turns of the fixture's Session that each take their // events and fail, waiting out each backoff, and answers the result of // the pass that recorded the last failure. func failTurns(t *testing.T, f *fixture, n int) session.Result { t.Helper() var result session.Result for turn := 1; turn <= n; turn++ { if turn > 1 { time.Sleep(backoffs[turn-2]) } f.pass(t, room, false) _, _ = f.store.Take(f.ref, turn) f.end(t, session.JobName("ops-a", turn), false) result = f.pass(t, room, false) } return result } func TestAFailedTurnWaitsOutABackoffBeforeTheNext(t *testing.T) { cases := []struct { name string failures int wait time.Duration }{ {"after one failure", 1, time.Minute}, {"after two failures in a row", 2, 4 * time.Minute}, } for _, c := range cases { t.Run(c.name, func(t *testing.T) { synctest.Test(t, func(t *testing.T) { f := start(t, []string{"k"}, 1) result := failTurns(t, f, c.failures) backingOff := f.session() time.Sleep(c.wait) f.pass(t, room, false) failed := backingOff.Status.Turns[c.failures-1].FinishedAt if backingOff.Status.WaitingOn != v1alpha1.WaitingOnBackoff || backingOff.Status.TurnCount != int32(c.failures) || !result.Wake.Equal(failed.Add(c.wait)) { t.Errorf("status = %+v, woke for %v; want waiting on backoff until %v", backingOff.Status, result.Wake, failed.Add(c.wait)) } if s := f.session(); s.Status.TurnCount != int32(c.failures+1) || s.Status.WaitingOn != v1alpha1.WaitingOnJob { t.Errorf("after the backoff: %+v, want the next turn running", s.Status) } }) }) } } func TestThreeFailedTurnsInARowFailTheSession(t *testing.T) { synctest.Test(t, func(t *testing.T) { f := start(t, []string{"k"}, 1) result := failTurns(t, f, 3) s := f.session() time.Sleep(10 * time.Minute) f.pass(t, room, false) _, retried := world.Get[cluster.Job](f.w, cluster.Jobs, "telemetry", "ops-a-4") time.Sleep(24 * time.Hour) f.pass(t, room, false) _, exists := world.Get[v1alpha1.Session](f.w, cluster.Sessions, "telemetry", "ops-a") ready := s.Status.Conditions[0] if s.Status.Phase != v1alpha1.PhaseFailed || s.Status.WaitingOn != v1alpha1.WaitingOnNothing || len(s.Status.Keys) != 0 || ready.Reason != v1alpha1.ReasonTurnsFailed { t.Errorf("status = %+v, want Failed on nothing, holding no keys, Ready False with TurnsFailed", s.Status) } if warned := warnings(f, v1alpha1.ReasonTurnsFailed); warned != 1 { t.Errorf("%d Warning Events with TurnsFailed, want one", warned) } if retried || s.Status.FinishedAt == nil || !result.Wake.Equal(s.Status.FinishedAt.Add(24*time.Hour)) || exists { t.Errorf("retried = %v, woke for %v, exists after 24h = %v; want no turn 4 and a deletion at its end of life", retried, result.Wake, exists) } }) } func warnings(f *fixture, reason string) int { count := 0 for _, e := range f.w.API.Events.About("Session", "ops-a") { if e.Type == "Warning" && e.Reason == reason { count++ } } return count } func TestInterruptedTurnsAreNoFailures(t *testing.T) { synctest.Test(t, func(t *testing.T) { f := start(t, []string{"k"}, 1) for turn := 1; turn <= 3; turn++ { f.pass(t, room, false) _, _ = f.store.Take(f.ref, turn) f.pass(t, room, true) } f.pass(t, room, false) if s := f.session(); s.Status.TurnCount != 4 || s.Status.Phase != v1alpha1.PhaseRunning { t.Errorf("status = %+v, want a fourth turn running after three interruptions", s.Status) } }) } // succeedTurns runs n turns of the fixture's Session that each take // their events and succeed, with a new event after each one. func succeedTurns(t *testing.T, f *fixture, n int) { t.Helper() for turn := 1; turn <= n; turn++ { f.pass(t, room, false) _, _ = f.store.Take(f.ref, turn) f.end(t, session.JobName("ops-a", turn), true) _ = f.store.Deliver(f.ref, dataplane.Event{ID: fmt.Sprintf("after-%d", turn), Signal: "alerts"}) } } func TestASessionThatNeedsATurnPastMaxTurnsFails(t *testing.T) { cases := []struct { name string maxTurns int32 phase v1alpha1.Phase reason string keys int }{ {"with a bound of two", 2, v1alpha1.PhaseFailed, v1alpha1.ReasonMaxTurns, 0}, {"with a bound of three", 3, v1alpha1.PhaseRunning, v1alpha1.ReasonReady, 1}, {"with no bound", 0, v1alpha1.PhaseRunning, v1alpha1.ReasonReady, 1}, } for _, c := range cases { t.Run(c.name, func(t *testing.T) { synctest.Test(t, func(t *testing.T) { f := start(t, []string{"k"}, 1) m := readyMandate() m.Spec.Sessions.MaxTurns = c.maxTurns f.w.Put(cluster.Mandates, m) synctest.Wait() succeedTurns(t, f, 2) f.pass(t, room, false) s := f.session() if s.Status.Phase != c.phase || s.Status.Conditions[0].Reason != c.reason || len(s.Status.Keys) != c.keys { t.Errorf("status = %+v, want %s with %s and %d keys", s.Status, c.phase, c.reason, c.keys) } }) }) } }