mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2024-12-30 07:40:06 +01:00
43fc1183b9
This makes test code more clear and reduces the number of code lines by 500.
This also simplifies debugging tests. See https://itnext.io/f-tests-as-a-replacement-for-table-driven-tests-in-go-8814a8b19e9e
While at it, consistently use t.Fatal* instead of t.Error* across tests, since t.Error*
requires more boilerplate code, which can result in additional bugs inside tests.
While t.Error* allows writing logging errors for the same, this doesn't simplify fixing
broken tests most of the time.
This is a follow-up for a9525da8a4
51 lines
1.2 KiB
Go
51 lines
1.2 KiB
Go
package notifier
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
metricset "github.com/VictoriaMetrics/metrics"
|
|
)
|
|
|
|
func TestBlackHoleNotifier_Send(t *testing.T) {
|
|
bh := newBlackHoleNotifier()
|
|
if err := bh.Send(context.Background(), []Alert{{
|
|
GroupID: 0,
|
|
Name: "alert0",
|
|
Start: time.Now().UTC(),
|
|
End: time.Now().UTC(),
|
|
Annotations: map[string]string{"a": "b", "c": "d", "e": "f"},
|
|
}}, nil); err != nil {
|
|
t.Fatalf("unexpected error %s", err)
|
|
}
|
|
|
|
alertCount := bh.metrics.alertsSent.Get()
|
|
if alertCount != 1 {
|
|
t.Fatalf("expect value 1; instead got %d", alertCount)
|
|
}
|
|
}
|
|
|
|
func TestBlackHoleNotifier_Close(t *testing.T) {
|
|
bh := newBlackHoleNotifier()
|
|
if err := bh.Send(context.Background(), []Alert{{
|
|
GroupID: 0,
|
|
Name: "alert0",
|
|
Start: time.Now().UTC(),
|
|
End: time.Now().UTC(),
|
|
Annotations: map[string]string{"a": "b", "c": "d", "e": "f"},
|
|
}}, nil); err != nil {
|
|
t.Fatalf("unexpected error %s", err)
|
|
}
|
|
|
|
bh.Close()
|
|
|
|
defaultMetrics := metricset.GetDefaultSet()
|
|
alertMetricName := "vmalert_alerts_sent_total{addr=\"blackhole\"}"
|
|
for _, name := range defaultMetrics.ListMetricNames() {
|
|
if name == alertMetricName {
|
|
t.Fatalf("Metric name should have unregistered.But still present")
|
|
}
|
|
}
|
|
}
|