|
@@ -6,7 +6,9 @@ import (
|
|
|
"math"
|
|
"math"
|
|
|
"testing"
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
+ "github.com/grafana/grafana/pkg/setting"
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
|
|
|
+ "time"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
type FakeEvalHandler struct {
|
|
type FakeEvalHandler struct {
|
|
@@ -37,6 +39,9 @@ func (handler *FakeResultHandler) Handle(evalContext *EvalContext) error {
|
|
|
func TestEngineProcessJob(t *testing.T) {
|
|
func TestEngineProcessJob(t *testing.T) {
|
|
|
Convey("Alerting engine job processing", t, func() {
|
|
Convey("Alerting engine job processing", t, func() {
|
|
|
engine := NewEngine()
|
|
engine := NewEngine()
|
|
|
|
|
+ setting.AlertingEvaluationTimeout = 30 * time.Second
|
|
|
|
|
+ setting.AlertingNotificationTimeout = 30 * time.Second
|
|
|
|
|
+ setting.AlertingMaxAttempts = 3
|
|
|
engine.resultHandler = &FakeResultHandler{}
|
|
engine.resultHandler = &FakeResultHandler{}
|
|
|
job := &Job{Running: true, Rule: &Rule{}}
|
|
job := &Job{Running: true, Rule: &Rule{}}
|
|
|
|
|
|
|
@@ -45,9 +50,9 @@ func TestEngineProcessJob(t *testing.T) {
|
|
|
Convey("error + not last attempt -> retry", func() {
|
|
Convey("error + not last attempt -> retry", func() {
|
|
|
engine.evalHandler = NewFakeEvalHandler(0)
|
|
engine.evalHandler = NewFakeEvalHandler(0)
|
|
|
|
|
|
|
|
- for i := 1; i < alertMaxAttempts; i++ {
|
|
|
|
|
|
|
+ for i := 1; i < setting.AlertingMaxAttempts; i++ {
|
|
|
attemptChan := make(chan int, 1)
|
|
attemptChan := make(chan int, 1)
|
|
|
- cancelChan := make(chan context.CancelFunc, alertMaxAttempts)
|
|
|
|
|
|
|
+ cancelChan := make(chan context.CancelFunc, setting.AlertingMaxAttempts)
|
|
|
|
|
|
|
|
engine.processJob(i, attemptChan, cancelChan, job)
|
|
engine.processJob(i, attemptChan, cancelChan, job)
|
|
|
nextAttemptID, more := <-attemptChan
|
|
nextAttemptID, more := <-attemptChan
|
|
@@ -61,9 +66,9 @@ func TestEngineProcessJob(t *testing.T) {
|
|
|
Convey("error + last attempt -> no retry", func() {
|
|
Convey("error + last attempt -> no retry", func() {
|
|
|
engine.evalHandler = NewFakeEvalHandler(0)
|
|
engine.evalHandler = NewFakeEvalHandler(0)
|
|
|
attemptChan := make(chan int, 1)
|
|
attemptChan := make(chan int, 1)
|
|
|
- cancelChan := make(chan context.CancelFunc, alertMaxAttempts)
|
|
|
|
|
|
|
+ cancelChan := make(chan context.CancelFunc, setting.AlertingMaxAttempts)
|
|
|
|
|
|
|
|
- engine.processJob(alertMaxAttempts, attemptChan, cancelChan, job)
|
|
|
|
|
|
|
+ engine.processJob(setting.AlertingMaxAttempts, attemptChan, cancelChan, job)
|
|
|
nextAttemptID, more := <-attemptChan
|
|
nextAttemptID, more := <-attemptChan
|
|
|
|
|
|
|
|
So(nextAttemptID, ShouldEqual, 0)
|
|
So(nextAttemptID, ShouldEqual, 0)
|
|
@@ -74,7 +79,7 @@ func TestEngineProcessJob(t *testing.T) {
|
|
|
Convey("no error -> no retry", func() {
|
|
Convey("no error -> no retry", func() {
|
|
|
engine.evalHandler = NewFakeEvalHandler(1)
|
|
engine.evalHandler = NewFakeEvalHandler(1)
|
|
|
attemptChan := make(chan int, 1)
|
|
attemptChan := make(chan int, 1)
|
|
|
- cancelChan := make(chan context.CancelFunc, alertMaxAttempts)
|
|
|
|
|
|
|
+ cancelChan := make(chan context.CancelFunc, setting.AlertingMaxAttempts)
|
|
|
|
|
|
|
|
engine.processJob(1, attemptChan, cancelChan, job)
|
|
engine.processJob(1, attemptChan, cancelChan, job)
|
|
|
nextAttemptID, more := <-attemptChan
|
|
nextAttemptID, more := <-attemptChan
|
|
@@ -88,7 +93,7 @@ func TestEngineProcessJob(t *testing.T) {
|
|
|
Convey("Should trigger as many retries as needed", func() {
|
|
Convey("Should trigger as many retries as needed", func() {
|
|
|
|
|
|
|
|
Convey("never success -> max retries number", func() {
|
|
Convey("never success -> max retries number", func() {
|
|
|
- expectedAttempts := alertMaxAttempts
|
|
|
|
|
|
|
+ expectedAttempts := setting.AlertingMaxAttempts
|
|
|
evalHandler := NewFakeEvalHandler(0)
|
|
evalHandler := NewFakeEvalHandler(0)
|
|
|
engine.evalHandler = evalHandler
|
|
engine.evalHandler = evalHandler
|
|
|
|
|
|
|
@@ -106,7 +111,7 @@ func TestEngineProcessJob(t *testing.T) {
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
Convey("some errors before success -> some retries", func() {
|
|
Convey("some errors before success -> some retries", func() {
|
|
|
- expectedAttempts := int(math.Ceil(float64(alertMaxAttempts) / 2))
|
|
|
|
|
|
|
+ expectedAttempts := int(math.Ceil(float64(setting.AlertingMaxAttempts) / 2))
|
|
|
evalHandler := NewFakeEvalHandler(expectedAttempts)
|
|
evalHandler := NewFakeEvalHandler(expectedAttempts)
|
|
|
engine.evalHandler = evalHandler
|
|
engine.evalHandler = evalHandler
|
|
|
|
|
|