-
Notifications
You must be signed in to change notification settings - Fork 23
/
job_test.go
139 lines (103 loc) · 2.75 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 crontab_test
import (
"sync"
"testing"
"time"
"github.com/mileusna/crontab"
)
func TestJobError(t *testing.T) {
ctab := crontab.New()
if err := ctab.AddJob("* * * * *", myFunc, 10); err == nil {
t.Error("This AddJob should return Error, wrong number of args")
}
if err := ctab.AddJob("* * * * *", nil); err == nil {
t.Error("This AddJob should return Error, fn is nil")
}
var x int
if err := ctab.AddJob("* * * * *", x); err == nil {
t.Error("This AddJob should return Error, fn is not func kind")
}
if err := ctab.AddJob("* * * * *", myFunc2, "s", 10, 12); err == nil {
t.Error("This AddJob should return Error, wrong number of args")
}
if err := ctab.AddJob("* * * * *", myFunc2, "s", "s2"); err == nil {
t.Error("This AddJob should return Error, args are not the correct type")
}
if err := ctab.AddJob("* * * * * *", myFunc2, "s", "s2"); err == nil {
t.Error("This AddJob should return Error, syntax error")
}
// custom types and interfaces as function params
var m MyTypeInterface
if err := ctab.AddJob("* * * * *", myFuncStruct, m); err != nil {
t.Error(err)
}
if err := ctab.AddJob("* * * * *", myFuncInterface, m); err != nil {
t.Error(err)
}
var mwo MyTypeNoInterface
if err := ctab.AddJob("* * * * *", myFuncInterface, mwo); err == nil {
t.Error("This should return error, type that don't implements interface assigned as param")
}
ctab.Shutdown()
}
var testN int
var testS string
func TestCrontab(t *testing.T) {
testN = 0
testS = ""
ctab := crontab.Fake(2) // fake crontab wiht 2sec timer to speed up test
var wg sync.WaitGroup
wg.Add(2)
if err := ctab.AddJob("* * * * *", func() { testN++; wg.Done() }); err != nil {
t.Fatal(err)
}
if err := ctab.AddJob("* * * * *", func(s string) { testS = s; wg.Done() }, "param"); err != nil {
t.Fatal(err)
}
done := make(chan struct{})
go func() {
wg.Wait()
close(done)
}()
select {
case <-done:
case <-time.After(5 * time.Second):
}
if testN != 1 {
t.Error("func 1 not executed as scheduled")
}
if testS != "param" {
t.Error("func 2 not executed as scheduled")
}
ctab.Shutdown()
}
func TestRunAll(t *testing.T) {
testN = 0
testS = ""
ctab := crontab.New()
var wg sync.WaitGroup
wg.Add(2)
if err := ctab.AddJob("* * * * *", func() { testN++; wg.Done() }); err != nil {
t.Fatal(err)
}
if err := ctab.AddJob("* * * * *", func(s string) { testS = s; wg.Done() }, "param"); err != nil {
t.Fatal(err)
}
ctab.RunAll()
wg.Wait()
if testN != 1 {
t.Error("func not executed on RunAll()")
}
if testS != "param" {
t.Error("func not executed on RunAll() or arg not passed")
}
ctab.Clear()
ctab.RunAll()
if testN != 1 {
t.Error("Jobs not cleared")
}
if testS != "param" {
t.Error("Jobs not cleared")
}
ctab.Shutdown()
}