sww tests: wait for the starting line as long as the start takes (closes #324)
check / check (push) Waiting to run
check / check (push) Waiting to run
A start on a loaded host can take longer than the 10-second wait for a line. The new output.lineBeforeExit waits until the line is written or sww exits, so only the test binary's own timeout ends the wait. It replaces the waits that span the start in runUntilStopped, TestServesUntilToldToStop and TestLogLevelHoldsBackTheLessSevereProcessLines. Model: opus-5-5
This commit is contained in:
1 parent
f7e3190654
commit
3c6f000eca
1 file changed
+30
-4
@@ -92,6 +92,31 @@ func (o *output) line(t *testing.T, key, value string) map[string]any {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// lineBeforeExit returns the first line whose msg is msg, waiting for it
|
||||||
|
// while sww runs. It waits as long as that takes, so that a slow start
|
||||||
|
// cannot fail the test, and fails the test if sww exits first.
|
||||||
|
func (o *output) lineBeforeExit(
|
||||||
|
t *testing.T, msg string, exited <-chan int,
|
||||||
|
) map[string]any {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
for {
|
||||||
|
fields := o.findLine(t, func(fields map[string]any) bool {
|
||||||
|
return fields["msg"] == msg
|
||||||
|
})
|
||||||
|
if fields != nil {
|
||||||
|
return fields
|
||||||
|
}
|
||||||
|
|
||||||
|
select {
|
||||||
|
case status := <-exited:
|
||||||
|
t.Fatalf("exit status %d while waiting for a line with msg %q; output:\n%s",
|
||||||
|
status, msg, o.text())
|
||||||
|
case <-time.After(pollInterval):
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// findLine returns the first line written so far for which matches is true, or
|
// findLine returns the first line written so far for which matches is true, or
|
||||||
// nil.
|
// nil.
|
||||||
func (o *output) findLine(
|
func (o *output) findLine(
|
||||||
@@ -236,11 +261,12 @@ func TestServesUntilToldToStop(t *testing.T) {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
// The default rule file is read.
|
// The default rule file is read.
|
||||||
if rules := out.line(t, "msg", "read the rule files")["rules"]; rules != 12.0 {
|
readRules := out.lineBeforeExit(t, "read the rule files", exited)
|
||||||
|
if rules := readRules["rules"]; rules != 12.0 {
|
||||||
t.Errorf("read %v rules from the default rule file, want 12", rules)
|
t.Errorf("read %v rules from the default rule file, want 12", rules)
|
||||||
}
|
}
|
||||||
|
|
||||||
starting := out.line(t, "msg", "starting")
|
starting := out.lineBeforeExit(t, "starting", exited)
|
||||||
wantStartingLine(t, starting, dir)
|
wantStartingLine(t, starting, dir)
|
||||||
|
|
||||||
addr, _ := starting["address"].(string)
|
addr, _ := starting["address"].(string)
|
||||||
@@ -286,7 +312,7 @@ func TestLogLevelHoldsBackTheLessSevereProcessLines(t *testing.T) {
|
|||||||
}, out)
|
}, out)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
out.line(t, "msg", "fetching a list failed")
|
out.lineBeforeExit(t, "fetching a list failed", exited)
|
||||||
stop()
|
stop()
|
||||||
|
|
||||||
select {
|
select {
|
||||||
@@ -1261,7 +1287,7 @@ func runUntilStopped(
|
|||||||
exited <- run(ctx, env, out)
|
exited <- run(ctx, env, out)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
addr, _ := out.line(t, "msg", "starting")["address"].(string)
|
addr, _ := out.lineBeforeExit(t, "starting", exited)["address"].(string)
|
||||||
use("http://" + addr + "/")
|
use("http://" + addr + "/")
|
||||||
stop()
|
stop()
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user