-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconf_test.go
152 lines (127 loc) · 2.69 KB
/
conf_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
package cronjob
import (
"bufio"
"bytes"
"log"
"sync"
"testing"
"time"
)
func TestWithLogger(t *testing.T) {
t.Parallel()
buf := &bytes.Buffer{}
logger := log.New(buf, "[Test]", log.Flags())
cron := New(WithLogger(logger))
// start and stop should generate messages into the logger.
cron.Start()
cron.Stop()
time.Sleep(1 * time.Second)
if got, want := buf.Len(), 0; got <= want {
t.Fatalf("got: %v want: non zero + positive", got)
}
}
func TestWithVerbose(t *testing.T) {
t.Parallel()
buf := &bytes.Buffer{}
logger := log.New(buf, "[Test]", log.Flags())
cron := New(WithLogger(logger), WithVerbose())
// start and stop should generate messages into the logger.
cron.Start()
cron.AddFunc(func() error { return nil }, In(cron.Now(), 5*time.Hour))
cron.Stop()
time.Sleep(1 * time.Second)
reader := bufio.NewReader(buf)
// advance first 2 lines.
reader.ReadLine()
reader.ReadLine()
// expected 3rd line with verbose mode.
if _, _, err := reader.ReadLine(); err != nil {
t.Fatal(err)
}
}
func TestWithLocation(t *testing.T) {
t.Parallel()
cron := New(WithLocation(time.UTC))
if got, want := cron.Location(), time.UTC; got != want {
t.Fatalf("got: %v want: %v", got, want)
}
}
/* Job Confs --------------------------------------------------------------------------- */
func TestWithChain(t *testing.T) {
t.Parallel()
var count int
cron := New()
cron.AddFunc(
func() error { return nil },
In(cron.Now(), 1*time.Second),
WithChain(
NewChain(func(fj FuncJob) FuncJob {
return func() error {
count++
return fj()
}
}),
),
)
cron.Start()
defer cron.Stop()
time.Sleep(2 * time.Second)
if got, want := count, 1; got != want {
t.Fatalf("got: %v want: %v", got, want)
}
}
func TestWithRunOnStart(t *testing.T) {
t.Parallel()
t.Run("Without Chains", func(t *testing.T) {
t.Parallel()
wg := &sync.WaitGroup{}
wg.Add(1)
cron := New()
cron.AddFunc(
func() error { wg.Done(); return nil },
In(
cron.Now(),
10*time.Second,
),
WithRunOnStart(),
)
cron.Start()
defer cron.Stop()
select {
case <-wait(wg):
// ran.
case <-time.After(1 * time.Second):
t.Fatal("job didn't run.")
}
})
t.Run("With Chains", func(t *testing.T) {
t.Parallel()
wg := &sync.WaitGroup{}
wg.Add(2)
cron := New()
cron.AddFunc(
func() error { wg.Done(); return nil },
In(
cron.Now(),
10*time.Second,
),
WithRunOnStart(),
WithChain(
NewChain(func(fj FuncJob) FuncJob {
return func() error {
wg.Done()
return fj()
}
}),
),
)
cron.Start()
defer cron.Stop()
select {
case <-wait(wg):
// ran.
case <-time.After(1 * time.Second):
t.Fatal("job didn't run.")
}
})
}