Skip to content

Commit 25fbea7

Browse files
authored
Print the failing event in the event of t.Fatalf (#174)
Fixes #170
1 parent 28fbdc8 commit 25fbea7

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

internal/client/client.go

+20-9
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,25 @@ func (c *CSAPI) SyncUntil(t *testing.T, since, filter, key string, check func(gj
142142
t.Helper()
143143
start := time.Now()
144144
checkCounter := 0
145+
// Print failing events in a defer() so we handle t.Fatalf in the same way as t.Errorf
146+
var wasFailed = t.Failed()
147+
var lastEvent *gjson.Result
148+
timedOut := false
149+
defer func() {
150+
if !wasFailed && t.Failed() {
151+
raw := ""
152+
if lastEvent != nil {
153+
raw = lastEvent.Raw
154+
}
155+
if !timedOut {
156+
t.Logf("SyncUntil: failing event %s", raw)
157+
}
158+
}
159+
}()
145160
for {
146161
if time.Since(start) > c.SyncUntilTimeout {
147-
t.Fatalf("syncUntil timed out. Called check function %d times", checkCounter)
162+
timedOut = true
163+
t.Fatalf("SyncUntil: timed out. Called check function %d times", checkCounter)
148164
}
149165
query := url.Values{
150166
"timeout": []string{"1000"},
@@ -161,17 +177,12 @@ func (c *CSAPI) SyncUntil(t *testing.T, since, filter, key string, check func(gj
161177
keyRes := gjson.GetBytes(body, key)
162178
if keyRes.IsArray() {
163179
events := keyRes.Array()
164-
for _, ev := range events {
165-
wasFailed := t.Failed()
180+
for i, ev := range events {
181+
lastEvent = &events[i]
166182
if check(ev) {
167-
if !wasFailed && t.Failed() {
168-
t.Logf("failing event %s", ev.Raw)
169-
}
170183
return
171184
}
172-
if !wasFailed && t.Failed() {
173-
t.Logf("failing event %s", ev.Raw)
174-
}
185+
wasFailed = t.Failed()
175186
checkCounter++
176187
}
177188
}

0 commit comments

Comments
 (0)