From 95d7f0d62377f2b6f0981d0c5c08a6e6bd273d2f Mon Sep 17 00:00:00 2001 From: Paulo Janotti Date: Sun, 17 Nov 2024 10:01:44 -0700 Subject: [PATCH] Fix data race on windowseventlogreceiver tests --- .../receiver_windows_test.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/receiver/windowseventlogreceiver/receiver_windows_test.go b/receiver/windowseventlogreceiver/receiver_windows_test.go index 9608d3e3585d..3be8a57cade0 100644 --- a/receiver/windowseventlogreceiver/receiver_windows_test.go +++ b/receiver/windowseventlogreceiver/receiver_windows_test.go @@ -324,11 +324,17 @@ func assertEventSourceInstallation(t *testing.T, src string) (uninstallEventSour func assertExpectedLogRecords(t *testing.T, sink *consumertest.LogsSink, expectedEventSrc string, expectedEventCount int) []plog.LogRecord { var actualLogRecords []plog.LogRecord - // logs sometimes take a while to be written, so a substantial wait buffer is needed - assert.EventuallyWithT(t, func(c *assert.CollectT) { + // We can't use assert.Eventually because the condition function is launched in a separate goroutine + // and we want to return the slice filled by the condition function. Use a local condition check + // to avoid data race conditions. + startTime := time.Now() + actualLogRecords = filterAllLogRecordsBySource(t, sink, expectedEventSrc) + for len(actualLogRecords) != expectedEventCount && time.Since(startTime) < 10*time.Second { + time.Sleep(250 * time.Millisecond) actualLogRecords = filterAllLogRecordsBySource(t, sink, expectedEventSrc) - assert.Len(c, actualLogRecords, expectedEventCount) - }, 10*time.Second, 250*time.Millisecond) + } + + require.Len(t, actualLogRecords, expectedEventCount) return actualLogRecords }