1
0
mirror of https://github.com/redis/go-redis.git synced 2025-12-03 18:31:14 +03:00

fix flaky tests

This commit is contained in:
Nedyalko Dyakov
2025-11-19 10:58:17 +02:00
parent b635202001
commit cf2d5d3aff
2 changed files with 34 additions and 7 deletions

View File

@@ -8905,27 +8905,37 @@ var _ = Describe("Commands", func() {
const key = "latency-monitor-threshold" const key = "latency-monitor-threshold"
old := client.ConfigGet(ctx, key).Val() 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]) defer client.ConfigSet(ctx, key, old[key])
result, err := client.Latency(ctx).Result() result, err := client.Latency(ctx).Result()
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(len(result)).Should(Equal(0)) 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()) Expect(err).NotTo(HaveOccurred())
result, err = client.Latency(ctx).Result() result, err = client.Latency(ctx).Result()
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(len(result)).Should(Equal(1)) Expect(len(result)).Should(BeNumerically(">=", 1))
// reset latency by event name // 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()) 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() result, err = client.Latency(ctx).Result()
Expect(err).NotTo(HaveOccurred()) 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")
}
}
}) })
}) })
}) })

View File

@@ -700,8 +700,25 @@ func TestConnectionHook(t *testing.T) {
t.Errorf("Connection should be pooled after handoff (shouldPool=%v, shouldRemove=%v)", shouldPool, shouldRemove) t.Errorf("Connection should be pooled after handoff (shouldPool=%v, shouldRemove=%v)", shouldPool, shouldRemove)
} }
// Wait for handoff to complete // Wait for handoff to complete with polling instead of fixed sleep
time.Sleep(50 * time.Millisecond) // 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 // After handoff completion, connection should be usable again
if !conn.IsUsable() { if !conn.IsUsable() {