| 1 | package 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 | |
| 8 | import ( |
| 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. |
| 20 | func 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. |
| 38 | type timingWatch struct { |
| 39 | in *integrator |
| 40 | r request |
| 41 | lines []string |
| 42 | records []string |
| 43 | } |
| 44 | |
| 45 | func (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 | |
| 52 | func 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. |
| 110 | func 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 | } |