From 86eaae7f6eb09d81c34b5164a8af1aeb9b5f8efe Mon Sep 17 00:00:00 2001 From: Dylan Myers Date: Thu, 30 Jul 2026 09:30:12 -0400 Subject: [PATCH] chore(tests): poll apache generator tests instead of sleeping (PIPE-986) Assisted-by: Claude Opus 4.8 --- generator/apache/apache_test.go | 40 ++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/generator/apache/apache_test.go b/generator/apache/apache_test.go index 914e86e..675b7a6 100644 --- a/generator/apache/apache_test.go +++ b/generator/apache/apache_test.go @@ -150,8 +150,10 @@ func TestApacheGenerator_Start(t *testing.T) { err = generator.Start(context.Background()) assert.NoError(t, err) - // Wait for some logs to be generated - time.Sleep(200 * time.Millisecond) + // Poll until logs have been generated, then stop. + require.Eventually(t, func() bool { + return len(consumer.writes()) > 0 + }, 5*time.Second, 10*time.Millisecond) // Stop the generator ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) @@ -180,8 +182,10 @@ func TestApacheGenerator_Stop_GracefulShutdown(t *testing.T) { err = generator.Start(context.Background()) require.NoError(t, err) - // Let it run briefly - time.Sleep(50 * time.Millisecond) + // Poll until logs have been generated, then stop. + require.Eventually(t, func() bool { + return len(consumer.writes()) > 0 + }, 5*time.Second, 10*time.Millisecond) // Stop with context ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) @@ -209,8 +213,10 @@ func TestApacheGenerator_WriteErrors_Backoff(t *testing.T) { err = generator.Start(context.Background()) require.NoError(t, err) - // Let it run briefly to trigger write errors and backoff - time.Sleep(200 * time.Millisecond) + // Poll until write errors have been recorded, then stop. + require.Eventually(t, func() bool { + return len(consumer.getErrors()) > 0 + }, 5*time.Second, 10*time.Millisecond) // Stop the generator ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) @@ -232,8 +238,10 @@ func TestApacheGenerator_ConcurrentWorkers(t *testing.T) { err = generator.Start(context.Background()) require.NoError(t, err) - // Let multiple workers run - time.Sleep(200 * time.Millisecond) + // Poll until many logs have been written by the workers, then stop. + require.Eventually(t, func() bool { + return len(consumer.writes()) > 10 + }, 5*time.Second, 10*time.Millisecond) // Stop the generator ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) @@ -255,7 +263,10 @@ func TestFormatAsApacheCLF_DefaultLog(t *testing.T) { err = generator.Start(context.Background()) require.NoError(t, err) - time.Sleep(50 * time.Millisecond) + // Poll until logs have been generated, then stop. + require.Eventually(t, func() bool { + return len(consumer.writes()) > 0 + }, 5*time.Second, 10*time.Millisecond) ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) defer cancel() @@ -287,7 +298,10 @@ func TestFormatAsApacheCLF_ParseFunc(t *testing.T) { err = generator.Start(context.Background()) require.NoError(t, err) - time.Sleep(50 * time.Millisecond) + // Poll until logs have been generated, then stop. + require.Eventually(t, func() bool { + return len(consumer.writes()) > 0 + }, 5*time.Second, 10*time.Millisecond) ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) defer cancel() @@ -351,7 +365,11 @@ func TestApacheLogGenerator_CountLimited(t *testing.T) { t.Fatal("tracker should have been exhausted") } - time.Sleep(100 * time.Millisecond) + // Poll until all 5 writes have landed (the tracker caps at 5, so it will not + // exceed), then stop. + require.Eventually(t, func() bool { + return len(consumer.writes()) == 5 + }, 5*time.Second, 10*time.Millisecond) ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel()