@@ -142,9 +142,25 @@ func (c *CSAPI) SyncUntil(t *testing.T, since, filter, key string, check func(gj
142
142
t .Helper ()
143
143
start := time .Now ()
144
144
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
+ }()
145
160
for {
146
161
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 )
148
164
}
149
165
query := url.Values {
150
166
"timeout" : []string {"1000" },
@@ -161,17 +177,12 @@ func (c *CSAPI) SyncUntil(t *testing.T, since, filter, key string, check func(gj
161
177
keyRes := gjson .GetBytes (body , key )
162
178
if keyRes .IsArray () {
163
179
events := keyRes .Array ()
164
- for _ , ev := range events {
165
- wasFailed := t . Failed ()
180
+ for i , ev := range events {
181
+ lastEvent = & events [ i ]
166
182
if check (ev ) {
167
- if ! wasFailed && t .Failed () {
168
- t .Logf ("failing event %s" , ev .Raw )
169
- }
170
183
return
171
184
}
172
- if ! wasFailed && t .Failed () {
173
- t .Logf ("failing event %s" , ev .Raw )
174
- }
185
+ wasFailed = t .Failed ()
175
186
checkCounter ++
176
187
}
177
188
}
0 commit comments