From cf2d5d3aff20f4db03ad1c565367c729b03814b4 Mon Sep 17 00:00:00 2001 From: Nedyalko Dyakov Date: Wed, 19 Nov 2025 10:58:17 +0200 Subject: [PATCH] fix flaky tests --- commands_test.go | 20 +++++++++++++++----- maintnotifications/pool_hook_test.go | 21 +++++++++++++++++++-- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/commands_test.go b/commands_test.go index edbae4e7..24640c23 100644 --- a/commands_test.go +++ b/commands_test.go @@ -8905,27 +8905,37 @@ var _ = Describe("Commands", func() { const key = "latency-monitor-threshold" old := client.ConfigGet(ctx, key).Val() - client.ConfigSet(ctx, key, "1") + // Use a higher threshold (100ms) to avoid capturing normal operations + // that could cause flakiness due to timing variations + client.ConfigSet(ctx, key, "100") defer client.ConfigSet(ctx, key, old[key]) result, err := client.Latency(ctx).Result() Expect(err).NotTo(HaveOccurred()) Expect(len(result)).Should(Equal(0)) - err = client.Do(ctx, "DEBUG", "SLEEP", 0.01).Err() + // Use a longer sleep (150ms) to ensure it exceeds the 100ms threshold + err = client.Do(ctx, "DEBUG", "SLEEP", 0.15).Err() Expect(err).NotTo(HaveOccurred()) result, err = client.Latency(ctx).Result() Expect(err).NotTo(HaveOccurred()) - Expect(len(result)).Should(Equal(1)) + Expect(len(result)).Should(BeNumerically(">=", 1)) // reset latency by event name - err = client.LatencyReset(ctx, result[0].Name).Err() + eventName := result[0].Name + err = client.LatencyReset(ctx, eventName).Err() Expect(err).NotTo(HaveOccurred()) + // Verify the specific event was reset (not that all events are gone) + // This avoids flakiness from other operations triggering latency events result, err = client.Latency(ctx).Result() Expect(err).NotTo(HaveOccurred()) - Expect(len(result)).Should(Equal(0)) + for _, event := range result { + if event.Name == eventName { + Fail("Event " + eventName + " should have been reset") + } + } }) }) }) diff --git a/maintnotifications/pool_hook_test.go b/maintnotifications/pool_hook_test.go index 6ec61eed..01fa35a2 100644 --- a/maintnotifications/pool_hook_test.go +++ b/maintnotifications/pool_hook_test.go @@ -700,8 +700,25 @@ func TestConnectionHook(t *testing.T) { t.Errorf("Connection should be pooled after handoff (shouldPool=%v, shouldRemove=%v)", shouldPool, shouldRemove) } - // Wait for handoff to complete - time.Sleep(50 * time.Millisecond) + // Wait for handoff to complete with polling instead of fixed sleep + // This avoids flakiness on slow CI runners where 50ms may not be enough + maxWait := 500 * time.Millisecond + pollInterval := 10 * time.Millisecond + deadline := time.Now().Add(maxWait) + + handoffCompleted := false + for time.Now().Before(deadline) { + if conn.IsUsable() && !processor.IsHandoffPending(conn) { + handoffCompleted = true + break + } + time.Sleep(pollInterval) + } + + if !handoffCompleted { + t.Fatalf("Handoff did not complete within %v (IsUsable=%v, IsHandoffPending=%v)", + maxWait, conn.IsUsable(), processor.IsHandoffPending(conn)) + } // After handoff completion, connection should be usable again if !conn.IsUsable() {