| 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 | |
| 15 | "the-valley/change" |
| 16 | ) |
| 17 | |
| 18 | func TestADecisionIsFollowedByItsTimingLine(t *testing.T) { |
| 19 | s := newScratchRepo(t) |
| 20 | base := s.git("rev-parse", "HEAD") |
| 21 | drafted := s.commit("drafted", base) |
| 22 | s.record(draftID, change.Draft, drafted) |
| 23 | r := request{ |
| 24 | ref: requestPrefix + "/main/" + change.RequestName(draftID), |
| 25 | change: change.RequestName(draftID), |
| 26 | target: "refs/heads/main", |
| 27 | head: drafted, |
| 28 | } |
| 29 | s.git("update-ref", r.ref, drafted) |
| 30 | |
| 31 | out, err := os.Create(filepath.Join(t.TempDir(), "out")) |
| 32 | if err != nil { |
| 33 | t.Fatal(err) |
| 34 | } |
| 35 | defer out.Close() |
| 36 | in := &integrator{repo: s.repo, project: "project", out: out} |
| 37 | pass := func() []string { |
| 38 | t.Helper() |
| 39 | idx, err := in.readRecords() |
| 40 | if err != nil { |
| 41 | t.Fatal(err) |
| 42 | } |
| 43 | if err := in.handle(r, idx); err != nil { |
| 44 | t.Fatal(err) |
| 45 | } |
| 46 | data, err := os.ReadFile(out.Name()) |
| 47 | if err != nil { |
| 48 | t.Fatal(err) |
| 49 | } |
| 50 | var timings []string |
| 51 | for _, line := range strings.Split(string(data), "\n") { |
| 52 | if rest, ok := strings.CutPrefix(line, " timing "); ok { |
| 53 | timings = append(timings, rest) |
| 54 | } |
| 55 | } |
| 56 | return timings |
| 57 | } |
| 58 | |
| 59 | timings := pass() |
| 60 | if len(timings) != 1 { |
| 61 | t.Fatalf("one refusal printed %d timing lines, want 1", len(timings)) |
| 62 | } |
| 63 | var got decisionTiming |
| 64 | if err := json.Unmarshal([]byte(timings[0]), &got); err != nil { |
| 65 | t.Fatalf("the timing line is not JSON: %v: %s", err, timings[0]) |
| 66 | } |
| 67 | if got.Project != "project" || got.Request != r.ref || got.Change != r.change || got.Head != drafted || |
| 68 | got.Target != "refs/heads/main" || got.Tip != base || got.Outcome != string(refusedNotReady) { |
| 69 | t.Errorf("the timing line says %+v", got) |
| 70 | } |
| 71 | if _, ok := got.StagesMS[stageRefusal]; !ok || len(got.StagesMS) != 1 { |
| 72 | t.Errorf("a refusal is timed in the stages %v, want the refusal stage alone", got.StagesMS) |
| 73 | } |
| 74 | if got.DurationMS < got.StagesMS[stageRefusal] { |
| 75 | t.Errorf("the decision took %dms, less than its refusal stage's %dms", got.DurationMS, got.StagesMS[stageRefusal]) |
| 76 | } |
| 77 | |
| 78 | // The same refusal on the next pass is not reported, and not timed. |
| 79 | if timings := pass(); len(timings) != 1 { |
| 80 | t.Fatalf("a refusal repeated printed a timing line again: %q", timings) |
| 81 | } |
| 82 | } |