| 1 | package thintest |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "strconv" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | "time" |
| 10 | |
| 11 | "amadan.net/oficina/equipo/internal/wire" |
| 12 | ) |
| 13 | |
| 14 | // Turn batching: messages sent while a turn runs wait in one queued turn |
| 15 | // and go to the coordinator together when it ends, exactly once, however |
| 16 | // it ends and whatever crashes between. The runner here speaks protocol |
| 17 | // 1, the oldest the instance serves. |
| 18 | |
| 19 | // busyWithThree starts a turn, has the runner take it, and queues three |
| 20 | // messages behind it. |
| 21 | func (r *rig) busyWithThree(runner *FakeRunner) (wire.Sent, []wire.Sent) { |
| 22 | r.t.Helper() |
| 23 | busy, err := r.client.Send(r.ctx, "one", "k0") |
| 24 | if err != nil { |
| 25 | r.t.Fatal(err) |
| 26 | } |
| 27 | f, _, err := runner.Expect(r.ctx, wire.FrameMessage) |
| 28 | if err != nil { |
| 29 | r.t.Fatal(err) |
| 30 | } |
| 31 | runner.Send(r.ctx, "a0", wire.FrameAck, wire.Ack{ID: f.ID}) |
| 32 | r.turnIs(busy.Turn.ID, wire.TurnRunning) |
| 33 | var queued []wire.Sent |
| 34 | for i, text := range []string{"alpha", "bravo", "charlie"} { |
| 35 | s, err := r.client.Send(r.ctx, text, "kq"+string(rune('1'+i))) |
| 36 | if err != nil || s.Turn.ID == busy.Turn.ID || s.Turn.State != wire.TurnQueued || len(queued) > 0 && s.Turn.ID != queued[0].Turn.ID { |
| 37 | r.t.Fatalf("message %d: %+v %v", i+1, s.Turn, err) |
| 38 | } |
| 39 | queued = append(queued, s) |
| 40 | } |
| 41 | return busy, queued |
| 42 | } |
| 43 | |
| 44 | // acked waits for the instance's ack of one of the runner's frames. |
| 45 | func (r *rig) acked(runner *FakeRunner, id string) { |
| 46 | r.t.Helper() |
| 47 | for { |
| 48 | _, body, err := runner.Expect(r.ctx, wire.FrameAck) |
| 49 | if err != nil { |
| 50 | r.t.Fatalf("no ack for %s: %v", id, err) |
| 51 | } |
| 52 | if body.(*wire.Ack).ID == id { |
| 53 | return |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // turnIs waits until the API reads a turn in state. |
| 59 | func (r *rig) turnIs(id, state string) wire.Turn { |
| 60 | r.t.Helper() |
| 61 | var got wire.Turn |
| 62 | for deadline := time.Now().Add(10 * time.Second); time.Now().Before(deadline); time.Sleep(20 * time.Millisecond) { |
| 63 | var err error |
| 64 | if got, err = r.client.Turn(r.ctx, id); err == nil && got.State == state { |
| 65 | return got |
| 66 | } |
| 67 | } |
| 68 | r.t.Fatalf("turn %s is %s (%s), not %s", id, got.State, got.Reason, state) |
| 69 | return got |
| 70 | } |
| 71 | |
| 72 | // batchFrame expects the next message frame to be the batch: the three |
| 73 | // messages apart and in order, and nothing else. |
| 74 | func (r *rig) batchFrame(runner *FakeRunner, batch string, texts ...string) wire.Frame { |
| 75 | r.t.Helper() |
| 76 | return r.batchFrameCarrying(runner, batch, nil, texts...) |
| 77 | } |
| 78 | |
| 79 | // batchFrameCarrying is batchFrame for a batch after failed turns: their |
| 80 | // person's words (carried) come first in PersonText, marked unanswered |
| 81 | // in Text before the batch. |
| 82 | func (r *rig) batchFrameCarrying(runner *FakeRunner, batch string, carried []string, texts ...string) wire.Frame { |
| 83 | r.t.Helper() |
| 84 | ctx, cancel := context.WithTimeout(r.ctx, 10*time.Second) |
| 85 | defer cancel() |
| 86 | f, body, err := runner.Expect(ctx, wire.FrameMessage) |
| 87 | if err != nil { |
| 88 | r.t.Fatalf("no next turn: %v", err) |
| 89 | } |
| 90 | cmd := body.(*wire.MessageCommand) |
| 91 | if cmd.TurnID != batch || len(cmd.PersonText) != len(carried)+len(texts) { |
| 92 | r.t.Fatalf("next turn %s carries %q, not the batch %s", cmd.TurnID, cmd.PersonText, batch) |
| 93 | } |
| 94 | at := -1 |
| 95 | for i, text := range carried { |
| 96 | n := strings.Index(cmd.Text, "Not yet answered, the person's words: "+strconv.Quote(text)) |
| 97 | if n <= at || cmd.PersonText[i] != text { |
| 98 | r.t.Fatalf("batch lacks unanswered %q in place:\n%s", text, cmd.Text) |
| 99 | } |
| 100 | at = n |
| 101 | } |
| 102 | for i, text := range texts { |
| 103 | n := strings.Index(cmd.Text, "Message "+string(rune('1'+i))+" of "+string(rune('0'+len(texts)))+":\n"+text) |
| 104 | if n <= at || cmd.PersonText[len(carried)+i] != text { |
| 105 | r.t.Fatalf("batch lacks %q in place:\n%s", text, cmd.Text) |
| 106 | } |
| 107 | at = n |
| 108 | } |
| 109 | shape(r.t, "frames/message.json", runner.Received[len(runner.Received)-1]) |
| 110 | return f |
| 111 | } |
| 112 | |
| 113 | // exactlyOnce checks a batch went to the runner in one message frame, |
| 114 | // that the conversation has turns turns, and that no turn has two replies. |
| 115 | func (r *rig) exactlyOnce(batch string, turns int) { |
| 116 | r.t.Helper() |
| 117 | var frames, all, replies int |
| 118 | db := r.inst.DB.Writer() |
| 119 | if err := db.QueryRow("SELECT count(*) FROM runner_commands WHERE kind = ? AND body LIKE ?", wire.FrameMessage, `%"turn_id":"`+batch+`"%`).Scan(&frames); err != nil { |
| 120 | r.t.Fatal(err) |
| 121 | } |
| 122 | if err := db.QueryRow("SELECT count(*) FROM chat_turns").Scan(&all); err != nil { |
| 123 | r.t.Fatal(err) |
| 124 | } |
| 125 | if err := db.QueryRow("SELECT coalesce(max(n), 0) FROM (SELECT count(*) AS n FROM chat_messages WHERE role = ? GROUP BY turn_id)", wire.RoleCoordinator).Scan(&replies); err != nil { |
| 126 | r.t.Fatal(err) |
| 127 | } |
| 128 | if frames != 1 || all != turns { |
| 129 | r.t.Fatalf("batch %s went out %d times; %d turns, want %d", batch, frames, all, turns) |
| 130 | } |
| 131 | if replies > 1 { |
| 132 | r.t.Fatalf("a turn has %d replies", replies) |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | // A crash between the end of a turn and the start of the next leaves |
| 137 | // neither: the instance commits both in one transaction, so the runner's |
| 138 | // reply comes again after the restart and starts exactly one next turn. |
| 139 | func TestBatchCrashBetweenTurns(t *testing.T) { |
| 140 | r := newRig(t) |
| 141 | runner := r.dial(0) |
| 142 | busy, queued := r.busyWithThree(runner) |
| 143 | batch := queued[0].Turn.ID |
| 144 | db := r.inst.DB.Writer() |
| 145 | if _, err := db.Exec(`CREATE TRIGGER batch_crash BEFORE INSERT ON runner_commands WHEN NEW.kind = 'message' BEGIN SELECT RAISE(ABORT, 'crash'); END`); err != nil { |
| 146 | t.Fatal(err) |
| 147 | } |
| 148 | reply := wire.Reply{TurnID: busy.Turn.ID, Attempt: 1, Text: "first"} |
| 149 | runner.Send(r.ctx, "rep1", wire.FrameReply, reply) |
| 150 | for { |
| 151 | _, body, err := runner.Next(r.ctx) |
| 152 | if err != nil { |
| 153 | break |
| 154 | } |
| 155 | if a, ok := body.(*wire.Ack); ok && a.ID == "rep1" { |
| 156 | t.Fatal("the reply was acked with the next turn refused") |
| 157 | } |
| 158 | } |
| 159 | if st, _ := r.client.Turn(r.ctx, busy.Turn.ID); st.State != wire.TurnRunning { |
| 160 | t.Fatalf("turn ended without its successor: %s", st.State) |
| 161 | } |
| 162 | if st, _ := r.client.Turn(r.ctx, batch); st.State != wire.TurnQueued || len(st.MessageIDs) != 3 { |
| 163 | t.Fatalf("batch %+v", st) |
| 164 | } |
| 165 | if _, err := db.Exec(`DROP TRIGGER batch_crash`); err != nil { |
| 166 | t.Fatal(err) |
| 167 | } |
| 168 | r.inst.Restart(t) |
| 169 | again := r.dial(runner.Welcome.Generation) |
| 170 | again.Send(r.ctx, "s1", wire.FrameSync, wire.Sync{Sessions: []wire.SessionState{}, Runs: []wire.Run{{TurnID: busy.Turn.ID, Attempt: 1, State: wire.TurnRunning}}}) |
| 171 | // Frames apply in order: once a no-op report is acked, the sync is in. |
| 172 | again.Send(r.ctx, "s1b", wire.FrameTurn, wire.TurnReport{TurnID: busy.Turn.ID, Attempt: 1, State: wire.TurnRunning}) |
| 173 | r.acked(again, "s1b") |
| 174 | // The runner still holds the turn: the batch waits for it. |
| 175 | var early int |
| 176 | if err := r.inst.DB.Writer().QueryRow("SELECT count(*) FROM runner_commands WHERE kind = ? AND body LIKE ?", wire.FrameMessage, `%"turn_id":"`+batch+`"%`).Scan(&early); err != nil || early != 0 { |
| 177 | t.Fatalf("batch went out beside the running turn: %d %v", early, err) |
| 178 | } |
| 179 | again.Send(r.ctx, "rep1", wire.FrameReply, reply) |
| 180 | f := r.batchFrame(again, batch, "alpha", "bravo", "charlie") |
| 181 | // The same reply once more, and a reconnect that resends the unacked |
| 182 | // frame: still the one batch. |
| 183 | again.Send(r.ctx, "rep1", wire.FrameReply, reply) |
| 184 | again.Close() |
| 185 | back := r.dial(runner.Welcome.Generation) |
| 186 | if f2 := r.batchFrame(back, batch, "alpha", "bravo", "charlie"); f2.ID != f.ID { |
| 187 | t.Fatalf("reconnect wrote a new frame %s, not %s", f2.ID, f.ID) |
| 188 | } |
| 189 | back.Send(r.ctx, "ab", wire.FrameAck, wire.Ack{ID: f.ID}) |
| 190 | r.turnIs(batch, wire.TurnRunning) |
| 191 | back.Send(r.ctx, "rep2", wire.FrameReply, wire.Reply{TurnID: batch, Attempt: 1, Text: "all three"}) |
| 192 | r.turnIs(batch, wire.TurnReplied) |
| 193 | r.exactlyOnce(batch, 2) |
| 194 | } |
| 195 | |
| 196 | // A turn whose end committed without its successor (an older instance, or |
| 197 | // a failure this one does not have) recovers on the runner's next sync: |
| 198 | // exactly one next turn, the waiting messages in it. |
| 199 | func TestBatchStrandedRecovers(t *testing.T) { |
| 200 | r := newRig(t) |
| 201 | runner := r.dial(0) |
| 202 | busy, queued := r.busyWithThree(runner) |
| 203 | batch := queued[0].Turn.ID |
| 204 | if _, err := r.inst.DB.Writer().Exec("UPDATE chat_turns SET state = ? WHERE id = ?", wire.TurnFailed, busy.Turn.ID); err != nil { |
| 205 | t.Fatal(err) |
| 206 | } |
| 207 | runner.Close() |
| 208 | r.inst.Restart(t) |
| 209 | again := r.dial(runner.Welcome.Generation) |
| 210 | again.Send(r.ctx, "s1", wire.FrameSync, wire.Sync{Sessions: []wire.SessionState{}, Runs: []wire.Run{}}) |
| 211 | f := r.batchFrameCarrying(again, batch, []string{"one"}, "alpha", "bravo", "charlie") |
| 212 | again.Send(r.ctx, "s2", wire.FrameSync, wire.Sync{Sessions: []wire.SessionState{}, Runs: []wire.Run{}}) |
| 213 | again.Send(r.ctx, "ab", wire.FrameAck, wire.Ack{ID: f.ID}) |
| 214 | r.turnIs(batch, wire.TurnRunning) |
| 215 | r.exactlyOnce(batch, 2) |
| 216 | } |
| 217 | |
| 218 | // However the running turn ends (failed, cancelled, or lost across a |
| 219 | // runner reconnect), the batch goes next, once, and the ended turn keeps |
| 220 | // its own message. A failed turn's words ride before the batch, marked |
| 221 | // unanswered; a cancelled turn's do not (the person stopped it). |
| 222 | func TestBatchAfterTurnEnds(t *testing.T) { |
| 223 | for _, end := range []string{"failed", "cancelled", "interrupted"} { |
| 224 | t.Run(end, func(t *testing.T) { |
| 225 | r := newRig(t) |
| 226 | runner := r.dial(0) |
| 227 | busy, queued := r.busyWithThree(runner) |
| 228 | batch := queued[0].Turn.ID |
| 229 | switch end { |
| 230 | case "failed": |
| 231 | runner.Send(r.ctx, "e1", wire.FrameTurn, wire.TurnReport{TurnID: busy.Turn.ID, Attempt: 1, State: wire.TurnFailed, Reason: "provider_failed"}) |
| 232 | case "cancelled": |
| 233 | if _, err := r.client.Cancel(r.ctx, busy.Turn.ID); err != nil { |
| 234 | t.Fatal(err) |
| 235 | } |
| 236 | if _, _, err := runner.Expect(r.ctx, wire.FrameCancel); err != nil { |
| 237 | t.Fatal(err) |
| 238 | } |
| 239 | runner.Send(r.ctx, "e1", wire.FrameTurn, wire.TurnReport{TurnID: busy.Turn.ID, Attempt: 1, State: wire.TurnCancelled, Reason: "cancelled"}) |
| 240 | case "interrupted": |
| 241 | // A new runner process no longer holds the turn. |
| 242 | runner.Close() |
| 243 | runner = r.dial(0) |
| 244 | runner.Send(r.ctx, "e1", wire.FrameSync, wire.Sync{Sessions: []wire.SessionState{}, Runs: []wire.Run{}}) |
| 245 | } |
| 246 | var carried []string |
| 247 | if end != "cancelled" { |
| 248 | carried = []string{"one"} |
| 249 | } |
| 250 | r.batchFrameCarrying(runner, batch, carried, "alpha", "bravo", "charlie") |
| 251 | if st, _ := r.client.Turn(r.ctx, busy.Turn.ID); !wire.Terminal(st.State) || len(st.MessageIDs) != 1 { |
| 252 | t.Fatalf("ended turn %+v", st) |
| 253 | } |
| 254 | r.exactlyOnce(batch, 2) |
| 255 | }) |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | // A turn the runner retries (a new attempt on another connection) keeps |
| 260 | // the batch waiting: a message sent meanwhile joins it, and the batch |
| 261 | // goes once, after the retried attempt's reply. A late reply from the |
| 262 | // abandoned attempt changes nothing. |
| 263 | func TestBatchWaitsForRetriedTurn(t *testing.T) { |
| 264 | r := newRig(t) |
| 265 | runner := r.dial(0) |
| 266 | busy, queued := r.busyWithThree(runner) |
| 267 | batch := queued[0].Turn.ID |
| 268 | runner.Send(r.ctx, "r2", wire.FrameTurn, wire.TurnReport{TurnID: busy.Turn.ID, Attempt: 2, State: wire.TurnRunning}) |
| 269 | for got := r.turnIs(busy.Turn.ID, wire.TurnRunning); got.Attempt != 2; got = r.turnIs(busy.Turn.ID, wire.TurnRunning) { |
| 270 | time.Sleep(20 * time.Millisecond) |
| 271 | } |
| 272 | fourth, err := r.client.Send(r.ctx, "delta", "kq4") |
| 273 | if err != nil || fourth.Turn.ID != batch || len(fourth.Turn.MessageIDs) != 4 { |
| 274 | t.Fatalf("message during the retry: %+v %v", fourth.Turn, err) |
| 275 | } |
| 276 | runner.Send(r.ctx, "late", wire.FrameReply, wire.Reply{TurnID: busy.Turn.ID, Attempt: 1, Text: "stale"}) |
| 277 | runner.Send(r.ctx, "rep", wire.FrameReply, wire.Reply{TurnID: busy.Turn.ID, Attempt: 2, Text: "first"}) |
| 278 | r.batchFrame(runner, batch, "alpha", "bravo", "charlie", "delta") |
| 279 | if got := r.turnIs(busy.Turn.ID, wire.TurnReplied); got.Attempt != 2 { |
| 280 | t.Fatalf("replied on attempt %d", got.Attempt) |
| 281 | } |
| 282 | r.exactlyOnce(batch, 2) |
| 283 | } |
| 284 | |
| 285 | // Cancelling the waiting batch ends all its messages together, unsent; |
| 286 | // the next message starts a turn of its own without them. |
| 287 | func TestBatchCancelledWhileWaiting(t *testing.T) { |
| 288 | r := newRig(t) |
| 289 | runner := r.dial(0) |
| 290 | busy, queued := r.busyWithThree(runner) |
| 291 | batch := queued[0].Turn.ID |
| 292 | if _, err := r.client.Cancel(r.ctx, batch); err != nil { |
| 293 | t.Fatal(err) |
| 294 | } |
| 295 | if got := r.turnIs(batch, wire.TurnCancelled); len(got.MessageIDs) != 3 { |
| 296 | t.Fatalf("cancelled batch %+v", got) |
| 297 | } |
| 298 | later, err := r.client.Send(r.ctx, "echo", "kq5") |
| 299 | if err != nil || later.Turn.ID == batch || len(later.Turn.MessageIDs) != 1 { |
| 300 | t.Fatalf("later message %+v %v", later.Turn, err) |
| 301 | } |
| 302 | runner.Send(r.ctx, "rep", wire.FrameReply, wire.Reply{TurnID: busy.Turn.ID, Attempt: 1, Text: "first"}) |
| 303 | _, body, err := runner.Expect(r.ctx, wire.FrameMessage) |
| 304 | if err != nil { |
| 305 | t.Fatal(err) |
| 306 | } |
| 307 | if cmd := body.(*wire.MessageCommand); cmd.TurnID != later.Turn.ID || withoutProjectContext(t, cmd.Text) != "echo" { |
| 308 | t.Fatalf("after a cancelled batch the runner got %s %q", cmd.TurnID, cmd.Text) |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | // Each waiting message keeps its own bubble in the queued turn, with the |
| 313 | // one quiet note under the latest. The note goes for all of them together |
| 314 | // the moment the turn picks them up (its frame is out), before the runner |
| 315 | // acks it, and the bubbles stay. |
| 316 | func TestBatchMarkerClearsTogether(t *testing.T) { |
| 317 | r := newRig(t) |
| 318 | runner := r.dial(0) |
| 319 | busy, queued := r.busyWithThree(runner) |
| 320 | batch := queued[0].Turn.ID |
| 321 | const note = "<p equipo-card-note data-queued>" |
| 322 | bubbles := func(page string) int { |
| 323 | at := -1 |
| 324 | for i, m := range queued { |
| 325 | n := strings.Index(page, `data-message="`+m.Message.ID+`" data-turn-id="`+batch+`"`) |
| 326 | if n <= at { |
| 327 | t.Fatalf("bubble %d missing or out of order", i+1) |
| 328 | } |
| 329 | at = n |
| 330 | } |
| 331 | return at |
| 332 | } |
| 333 | page := r.inst.Get(t, "/conversation") |
| 334 | if last := bubbles(page); strings.Count(page, note) != 1 || strings.Index(page, note) < last { |
| 335 | t.Fatal("waiting messages lack the one note under the latest") |
| 336 | } |
| 337 | runner.Send(r.ctx, "rep", wire.FrameReply, wire.Reply{TurnID: busy.Turn.ID, Attempt: 1, Text: "first"}) |
| 338 | f := r.batchFrame(runner, batch, "alpha", "bravo", "charlie") |
| 339 | if page = r.inst.Get(t, "/conversation"); strings.Contains(page, note) { |
| 340 | t.Fatal("the note stayed after the turn picked the messages up") |
| 341 | } |
| 342 | bubbles(page) |
| 343 | runner.Send(r.ctx, "ab", wire.FrameAck, wire.Ack{ID: f.ID}) |
| 344 | r.turnIs(batch, wire.TurnRunning) |
| 345 | if page = r.inst.Get(t, "/conversation"); strings.Contains(page, note) { |
| 346 | t.Fatal("the note came back while the turn runs") |
| 347 | } |
| 348 | bubbles(page) |
| 349 | } |
| 350 | |
| 351 | // Messages that would not fit one frame together go as several batches, |
| 352 | // one turn after another, in order. Each frame fits (the runner reads at |
| 353 | // most wire.MaxFrameBytes): the oldest tail goes first, and a later |
| 354 | // batch's messages are never in an earlier one's tail. |
| 355 | func TestBatchSplitsToFitTheFrame(t *testing.T) { |
| 356 | r := newRig(t) |
| 357 | runner := r.dial(0) |
| 358 | busy, err := r.client.Send(r.ctx, "one", "k0") |
| 359 | if err != nil { |
| 360 | t.Fatal(err) |
| 361 | } |
| 362 | f, _, _ := runner.Expect(r.ctx, wire.FrameMessage) |
| 363 | runner.Send(r.ctx, "a0", wire.FrameAck, wire.Ack{ID: f.ID}) |
| 364 | r.turnIs(busy.Turn.ID, wire.TurnRunning) |
| 365 | var texts []string |
| 366 | turns := map[string][]string{} |
| 367 | var order []string |
| 368 | for i := range 12 { |
| 369 | // Near the largest message, with characters JSON escapes (the |
| 370 | // person API reads at most twice the limit). |
| 371 | text := fmt.Sprintf("m%02d %s%s", i+1, strings.Repeat("x", wire.MaxMessageBytes-2008), strings.Repeat("<", 2000)) |
| 372 | s, err := r.client.Send(r.ctx, text, fmt.Sprintf("kb%d", i)) |
| 373 | if err != nil { |
| 374 | t.Fatal(err) |
| 375 | } |
| 376 | if _, ok := turns[s.Turn.ID]; !ok { |
| 377 | order = append(order, s.Turn.ID) |
| 378 | } |
| 379 | turns[s.Turn.ID] = append(turns[s.Turn.ID], text) |
| 380 | texts = append(texts, text) |
| 381 | } |
| 382 | if len(order) < 2 || len(turns[order[0]]) < 2 { |
| 383 | t.Fatalf("12 large messages made batches of %v", order) |
| 384 | } |
| 385 | end := wire.Reply{TurnID: busy.Turn.ID, Attempt: 1, Text: "first"} |
| 386 | var got []string |
| 387 | for i, batch := range order { |
| 388 | runner.Send(r.ctx, fmt.Sprintf("rep%d", i), wire.FrameReply, end) |
| 389 | mf, body, err := runner.Expect(r.ctx, wire.FrameMessage) |
| 390 | if err != nil { |
| 391 | t.Fatalf("batch %d: %v", i+1, err) |
| 392 | } |
| 393 | cmd := body.(*wire.MessageCommand) |
| 394 | if cmd.TurnID != batch || len(cmd.PersonText) != len(turns[batch]) { |
| 395 | t.Fatalf("batch %d is %s with %d messages", i+1, cmd.TurnID, len(cmd.PersonText)) |
| 396 | } |
| 397 | for _, m := range cmd.Tail { |
| 398 | for _, later := range order[i+1:] { |
| 399 | for _, text := range turns[later] { |
| 400 | if m.Text == text { |
| 401 | t.Fatalf("batch %d's tail holds a message still waiting", i+1) |
| 402 | } |
| 403 | } |
| 404 | } |
| 405 | } |
| 406 | if len(cmd.Tail) == 0 || cmd.Tail[len(cmd.Tail)-1].Text != end.Text { |
| 407 | t.Fatalf("batch %d's tail lost the newest reply", i+1) |
| 408 | } |
| 409 | got = append(got, cmd.PersonText...) |
| 410 | runner.Send(r.ctx, fmt.Sprintf("ab%d", i), wire.FrameAck, wire.Ack{ID: mf.ID}) |
| 411 | r.turnIs(batch, wire.TurnRunning) |
| 412 | end = wire.Reply{TurnID: batch, Attempt: 1, Text: fmt.Sprintf("reply %d", i+1)} |
| 413 | } |
| 414 | if len(got) != len(texts) { |
| 415 | t.Fatalf("%d of %d messages reached the coordinator", len(got), len(texts)) |
| 416 | } |
| 417 | for i := range texts { |
| 418 | if got[i] != texts[i] { |
| 419 | t.Fatalf("message %d out of place", i+1) |
| 420 | } |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | // A failed batch says the coordinator could not answer these messages; a |
| 425 | // failed turn of one message still says this message. (A reason with |
| 426 | // words of its own says those: chat_failed.go.) |
| 427 | func TestBatchFailedSaysThemAll(t *testing.T) { |
| 428 | r := newRig(t) |
| 429 | runner := r.dial(0) |
| 430 | busy, queued := r.busyWithThree(runner) |
| 431 | batch := queued[0].Turn.ID |
| 432 | runner.Send(r.ctx, "rep", wire.FrameReply, wire.Reply{TurnID: busy.Turn.ID, Attempt: 1, Text: "first"}) |
| 433 | f := r.batchFrame(runner, batch, "alpha", "bravo", "charlie") |
| 434 | runner.Send(r.ctx, "ab", wire.FrameAck, wire.Ack{ID: f.ID}) |
| 435 | runner.Send(r.ctx, "fb", wire.FrameTurn, wire.TurnReport{TurnID: batch, Attempt: 1, State: wire.TurnFailed, Reason: ""}) |
| 436 | r.turnIs(batch, wire.TurnFailed) |
| 437 | if page := r.inst.Get(t, "/conversation"); !strings.Contains(page, `data-ended="`+batch+`"><p>The coordinator could not answer these messages.</p>`) { |
| 438 | t.Fatal("a failed batch lacks the note for its messages") |
| 439 | } |
| 440 | one, err := r.client.Send(r.ctx, "delta", "kq6") |
| 441 | if err != nil { |
| 442 | t.Fatal(err) |
| 443 | } |
| 444 | of, _, _ := runner.Expect(r.ctx, wire.FrameMessage) |
| 445 | runner.Send(r.ctx, "a1", wire.FrameAck, wire.Ack{ID: of.ID}) |
| 446 | runner.Send(r.ctx, "f1", wire.FrameTurn, wire.TurnReport{TurnID: one.Turn.ID, Attempt: 1, State: wire.TurnFailed, Reason: ""}) |
| 447 | r.turnIs(one.Turn.ID, wire.TurnFailed) |
| 448 | if page := r.inst.Get(t, "/conversation"); !strings.Contains(page, `data-ended="`+one.Turn.ID+`"><p>The coordinator could not answer this message.</p>`) { |
| 449 | t.Fatal("a failed single message lacks its note") |
| 450 | } |
| 451 | } |
| 452 | |