Skip to content

Commit 6e57275

Browse files
groupskyclaude
andcommitted
test: a check waiter without a request stops
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent ece6094 commit 6e57275

3 files changed

Lines changed: 44 additions & 2 deletions

File tree

‎DESIGN.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ an approval, and `pullgate apply` never reads this directory.
360360
`pullgate-check@NAME.path` watches the file `check/request`
361361
(`PathExists=`; another file there starts nothing) and starts `pullgate-run@NAME.service`, the unit of the timer. `pullgate run`,
362362
with the lock held (a run that had to wait for the lock, and then finds
363-
no request, stops: the run before it took the request and replied):
363+
no request, stops: another run took the request):
364364

365365
1. Reads and removes every file in the directory, with the checks of an
366366
approval: through `os.OpenRoot` with `O_NOFOLLOW`, a regular file owned

‎internal/gate/check_test.go‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,45 @@ func TestCheckLimitReplySentOnce(t *testing.T) {
289289
t.Fatalf("one message about the limit, got %d", n)
290290
}
291291
}
292+
293+
// TestCheckWaiterStopsWhenRequestIsGone: a run that waited for the lock and
294+
// then finds no request (another run took it) stops: no fetch, no reply.
295+
func TestCheckWaiterStopsWhenRequestIsGone(t *testing.T) {
296+
f := newFixture(t, ``, "")
297+
f.commit("first", "one", nil)
298+
if err := f.env.Dir.Ensure(); err != nil {
299+
t.Fatal(err)
300+
}
301+
l, err := f.env.Dir.Lock(false)
302+
if err != nil {
303+
t.Fatal(err)
304+
}
305+
f.request()
306+
done := make(chan Result, 1)
307+
go func() {
308+
r, err := f.env.Run(context.Background())
309+
if err != nil {
310+
r = Result("error: " + err.Error())
311+
}
312+
done <- r
313+
}()
314+
select {
315+
case r := <-done:
316+
t.Fatalf("the run must wait while the lock is held: %s", r)
317+
case <-time.After(300 * time.Millisecond):
318+
}
319+
// The run that holds the lock takes the request.
320+
if n, _, err := f.env.Spool.TakeChecks(os.Getuid()); err != nil || n != 1 {
321+
t.Fatalf("TakeChecks: %d, %v", n, err)
322+
}
323+
_ = l.Close()
324+
if r := <-done; r != Busy {
325+
t.Fatalf("a waiter without a request must stop as busy: %s", r)
326+
}
327+
if st := f.state(); st.LastTip != "" {
328+
t.Fatal("the waiter must not fetch")
329+
}
330+
if out := f.outbox(); len(out) != 0 {
331+
t.Fatalf("the waiter must not reply: %d messages", len(out))
332+
}
333+
}

‎internal/gate/run.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func (e *Env) Run(ctx context.Context) (Result, error) {
126126
e.Log.Info("busy: a check request waits for the lock")
127127
lock, err = e.Dir.Lock(true)
128128
if err == nil && !e.Spool.HasCheck() {
129-
// The run that held the lock took the request and replied.
129+
// Another run took the request (and replied, if it was a good one).
130130
_ = lock.Close()
131131
e.Log.Info("busy: the run before this one took the check request")
132132
return Busy, nil

0 commit comments

Comments
 (0)