-
Notifications
You must be signed in to change notification settings - Fork 0
/
job_test.go
170 lines (152 loc) · 3.71 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
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
package main
import (
"errors"
"reflect"
"testing"
"time"
)
type testJob struct {
isRunning bool
isDone bool
err error
}
func (j *testJob) GetStatus() (bool, bool, error) {
return j.isRunning, j.isDone, j.err
}
func (j *testJob) Start() {
j.isRunning = true
time.Sleep(time.Second)
}
func (j *testJob) Stop() {
j.isRunning = false
}
func Test_jobInternal_Start(t *testing.T) {
type fields struct {
isRunning bool
original Job
runningBeforeChannel bool
waitForChannel bool
done chan bool
}
tests := []struct {
name string
fields fields
}{
{
"was not running",
fields{false, &testJob{false, false, nil}, true, true, make(chan bool, 1)},
},
{
"was running",
fields{true, &testJob{true, false, nil}, true, true, make(chan bool, 1)},
},
{
"was done",
fields{false, &testJob{false, true, nil}, false, true, make(chan bool, 1)},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
j := &jobInternal{
isRunning: tt.fields.isRunning,
original: tt.fields.original,
}
j.Start(tt.fields.done)
// wait a bit for the go routine to actually run
time.Sleep(time.Millisecond * 200)
// before setting the signal ok, we want to make sure all is running
if j.isRunning != tt.fields.runningBeforeChannel {
t.Errorf("before channel = %v, want %v", j.isRunning, tt.fields.runningBeforeChannel)
}
if IsRunning, _, _ := j.original.GetStatus(); IsRunning != tt.fields.runningBeforeChannel {
t.Errorf("before channel = %v, want %v", IsRunning, tt.fields.runningBeforeChannel)
}
// wait for done signal
if tt.fields.waitForChannel {
<-tt.fields.done
}
// after all done set it as not running
if j.GetStatus().IsRunning != false {
t.Errorf("after channel = %v, want %v", j.GetStatus().IsRunning, false)
}
if IsRunning, _, _ := j.original.GetStatus(); IsRunning != false {
t.Errorf("after channel = %v, want %v", IsRunning, false)
}
})
}
}
func Test_jobInternal_Stop(t *testing.T) {
type fields struct {
isRunning bool
original Job
waitForChannel bool
done chan bool
}
tests := []struct {
name string
fields fields
want bool
}{
{
"was not running",
fields{false, &testJob{false, false, nil}, false, make(chan bool, 1)},
false,
},
{
"was running",
fields{true, &testJob{true, false, nil}, true, make(chan bool, 1)},
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
j := &jobInternal{
isRunning: tt.fields.isRunning,
original: tt.fields.original,
done: &tt.fields.done,
}
j.Stop()
// wait for done signal
if tt.fields.waitForChannel {
<-tt.fields.done
}
if j.isRunning != tt.want {
t.Errorf("jobInternal.Stop() = %v, want %v", j.isRunning, tt.want)
}
if IsRunning, _, _ := j.original.GetStatus(); IsRunning != tt.want {
t.Errorf("jobInternal.original.Stop() = %v, want %v", IsRunning, tt.want)
}
})
}
}
func Test_jobInternal_GetStatus(t *testing.T) {
tests := []struct {
name string
job Job
want jobStatus
}{
{
"running, not done and no error",
&testJob{true, false, nil},
jobStatus{true, false, nil},
},
{
"not running, done and no error",
&testJob{false, true, nil},
jobStatus{false, true, nil},
},
{
"not running, not done and error",
&testJob{false, false, errors.New("foo")},
jobStatus{false, false, errors.New("foo")},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
j := &jobInternal{original: tt.job}
if got := j.GetStatus(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("jobInternal.GetStatus() = %v, want %v", got, tt.want)
}
})
}
}