From a9e6a0f372d4d28ea79d2ecd122c8a0b21462809 Mon Sep 17 00:00:00 2001 From: Nico <32375741+nulmete@users.noreply.github.com> Date: Wed, 7 Jan 2026 14:08:42 -0300 Subject: [PATCH] Fix nats_test.go flaky tests (#37976) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Context: https://fleetdm.slack.com/archives/C019WG4GH0A/p1767759920870649 Looks like some of the tests in `nats_test.go` are failing due to timeouts. The timeout used for the context in these tests is 5 seconds. Instead of increasing it, I decided to reduce the number of logs that are written in tests as well as some sleep statements that I don't think are needed. Note that in `nats.go` we use `context.WithTimeout` so that's why the context deadline exceeded error is raised. Screenshot 2026-01-07 at 9 38 04 AM ## Testing Did 5 test runs and measured the test completion time ### Before Screenshot 2026-01-07 at 9 39 59 AM Completion time (avg): 1.23s ### After Screenshot 2026-01-07 at 9 38 53 AM Completion time (avg): 0.31s --- server/logging/nats_test.go | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/server/logging/nats_test.go b/server/logging/nats_test.go index 0133b0e36c..3a3c1e01ae 100644 --- a/server/logging/nats_test.go +++ b/server/logging/nats_test.go @@ -20,7 +20,7 @@ import ( ) const ( - natsTestLogCount = 1000 + natsTestLogCount = 2 natsTestDirectSubject = "test.logs.direct" natsTestStreamSubject = "test.logs.stream" natsTestStreamName = "test-logs-stream" @@ -68,15 +68,15 @@ func makeNatsServer(t *testing.T) *server.Server { return ns } -// makeNatsLogs creates a number of test logs. +// makeNatsLogs creates test logs. func makeNatsLogs(t *testing.T) []json.RawMessage { t.Helper() var logs []json.RawMessage - for n := range natsTestLogCount { + for i := range natsTestLogCount { logs = append(logs, - json.RawMessage(fmt.Sprintf(`{"foo":"bar %d"}`, n)), + json.RawMessage(fmt.Sprintf(`{"foo":"bar %d"}`, i)), ) } @@ -235,9 +235,6 @@ func TestNatsLogWriter(t *testing.T) { require.Error(t, err) - // Wait a moment to ensure no messages were published. - time.Sleep(100 * time.Millisecond) - // Ensure no messages were received. lock.Lock() require.Equal(t, 0, seen) @@ -345,9 +342,6 @@ func TestNatsLogWriter(t *testing.T) { require.Error(t, err) - // Wait a moment to ensure no messages were published. - time.Sleep(100 * time.Millisecond) - // Verify that the stream has no messages. info, err := st.Info(ctx)