Skip to content

Commit

Permalink
filters/diag: speedup TestAbsorbSilent (#2559)
Browse files Browse the repository at this point in the history
Instead of waiting for each match to timeout wait for a single
unexpected match.

Before:
```
$ go test ./filters/diag/ -count=1
ok      github.com/zalando/skipper/filters/diag 22.729s
```

After:
```
ok      github.com/zalando/skipper/filters/diag 7.836s
```

Signed-off-by: Alexander Yastrebov <[email protected]>
  • Loading branch information
AlexanderYastrebov authored Aug 31, 2023
1 parent ff6afc5 commit 79d98b1
Showing 1 changed file with 22 additions and 37 deletions.
59 changes: 22 additions & 37 deletions filters/diag/absorb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,9 @@ import (
"github.com/zalando/skipper/proxy/proxytest"
)

const (
bodySize = 1 << 12
logTimeout = 3 * time.Second
)

func testAbsorb(t *testing.T, silent bool) {
const bodySize = 1 << 12

l := loggingtest.New()
defer l.Close()
a := withLogger(silent, l)
Expand All @@ -34,55 +31,43 @@ func testAbsorb(t *testing.T, silent bool) {
)
defer p.Close()

req, err := http.NewRequest(
"POST",
p.URL,
io.LimitReader(
rand.New(rand.NewSource(time.Now().UnixNano())),
bodySize,
),
)
body := io.LimitReader(rand.New(rand.NewSource(time.Now().UnixNano())), bodySize)
req, err := http.NewRequest("POST", p.URL, body)
if err != nil {
t.Fatal(err)
}

req.Header.Set("X-Flow-Id", "foo-bar-baz")
rsp, err := (&http.Client{}).Do(req)
rsp, err := p.Client().Do(req)
if err != nil {
t.Fatal(err)
}

defer rsp.Body.Close()

if rsp.StatusCode != http.StatusOK {
t.Fatalf("invalid status code received: %d", rsp.StatusCode)
}

expectLog := func(content string, err error) {
if err != nil {
t.Fatalf("%s: %v", content, err)
}
const lastMessage = "testAbsorb finished"
l.Info(lastMessage)
if err := l.WaitFor(lastMessage, 100*time.Millisecond); err != nil {
t.Errorf("Expected %s: %v", lastMessage, err)
}

expectNoLog := func(content string, err error) {
if err != loggingtest.ErrWaitTimeout {
t.Fatalf("%s: unexpected log entry", content)
if silent {
if err := l.WaitFor("foo-bar-baz", 100*time.Millisecond); err != loggingtest.ErrWaitTimeout {
t.Error("Unexpected log entry")
}
}

for _, content := range []string{
"received request",
"foo-bar-baz",
"consumed",
fmt.Sprint(bodySize),
"request finished",
} {
err := l.WaitFor(content, logTimeout)
if silent {
expectNoLog(content, err)
continue
} else {
for _, content := range []string{
"received request to be absorbed: foo-bar-baz",
fmt.Sprintf("request foo-bar-baz, consumed bytes: %d", bodySize),
"request finished: foo-bar-baz",
} {
if err := l.WaitFor(content, 100*time.Millisecond); err != nil {
t.Errorf("Expected %s: %v", content, err)
}
}

expectLog(content, err)
}
}

Expand Down

0 comments on commit 79d98b1

Please sign in to comment.