forked from Axway/agent-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasejob_test.go
85 lines (69 loc) · 1.6 KB
/
basejob_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
package jobs
import (
"context"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
type singleJobImpl struct {
Job
name string
runTime time.Duration
ready bool
readyLock *sync.Mutex
}
func (j *singleJobImpl) Execute() error {
time.Sleep(j.runTime)
return nil
}
func (j *singleJobImpl) Status() error {
return nil
}
func (j *singleJobImpl) Ready() bool {
j.readyLock.Lock()
defer j.readyLock.Unlock()
return j.ready
}
func (j *singleJobImpl) setReady(ready bool) {
j.readyLock.Lock()
defer j.readyLock.Unlock()
j.ready = ready
}
func statusWaiter(ctx context.Context, t *testing.T, statuses []JobStatus, jobID string, doneChan chan interface{}) {
for _, status := range statuses {
for {
select {
case <-ctx.Done():
assert.Fail(t, "did not get all statuses")
doneChan <- nil
return
default:
}
curStat := GetJobStatus(jobID)
if curStat == jobStatusToString[status] {
break
}
}
}
doneChan <- nil
}
func TestSingleRunJob(t *testing.T) {
job := &singleJobImpl{
name: "SingleJob",
runTime: 1 * time.Second,
ready: false,
readyLock: &sync.Mutex{},
}
jobID, _ := RegisterSingleRunJob(job)
globalPool.jobs[jobID].(*baseJob).setBackoff(newBackoffTimeout(time.Millisecond, time.Millisecond, 1))
statuses := []JobStatus{JobStatusRunning, JobStatusFinished}
ctx, cancelFunc := context.WithTimeout(context.Background(), time.Second*10)
defer cancelFunc()
testDone := make(chan interface{})
go statusWaiter(ctx, t, statuses, jobID, testDone)
job.setReady(true)
<-testDone
assert.Nil(t, ctx.Err())
UnregisterJob(jobID)
}