| 1 | package thintest |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | "time" |
| 6 | |
| 7 | "amadan.net/oficina/equipo/internal/wire" |
| 8 | ) |
| 9 | |
| 10 | // warmCommands counts the warm commands the instance wrote for runners. |
| 11 | func (r *rig) warmCommands() int { |
| 12 | r.t.Helper() |
| 13 | var n int |
| 14 | if err := r.inst.DB.Writer().QueryRow("SELECT count(*) FROM runner_commands WHERE kind = ?", wire.FrameWarm).Scan(&n); err != nil { |
| 15 | r.t.Fatal(err) |
| 16 | } |
| 17 | return n |
| 18 | } |
| 19 | |
| 20 | // TestContractAssistantWarmOnOpen: Base reading the Assistant's |
| 21 | // snapshot, or opening its stream, asks the person's primary at |
| 22 | // protocol 24 to warm their Assistant: one warm command {person}, then |
| 23 | // none for five minutes however often Base reads. A primary below 24 is |
| 24 | // sent none and none is written for it. |
| 25 | func TestContractAssistantWarmOnOpen(t *testing.T) { |
| 26 | r := newRig(t) |
| 27 | runner := r.dialAt(wire.ProtocolWarm) |
| 28 | a := r.client.Assistant() |
| 29 | if _, err := a.Snapshot(r.ctx); err != nil { |
| 30 | t.Fatal(err) |
| 31 | } |
| 32 | f, body, err := runner.Expect(r.ctx, wire.FrameWarm) |
| 33 | if err != nil { |
| 34 | t.Fatal(err) |
| 35 | } |
| 36 | if w, ok := body.(*wire.WarmCommand); !ok || w.Person != Owner { |
| 37 | t.Fatalf("warm %+v", body) |
| 38 | } |
| 39 | runner.Send(r.ctx, "ack-warm", wire.FrameAck, wire.Ack{ID: f.ID}) |
| 40 | |
| 41 | // Within the window: more reads and a stream warm nothing; the next |
| 42 | // frame the runner sees is the message. |
| 43 | if _, err := a.Snapshot(r.ctx); err != nil { |
| 44 | t.Fatal(err) |
| 45 | } |
| 46 | s, err := a.Open(r.ctx, 0) |
| 47 | if err != nil { |
| 48 | t.Fatal(err) |
| 49 | } |
| 50 | s.Close() |
| 51 | r.assistantTurn(runner, a, "fix my website", "w-1") |
| 52 | if n := r.warmCommands(); n != 1 { |
| 53 | t.Fatalf("%d warm commands within the window", n) |
| 54 | } |
| 55 | |
| 56 | // Five minutes on (the runner still heard from), the stream's |
| 57 | // opening warms again. |
| 58 | var rev int64 |
| 59 | r.wait("the runner's revision", func(c *Conversation) bool { rev = c.Runner.Revision; return true }) |
| 60 | later := time.Now().UTC().Add(warmWindow + time.Second) |
| 61 | r.inst.Store.Chat().SetClock(func() time.Time { return later }) |
| 62 | t.Cleanup(func() { r.inst.Store.Chat().SetClock(func() time.Time { return time.Now().UTC() }) }) |
| 63 | if err := runner.Send(r.ctx, "h-later", wire.FrameHello, primaryAt(wire.Hello{Generation: runner.Welcome.Generation, Repos: []string{}, Connections: []wire.HeldConnection{}, Versions: map[string]string{"runner": "test"}}, wire.ProtocolWarm)); err != nil { |
| 64 | t.Fatal(err) |
| 65 | } |
| 66 | // Offline by the later clock until this hello: a new revision. |
| 67 | r.wait("runner heard later", func(c *Conversation) bool { return c.Runner.Online && c.Runner.Revision > rev }) |
| 68 | s, err = a.Open(r.ctx, 0) |
| 69 | if err != nil { |
| 70 | t.Fatal(err) |
| 71 | } |
| 72 | defer s.Close() |
| 73 | if _, body, err := runner.Expect(r.ctx, wire.FrameWarm); err != nil || body.(*wire.WarmCommand).Person != Owner { |
| 74 | t.Fatalf("no second warm: %v", err) |
| 75 | } |
| 76 | |
| 77 | // A primary at protocol 23 is sent none. |
| 78 | old := newRig(t) |
| 79 | oldRunner := old.dialAt(wire.ProtocolGlance) |
| 80 | oa := old.client.Assistant() |
| 81 | if _, err := oa.Snapshot(old.ctx); err != nil { |
| 82 | t.Fatal(err) |
| 83 | } |
| 84 | old.assistantTurn(oldRunner, oa, "fix my website", "o-1") |
| 85 | if n := old.warmCommands(); n != 0 { |
| 86 | t.Fatalf("%d warm commands for a runner at 23", n) |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | // warmWindow is the instance's warmEvery. |
| 91 | const warmWindow = 5 * time.Minute |
| 92 | |