-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtask_test.go
70 lines (60 loc) · 1.45 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
package iocast
import (
"context"
"errors"
"testing"
)
var (
totalAttempts = 0
)
func testTaskFn(_ context.Context, args string) (string, error) {
return args, nil
}
func testFailingTaskFn(_ context.Context, _ string) (string, error) {
totalAttempts++
return "", errors.New("something went wrong")
}
func TestTask(t *testing.T) {
args := "test"
taskFn := NewTaskFunc(context.Background(), args, testTaskFn)
task := TaskBuilder("simple", taskFn).Build()
taskFnWithRetries := NewTaskFunc(context.Background(), args, testFailingTaskFn)
taskWithRetries := TaskBuilder("retries", taskFnWithRetries).MaxRetries(3).Build()
tests := []struct {
name string
expected string
job Job
}{
{
"simple task",
args,
task,
},
{
"task with retries",
args,
taskWithRetries,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.job.Exec(context.Background())
switch tt.name {
case "simple task":
result := <-task.Wait()
if result.Out != args {
t.Errorf("Exec returned unexpected result output: got %v want %v", result.Out, args)
}
case "task with retries":
result := <-taskWithRetries.Wait()
expectedMsg := "something went wrong"
if result.Err.Error() != expectedMsg {
t.Errorf("Exec returned unexpected result error: got %v want %v", result.Err.Error(), expectedMsg)
}
if totalAttempts != 4 {
t.Error("unexpected total attempts made")
}
}
})
}
}