moira

Форк
0
/
notification_event_test.go 
206 строк · 6.5 Кб
1
package redis
2

3
import (
4
	"testing"
5
	"time"
6

7
	"github.com/satori/go.uuid"
8
	. "github.com/smartystreets/goconvey/convey"
9

10
	"go.avito.ru/DO/moira"
11
	"go.avito.ru/DO/moira/database"
12
	"go.avito.ru/DO/moira/test-helpers"
13
)
14

15
func TestNotificationEvents(t *testing.T) {
16
	logger := test_helpers.GetTestLogger()
17
	dataBase := NewDatabase(logger, config)
18
	dataBase.flush()
19
	defer dataBase.flush()
20

21
	Convey("Notification events manipulation", t, func() {
22
		Convey("Test push-get-get count-fetch", func() {
23
			Convey("Should no events", func() {
24
				actual, err := dataBase.GetNotificationEvents(notificationEvent.TriggerID, 0, 1)
25
				So(err, ShouldBeNil)
26
				So(actual, ShouldResemble, make([]*moira.NotificationEvent, 0))
27

28
				total := dataBase.GetNotificationEventCount(notificationEvent.TriggerID, 0)
29
				So(total, ShouldEqual, 0)
30

31
				actual1, err := dataBase.FetchNotificationEvent(false)
32
				So(err, ShouldBeError)
33
				So(err, ShouldResemble, database.ErrNil)
34
				So(actual1, ShouldResemble, moira.NotificationEvent{})
35
			})
36

37
			Convey("Should has one events after push", func() {
38
				err := dataBase.PushNotificationEvent(&notificationEvent)
39
				So(err, ShouldBeNil)
40

41
				actual, err := dataBase.GetNotificationEvents(notificationEvent.TriggerID, 0, 1)
42
				So(err, ShouldBeNil)
43
				So(actual, ShouldResemble, []*moira.NotificationEvent{&notificationEvent})
44

45
				total := dataBase.GetNotificationEventCount(notificationEvent.TriggerID, 0)
46
				So(total, ShouldEqual, 1)
47

48
				actual1, err := dataBase.FetchNotificationEvent(false)
49
				So(err, ShouldBeNil)
50
				So(actual1, ShouldResemble, notificationEvent)
51
			})
52

53
			Convey("Should has event by triggerID after fetch", func() {
54
				actual, err := dataBase.GetNotificationEvents(notificationEvent.TriggerID, 0, 1)
55
				So(err, ShouldBeNil)
56
				So(actual, ShouldResemble, []*moira.NotificationEvent{&notificationEvent})
57

58
				total := dataBase.GetNotificationEventCount(notificationEvent.TriggerID, 0)
59
				So(total, ShouldEqual, 1)
60
			})
61

62
			Convey("Should no events to fetch after fetch", func() {
63
				actual1, err := dataBase.FetchNotificationEvent(false)
64
				So(err, ShouldBeError)
65
				So(err, ShouldResemble, database.ErrNil)
66
				So(actual1, ShouldResemble, moira.NotificationEvent{})
67
			})
68
		})
69

70
		Convey("Test push-fetch multiple event by differ triggerIDs", func() {
71
			Convey("Push events and get it by triggerIDs", func() {
72
				err := dataBase.PushNotificationEvent(&notificationEvent1)
73
				So(err, ShouldBeNil)
74

75
				err = dataBase.PushNotificationEvent(&notificationEvent2)
76
				So(err, ShouldBeNil)
77

78
				actual, err := dataBase.GetNotificationEvents(notificationEvent1.TriggerID, 0, 1)
79
				So(err, ShouldBeNil)
80
				So(actual, ShouldResemble, []*moira.NotificationEvent{&notificationEvent1})
81

82
				total := dataBase.GetNotificationEventCount(notificationEvent1.TriggerID, 0)
83
				So(total, ShouldEqual, 1)
84

85
				actual, err = dataBase.GetNotificationEvents(notificationEvent2.TriggerID, 0, 1)
86
				So(err, ShouldBeNil)
87
				So(actual, ShouldResemble, []*moira.NotificationEvent{&notificationEvent2})
88

89
				total = dataBase.GetNotificationEventCount(notificationEvent2.TriggerID, 0)
90
				So(total, ShouldEqual, 1)
91
			})
92

93
			Convey("Fetch one of them and check for existing again", func() {
94
				actual1, err := dataBase.FetchNotificationEvent(false)
95
				So(err, ShouldBeNil)
96
				So(actual1, ShouldResemble, notificationEvent1)
97

98
				actual, err := dataBase.GetNotificationEvents(notificationEvent1.TriggerID, 0, 1)
99
				So(err, ShouldBeNil)
100
				So(actual, ShouldResemble, []*moira.NotificationEvent{&notificationEvent1})
101

102
				total := dataBase.GetNotificationEventCount(notificationEvent1.TriggerID, 0)
103
				So(total, ShouldEqual, 1)
104
			})
105

106
			Convey("Fetch second then fetch and and check for ErrNil", func() {
107
				actual, err := dataBase.FetchNotificationEvent(false)
108
				So(err, ShouldBeNil)
109
				So(actual, ShouldResemble, notificationEvent2)
110

111
				actual, err = dataBase.FetchNotificationEvent(false)
112
				So(err, ShouldBeError)
113
				So(err, ShouldResemble, database.ErrNil)
114
				So(actual, ShouldResemble, moira.NotificationEvent{})
115
			})
116
		})
117

118
		Convey("Test get by ranges", func() {
119
			now := time.Now().Unix()
120
			event := moira.NotificationEvent{
121
				Timestamp: now,
122
				State:     moira.NODATA,
123
				OldState:  moira.NODATA,
124
				TriggerID: uuid.NewV4().String(),
125
				Metric:    "my.metric",
126
			}
127

128
			err := dataBase.PushNotificationEvent(&event)
129
			So(err, ShouldBeNil)
130

131
			actual, err := dataBase.GetNotificationEvents(event.TriggerID, 0, 1)
132
			So(err, ShouldBeNil)
133
			So(actual, ShouldResemble, []*moira.NotificationEvent{&event})
134

135
			total := dataBase.GetNotificationEventCount(event.TriggerID, 0)
136
			So(total, ShouldEqual, 1)
137

138
			total = dataBase.GetNotificationEventCount(event.TriggerID, now-1)
139
			So(total, ShouldEqual, 1)
140

141
			total = dataBase.GetNotificationEventCount(event.TriggerID, now)
142
			So(total, ShouldEqual, 1)
143

144
			total = dataBase.GetNotificationEventCount(event.TriggerID, now+1)
145
			So(total, ShouldEqual, 0)
146

147
			actual, err = dataBase.GetNotificationEvents(event.TriggerID, 1, 1)
148
			So(err, ShouldBeNil)
149
			So(actual, ShouldResemble, make([]*moira.NotificationEvent, 0))
150
		})
151
	})
152
}
153

