package thin import ( "context" "encoding/json" "testing" "amadan.net/oficina/equipo/internal/wire" ) // A turn held waiting for a connection ran nothing: restarting the runner // (twice, as two restages would) neither fails it runner_restarted nor // moves it to a new attempt; it stays held. func TestRecoverKeepsWaitingTurns(t *testing.T) { ctx := context.Background() s, err := OpenState(ctx, t.TempDir()) if err != nil { t.Fatal(err) } defer s.Close() if err = s.Accept(ctx, "c1", wire.MessageCommand{Person: "p@example.com", TurnID: "t1", Attempt: 1, Text: "hello", Tail: []wire.TailMessage{}}); err != nil { t.Fatal(err) } r, ok, err := s.next(ctx, "p@example.com", wire.LaneCoordinator) if err != nil || !ok { t.Fatal(ok, err) } if err = s.Hold(ctx, r, wire.ReasonNoConnection); err != nil { t.Fatal(err) } for i := 0; i < 2; i++ { if err = s.Recover(ctx); err != nil { t.Fatal(err) } } held, ok, err := s.next(ctx, "p@example.com", wire.LaneCoordinator) if err != nil || !ok || held.Attempt != 1 { t.Fatalf("after two restarts: %+v %v %v", held, ok, err) } frames, _, err := s.Unacked(ctx, 0) if err != nil { t.Fatal(err) } for _, f := range frames { var v wire.TurnReport if f.Type == wire.FrameTurn && json.Unmarshal(f.Data, &v) == nil && (v.Reason == "runner_restarted" || v.State == wire.TurnRunning) { t.Fatalf("a restart reported %+v for a waiting turn", v) } } }