gunk-dev/the-valley

wake_test.go

sourceccb7354855a5main's published tip history
6.3 KiB
1package main
2
3import (
4 "errors"
5 "io"
6 "io/fs"
7 "os"
8 "path/filepath"
9 "strconv"
10 "sync"
11 "sync/atomic"
12 "testing"
13)
14
15// mark is a writer's mark, as the integrator and the host hook make it.
16func mark(t *testing.T, dir string) {
17 f, err := os.OpenFile(filepath.Join(dir, "due"), os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o440)
18 if errors.Is(err, fs.ErrExist) {
19 return
20 }
21 if err != nil {
22 t.Error(err)
23 return
24 }
25 f.Close()
26}
27
28// Writers move a counter and then mark due, as a push commits its refs and
29// then runs the hook. Passes claim, read the counter and acknowledge,
30// while the writers run. Once they stop, the passes run until nothing is
31// due. The last pass must have read the last move: a mark made during a
32// pass survives its acknowledgement. And the directory never holds more
33// than its three files.
34func TestWakeLosesNoMark(t *testing.T) {
35 dir := t.TempDir()
36 wakeDir := filepath.Join(dir, "wake")
37 if err := os.Mkdir(wakeDir, 0o770); err != nil {
38 t.Fatal(err)
39 }
40 counter := filepath.Join(dir, "counter")
41 var moved atomic.Int64
42 var write sync.Mutex
43 move := func() {
44 write.Lock()
45 defer write.Unlock()
46 n := moved.Add(1)
47 os.WriteFile(counter+".new", []byte(strconv.FormatInt(n, 10)), 0o600)
48 os.Rename(counter+".new", counter)
49 }
50 read := func() int64 {
51 b, _ := os.ReadFile(counter)
52 n, _ := strconv.ParseInt(string(b), 10, 64)
53 return n
54 }
55
56 var seen atomic.Int64
57 pass := func() bool {
58 w := claimWake(wakeDir, io.Discard)
59 defer w.close()
60 if w.err != nil {
61 t.Error(w.err)
62 return false
63 }
64 _, err := os.Stat(filepath.Join(wakeDir, "claimed"))
65 claimed := err == nil
66 seen.Store(read())
67 if err := w.ack(); err != nil {
68 t.Error(err)
69 }
70 entries, _ := os.ReadDir(wakeDir)
71 for _, e := range entries {
72 if e.Name() != "due" && e.Name() != "claimed" && e.Name() != "lock" {
73 t.Errorf("the wake directory holds %s", e.Name())
74 }
75 }
76 return claimed
77 }
78
79 var writers, passes sync.WaitGroup
80 stop := make(chan struct{})
81 for range 8 {
82 writers.Add(1)
83 go func() {
84 defer writers.Done()
85 for range 300 {
86 move()
87 mark(t, wakeDir)
88 }
89 }()
90 }
91 for range 3 {
92 passes.Add(1)
93 go func() {
94 defer passes.Done()
95 for {
96 select {
97 case <-stop:
98 return
99 default:
100 pass()
101 }
102 }
103 }()
104 }
105 writers.Wait()
106 close(stop)
107 passes.Wait()
108
109 // What runs a pass while due is there runs passes until it is not.
110 for {
111 if _, err := os.Stat(filepath.Join(wakeDir, "due")); err != nil {
112 break
113 }
114 pass()
115 }
116 if got, want := seen.Load(), moved.Load(); got != want {
117 t.Fatalf("the last pass read move %d of %d: a mark was lost", got, want)
118 }
119 if _, err := os.Stat(filepath.Join(wakeDir, "claimed")); err == nil {
120 t.Fatal("claimed was left after a pass that handled it")
121 }
122}
123
124// A pass that is killed after its claim leaves claimed and no due. A new
125// mark and the claim stand side by side, and the next pass that handles
126// what it reads clears both.
127func TestWakeKeepsAnUnacknowledgedClaim(t *testing.T) {
128 dir := t.TempDir()
129 mark(t, dir)
130 w := claimWake(dir, io.Discard)
131 if w.err != nil {
132 t.Fatal(w.err)
133 }
134 w.close() // killed: no ack
135 if _, err := os.Stat(filepath.Join(dir, "claimed")); err != nil {
136 t.Fatal("the killed pass left no claimed")
137 }
138 mark(t, dir)
139 mark(t, dir) // a second mark is the same mark
140 w = claimWake(dir, io.Discard)
141 if w.err != nil {
142 t.Fatal(w.err)
143 }
144 if err := w.ack(); err != nil {
145 t.Fatal(err)
146 }
147 w.close()
148 entries, _ := os.ReadDir(dir)
149 if len(entries) != 1 || entries[0].Name() != "lock" {
150 t.Fatalf("the wake directory holds %v after a handled pass", entries)
151 }
152}
153
154// A wake directory that cannot be renamed in is a claim failure, which
155// acknowledges nothing and leaves due for the next pass.
156func TestWakeClaimFailure(t *testing.T) {
157 if os.Geteuid() == 0 {
158 t.Skip("root renames in a read-only directory")
159 }
160 dir := t.TempDir()
161 mark(t, dir)
162 if f, err := os.Create(filepath.Join(dir, "lock")); err == nil {
163 f.Close()
164 }
165 os.Chmod(dir, 0o500)
166 defer os.Chmod(dir, 0o700)
167 w := claimWake(dir, io.Discard)
168 defer w.close()
169 if w.err == nil {
170 t.Fatal("a rename in a read-only directory was claimed")
171 }
172 if err := w.ack(); err != nil {
173 t.Fatal(err)
174 }
175 if _, err := os.Stat(filepath.Join(dir, "due")); err != nil {
176 t.Fatal("a failed claim lost due")
177 }
178}
179
180// One pass holds the lock while writers move a counter and mark due, as
181// pushes do while a long batch runs. Whenever it finds a mark, it claims
182// it again and reads the counter, as a scan does. Once the writers stop,
183// it runs until it finds no mark, and acknowledges. It must have read the
184// last move: a mark made during a scan is claimed by the next one, never
185// acknowledged away. And the directory never holds more than its three
186// files.
187func TestWakeHolderLosesNoMarkMadeDuringAScan(t *testing.T) {
188 dir := t.TempDir()
189 wakeDir := filepath.Join(dir, "wake")
190 if err := os.Mkdir(wakeDir, 0o770); err != nil {
191 t.Fatal(err)
192 }
193 counter := filepath.Join(dir, "counter")
194 var moved atomic.Int64
195 var write sync.Mutex
196 move := func() {
197 write.Lock()
198 defer write.Unlock()
199 n := moved.Add(1)
200 os.WriteFile(counter+".new", []byte(strconv.FormatInt(n, 10)), 0o600)
201 os.Rename(counter+".new", counter)
202 }
203 read := func() int64 {
204 b, _ := os.ReadFile(counter)
205 n, _ := strconv.ParseInt(string(b), 10, 64)
206 return n
207 }
208
209 w := claimWake(wakeDir, io.Discard)
210 defer w.close()
211 if w.err != nil {
212 t.Fatal(w.err)
213 }
214 var writers sync.WaitGroup
215 for i := 0; i < 4; i++ {
216 writers.Add(1)
217 go func() {
218 defer writers.Done()
219 for j := 0; j < 200; j++ {
220 move()
221 mark(t, wakeDir)
222 }
223 }()
224 }
225 var stopped atomic.Bool
226 go func() { writers.Wait(); stopped.Store(true) }()
227 var seen int64
228 scan := func() {
229 w.again(io.Discard)
230 seen = read()
231 entries, _ := os.ReadDir(wakeDir)
232 for _, e := range entries {
233 if e.Name() != "due" && e.Name() != "claimed" && e.Name() != "lock" {
234 t.Errorf("the wake directory holds %s", e.Name())
235 }
236 }
237 }
238 scan()
239 for !stopped.Load() || w.marked() {
240 if w.marked() {
241 scan()
242 }
243 }
244 if w.err != nil {
245 t.Fatal(w.err)
246 }
247 if err := w.ack(); err != nil {
248 t.Fatal(err)
249 }
250 if seen != moved.Load() {
251 t.Errorf("the pass last read move %d of %d, and no mark was left for the rest", seen, moved.Load())
252 }
253 if _, err := os.Stat(filepath.Join(wakeDir, "due")); err == nil {
254 t.Error("due is still there after the pass found no mark")
255 }
256}