2018-02-16 10:00:13 -06:00
|
|
|
package alerting
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"math"
|
|
|
|
"testing"
|
|
|
|
|
2019-05-20 05:13:32 -05:00
|
|
|
"time"
|
|
|
|
|
2019-03-29 00:58:37 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2018-02-16 10:00:13 -06:00
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
|
|
)
|
|
|
|
|
|
|
|
type FakeEvalHandler struct {
|
2018-04-13 11:40:14 -05:00
|
|
|
SuccessCallID int // 0 means never success
|
2018-02-16 10:00:13 -06:00
|
|
|
CallNb int
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewFakeEvalHandler(successCallID int) *FakeEvalHandler {
|
|
|
|
return &FakeEvalHandler{
|
|
|
|
SuccessCallID: successCallID,
|
|
|
|
CallNb: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (handler *FakeEvalHandler) Eval(evalContext *EvalContext) {
|
|
|
|
handler.CallNb++
|
|
|
|
if handler.CallNb != handler.SuccessCallID {
|
|
|
|
evalContext.Error = errors.New("Fake evaluation failure")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type FakeResultHandler struct{}
|
|
|
|
|
2019-05-20 05:13:32 -05:00
|
|
|
func (handler *FakeResultHandler) handle(evalContext *EvalContext) error {
|
2018-02-16 10:00:13 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEngineProcessJob(t *testing.T) {
|
|
|
|
Convey("Alerting engine job processing", t, func() {
|
2019-06-03 03:25:58 -05:00
|
|
|
engine := &AlertEngine{}
|
2019-05-20 05:13:32 -05:00
|
|
|
engine.Init()
|
2019-03-29 00:58:37 -05:00
|
|
|
setting.AlertingEvaluationTimeout = 30 * time.Second
|
|
|
|
setting.AlertingNotificationTimeout = 30 * time.Second
|
|
|
|
setting.AlertingMaxAttempts = 3
|
2018-02-16 10:00:13 -06:00
|
|
|
engine.resultHandler = &FakeResultHandler{}
|
|
|
|
job := &Job{Running: true, Rule: &Rule{}}
|
|
|
|
|
|
|
|
Convey("Should trigger retry if needed", func() {
|
|
|
|
|
|
|
|
Convey("error + not last attempt -> retry", func() {
|
|
|
|
engine.evalHandler = NewFakeEvalHandler(0)
|
|
|
|
|
2019-03-29 00:58:37 -05:00
|
|
|
for i := 1; i < setting.AlertingMaxAttempts; i++ {
|
2018-02-16 10:00:13 -06:00
|
|
|
attemptChan := make(chan int, 1)
|
2019-03-29 00:58:37 -05:00
|
|
|
cancelChan := make(chan context.CancelFunc, setting.AlertingMaxAttempts)
|
2018-02-16 10:00:13 -06:00
|
|
|
|
|
|
|
engine.processJob(i, attemptChan, cancelChan, job)
|
|
|
|
nextAttemptID, more := <-attemptChan
|
|
|
|
|
|
|
|
So(nextAttemptID, ShouldEqual, i+1)
|
|
|
|
So(more, ShouldEqual, true)
|
|
|
|
So(<-cancelChan, ShouldNotBeNil)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("error + last attempt -> no retry", func() {
|
|
|
|
engine.evalHandler = NewFakeEvalHandler(0)
|
|
|
|
attemptChan := make(chan int, 1)
|
2019-03-29 00:58:37 -05:00
|
|
|
cancelChan := make(chan context.CancelFunc, setting.AlertingMaxAttempts)
|
2018-02-16 10:00:13 -06:00
|
|
|
|
2019-03-29 00:58:37 -05:00
|
|
|
engine.processJob(setting.AlertingMaxAttempts, attemptChan, cancelChan, job)
|
2018-02-16 10:00:13 -06:00
|
|
|
nextAttemptID, more := <-attemptChan
|
|
|
|
|
|
|
|
So(nextAttemptID, ShouldEqual, 0)
|
|
|
|
So(more, ShouldEqual, false)
|
|
|
|
So(<-cancelChan, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("no error -> no retry", func() {
|
|
|
|
engine.evalHandler = NewFakeEvalHandler(1)
|
|
|
|
attemptChan := make(chan int, 1)
|
2019-03-29 00:58:37 -05:00
|
|
|
cancelChan := make(chan context.CancelFunc, setting.AlertingMaxAttempts)
|
2018-02-16 10:00:13 -06:00
|
|
|
|
|
|
|
engine.processJob(1, attemptChan, cancelChan, job)
|
|
|
|
nextAttemptID, more := <-attemptChan
|
|
|
|
|
|
|
|
So(nextAttemptID, ShouldEqual, 0)
|
|
|
|
So(more, ShouldEqual, false)
|
|
|
|
So(<-cancelChan, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Should trigger as many retries as needed", func() {
|
|
|
|
|
2018-04-13 11:40:14 -05:00
|
|
|
Convey("never success -> max retries number", func() {
|
2019-03-29 00:58:37 -05:00
|
|
|
expectedAttempts := setting.AlertingMaxAttempts
|
2018-02-16 10:00:13 -06:00
|
|
|
evalHandler := NewFakeEvalHandler(0)
|
|
|
|
engine.evalHandler = evalHandler
|
|
|
|
|
|
|
|
engine.processJobWithRetry(context.TODO(), job)
|
|
|
|
So(evalHandler.CallNb, ShouldEqual, expectedAttempts)
|
|
|
|
})
|
|
|
|
|
2018-04-13 11:40:14 -05:00
|
|
|
Convey("always success -> never retry", func() {
|
2018-02-16 10:00:13 -06:00
|
|
|
expectedAttempts := 1
|
|
|
|
evalHandler := NewFakeEvalHandler(1)
|
|
|
|
engine.evalHandler = evalHandler
|
|
|
|
|
|
|
|
engine.processJobWithRetry(context.TODO(), job)
|
|
|
|
So(evalHandler.CallNb, ShouldEqual, expectedAttempts)
|
|
|
|
})
|
|
|
|
|
2018-04-13 11:40:14 -05:00
|
|
|
Convey("some errors before success -> some retries", func() {
|
2019-03-29 00:58:37 -05:00
|
|
|
expectedAttempts := int(math.Ceil(float64(setting.AlertingMaxAttempts) / 2))
|
2018-02-16 10:00:13 -06:00
|
|
|
evalHandler := NewFakeEvalHandler(expectedAttempts)
|
|
|
|
engine.evalHandler = evalHandler
|
|
|
|
|
|
|
|
engine.processJobWithRetry(context.TODO(), job)
|
|
|
|
So(evalHandler.CallNb, ShouldEqual, expectedAttempts)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|