-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtask_test.go
254 lines (239 loc) · 6.68 KB
/
task_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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// Copyright 2021 ecodeclub
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package notify_go
import (
"context"
"errors"
"testing"
"time"
"github.com/ecodeclub/ekit/slice"
"github.com/ecodeclub/notify-go/pkg/notifier"
"github.com/ecodeclub/notify-go/pkg/notifier/mocks"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"
)
/*
测试场景:
1. 调用一次:超时、不超时没有返回error、不超时返回error
2. 并发调用多次:一般不存在这种场景, 同一个TriggerTask只会调用一次, 不同的task间并发安全
*/
func TestTriggerTask_Send(t *testing.T) {
t.Parallel()
ctrl := gomock.NewController(t)
defer ctrl.Finish()
channel := mocks.NewMockIChannel(ctrl)
o1 := channel.EXPECT().Execute(gomock.Any(), gomock.Any()).Return(nil)
o2 := channel.EXPECT().Execute(gomock.Any(), gomock.Any()).Return(context.DeadlineExceeded)
o3 := channel.EXPECT().Execute(gomock.Any(), gomock.Any()).Return(errors.New("test error appear"))
gomock.InOrder(o1, o2, o3)
type fields struct {
Err chan error
TriggerTime time.Time
Notification *Notification
}
tests := []struct {
name string
fields fields
wantErr error
}{
{
name: "normal",
fields: fields{
Err: make(chan error),
TriggerTime: time.Now().Add(1 * time.Second),
Notification: NewNotification(channel, []notifier.Receiver{}, notifier.Content{}),
},
},
{
name: "time out",
fields: fields{
Err: make(chan error),
TriggerTime: time.Now().Add(4 * time.Second),
Notification: NewNotification(channel, []notifier.Receiver{}, notifier.Content{}),
},
wantErr: context.DeadlineExceeded,
},
{
name: "error",
fields: fields{
Err: make(chan error),
TriggerTime: time.Now().Add(1 * time.Second),
Notification: NewNotification(channel, []notifier.Receiver{}, notifier.Content{}),
},
wantErr: errors.New("test error appear"),
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
tt := &TriggerTask{
Err: tc.fields.Err,
TriggerTime: tc.fields.TriggerTime,
Notification: tc.fields.Notification,
HookBefore: func() {},
HookAfter: func() {},
}
ctx, cancel := context.WithTimeout(context.TODO(), 3*time.Second)
defer cancel()
go tt.Send(ctx)
err := <-tc.fields.Err
assert.Equal(t, err, tc.wantErr)
})
}
}
/*
测试场景:
1. 调用一次:超时、不超时没有返回error、不超时返回error
2. 并发调用多次:一般不存在这种场景
*/
func TestCircleTask_Send(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
channel := mocks.NewMockIChannel(ctrl)
channel.EXPECT().Execute(gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
tests := []struct {
name string
Notification *Notification
cronExpr string
begin time.Time
end time.Time
wantFailCnt uint64
wantCnt uint64
}{
{
name: "base",
Notification: NewNotification(channel, []notifier.Receiver{}, notifier.Content{}),
begin: time.Now().Add(time.Second),
end: time.Now().Add(5 * time.Second),
cronExpr: "* * * * * * *", // 每秒执行
wantCnt: 4,
wantFailCnt: 0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ct := NewCircleTask(tt.Notification, tt.cronExpr, tt.begin, tt.end)
ctx, cancel := context.WithTimeout(context.TODO(), 3*time.Second)
defer cancel()
ct.Send(ctx)
assert.Equal(t, tt.wantCnt, ct.circleNum)
})
}
}
func TestCircleTask_fillCronTimes(t *testing.T) {
type args struct {
expr string
begin time.Time
end time.Time
}
tests := []struct {
name string
args args
want []time.Time
}{
{
name: "every minute 历史时间全部被过滤",
args: args{
expr: "* * * * *",
begin: func() time.Time {
ti, _ := time.ParseInLocation("2006-01-02 15:04:05", "2023-10-26 15:31:00", time.Local)
return ti
}(),
end: func() time.Time {
ti, _ := time.ParseInLocation("2006-01-02 15:04:05", "2023-10-26 15:33:00", time.Local)
return ti
}(),
},
want: make([]time.Time, 0),
},
{
name: "every minute 开始时间晚于结束时间",
args: args{
expr: "* * * * *",
begin: func() time.Time {
ti, _ := time.ParseInLocation("2006-01-02 15:04:05", "2023-10-26 15:33:00", time.Local)
return ti
}(),
end: func() time.Time {
ti, _ := time.ParseInLocation("2006-01-02 15:04:05", "2023-10-26 15:31:00", time.Local)
return ti
}(),
},
want: make([]time.Time, 0),
},
{
name: "every second",
args: args{
expr: "* * * * * * *",
begin: func() time.Time { return time.Now().Add(100 * time.Second) }(),
end: func() time.Time { return time.Now().Add(105 * time.Second) }(),
},
want: func() []time.Time {
res := make([]time.Time, 0)
begin := time.Now().Add(100 * time.Second)
end := time.Now().Add(105 * time.Second)
begin = begin.Add(time.Second)
for begin.Before(end) {
res = append(res, begin)
begin = begin.Add(time.Second)
}
return res
}(),
},
{
name: "every minute",
args: args{
expr: "* * * * *",
begin: func() time.Time {
return time.Now().Add(100 * time.Minute)
}(),
end: func() time.Time {
return time.Now().Add(104 * time.Minute)
}(),
},
want: func() []time.Time {
res := make([]time.Time, 0)
begin := time.Now().Add(100 * time.Minute)
end := time.Now().Add(104 * time.Minute)
begin = begin.Add(time.Minute)
for begin.Before(end) {
res = append(res, begin.Truncate(time.Minute)) // 秒数需要置0
begin = begin.Add(time.Minute)
}
return res
}(),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ct := &CircleTask{
CronExpr: tt.args.expr,
}
ct.fillCronTimes(tt.args.begin, tt.args.end)
cronTimes := make([]time.Time, 0)
for {
ti, done := ct.IterCronTimes.Next()
if done {
break
}
cronTimes = append(cronTimes, ti)
}
assert.Equal(t, toStr(tt.want), toStr(cronTimes))
})
}
}
func toStr(arr []time.Time) []string {
return slice.Map[time.Time](arr, func(idx int, src time.Time) string {
return src.Format("2006-01-02 15:04:05")
})
}