Skip to content

Commit 4941ca6

Browse files
committed
Fix timing
1 parent ec9354b commit 4941ca6

1 file changed

Lines changed: 23 additions & 15 deletions

File tree

pkg/notification/backstop_notifier_test.go

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -90,27 +90,35 @@ func TestBackstopNotifier_SendsAfterTimeout(t *testing.T) {
9090

9191
func TestBackstopNotifier_SendsRepeatedly(t *testing.T) {
9292
mock := &testNotifier{}
93-
backstop := NewBackstopNotifier(mock, 100*time.Millisecond)
93+
backstop := NewBackstopNotifier(mock, 50*time.Millisecond)
9494
defer func() { _ = backstop.Close() }()
9595

96-
// Wait for first backstop
97-
time.Sleep(150 * time.Millisecond)
98-
99-
// Wait for second backstop
100-
time.Sleep(150 * time.Millisecond)
96+
// Wait for at least 2 backstop notifications
97+
deadline := time.Now().Add(500 * time.Millisecond)
98+
for time.Now().Before(deadline) {
99+
notifications := mock.getNotifications()
100+
backstopCount := 0
101+
for _, n := range notifications {
102+
if n.Pattern == "backstop" {
103+
backstopCount++
104+
}
105+
}
106+
if backstopCount >= 2 {
107+
// Success - we got at least 2 backstop notifications
108+
return
109+
}
110+
time.Sleep(10 * time.Millisecond)
111+
}
101112

102-
// Should have two backstop notifications
113+
// If we get here, we timed out waiting
103114
notifications := mock.getNotifications()
104-
if len(notifications) != 2 {
105-
t.Errorf("Expected 2 notifications, got %d", len(notifications))
106-
}
107-
108-
// Both should be backstop notifications
109-
for i, n := range notifications {
110-
if n.Pattern != "backstop" {
111-
t.Errorf("Notification %d: expected backstop pattern, got %s", i, n.Pattern)
115+
backstopCount := 0
116+
for _, n := range notifications {
117+
if n.Pattern == "backstop" {
118+
backstopCount++
112119
}
113120
}
121+
t.Errorf("Expected at least 2 backstop notifications, got %d", backstopCount)
114122
}
115123

116124
func TestBackstopNotifier_NoTimeoutNoBackstop(t *testing.T) {

0 commit comments

Comments
 (0)