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.
This commit is contained in:
parent
734b369500
commit
0cd66da8f6
1 changed files with 17 additions and 12 deletions
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Reference in a new issue