-
Notifications
You must be signed in to change notification settings - Fork 0
/
task_test.go
311 lines (295 loc) · 6.78 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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package scheduler
import (
"errors"
"net/http"
"strings"
"testing"
"time"
)
func TestTask_TaskID(t *testing.T) {
t.Parallel()
tests := []struct {
name string
task *Task
want string
}{
{
name: "generate taskID from prefix, id and scheduledAt",
task: &Task{
Prefix: "pre-",
ID: "id",
ScheduledAt: time.Unix(1, 234567890),
Version: 1,
},
want: "pre-id_499602d2v1",
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got := tt.task.TaskID()
if got != tt.want {
t.Errorf("got: %v, want: %v", got, tt.want)
}
})
}
}
func TestTask_Validate(t *testing.T) {
t.Parallel()
tests := []struct {
name string
task *Task
want error
}{
{
name: "empty task id",
task: &Task{
Prefix: "pre-",
ID: "",
ScheduledAt: time.Unix(1, 234567890),
},
want: ErrTaskValidation,
},
{
name: "task id contains invalid character (/)",
task: &Task{
Prefix: "pre-",
ID: "a/b",
ScheduledAt: time.Unix(1, 234567890),
},
want: ErrTaskValidation,
},
{
name: "valid task id",
task: &Task{
Prefix: "pre-",
ID: "a-b_c0",
ScheduledAt: time.Unix(1, 234567890),
},
want: nil,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
err := tt.task.Validate()
if !errors.Is(err, tt.want) {
t.Errorf("got: %v, want: %v", err, tt.want)
}
})
}
}
func TestTask_validateTaskID(t *testing.T) {
t.Parallel()
tests := []struct {
name string
task *Task
want error
}{
{
name: "task id length over 500",
task: &Task{
Prefix: "pre-", // 4 caracters
ID: strings.Join(make([]string, 487), "_"), // 486 caracters
ScheduledAt: time.Unix(1, 234567890), // 9 caracters as encoded string
Version: 1, // 2 caracters
},
want: ErrTaskValidation,
},
{
name: "task id length less then 500",
task: &Task{
Prefix: "pre-", // 4 caracters
ID: strings.Join(make([]string, 486), "_"), // 485 caracters
ScheduledAt: time.Unix(1, 234567890), // 9 caracters as encoded string
Version: 1, // 2 caracters
},
want: nil,
},
{
name: "task id contains invalid character (/)",
task: &Task{
Prefix: "pre-",
ID: "a/b",
ScheduledAt: time.Unix(1, 234567890),
},
want: ErrTaskValidation,
},
{
name: "task id contains invalid character (#)",
task: &Task{
Prefix: "pre-",
ID: "#ab",
ScheduledAt: time.Unix(1, 234567890),
},
want: ErrTaskValidation,
},
{
name: "valid task id",
task: &Task{
Prefix: "pre-",
ID: "a-b_c0",
ScheduledAt: time.Unix(1, 234567890),
},
want: nil,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
err := tt.task.validateTaskID()
if !errors.Is(err, tt.want) {
t.Errorf("got: %v, want: %v", err, tt.want)
}
})
}
}
func TestTask_Compare(t *testing.T) {
t.Parallel()
tests := []struct {
name string
task *Task
target *Task
want bool
}{
{
name: "same task",
task: &Task{
Prefix: "pre-",
ID: "test",
ScheduledAt: time.Unix(10, 1),
Request: func() *http.Request {
req, _ := http.NewRequest(http.MethodPost, "https://example.com", strings.NewReader("test"))
req.Header.Set("User-Agent", "test-client")
return req
}(),
Authorization: &OAuthToken{
ServiceAccountEmail: "[email protected]",
Scope: "https://example.com/test",
},
},
target: &Task{
Prefix: "pre-",
ID: "test",
ScheduledAt: time.Unix(10, 1),
Request: func() *http.Request {
req, _ := http.NewRequest(http.MethodPost, "https://example.com", strings.NewReader("test"))
req.Header.Set("User-Agent", "test-client")
return req
}(),
Authorization: &OAuthToken{
ServiceAccountEmail: "[email protected]",
Scope: "https://example.com/test",
},
},
want: true,
},
{
name: "task with different comparisonID",
task: &Task{
Prefix: "pre-",
ID: "test",
ScheduledAt: time.Unix(10, 1),
},
target: &Task{
Prefix: "pre-",
ID: "test",
ScheduledAt: time.Unix(10, 2),
},
want: false,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got := tt.task.Compare(tt.target)
if got != tt.want {
t.Errorf("got: %v, want: %v", got, tt.want)
}
})
}
}
func TestParseTaskName(t *testing.T) {
t.Parallel()
tests := []struct {
name string
prefix string
taskName string
wantID string
wantVersion int
isError bool
}{
{
name: "valid task name",
prefix: "pre-",
taskName: "projects/tokyo-rain-123/locations/asia-northeast1/queues/scheduler/pre-id_499602d2v1",
wantID: "id",
wantVersion: 1,
},
{
name: "task name with trimmed queue path prefix",
prefix: "pre-",
taskName: "pre-id_499602d2v1",
wantID: "id",
wantVersion: 1,
},
{
name: "invalid prefix",
prefix: "pre-",
taskName: "projects/tokyo-rain-123/locations/asia-northeast1/queues/scheduler/pre_id_499602d2v1",
wantID: "id",
wantVersion: 1,
isError: true,
},
{
name: "invalid task timestamp separator",
prefix: "pre-",
taskName: "projects/tokyo-rain-123/locations/asia-northeast1/queues/scheduler/pre-id-499602d2v1",
wantID: "id",
wantVersion: 1,
isError: true,
},
{
name: "invalid task version separator",
prefix: "pre-",
taskName: "projects/tokyo-rain-123/locations/asia-northeast1/queues/scheduler/pre-id_499602d2_1",
wantID: "id",
wantVersion: 1,
isError: true,
},
{
name: "failed to parse version",
prefix: "pre-",
taskName: "projects/tokyo-rain-123/locations/asia-northeast1/queues/scheduler/pre-id_499602d2vf",
wantID: "id",
wantVersion: 1,
isError: true,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
gotID, gotVersion, err := ParseTaskName(tt.prefix, tt.taskName)
if tt.isError {
if err == nil {
t.Fatal("no error occurred")
}
return
}
if err != nil {
t.Fatalf("unexpected error occurred: %v", err)
}
if gotID != tt.wantID {
t.Errorf("got: %v, want: %v", gotID, tt.wantID)
}
if gotVersion != tt.wantVersion {
t.Errorf("got: %v, want: %v", gotVersion, tt.wantVersion)
}
})
}
}