-
Notifications
You must be signed in to change notification settings - Fork 1
/
job_test.go
139 lines (111 loc) · 2.61 KB
/
job_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
package workerpool_test
import (
"testing"
"github.com/mdouchement/workerpool"
"github.com/stretchr/testify/assert"
)
func TestJob_Init(t *testing.T) {
job := &workerpool.Job{}
job.Init(&nullLogger{})
// Check missing ActionFunc
assert.Equal(t, workerpool.ErrActionNotDefined, job.Error())
job = &workerpool.Job{
ActionFunc: func(j *workerpool.Job) error {
return nil
},
}
job.Init(&nullLogger{})
assert.Nil(t, job.Error())
}
func TestJob_ID(t *testing.T) {
job := &workerpool.Job{
ActionFunc: func(j *workerpool.Job) error {
return nil
},
}
job.Init(&nullLogger{})
assert.Regexp(t, `^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-4[a-fA-F0-9]{3}-[8|9|aA|bB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}$`, job.ID())
}
func TestJob_Context(t *testing.T) {
job := &workerpool.Job{
ActionFunc: func(j *workerpool.Job) error {
return nil
},
}
job.Init(&nullLogger{})
ctx := job.Context()
go job.Cancel()
done := <-ctx.Done()
assert.Equal(t, struct{}{}, done)
}
func TestJob_Run(t *testing.T) {
hasRun := false
job := &workerpool.Job{
ActionFunc: func(j *workerpool.Job) error {
hasRun = true
return nil
},
}
job.Init(&nullLogger{})
job.Run()
assert.True(t, hasRun)
assert.Equal(t, workerpool.COMPLETED, job.Status())
}
func TestJob_Cancel(t *testing.T) {
cancelled := false
job := &workerpool.Job{
ActionFunc: func(j *workerpool.Job) error {
return nil
},
CancelFunc: func(j *workerpool.Job) error {
cancelled = true
return nil
},
}
job.Init(&nullLogger{})
ctx := job.Context()
go job.Cancel()
done := <-ctx.Done()
assert.Equal(t, struct{}{}, done)
assert.True(t, cancelled)
}
func TestJob_Status(t *testing.T) {
job := &workerpool.Job{
ActionFunc: func(j *workerpool.Job) error {
return nil
},
}
job.Init(&nullLogger{})
assert.Equal(t, workerpool.PENDING, job.Status())
}
func TestJob_Error(t *testing.T) {
job := &workerpool.Job{}
job.Init(&nullLogger{})
assert.Equal(t, workerpool.ErrActionNotDefined, job.Error())
}
func TestJob_ErrHandler(t *testing.T) {
var errFromHandler error
job := &workerpool.Job{
ErrHandler: func(_ *workerpool.Job, err error, panic bool) {
errFromHandler = err
},
}
job.Init(&nullLogger{})
assert.Equal(t, workerpool.ErrActionNotDefined, errFromHandler)
}
func TestJob_OnStatusChangeFunc(t *testing.T) {
status := workerpool.PENDING
job := &workerpool.Job{
ActionFunc: func(j *workerpool.Job) error {
return nil
},
OnStatusChangeFunc: func(j *workerpool.Job) error {
status = j.Status()
return nil
},
}
job.Init(&nullLogger{})
go job.Run()
<-job.Context().Done()
assert.Equal(t, workerpool.COMPLETED, status)
}