@@ -22,7 +22,7 @@ func TestNoEntries(t *testing.T) {
22
22
// Add a job, start cron, expect it runs.
23
23
func TestAddBeforeRunning (t * testing.T ) {
24
24
cron := New ()
25
- cron .Add ("* * * * * ?" , func () {
25
+ cron .AddFunc ("* * * * * ?" , func () {
26
26
cron .Stop ()
27
27
})
28
28
done := startAndSignal (cron )
@@ -40,7 +40,7 @@ func TestAddWhileRunning(t *testing.T) {
40
40
cron := New ()
41
41
done := startAndSignal (cron )
42
42
go func () {
43
- cron .Add ("* * * * * ?" , func () {
43
+ cron .AddFunc ("* * * * * ?" , func () {
44
44
cron .Stop ()
45
45
})
46
46
}()
@@ -57,11 +57,11 @@ func TestAddWhileRunning(t *testing.T) {
57
57
// that the immediate entry runs immediately.
58
58
func TestMultipleEntries (t * testing.T ) {
59
59
cron := New ()
60
- cron .Add ("0 0 0 1 1 ?" , func () {})
61
- cron .Add ("* * * * * ?" , func () {
60
+ cron .AddFunc ("0 0 0 1 1 ?" , func () {})
61
+ cron .AddFunc ("* * * * * ?" , func () {
62
62
cron .Stop ()
63
63
})
64
- cron .Add ("0 0 0 31 12 ?" , func () {})
64
+ cron .AddFunc ("0 0 0 31 12 ?" , func () {})
65
65
done := startAndSignal (cron )
66
66
67
67
select {
@@ -77,7 +77,7 @@ func TestLocalTimezone(t *testing.T) {
77
77
now := time .Now ().Local ()
78
78
spec := fmt .Sprintf ("%d %d %d %d %d ?" ,
79
79
now .Second ()+ 1 , now .Minute (), now .Hour (), now .Day (), now .Month ())
80
- cron .Add (spec , func () { cron .Stop () })
80
+ cron .AddFunc (spec , func () { cron .Stop () })
81
81
done := startAndSignal (cron )
82
82
83
83
select {
@@ -87,6 +87,40 @@ func TestLocalTimezone(t *testing.T) {
87
87
}
88
88
}
89
89
90
+ type testRunnable struct {
91
+ cron * Cron
92
+ name string
93
+ }
94
+
95
+ func (t testRunnable ) Run () {
96
+ t .cron .Stop ()
97
+ }
98
+
99
+ // Simple test using Runnables.
100
+ func TestRunnable (t * testing.T ) {
101
+ cron := New ()
102
+ cron .AddJob ("0 0 0 30 Feb ?" , testRunnable {cron , "job0" })
103
+ cron .AddJob ("0 0 0 1 1 ?" , testRunnable {cron , "job1" })
104
+ cron .AddJob ("* * * * * ?" , testRunnable {cron , "job2" })
105
+ cron .AddJob ("1 0 0 1 1 ?" , testRunnable {cron , "job3" })
106
+
107
+ done := startAndSignal (cron )
108
+ select {
109
+ case <- time .After (2 * time .Second ):
110
+ t .FailNow ()
111
+ case <- done :
112
+ }
113
+
114
+ // Ensure the entries are in the right order.
115
+ answers := []string {"job2" , "job1" , "job3" , "job0" }
116
+ for i , answer := range answers {
117
+ actual := cron .Entries [i ].Job .(testRunnable ).name
118
+ if actual != answer {
119
+ t .Errorf ("Jobs not in the right order. (expected) %s != %s (actual)" , answer , actual )
120
+ }
121
+ }
122
+ }
123
+
90
124
// Return a channel that signals when the cron's Start() method returns.
91
125
func startAndSignal (cron * Cron ) <- chan struct {} {
92
126
ch := make (chan struct {})
0 commit comments