Added unit test

Signed-off-by: Marco Pracucci <marco@pracucci.com>
This commit is contained in:
Marco Pracucci 2021-05-11 15:48:02 +02:00
parent 72ef6e04e1
commit 1ad22c808f
No known key found for this signature in database
GPG Key ID: 74C1BD403D2DF9B5
1 changed files with 48 additions and 0 deletions

View File

@ -15,6 +15,7 @@ package dispatch
import ( import (
"context" "context"
"fmt"
"reflect" "reflect"
"sort" "sort"
"sync" "sync"
@ -537,3 +538,50 @@ func TestDispatcherRace(t *testing.T) {
go dispatcher.Run() go dispatcher.Run()
dispatcher.Stop() dispatcher.Stop()
} }
func TestDispatcherRaceOnFirstAlertNotDeliveredWhenGroupWaitIsZero(t *testing.T) {
const numAlerts = 100000
logger := log.NewNopLogger()
marker := types.NewMarker(prometheus.NewRegistry())
alerts, err := mem.NewAlerts(context.Background(), marker, time.Hour, logger)
if err != nil {
t.Fatal(err)
}
defer alerts.Close()
route := &Route{
RouteOpts: RouteOpts{
Receiver: "default",
GroupBy: map[model.LabelName]struct{}{"alertname": {}},
GroupWait: 0,
GroupInterval: 1 * time.Hour, // Should never hit in this test.
RepeatInterval: 1 * time.Hour, // Should never hit in this test.
},
}
timeout := func(d time.Duration) time.Duration { return d }
recorder := &recordStage{alerts: make(map[string]map[model.Fingerprint]*types.Alert)}
dispatcher := NewDispatcher(alerts, route, recorder, marker, timeout, logger, NewDispatcherMetrics(prometheus.NewRegistry()))
go dispatcher.Run()
defer dispatcher.Stop()
// Push all alerts.
for i := 0; i < numAlerts; i++ {
alert := newAlert(model.LabelSet{"alertname": model.LabelValue(fmt.Sprintf("Alert_%d", i))})
require.NoError(t, alerts.Put(alert))
}
// Wait until the alerts have been notified or the waiting timeout expires.
for deadline := time.Now().Add(5 * time.Second); time.Now().Before(deadline); {
if len(recorder.Alerts()) >= numAlerts {
break
}
// Throttle.
time.Sleep(10 * time.Millisecond)
}
// We expect all alerts to be notified immediately, since they all belong to different groups.
require.Equal(t, numAlerts, len(recorder.Alerts()))
}