Skip to content

Commit

Permalink
test: for starting goroutine
Browse files Browse the repository at this point in the history
  • Loading branch information
AleksandrMatsko committed Dec 11, 2024
1 parent 6ca7722 commit 13c318e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 13 deletions.
5 changes: 4 additions & 1 deletion metrics/notifier.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package metrics

import "time"
import (
"time"
)

// NotifierMetrics is a collection of metrics used in notifier.
type NotifierMetrics struct {
Expand Down Expand Up @@ -73,6 +75,7 @@ func (metrics *NotifierMetrics) MarkSendingFailed() {
func (metrics *NotifierMetrics) MarkNotifierIsAlive(isAlive bool) {
if isAlive {
metrics.notifierIsAlive.Mark(1)
return
}

metrics.notifierIsAlive.Mark(0)
Expand Down
4 changes: 0 additions & 4 deletions notifier/alive_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package notifier

import (
"context"
"fmt"
"time"

"github.com/moira-alert/moira"
Expand Down Expand Up @@ -54,13 +53,10 @@ func (watcher *AliveWatcher) stateChecker(ctx context.Context) {

func (watcher *AliveWatcher) checkNotifierState() {
state, _ := watcher.database.GetNotifierState()
fmt.Println(state)
if state != moira.SelfStateOK {
watcher.notifierMetrics.MarkNotifierIsAlive(false)
fmt.Println("Marked as not alive")
return
}

watcher.notifierMetrics.MarkNotifierIsAlive(true)
fmt.Println("Marked as alive")
}
49 changes: 41 additions & 8 deletions notifier/alive_watcher_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package notifier

import (
"context"
"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"
Expand All @@ -27,20 +27,15 @@ func initAliveMeter(mockCtrl *gomock.Controller) (*mock_metrics.MockRegistry, *m
}

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,
}
dataBase := mock_moira_alert.NewMockDatabase(mockCtrl)

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

aliveWatcher := NewAliveWatcher(logger, dataBase, testConf, testNotifierMetrics)
aliveWatcher := NewAliveWatcher(nil, dataBase, Config{}, testNotifierMetrics)

Convey("checkNotifierState", t, func() {
Convey("when OK", func() {
Expand Down Expand Up @@ -77,3 +72,41 @@ func TestAliveWatcher_checkNotifierState(t *testing.T) {
})
})
}

func TestAliveWatcher_Start(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

logger := mock_moira_alert.NewMockLogger(mockCtrl)
eventsBuilder := mock_moira_alert.NewMockEventBuilder(mockCtrl)
logger.EXPECT().Info().Return(eventsBuilder).AnyTimes()

dataBase := mock_moira_alert.NewMockDatabase(mockCtrl)

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

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

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

Convey("AliveWatcher stops on cancel", t, func() {
eventsBuilder.EXPECT().Interface("check_timeout_seconds", testConf.CheckNotifierStateTimeout.Seconds()).Return(eventsBuilder)
eventsBuilder.EXPECT().Msg("Moira Notifier alive watcher started")

eventsBuilder.EXPECT().Msg("Moira Notifier alive watcher stopped")

dataBase.EXPECT().GetNotifierState().Return(moira.SelfStateOK, nil).AnyTimes()
mockAliveMeter.EXPECT().Mark(int64(1)).AnyTimes()

ctx, cancel := context.WithCancel(context.Background())
aliveWatcher.Start(ctx)

select {
case <-time.After(time.Second * 3):
cancel()
}
})
}

0 comments on commit 13c318e

Please sign in to comment.