forked from timjwright/godoit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjobset_test.go
139 lines (117 loc) · 3.5 KB
/
jobset_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
package main
import (
"testing"
"log"
"github.com/stretchr/testify/assert"
"os"
"io/ioutil"
"path"
"time"
)
var executions = make(map [string]int)
var executor = func(name, path string) {
log.Printf("Executing %s: %s", name, path)
if _,ok := executions[name]; ok {
executions[name] = executions[name]+1
} else {
executions[name] = 1
}
}
func TestScanEmptyDir(t *testing.T) {
withJobSet(func(jobSet *JobSet) {
// Scan an empty directory
assertRescanUpdates(t, jobSet, false)
assertRescanUpdates(t, jobSet, false)
})
}
func TestScanDirWithCommentFile(t *testing.T) {
withJobSet(func(jobSet *JobSet) {
// Commened out job does not create a job
createJob(jobSet, "random_file")
assertRescanUpdates(t, jobSet, false)
})
}
func TestScanDirWithIgnoredFile(t *testing.T) {
withJobSet(func(jobSet *JobSet) {
// Commened out job does not create a job
createJob(jobSet, "#* * * * * * job name")
assertRescanUpdates(t, jobSet, false)
})
}
func TestScanCreate1SecondJob(t *testing.T) {
withJobSet(func(jobSet *JobSet) {
// Commened out job does not create a job
createJob(jobSet, "* * * * * * TestScanCreate1SecondJob")
assertRescanUpdates(t, jobSet, true)
assertJobCount(t, jobSet, 1)
time.Sleep(time.Second * 6)
assertRescanUpdates(t, jobSet, false)
assertExecutions(t, "TestScanCreate1SecondJob", 5)
})
}
func TestScanRemoveJob(t *testing.T) {
withJobSet(func(jobSet *JobSet) {
createJob(jobSet, "* * * * * * TestScanRemoveJob")
assertRescanUpdates(t, jobSet, true)
assertJobCount(t, jobSet, 1)
time.Sleep(time.Second * 2)
removeJob(t, jobSet, "* * * * * * TestScanRemoveJob")
assertRescanUpdates(t, jobSet, true)
assertJobCount(t, jobSet, 0)
time.Sleep(time.Second * 4)
assertExecutions(t, "TestScanRemoveJob", 1)
})
}
func TestScanSwapJobs(t *testing.T) {
withJobSet(func(jobSet *JobSet) {
createJob(jobSet, "* * * * * * TestScanSwapJobs")
createJob(jobSet, "* * * * * * TestScanSwapJobsX")
assertRescanUpdates(t, jobSet, true)
jobSet.printJobs()
assertJobCount(t, jobSet, 2)
time.Sleep(time.Second * 2)
createJob(jobSet, "* * * * * * TestScanSwapJobs_Other")
removeJob(t, jobSet, "* * * * * * TestScanSwapJobs")
assertRescanUpdates(t, jobSet, true)
jobSet.printJobs()
assertJobCount(t, jobSet, 2)
time.Sleep(time.Second * 5)
assertExecutions(t, "TestScanSwapJobs", 1)
assertExecutions(t, "TestScanSwapJobsX", 5)
assertExecutions(t, "TestScanSwapJobs_Other", 3)
})
}
type withJobSetFunc func(jobSet *JobSet)
func withJobSet(aFunc withJobSetFunc) {
dir,_ := ioutil.TempDir("", "")
defer os.RemoveAll(dir)
jobSet := NewJobSet(executor, dir)
defer jobSet.Stop()
println(dir)
aFunc(jobSet)
}
func createJob(jobSet *JobSet, script string) {
f,_ := os.Create(path.Join(jobSet.directory,script))
f.Close()
}
func removeJob(t *testing.T, jobSet *JobSet, script string) {
if err := os.Remove(path.Join(jobSet.directory,script)); err != nil {
t.Fatalf("Failed to remove job %s, error %s", script, err)
}
}
func assertRescanUpdates(t *testing.T, jobSet *JobSet, expectUpdate bool) {
r:= jobSet.Scan();
assert.Equal(t,expectUpdate, r)
}
func assertJobCount(t *testing.T, jobSet *JobSet, expectedJobs int) {
assert.Equal(t,expectedJobs, len(jobSet.jobs))
}
func assertExecutions(t *testing.T, name string, minCount int) {
if actualCount,ok := executions[name]; ok {
assert.True(
t,actualCount >= minCount,
"Number of executions should be at least %n but was %n", minCount, actualCount)
} else {
t.Fatalf("No executions found for job %s", name)
}
}