| 1 | package thin |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "strings" |
| 6 | "sync" |
| 7 | "testing" |
| 8 | "time" |
| 9 | |
| 10 | "amadan.net/oficina/equipo/internal/runner" |
| 11 | "amadan.net/oficina/equipo/internal/wire" |
| 12 | ) |
| 13 | |
| 14 | // orderCoordinator records what happened to each coordinator process. |
| 15 | type orderCoordinator struct { |
| 16 | n int |
| 17 | log func(string) |
| 18 | } |
| 19 | |
| 20 | func (c *orderCoordinator) Turn(_ context.Context, in TurnInput, _ func(string)) TurnResult { |
| 21 | c.log("turn#" + itoa(int64(c.n)) + ":" + in.Text) |
| 22 | return TurnResult{Text: "ok"} |
| 23 | } |
| 24 | func (c *orderCoordinator) Close() { c.log("close#" + itoa(int64(c.n))) } |
| 25 | |
| 26 | // Astra 2b round 2: a pending reset is a prerequisite for running any |
| 27 | // turn. A reset recorded before a message (the order the instance sends |
| 28 | // them) is applied first, even when the message's wake comes first: the |
| 29 | // next turn runs on a new coordinator after the desk and session went. |
| 30 | func TestResetComesBeforeTheNextTurn(t *testing.T) { |
| 31 | ctx, cancel := context.WithCancel(context.Background()) |
| 32 | defer cancel() |
| 33 | state, err := OpenState(ctx, t.TempDir()) |
| 34 | if err != nil { |
| 35 | t.Fatal(err) |
| 36 | } |
| 37 | defer state.Close() |
| 38 | var mu sync.Mutex |
| 39 | var events []string |
| 40 | log := func(s string) { mu.Lock(); events = append(events, s); mu.Unlock() } |
| 41 | opened := 0 |
| 42 | e := &Engine{State: state, Emit: func(wire.DeltaReport) {}, Notify: func() {}, Open: func(string, string, runner.AccountBinding) (Coordinator, error) { |
| 43 | opened++ |
| 44 | log("open#" + itoa(int64(opened))) |
| 45 | return &orderCoordinator{n: opened, log: log}, nil |
| 46 | }, ResetDesk: func(string) error { log("reset"); return nil }} |
| 47 | if err := e.Start(ctx); err != nil { |
| 48 | t.Fatal(err) |
| 49 | } |
| 50 | defer e.Wait() |
| 51 | defer cancel() |
| 52 | waitFor := func(want string) { |
| 53 | t.Helper() |
| 54 | deadline := time.Now().Add(5 * time.Second) |
| 55 | for { |
| 56 | mu.Lock() |
| 57 | got := strings.Join(events, " ") |
| 58 | mu.Unlock() |
| 59 | if strings.Contains(got, want) { |
| 60 | return |
| 61 | } |
| 62 | if time.Now().After(deadline) { |
| 63 | t.Fatalf("events %q, want %q", got, want) |
| 64 | } |
| 65 | time.Sleep(10 * time.Millisecond) |
| 66 | } |
| 67 | } |
| 68 | person := "owner@example.com" |
| 69 | if err := state.Accept(ctx, "c1", wire.MessageCommand{Person: person, TurnID: "t1", Attempt: 1, Text: "first"}); err != nil { |
| 70 | t.Fatal(err) |
| 71 | } |
| 72 | e.Wake(person, wire.LaneCoordinator) |
| 73 | waitFor("turn#1:first") |
| 74 | // The stream reader records the reset, then the message, then wakes. |
| 75 | if _, err := state.AcceptReset(ctx, "c2", person); err != nil { |
| 76 | t.Fatal(err) |
| 77 | } |
| 78 | if err := state.Accept(ctx, "c3", wire.MessageCommand{Person: person, TurnID: "t2", Attempt: 1, Text: "second"}); err != nil { |
| 79 | t.Fatal(err) |
| 80 | } |
| 81 | e.Wake(person, wire.LaneCoordinator) |
| 82 | waitFor("turn#2:second") |
| 83 | mu.Lock() |
| 84 | got := strings.Join(events, " ") |
| 85 | mu.Unlock() |
| 86 | if got != "open#1 turn#1:first close#1 reset open#2 turn#2:second" { |
| 87 | t.Fatalf("order %q", got) |
| 88 | } |
| 89 | if pending, _ := state.pendingResets(ctx); len(pending) != 0 { |
| 90 | t.Fatalf("reset still pending: %v", pending) |
| 91 | } |
| 92 | } |
| 93 | |