gunk-dev/the-valley

timing_test.go

source747d22d16f38main's published tip history
6.4 KiB
1package main
2
3// A decision the integrator reports is followed by its timing line, and a
4// decision it does not report again is not timed again. A refusal is the
5// decision used here: it is decided on every pass, reported only when it
6// is new, and needs no policy or evidence to reach.
7
8import (
9 "encoding/json"
10 "os"
11 "path/filepath"
12 "strings"
13 "testing"
14 "time"
15
16 "the-valley/change"
17)
18
19// refusedRequest files a request for a draft, which a pass refuses.
20func refusedRequest(t *testing.T) (*scratchRepo, request) {
21 t.Helper()
22 s := newScratchRepo(t)
23 base := s.git("rev-parse", "HEAD")
24 drafted := s.commit("drafted", base)
25 s.record(draftID, change.Draft, drafted)
26 r := request{
27 ref: requestPrefix + "/main/" + change.RequestName(draftID),
28 change: change.RequestName(draftID),
29 target: "refs/heads/main",
30 head: drafted,
31 }
32 s.git("update-ref", r.ref, drafted)
33 return s, r
34}
35
36// timingWatch is where a test's timing lines go. Beside each line it keeps
37// what the request's outcome record said when the line was written.
38type timingWatch struct {
39 in *integrator
40 r request
41 lines []string
42 records []string
43}
44
45func (w *timingWatch) Write(p []byte) (int, error) {
46 record, _ := w.in.readMemo(w.r)
47 w.lines = append(w.lines, strings.TrimSuffix(string(p), "\n"))
48 w.records = append(w.records, record)
49 return len(p), nil
50}
51
52func TestADecisionIsFollowedByItsTimingLine(t *testing.T) {
53 s, r := refusedRequest(t)
54 out, err := os.Create(filepath.Join(t.TempDir(), "out"))
55 if err != nil {
56 t.Fatal(err)
57 }
58 defer out.Close()
59 in := &integrator{repo: s.repo, project: "project", out: out}
60 watch := &timingWatch{in: in, r: r}
61 in.timings = watch
62 pass := func() {
63 t.Helper()
64 idx, err := in.readRecords()
65 if err != nil {
66 t.Fatal(err)
67 }
68 if err := in.handle(r, idx, inputs{digest: "i"}); err != nil {
69 t.Fatal(err)
70 }
71 }
72
73 pass()
74 if len(watch.lines) != 1 {
75 t.Fatalf("one refusal printed %d timing lines, want 1", len(watch.lines))
76 }
77 line, ok := strings.CutPrefix(watch.lines[0], " timing ")
78 if !ok {
79 t.Fatalf("the timing line has no label: %q", watch.lines[0])
80 }
81 var got decisionTiming
82 if err := json.Unmarshal([]byte(line), &got); err != nil {
83 t.Fatalf("the timing line is not JSON: %v: %s", err, line)
84 }
85 if got.Project != "project" || got.Request != r.ref || got.Change != r.change || got.Head != r.head ||
86 got.Target != "refs/heads/main" || got.Tip != s.git("rev-parse", "HEAD") || got.Outcome != string(refusedNotReady) {
87 t.Errorf("the timing line says %+v", got)
88 }
89 if _, ok := got.StagesMS[stageRefusal]; !ok || len(got.StagesMS) != 1 {
90 t.Errorf("a refusal is timed in the stages %v, want the refusal stage alone", got.StagesMS)
91 }
92 if got.DurationMS < got.StagesMS[stageRefusal] {
93 t.Errorf("the decision took %dms, less than its refusal stage's %dms", got.DurationMS, got.StagesMS[stageRefusal])
94 }
95 // The outcome was recorded before its timing line was printed.
96 if !strings.HasSuffix(watch.records[0], " "+string(refusedNotReady)) {
97 t.Errorf("when the timing line was printed, the outcome record said %q", watch.records[0])
98 }
99
100 // The same refusal on the next pass is not reported, and not timed.
101 if pass(); len(watch.lines) != 1 {
102 t.Fatalf("a refusal repeated printed a timing line again: %q", watch.lines)
103 }
104}
105
106// A timing line nobody reads holds no decision up. Here the integrator's
107// timing output is a pipe that is full and never read, so the line can
108// never be written: the outcome is recorded all the same, and the pass
109// goes on once the line's deadline has passed.
110func TestATimingLineNobodyReadsHoldsNothingUp(t *testing.T) {
111 s, r := refusedRequest(t)
112 pr, pw, err := os.Pipe()
113 if err != nil {
114 t.Fatal(err)
115 }
116 defer pr.Close()
117 defer pw.Close()
118 pw.SetWriteDeadline(time.Now().Add(100 * time.Millisecond))
119 for err == nil {
120 _, err = pw.Write(make([]byte, 1<<16))
121 }
122 pw.SetWriteDeadline(time.Time{})
123
124 in := &integrator{repo: s.repo, project: "project", out: devNull(t), timings: newDroppable(pw)}
125 idx, err := in.readRecords()
126 if err != nil {
127 t.Fatal(err)
128 }
129 began := time.Now()
130 if err := in.handle(r, idx, inputs{digest: "i"}); err != nil {
131 t.Fatal(err)
132 }
133 if took := time.Since(began); took > timingDeadline+5*time.Second {
134 t.Errorf("a timing line nobody reads held the decision for %v", took)
135 }
136 if record, _ := in.readMemo(r); !strings.HasSuffix(record, " "+string(refusedNotReady)) {
137 t.Errorf("behind a timing line nobody reads, the outcome record says %q", record)
138 }
139}
140
141// A request is first seen once at each head, by the pass that first lists
142// it there. A moved head is seen anew, a request no longer listed is
143// forgotten, and a new process remembers nothing of the one before it.
144func TestARequestIsFirstSeenOnceAtEachHead(t *testing.T) {
145 a := request{ref: requestPrefix + "/main/a", head: "a1"}
146 b := request{ref: requestPrefix + "/main/b", head: "b1"}
147 s := newSightings()
148 pass := func(reason passReason, requests ...request) {
149 t.Helper()
150 s.start(reason)
151 s.list(requests)
152 time.Sleep(5 * time.Millisecond)
153 }
154 firstSeen := func(r request, wantPass int, wantAtStart bool) observation {
155 t.Helper()
156 o := s.of(r, time.Now())
157 if o.FirstSeenPass != wantPass || o.FirstSeenAtStart != wantAtStart || o.Process != s.process || o.Pass != s.passes {
158 t.Fatalf("%s at %s in pass %d: %+v, want first seen in pass %d", r.ref, r.head, s.passes, o, wantPass)
159 }
160 if o.SeenToBeganMS < o.ListedToBeganMS {
161 t.Errorf("%s was first seen after its pass listed it: %+v", r.ref, o)
162 }
163 return o
164 }
165
166 pass(reasonStartup, a)
167 firstSeen(a, 1, true)
168 if o := s.of(b, time.Now()); o != (observation{}) {
169 t.Errorf("a request the pass did not list has an observation: %+v", o)
170 }
171 pass(reasonDue, a, b)
172 seen := firstSeen(a, 1, true).FirstSeen
173 if o := firstSeen(b, 2, false); o.PassReason != string(reasonDue) || o.FirstSeen == seen {
174 t.Errorf("b, first listed by the due pass, says %+v", o)
175 }
176 moved := request{ref: b.ref, head: "b2"}
177 pass(reasonTimer, a, moved)
178 firstSeen(moved, 3, false)
179 if o := s.of(b, time.Now()); o != (observation{}) {
180 t.Errorf("a decision about b's old head has an observation: %+v", o)
181 }
182 pass(reasonFollowUp, a)
183 if len(s.seen) != 1 {
184 t.Errorf("after b was no longer listed, %d requests are remembered", len(s.seen))
185 }
186 pass(reasonDue, a, moved)
187 firstSeen(moved, 5, false)
188
189 restarted := newSightings()
190 if restarted.process == s.process {
191 t.Error("two starts share a process")
192 }
193 s = restarted
194 pass(reasonStartup, a)
195 firstSeen(a, 1, true)
196}