Skip to content

Commit 2353df0

Browse files
committed
internal/middleware/stats: make TestStats more reliable
TestStats depended on having a big sleep in the middle and then expecting that the time taken to the last byte was dominated by the sleep time. Instead of doing that use upper and lower bounds where possible to determine the range MillisToFirstByte and MillisToLastByte should fall between Fixes #61770 Change-Id: Ie266e34165670827035fb2a05e32c2aee268a2e0 Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/517975 TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Michael Matloob <[email protected]> Reviewed-by: Bryan Mills <[email protected]> kokoro-CI: kokoro <[email protected]>
1 parent a3b12ea commit 2353df0

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

internal/middleware/stats/stats_test.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,22 @@ import (
2020
func TestStats(t *testing.T) {
2121
data := []byte("this is the data we are going to serve")
2222
const code = 218
23+
var afterFirstWrite, afterSleep, handlerStart time.Time
2324
ts := httptest.NewServer(Stats()(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
25+
handlerStart = time.Now()
2426
ctx := r.Context()
2527
w.WriteHeader(code)
2628
set(ctx, "a", 1)
2729
w.Write(data[:10])
30+
afterFirstWrite = time.Now()
2831
set(ctx, "b", 2)
29-
time.Sleep(500 * time.Millisecond)
32+
time.Sleep(10 * time.Millisecond)
33+
afterSleep = time.Now()
3034
set(ctx, "a", 3)
3135
w.Write(data[10:])
3236
})))
3337
defer ts.Close()
38+
start := time.Now()
3439
res, err := ts.Client().Get(ts.URL)
3540
if err != nil {
3641
t.Fatal(err)
@@ -40,6 +45,7 @@ func TestStats(t *testing.T) {
4045
t.Fatalf("failed with status %d", res.StatusCode)
4146
}
4247
gotData, err := io.ReadAll(res.Body)
48+
end := time.Now()
4349
if err != nil {
4450
t.Fatal(err)
4551
}
@@ -64,19 +70,14 @@ func TestStats(t *testing.T) {
6470
if diff != "" {
6571
t.Errorf("mismatch (-want, +got):\n%s", diff)
6672
}
67-
const tolerance = 50 // 50 ms of tolerance for time measurements
68-
if g := got.MillisToFirstByte; !within(g, 0, tolerance) {
69-
t.Errorf("MillisToFirstByte is %d, wanted 0 - %d", g, tolerance)
73+
timeToFirstByteUpperBound := afterFirstWrite.Sub(start)
74+
if g := got.MillisToFirstByte; g > timeToFirstByteUpperBound.Milliseconds() {
75+
t.Errorf("MillisToFirstByte is %d, wanted <= %d", g, timeToFirstByteUpperBound)
7076
}
71-
if g := got.MillisToLastByte; !within(g, 500, tolerance) {
72-
t.Errorf("MillisToLastByte is %d, wanted 500 +/- %d", g, tolerance)
77+
timeToLastByteLowerBound := afterSleep.Sub(handlerStart)
78+
timeToLastByteUpperBound := end.Sub(start)
79+
if g := got.MillisToLastByte; g < timeToLastByteLowerBound.Milliseconds() || g > timeToLastByteUpperBound.Milliseconds() {
80+
t.Errorf("MillisToLastByte is %d, wanted >= %d and <= %d",
81+
g, timeToLastByteLowerBound.Milliseconds(), timeToLastByteUpperBound.Milliseconds())
7382
}
7483
}
75-
76-
func within(got, want, tolerance int64) bool {
77-
d := got - want
78-
if d < 0 {
79-
d = -d
80-
}
81-
return d <= tolerance
82-
}

0 commit comments

Comments
 (0)