7
"github.com/satori/go.uuid"
8
. "github.com/smartystreets/goconvey/convey"
10
"go.avito.ru/DO/moira"
11
"go.avito.ru/DO/moira/database"
12
"go.avito.ru/DO/moira/test-helpers"
15
func TestNotificationEvents(t *testing.T) {
16
logger := test_helpers.GetTestLogger()
17
dataBase := NewDatabase(logger, config)
19
defer dataBase.flush()
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)
26
So(actual, ShouldResemble, make([]*moira.NotificationEvent, 0))
28
total := dataBase.GetNotificationEventCount(notificationEvent.TriggerID, 0)
29
So(total, ShouldEqual, 0)
31
actual1, err := dataBase.FetchNotificationEvent(false)
32
So(err, ShouldBeError)
33
So(err, ShouldResemble, database.ErrNil)
34
So(actual1, ShouldResemble, moira.NotificationEvent{})
37
Convey("Should has one events after push", func() {
38
err := dataBase.PushNotificationEvent(¬ificationEvent)
41
actual, err := dataBase.GetNotificationEvents(notificationEvent.TriggerID, 0, 1)
43
So(actual, ShouldResemble, []*moira.NotificationEvent{¬ificationEvent})
45
total := dataBase.GetNotificationEventCount(notificationEvent.TriggerID, 0)
46
So(total, ShouldEqual, 1)
48
actual1, err := dataBase.FetchNotificationEvent(false)
50
So(actual1, ShouldResemble, notificationEvent)
53
Convey("Should has event by triggerID after fetch", func() {
54
actual, err := dataBase.GetNotificationEvents(notificationEvent.TriggerID, 0, 1)
56
So(actual, ShouldResemble, []*moira.NotificationEvent{¬ificationEvent})
58
total := dataBase.GetNotificationEventCount(notificationEvent.TriggerID, 0)
59
So(total, ShouldEqual, 1)
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{})
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(¬ificationEvent1)
75
err = dataBase.PushNotificationEvent(¬ificationEvent2)
78
actual, err := dataBase.GetNotificationEvents(notificationEvent1.TriggerID, 0, 1)
80
So(actual, ShouldResemble, []*moira.NotificationEvent{¬ificationEvent1})
82
total := dataBase.GetNotificationEventCount(notificationEvent1.TriggerID, 0)
83
So(total, ShouldEqual, 1)
85
actual, err = dataBase.GetNotificationEvents(notificationEvent2.TriggerID, 0, 1)
87
So(actual, ShouldResemble, []*moira.NotificationEvent{¬ificationEvent2})
89
total = dataBase.GetNotificationEventCount(notificationEvent2.TriggerID, 0)
90
So(total, ShouldEqual, 1)
93
Convey("Fetch one of them and check for existing again", func() {
94
actual1, err := dataBase.FetchNotificationEvent(false)
96
So(actual1, ShouldResemble, notificationEvent1)
98
actual, err := dataBase.GetNotificationEvents(notificationEvent1.TriggerID, 0, 1)
100
So(actual, ShouldResemble, []*moira.NotificationEvent{¬ificationEvent1})
102
total := dataBase.GetNotificationEventCount(notificationEvent1.TriggerID, 0)
103
So(total, ShouldEqual, 1)
106
Convey("Fetch second then fetch and and check for ErrNil", func() {
107
actual, err := dataBase.FetchNotificationEvent(false)
109
So(actual, ShouldResemble, notificationEvent2)
111
actual, err = dataBase.FetchNotificationEvent(false)
112
So(err, ShouldBeError)
113
So(err, ShouldResemble, database.ErrNil)
114
So(actual, ShouldResemble, moira.NotificationEvent{})
118
Convey("Test get by ranges", func() {
119
now := time.Now().Unix()
120
event := moira.NotificationEvent{
123
OldState: moira.NODATA,
124
TriggerID: uuid.NewV4().String(),
128
err := dataBase.PushNotificationEvent(&event)
131
actual, err := dataBase.GetNotificationEvents(event.TriggerID, 0, 1)
133
So(actual, ShouldResemble, []*moira.NotificationEvent{&event})
135
total := dataBase.GetNotificationEventCount(event.TriggerID, 0)
136
So(total, ShouldEqual, 1)
138
total = dataBase.GetNotificationEventCount(event.TriggerID, now-1)
139
So(total, ShouldEqual, 1)
141
total = dataBase.GetNotificationEventCount(event.TriggerID, now)
142
So(total, ShouldEqual, 1)
144
total = dataBase.GetNotificationEventCount(event.TriggerID, now+1)
145
So(total, ShouldEqual, 0)
147
actual, err = dataBase.GetNotificationEvents(event.TriggerID, 1, 1)
149
So(actual, ShouldResemble, make([]*moira.NotificationEvent, 0))
154
func TestNotificationEventErrorConnection(t *testing.T) {
155
logger := test_helpers.GetTestLogger()
156
dataBase := NewDatabase(logger, emptyConfig)
158
defer dataBase.flush()
160
var notificationEvent = moira.NotificationEvent{
161
Timestamp: time.Now().Unix(),
163
OldState: moira.NODATA,
164
TriggerID: "81588c33-eab3-4ad4-aa03-82a9560adad9",
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)
173
err = dataBase.PushNotificationEvent(¬ificationEvent)
174
So(err, ShouldNotBeNil)
176
total := dataBase.GetNotificationEventCount("123", 0)
177
So(total, ShouldEqual, 0)
179
actual2, err := dataBase.FetchNotificationEvent(false)
180
So(actual2, ShouldResemble, moira.NotificationEvent{})
181
So(err, ShouldNotBeNil)
185
var notificationEvent = moira.NotificationEvent{
186
Timestamp: time.Now().Unix(),
188
OldState: moira.NODATA,
189
TriggerID: "81588c33-eab3-4ad4-aa03-82a9560adad9",
193
var notificationEvent1 = moira.NotificationEvent{
194
Timestamp: time.Now().Unix(),
195
State: moira.EXCEPTION,
196
OldState: moira.NODATA,
197
TriggerID: uuid.NewV4().String(),
200
var notificationEvent2 = moira.NotificationEvent{
201
Timestamp: time.Now().Unix(),
203
OldState: moira.WARN,
204
TriggerID: uuid.NewV4().String(),
205
Metric: "my.metric1",