-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecutor_cron_test.go
More file actions
194 lines (156 loc) · 4.97 KB
/
executor_cron_test.go
File metadata and controls
194 lines (156 loc) · 4.97 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package durex_test
import (
"context"
"sync/atomic"
"testing"
"time"
"github.com/simonovic86/durex"
"github.com/simonovic86/durex/storage"
)
func TestExecutor_CronScheduling(t *testing.T) {
store := storage.NewMemory()
executor := durex.New(store)
var executionCount atomic.Int32
// Register command with cron expression (every minute)
executor.HandleFunc("cronJob", func(ctx context.Context, cmd *durex.Instance) (durex.Result, error) {
executionCount.Add(1)
// Stop after first execution for test
if executionCount.Load() >= 1 {
return durex.Empty(), nil
}
return durex.Repeat(), nil
}, durex.Cron("* * * * *"))
ctx := context.Background()
executor.Start(ctx)
defer executor.Stop()
// Add the cron job
instance, err := executor.Add(ctx, durex.Spec{
Name: "cronJob",
Cron: "*/1 * * * *",
})
if err != nil {
t.Fatalf("Add() error = %v", err)
}
// Wait for execution
time.Sleep(100 * time.Millisecond)
// Verify it executed
if executionCount.Load() != 1 {
t.Errorf("Expected 1 execution, got %d", executionCount.Load())
}
// Verify the instance has cron set
refreshed, err := store.Get(ctx, instance.ID)
if err != nil {
t.Fatalf("Get() error = %v", err)
}
if refreshed.Cron != "*/1 * * * *" {
t.Errorf("Instance.Cron = %q, want %q", refreshed.Cron, "*/1 * * * *")
}
}
func TestExecutor_CronRepeatScheduling(t *testing.T) {
store := storage.NewMemory()
executor := durex.New(store)
var executionCount atomic.Int32
// Register command with cron expression
executor.HandleFunc("cronRepeat", func(ctx context.Context, cmd *durex.Instance) (durex.Result, error) {
executionCount.Add(1)
// Always repeat
return durex.Repeat(), nil
})
ctx := context.Background()
executor.Start(ctx)
defer executor.Stop()
// Add the cron job with hourly schedule
_, err := executor.Add(ctx, durex.Spec{
Name: "cronRepeat",
Cron: "0 * * * *", // Every hour at minute 0
})
if err != nil {
t.Fatalf("Add() error = %v", err)
}
// Wait for execution
time.Sleep(100 * time.Millisecond)
// Verify it executed
if executionCount.Load() != 1 {
t.Errorf("Expected 1 execution, got %d", executionCount.Load())
}
// Get the command to check the next scheduled time
commands := store.All()
if len(commands) == 0 {
t.Fatal("Expected at least one command in storage")
}
cmd := commands[0]
if cmd.Status != durex.StatusRepeating {
t.Errorf("Status = %v, want %v", cmd.Status, durex.StatusRepeating)
}
// The ReadyAt should be scheduled to the next hour boundary
now := time.Now()
expectedNext := durex.NextCronTime("0 * * * *", now.Add(-time.Second))
if cmd.ReadyAt.Before(now) {
t.Errorf("ReadyAt = %v should be in the future", cmd.ReadyAt)
}
// Should be within the next hour
if cmd.ReadyAt.After(now.Add(time.Hour + time.Minute)) {
t.Errorf("ReadyAt = %v, expected within next hour (around %v)", cmd.ReadyAt, expectedNext)
}
}
func TestExecutor_CronTakesPrecedenceOverPeriod(t *testing.T) {
store := storage.NewMemory()
executor := durex.New(store, durex.WithDefaultRepeatInterval(time.Second))
var executionCount atomic.Int32
// Register command with both cron and period
executor.HandleFunc("cronVsPeriod", func(ctx context.Context, cmd *durex.Instance) (durex.Result, error) {
executionCount.Add(1)
return durex.Repeat(), nil
})
ctx := context.Background()
executor.Start(ctx)
defer executor.Stop()
// Add with both cron and period - cron should take precedence
_, err := executor.Add(ctx, durex.Spec{
Name: "cronVsPeriod",
Cron: "0 0 * * *", // Daily at midnight
Period: time.Second, // This should be ignored
})
if err != nil {
t.Fatalf("Add() error = %v", err)
}
// Wait for execution
time.Sleep(100 * time.Millisecond)
// Get the command
commands := store.All()
if len(commands) == 0 {
t.Fatal("Expected at least one command in storage")
}
cmd := commands[0]
// The ReadyAt should be set based on cron (next midnight), not period (1 second from now)
now := time.Now()
expectedNextMidnight := durex.NextCronTime("0 0 * * *", now)
// Should be close to next midnight, not 1 second from now
diff := cmd.ReadyAt.Sub(expectedNextMidnight)
if diff < -time.Second || diff > time.Second {
t.Errorf("ReadyAt = %v, expected around %v (diff: %v)", cmd.ReadyAt, expectedNextMidnight, diff)
}
}
func TestCronOption(t *testing.T) {
store := storage.NewMemory()
executor := durex.New(store)
// Use Cron option
executor.HandleFunc("testCron", func(ctx context.Context, cmd *durex.Instance) (durex.Result, error) {
return durex.Empty(), nil
}, durex.Cron("*/5 * * * *"))
ctx := context.Background()
executor.Start(ctx)
defer executor.Stop()
// Add a command and check it has the cron set
instance, err := executor.Add(ctx, durex.Spec{Name: "testCron"})
if err != nil {
t.Fatalf("Add() error = %v", err)
}
refreshed, err := store.Get(ctx, instance.ID)
if err != nil {
t.Fatalf("Get() error = %v", err)
}
if refreshed.Cron != "*/5 * * * *" {
t.Errorf("Instance.Cron = %q, want %q", refreshed.Cron, "*/5 * * * *")
}
}