From 0cd66da8f63a8ba280cc6b41d5758a79bf0327ae Mon Sep 17 00:00:00 2001 From: rcourtman Date: Thu, 4 Dec 2025 20:41:00 +0000 Subject: [PATCH] fix: Race condition in TestLoadActiveAlerts causing flaky test ClearActiveAlerts triggers an async save to disk, which can race with LoadActiveAlerts reading the file. The test now clears the in-memory map directly without triggering the async save. --- internal/alerts/alerts_test.go | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/internal/alerts/alerts_test.go b/internal/alerts/alerts_test.go index 3bcbedc..6f8dde9 100644 --- a/internal/alerts/alerts_test.go +++ b/internal/alerts/alerts_test.go @@ -14688,7 +14688,6 @@ func TestLoadActiveAlerts(t *testing.T) { t.Run("loads alerts from valid file", func(t *testing.T) { m := newTestManager(t) - m.ClearActiveAlerts() // Create an alert and save it startTime := time.Now().Add(-30 * time.Minute) @@ -14714,8 +14713,10 @@ func TestLoadActiveAlerts(t *testing.T) { // Save to disk _ = m.SaveActiveAlerts() - // Clear and reload - m.ClearActiveAlerts() + // Clear in-memory map only (don't use ClearActiveAlerts which triggers async save) + m.mu.Lock() + m.activeAlerts = make(map[string]*Alert) + m.mu.Unlock() err := m.LoadActiveAlerts() if err != nil { @@ -14739,7 +14740,6 @@ func TestLoadActiveAlerts(t *testing.T) { t.Run("skips old alerts", func(t *testing.T) { m := newTestManager(t) - m.ClearActiveAlerts() // Create an old alert (>24 hours) startTime := time.Now().Add(-25 * time.Hour) @@ -14760,8 +14760,10 @@ func TestLoadActiveAlerts(t *testing.T) { // Save to disk _ = m.SaveActiveAlerts() - // Clear and reload - m.ClearActiveAlerts() + // Clear in-memory map only (don't use ClearActiveAlerts which triggers async save) + m.mu.Lock() + m.activeAlerts = make(map[string]*Alert) + m.mu.Unlock() err := m.LoadActiveAlerts() if err != nil { @@ -14779,7 +14781,6 @@ func TestLoadActiveAlerts(t *testing.T) { t.Run("skips old acknowledged alerts", func(t *testing.T) { m := newTestManager(t) - m.ClearActiveAlerts() // Create an alert acknowledged >1 hour ago startTime := time.Now().Add(-30 * time.Minute) @@ -14804,8 +14805,10 @@ func TestLoadActiveAlerts(t *testing.T) { // Save to disk _ = m.SaveActiveAlerts() - // Clear and reload - m.ClearActiveAlerts() + // Clear in-memory map only (don't use ClearActiveAlerts which triggers async save) + m.mu.Lock() + m.activeAlerts = make(map[string]*Alert) + m.mu.Unlock() err := m.LoadActiveAlerts() if err != nil { @@ -14823,7 +14826,6 @@ func TestLoadActiveAlerts(t *testing.T) { t.Run("restores acknowledgment state", func(t *testing.T) { m := newTestManager(t) - m.ClearActiveAlerts() // Create an acknowledged alert startTime := time.Now().Add(-10 * time.Minute) @@ -14848,8 +14850,11 @@ func TestLoadActiveAlerts(t *testing.T) { // Save to disk _ = m.SaveActiveAlerts() - // Clear and reload - m.ClearActiveAlerts() + // Clear in-memory maps only (don't use ClearActiveAlerts which triggers async save) + m.mu.Lock() + m.activeAlerts = make(map[string]*Alert) + m.ackState = make(map[string]ackRecord) + m.mu.Unlock() err := m.LoadActiveAlerts() if err != nil {