Skip to content

Commit dea676d

Browse files
authored
tests: Make CollectResourcesAndMatch less flaky (#3136)
- Significantly increase time-out to account for GH runner performance - Close channel after fetcher is done to drain - Fail if more than expected items have been received Fixes cases like this: https://github.com/elastic/cloudbeat/actions/runs/14002061557/job/39210261271?pr=3131
1 parent f75040d commit dea676d

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

internal/inventory/testutil/testutil.go

+10-6
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,26 @@ func CollectResourcesAndMatch(t *testing.T, fetcher inventory.AssetFetcher, expe
3131
t.Helper()
3232

3333
ch := make(chan inventory.AssetEvent)
34-
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
34+
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
3535
defer cancel()
3636
go func() {
37+
defer close(ch)
3738
fetcher.Fetch(ctx, ch)
3839
}()
3940

4041
received := make([]inventory.AssetEvent, 0, len(expected))
41-
for len(expected) != len(received) {
42+
defer func() {
43+
assert.ElementsMatch(t, expected, received)
44+
}()
45+
for {
4246
select {
4347
case <-ctx.Done():
44-
assert.ElementsMatch(t, expected, received)
4548
return
46-
case event := <-ch:
49+
case event, ok := <-ch:
50+
if !ok {
51+
return
52+
}
4753
received = append(received, event)
4854
}
4955
}
50-
51-
assert.ElementsMatch(t, expected, received)
5256
}

0 commit comments

Comments
 (0)