154
func TestNotificationEventErrorConnection(t *testing.T) {
155
	logger := test_helpers.GetTestLogger()
156
	dataBase := NewDatabase(logger, emptyConfig)
157
	dataBase.flush()
158
	defer dataBase.flush()
159

160
	var notificationEvent = moira.NotificationEvent{
161
		Timestamp: time.Now().Unix(),
162
		State:     moira.NODATA,
163
		OldState:  moira.NODATA,
164
		TriggerID: "81588c33-eab3-4ad4-aa03-82a9560adad9",
165
		Metric:    "my.metric",
166
	}
167

168
	Convey("Should throw error when no connection", t, func() {
169
		actual1, err := dataBase.GetNotificationEvents("123", 0, 1)
170
		So(actual1, ShouldBeNil)
171
		So(err, ShouldNotBeNil)
172

173
		err = dataBase.PushNotificationEvent(&notificationEvent)
174
		So(err, ShouldNotBeNil)
175

176
		total := dataBase.GetNotificationEventCount("123", 0)
177
		So(total, ShouldEqual, 0)
178

179
		actual2, err := dataBase.FetchNotificationEvent(false)
180
		So(actual2, ShouldResemble, moira.NotificationEvent{})
181
		So(err, ShouldNotBeNil)
182
	})
183
}
184

185
var notificationEvent = moira.NotificationEvent{
186
	Timestamp: time.Now().Unix(),
187
	State:     moira.NODATA,
188
	OldState:  moira.NODATA,
189
	TriggerID: "81588c33-eab3-4ad4-aa03-82a9560adad9",
190
	Metric:    "my.metric",
191
}
192

193
var notificationEvent1 = moira.NotificationEvent{
194
	Timestamp: time.Now().Unix(),
195
	State:     moira.EXCEPTION,
196
	OldState:  moira.NODATA,
197
	TriggerID: uuid.NewV4().String(),
198
	Metric:    "my.metric",
199
}
200
var notificationEvent2 = moira.NotificationEvent{
201
	Timestamp: time.Now().Unix(),
202
	State:     moira.OK,
203
	OldState:  moira.WARN,
204
	TriggerID: uuid.NewV4().String(),
205
	Metric:    "my.metric1",
206
}
207

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.