Skip to content

Commit a2a9a95

Browse files
migrate common utils from core/internal/testutils/testutils.go (#214)
Co-authored-by: Jordan Krage <jmank88@gmail.com>
1 parent e9826d4 commit a2a9a95

2 files changed

Lines changed: 70 additions & 0 deletions

File tree

pkg/utils/tests/logs.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package tests
2+
3+
import (
4+
"strings"
5+
"testing"
6+
7+
"go.uber.org/zap/zaptest/observer"
8+
)
9+
10+
// AssertLogEventually waits until at least one log message containing the
11+
// specified msg is emitted.
12+
// NOTE: This does not "pop" messages so it cannot be used multiple times to
13+
// check for new instances of the same msg. See AssertLogCountEventually instead.
14+
//
15+
// Get a *observer.ObservedLogs like so:
16+
//
17+
// observedZapCore, observedLogs := observer.New(zap.DebugLevel)
18+
// lggr := logger.TestLogger(t, observedZapCore)
19+
func AssertLogEventually(t *testing.T, observedLogs *observer.ObservedLogs, msg string) {
20+
AssertLogCountEventually(t, observedLogs, msg, 1)
21+
}
22+
23+
// AssertLogCountEventually waits until at least count log message containing the
24+
// specified msg is emitted
25+
func AssertLogCountEventually(t *testing.T, observedLogs *observer.ObservedLogs, msg string, count int) {
26+
AssertEventually(t, func() bool {
27+
i := 0
28+
for _, l := range observedLogs.All() {
29+
if strings.Contains(l.Message, msg) {
30+
i++
31+
if i >= count {
32+
return true
33+
}
34+
}
35+
}
36+
return false
37+
})
38+
}
39+
40+
// RequireLogMessage fails the test if emitted logs don't contain the given message
41+
func RequireLogMessage(t *testing.T, observedLogs *observer.ObservedLogs, msg string) {
42+
for _, l := range observedLogs.All() {
43+
if strings.Contains(l.Message, msg) {
44+
return
45+
}
46+
}
47+
t.Log("observed logs", observedLogs.All())
48+
t.Fatalf("expected observed logs to contain msg %q, but it didn't", msg)
49+
}

pkg/utils/tests/tests.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"context"
55
"testing"
66
"time"
7+
8+
"github.com/stretchr/testify/assert"
79
)
810

911
func Context(t *testing.T) context.Context {
@@ -33,3 +35,22 @@ func WaitTimeout(t *testing.T) time.Duration {
3335
}
3436
return DefaultWaitTimeout
3537
}
38+
39+
// TestInterval is just a sensible poll interval that gives fast tests without
40+
// risk of spamming
41+
const TestInterval = 100 * time.Millisecond
42+
43+
// AssertEventually waits for f to return true
44+
func AssertEventually(t *testing.T, f func() bool) {
45+
assert.Eventually(t, f, WaitTimeout(t), TestInterval/2)
46+
}
47+
48+
// RequireSignal waits for the channel to close (or receive anything) and
49+
// fatals the test if the default wait timeout is exceeded
50+
func RequireSignal(t *testing.T, ch <-chan struct{}, failMsg string) {
51+
select {
52+
case <-ch:
53+
case <-time.After(WaitTimeout(t)):
54+
t.Fatal(failMsg)
55+
}
56+
}

0 commit comments

Comments
 (0)