Skip to content

Commit 536c4b6

Browse files
TMHSDigitalclaude
andcommitted
Interrupt e2e tests on result lines, not sleeps (#260)
TestE2EResume slept 1s and TestE2ESIGTERMExits143 slept 200ms before interrupting, betting that the scan was still mid-way; a slow runner could lose that bet. Both now run the CLI with a stdout that fires the signal once the Nth result line is written (runCLIUntilLines), so the interrupt always lands after the scan has produced results and long before it could finish. Fixes #260 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 983c0db commit 536c4b6

2 files changed

Lines changed: 42 additions & 22 deletions

File tree

‎main_e2e_test.go‎

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"strings"
1515
"sync"
1616
"testing"
17-
"time"
1817

1918
"golang.org/x/net/dns/dnsmessage"
2019

@@ -52,6 +51,38 @@ func (b *lockedBuffer) String() string {
5251
return b.buf.String()
5352
}
5453

54+
// lineTrigger is a stdout that calls fire once, after n lines have been
55+
// written, so a test can interrupt a scan partway through by its progress
56+
// rather than after a wall-clock sleep a slow runner could outlast (#260).
57+
type lineTrigger struct {
58+
lockedBuffer
59+
n int
60+
lines int
61+
fire func()
62+
}
63+
64+
func (w *lineTrigger) Write(p []byte) (int, error) {
65+
n, err := w.lockedBuffer.Write(p)
66+
w.mu.Lock()
67+
before := w.lines
68+
w.lines += bytes.Count(p[:n], []byte("\n"))
69+
fire := before < w.n && w.lines >= w.n
70+
w.mu.Unlock()
71+
if fire {
72+
w.fire()
73+
}
74+
return n, err
75+
}
76+
77+
// runCLIUntilLines runs the CLI like runCLI and calls fire once n lines of
78+
// its stdout have been written.
79+
func runCLIUntilLines(t *testing.T, n int, fire func(), args ...string) (int, string) {
80+
t.Helper()
81+
stdout := &lineTrigger{n: n, fire: fire}
82+
code := runWith(runArgs{args: args, stdin: strings.NewReader(""), stdout: stdout, stderr: io.Discard})
83+
return code, stdout.String()
84+
}
85+
5586
func writeFile(t *testing.T, name, content string) string {
5687
t.Helper()
5788
p := filepath.Join(t.TempDir(), name)
@@ -842,16 +873,13 @@ func TestE2EResume(t *testing.T) {
842873
t.Fatalf("reference run found only %d names", len(want))
843874
}
844875

845-
// Interrupted run: -rate 20 makes 40 names take ~2s; interrupt at ~1s.
846-
ready := make(chan struct{})
847-
signalReady, testSignals = ready, make(chan os.Signal, 1)
876+
// Interrupted run: -rate 20 makes 40 names take ~2s; interrupt once
877+
// three results are out, whatever the runner's speed (#260).
878+
signals := make(chan os.Signal, 1)
879+
signalReady, testSignals = nil, signals
848880
t.Cleanup(func() { signalReady, testSignals = nil, nil })
849-
go func() {
850-
<-ready
851-
time.Sleep(time.Second)
852-
testSignals <- os.Interrupt
853-
}()
854-
code, first := runCLI(t, "", append(append([]string{}, base...), "-rate", "20", "-state", statePath, "-o", outPath, "example.com")...)
881+
code, first := runCLIUntilLines(t, 3, func() { signals <- os.Interrupt },
882+
append(append([]string{}, base...), "-rate", "20", "-state", statePath, "-o", outPath, "example.com")...)
855883
signalReady, testSignals = nil, nil
856884
if code != 130 {
857885
t.Fatalf("interrupted run: exit %d, want 130", code)

‎main_signal_unix_test.go‎

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"strings"
88
"syscall"
99
"testing"
10-
"time"
1110
)
1211

1312
// TestE2ESIGTERMExits143 covers #82: SIGTERM (container stop, systemd) exits
@@ -19,17 +18,10 @@ func TestE2ESIGTERMExits143(t *testing.T) {
1918
}
2019
wl := writeFile(t, "wl.txt", words.String())
2120

22-
ready := make(chan struct{})
23-
signalReady = ready
24-
t.Cleanup(func() { signalReady = nil })
25-
go func() {
26-
<-ready
27-
time.Sleep(200 * time.Millisecond) // let the scan start
28-
_ = syscall.Kill(syscall.Getpid(), syscall.SIGTERM)
29-
}()
30-
31-
// 200 names at -rate 10 would take 20s; the signal ends it early.
32-
code, _ := runCLI(t, "", "-simulate", "-rate", "10", "-progress=false", "-w", wl, "example.com")
21+
// 200 names at -rate 10 would take 20s; the signal, sent once the first
22+
// result is out rather than after a fixed sleep (#260), ends it early.
23+
code, _ := runCLIUntilLines(t, 1, func() { _ = syscall.Kill(syscall.Getpid(), syscall.SIGTERM) },
24+
"-simulate", "-hit-rate", "100", "-rate", "10", "-progress=false", "-w", wl, "example.com")
3325
if code != 143 {
3426
t.Fatalf("exit %d after SIGTERM, want 143", code)
3527
}

0 commit comments

Comments
 (0)