gunk-dev/the-valley

deadline_test.go

source701f0334f1eamain's published tip history
3.6 KiB
1package main
2
3import (
4 "fmt"
5 "io"
6 "path"
7 "strings"
8 "testing"
9 "time"
10
11 "the-valley/knot"
12)
13
14// git can stop taking requests, or stall partway through an answer, and a
15// read blocked on it has to end when the deadline passes, not when git
16// next writes. Each case is a stream that stalls at one point.
17func TestAStalledReadEndsAtTheDeadline(t *testing.T) {
18 const deadline = 100 * time.Millisecond
19 id := strings.Repeat("ab", 20)
20 header := id + " blob 10\n"
21 for _, c := range []struct {
22 what string
23 takes bool // whether git reads the requests at all
24 answer string // what git answers before it stalls
25 }{
26 {"git takes no request", false, ""},
27 {"git answers nothing", true, ""},
28 {"git stalls in a header", true, id + " bl"},
29 {"git stalls in the content", true, header + header + "01234"},
30 } {
31 requests, in := io.Pipe()
32 out, answers := io.Pipe()
33 if c.takes {
34 go func() { _, _ = io.Copy(io.Discard, requests) }()
35 }
36 if c.answer != "" {
37 go func() { _, _ = answers.Write([]byte(c.answer)) }()
38 }
39 b := graphBudget
40 b.deadline = deadline
41 ctx, cancel := b.context()
42 s := newObjectStream(ctx, cancel, in, out, b)
43 start := time.Now()
44 _, err := s.read(id, "blob", 100)
45 elapsed := time.Since(start)
46 s.close()
47 if want := "could not be read within the 100ms the hook spends reading the plan graph"; err == nil || err.Error() != want {
48 t.Errorf("%s: error = %v, want %q", c.what, err, want)
49 }
50 if elapsed > deadline+time.Second {
51 t.Errorf("%s: the read ended %s after it began", c.what, elapsed)
52 }
53 }
54}
55
56// Every read below begins in time, and the one that reads the knot ends
57// after the deadline. Nothing else is wrong with the push, so it is the
58// last look at the clock, after every rule has passed, that refuses it.
59func TestAGraphCheckedPastTheDeadlineIsRefused(t *testing.T) {
60 r := newRecords(t)
61 ty, _ := knot.TypeNamed("idea")
62 slug := knot.Slug("Checked late")
63 k := &knot.Knot{Type: ty.Name, ID: knot.DeriveID(ty, slug), Slug: slug, Status: ty.Initial(),
64 Title: "Checked late", Created: "2026-10-15", CreatedBy: "operator"}
65 ideas := r.git(fmt.Sprintf("100644 blob %s\t%s\n", r.blob(string(knot.Render(k))), path.Base(k.Path())), "mktree")
66 root := r.git(fmt.Sprintf("040000 tree %s\t%s\n", ideas, ty.Directory), "mktree")
67 commit := r.git(fmt.Sprintf("tree %s\nauthor operator <p@localhost> 1700000000 +0000\ncommitter operator <p@localhost> 1700000000 +0000\n\n%s",
68 root, knot.Message("knot new", "0123456789abcdef")), "hash-object", "-t", "commit", "-w", "--stdin")
69 u := update{old: zeroID, new: commit, ref: knot.Ref}
70
71 if _, why := (policy{repo: lateRepository{deadline: knot.HookDeadline}}).checkGraph("operator", u); why != "" {
72 t.Fatalf("the push read in time: %s", why)
73 }
74 _, why := (policy{repo: lateRepository{deadline: 500 * time.Millisecond, wait: 600 * time.Millisecond}}).checkGraph("operator", u)
75 if want := "the hook could not finish checking it within the 500ms it spends on the plan graph"; why != want {
76 t.Errorf("the push read late: %q, want %q", why, want)
77 }
78}
79
80// lateRepository reads the plan graph within the deadline given, and
81// waits after each file it reads, as a read that began in time and ended
82// late would.
83type lateRepository struct {
84 gitRepository
85 deadline, wait time.Duration
86}
87
88func (l lateRepository) graph() (graphReader, error) {
89 b := graphBudget
90 b.deadline = l.deadline
91 objects, err := openObjectsWithin(b)
92 if err != nil {
93 return nil, err
94 }
95 return lateGraph{streamGraph{objects}, l.wait}, nil
96}
97
98type lateGraph struct {
99 streamGraph
100 wait time.Duration
101}
102
103func (g lateGraph) blob(id string, max int) ([]byte, error) {
104 data, err := g.streamGraph.blob(id, max)
105 time.Sleep(g.wait)
106 return data, err
107}