diff --git a/generator/apache_combined/apache_combined_test.go b/generator/apache_combined/apache_combined_test.go index 4add9f6..34936e9 100644 --- a/generator/apache_combined/apache_combined_test.go +++ b/generator/apache_combined/apache_combined_test.go @@ -146,8 +146,10 @@ func TestApacheCombinedGenerator_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) @@ -178,8 +180,10 @@ func TestApacheCombinedGenerator_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) @@ -207,8 +211,10 @@ func TestApacheCombinedGenerator_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) @@ -230,8 +236,10 @@ func TestApacheCombinedGenerator_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) @@ -253,7 +261,10 @@ func TestFormatAsApacheCombined_Structure(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() @@ -288,7 +299,10 @@ func TestFormatAsApacheCombined_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() @@ -342,7 +356,11 @@ func TestApacheCombinedLogGenerator_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()