From 0535bdf156464b415d15da84bcb8b78c00256079 Mon Sep 17 00:00:00 2001 From: IamTheFij Date: Sun, 9 Feb 2025 13:27:36 -0800 Subject: [PATCH] Fix incorrect alertCount --- monitor.go | 1 + monitor_test.go | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/monitor.go b/monitor.go index d907c66..6a65bc0 100644 --- a/monitor.go +++ b/monitor.go @@ -149,6 +149,7 @@ func (monitor *Monitor) failure() (notice *AlertNotice) { // If we're going to alert, increment count if notice != nil { monitor.alertCount++ + notice.AlertCount = monitor.alertCount } return notice diff --git a/monitor_test.go b/monitor_test.go index 9648d6b..7fd37f7 100644 --- a/monitor_test.go +++ b/monitor_test.go @@ -138,6 +138,47 @@ func TestMonitorSuccess(t *testing.T) { } } +func TestMonitorAlertCount(t *testing.T) { + var alertEvery int16 = 1 + + cases := []struct { + checkSuccess bool + alertCount int16 + name string + }{ + {false, 1, "First failure and first alert"}, + {false, 2, "Second failure and first alert"}, + {true, 2, "Success should preserve past alert count"}, + {false, 1, "First failure and first alert after success"}, + } + + // Unlike previous tests, this one requires a static Monitor with repeated + // calls to the failure method + monitor := Monitor{failureCount: 0, AlertAfter: 1, AlertEvery: &alertEvery} + + for _, c := range cases { + log.Printf("Testing case %s", c.name) + + var notice *AlertNotice + if c.checkSuccess { + notice = monitor.success() + } else { + notice = monitor.failure() + } + + if notice == nil { + t.Errorf("failure(%v) expected notice, got nil", c.name) + } + + if notice.AlertCount != c.alertCount { + t.Errorf("failure(%v), expected=%v actual=%v", c.name, c.alertCount, notice.AlertCount) + log.Printf("Case failed: %s", c.name) + } + + log.Println("-----") + } +} + // TestMonitorFailureAlertAfter tests that alerts will not trigger until // hitting the threshold provided by AlertAfter func TestMonitorFailureAlertAfter(t *testing.T) {