| 1 | package thin |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "testing" |
| 7 | |
| 8 | "amadan.net/oficina/equipo/internal/wire" |
| 9 | ) |
| 10 | |
| 11 | // A turn held waiting for a connection ran nothing: restarting the runner |
| 12 | // (twice, as two restages would) neither fails it runner_restarted nor |
| 13 | // moves it to a new attempt; it stays held. |
| 14 | func TestRecoverKeepsWaitingTurns(t *testing.T) { |
| 15 | ctx := context.Background() |
| 16 | s, err := OpenState(ctx, t.TempDir()) |
| 17 | if err != nil { |
| 18 | t.Fatal(err) |
| 19 | } |
| 20 | defer s.Close() |
| 21 | if err = s.Accept(ctx, "c1", wire.MessageCommand{Person: "p@example.com", TurnID: "t1", Attempt: 1, Text: "hello", Tail: []wire.TailMessage{}}); err != nil { |
| 22 | t.Fatal(err) |
| 23 | } |
| 24 | r, ok, err := s.next(ctx, "p@example.com", wire.LaneCoordinator) |
| 25 | if err != nil || !ok { |
| 26 | t.Fatal(ok, err) |
| 27 | } |
| 28 | if err = s.Hold(ctx, r, wire.ReasonNoConnection); err != nil { |
| 29 | t.Fatal(err) |
| 30 | } |
| 31 | for i := 0; i < 2; i++ { |
| 32 | if err = s.Recover(ctx); err != nil { |
| 33 | t.Fatal(err) |
| 34 | } |
| 35 | } |
| 36 | held, ok, err := s.next(ctx, "p@example.com", wire.LaneCoordinator) |
| 37 | if err != nil || !ok || held.Attempt != 1 { |
| 38 | t.Fatalf("after two restarts: %+v %v %v", held, ok, err) |
| 39 | } |
| 40 | frames, _, err := s.Unacked(ctx, 0) |
| 41 | if err != nil { |
| 42 | t.Fatal(err) |
| 43 | } |
| 44 | for _, f := range frames { |
| 45 | var v wire.TurnReport |
| 46 | if f.Type == wire.FrameTurn && json.Unmarshal(f.Data, &v) == nil && (v.Reason == "runner_restarted" || v.State == wire.TurnRunning) { |
| 47 | t.Fatalf("a restart reported %+v for a waiting turn", v) |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | |