Skip to content

Commit

Permalink
test: for alive watcher
Browse files Browse the repository at this point in the history
  • Loading branch information
AleksandrMatsko committed Dec 11, 2024
1 parent bbb6a9d commit cf24dc6
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions notifier/alive_watcher_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package notifier

import (
"errors"
"testing"
"time"

"github.com/moira-alert/moira"
logging "github.com/moira-alert/moira/logging/zerolog_adapter"
"github.com/moira-alert/moira/metrics"
. "github.com/smartystreets/goconvey/convey"
"go.uber.org/mock/gomock"

mock_moira_alert "github.com/moira-alert/moira/mock/moira-alert"
mock_metrics "github.com/moira-alert/moira/mock/moira-alert/metrics"
)

func initAliveMeter(mockCtrl *gomock.Controller) (*mock_metrics.MockRegistry, *mock_metrics.MockMeter) {
mockRegistry := mock_metrics.NewMockRegistry(mockCtrl)
mockAliveMeter := mock_metrics.NewMockMeter(mockCtrl)

mockRegistry.EXPECT().NewMeter(gomock.Any()).Times(5)
mockRegistry.EXPECT().NewHistogram(gomock.Any()).Times(3)
mockRegistry.EXPECT().NewMeter("", "alive").Return(mockAliveMeter)

return mockRegistry, mockAliveMeter
}

func TestAliveWatcher_checkNotifierState(t *testing.T) {
logger, _ = logging.GetLogger("test alive watcher")

mockCtrl := gomock.NewController(t)
dataBase := mock_moira_alert.NewMockDatabase(mockCtrl)
defer mockCtrl.Finish()

testConf := Config{
CheckNotifierStateTimeout: time.Second * 10,
}

mockRegistry, mockAliveMeter := initAliveMeter(mockCtrl)
testNotifierMetrics := metrics.ConfigureNotifierMetrics(mockRegistry, "")

aliveWatcher := NewAliveWatcher(logger, dataBase, testConf, testNotifierMetrics)

Convey("checkNotifierState", t, func() {
Convey("when OK", func() {
dataBase.EXPECT().GetNotifierState().Return(moira.SelfStateOK, nil)
mockAliveMeter.EXPECT().Mark(int64(1))

aliveWatcher.checkNotifierState()
})

Convey("when not OK state and no errors", func() {
notOKStates := []string{moira.SelfStateERROR, "err", "bad", "", "1"}

for _, badState := range notOKStates {
dataBase.EXPECT().GetNotifierState().Return(badState, nil)
mockAliveMeter.EXPECT().Mark(int64(0))

aliveWatcher.checkNotifierState()
}
})

Convey("when not OK state and errors", func() {
notOKState := ""
givenErrors := []error{
errors.New("one error"),
errors.New("another error"),
}

for _, err := range givenErrors {
dataBase.EXPECT().GetNotifierState().Return(notOKState, err)
mockAliveMeter.EXPECT().Mark(int64(0))

aliveWatcher.checkNotifierState()
}
})
})
}

0 comments on commit cf24dc6

Please sign in to comment.