-
Notifications
You must be signed in to change notification settings - Fork 0
/
snitch_test.go
138 lines (116 loc) · 3.11 KB
/
snitch_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package snitch
import (
"testing"
"time"
mock_snitch "github.com/barklan/snitch/mock"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
)
func mockPartialBack(t *testing.T) (*gomock.Controller, chan string, *Config, *mock_snitch.Mockbot) {
t.Helper()
ctrl := gomock.NewController(t)
m := mock_snitch.NewMockbot(ctrl)
conf := &Config{
TGToken: "abc",
TGChatID: 23423344,
Level: InfoLevel,
Cooldown: 50 * time.Millisecond,
CacheSize: 5,
}
c := make(chan string, 10)
return ctrl, c, conf, m
}
func TestZap(t *testing.T) {
t.Run("twice the same message should send only one", func(t *testing.T) {
ctrl, c, conf, m := mockPartialBack(t)
defer ctrl.Finish()
logger, err := zap.NewDevelopment()
if err != nil {
t.Errorf("failed to init zap logger")
}
snitch := &Zap{
c: c,
Conf: conf,
bot: m,
L: logger,
}
msg := "this is a log message"
m.EXPECT().ChatByID(gomock.Eq(conf.TGChatID))
m.EXPECT().Handle(gomock.Any(), gomock.Any())
m.EXPECT().Start()
m.EXPECT().Send(gomock.Any(), gomock.Eq(ErrorPrefix+msg)).AnyTimes()
back, err := newBackend(conf, m, c)
if err != nil {
t.Errorf("failed to init backend")
}
go back.start()
time.Sleep(50 * time.Millisecond)
snitch.Error(msg, zap.String("test", "test"))
snitch.Error(msg, zap.String("test", "test"))
})
t.Run("twice the same message after cooldown", func(t *testing.T) {
ctrl, c, conf, m := mockPartialBack(t)
defer ctrl.Finish()
logger, err := zap.NewDevelopment()
if err != nil {
t.Errorf("failed to init zap logger")
}
snitch := &Zap{
c: c,
Conf: conf,
bot: m,
L: logger,
}
msg := "this is a log message"
m.EXPECT().ChatByID(gomock.Eq(conf.TGChatID))
m.EXPECT().Handle(gomock.Any(), gomock.Any())
m.EXPECT().Start()
m.EXPECT().Send(gomock.Any(), gomock.Any()).Times(4)
back, err := newBackend(conf, m, c)
if err != nil {
t.Errorf("failed to init backend")
}
go back.start()
time.Sleep(50 * time.Millisecond)
snitch.Error(msg, zap.String("test", "test"))
time.Sleep(80 * time.Millisecond)
snitch.Error(msg, zap.String("test", "test"))
time.Sleep(80 * time.Millisecond)
snitch.Warn("another message")
time.Sleep(80 * time.Millisecond)
snitch.Info("and info message")
time.Sleep(80 * time.Millisecond)
snitch.Debug("debug message should not be sent")
})
t.Run("send critical notification in sync", func(t *testing.T) {
ctrl, c, conf, m := mockPartialBack(t)
defer ctrl.Finish()
logger, err := zap.NewDevelopment()
if err != nil {
t.Errorf("failed to init zap logger")
}
snitch := &Zap{
c: c,
Conf: conf,
bot: m,
L: logger,
}
msg := "some critical error"
m.EXPECT().ChatByID(gomock.Eq(conf.TGChatID))
m.EXPECT().Handle(gomock.Any(), gomock.Any())
m.EXPECT().Start()
m.EXPECT().ChatByID(gomock.Eq(conf.TGChatID))
back, err := newBackend(conf, m, c)
if err != nil {
t.Errorf("failed to init backend")
}
go back.start()
time.Sleep(50 * time.Millisecond)
assert.Panics(
t,
func() {
snitch.Panic(msg, zap.String("test", "test"))
})
})
}