chore: improve access to default fields in cron parser (#5750)
Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> Co-authored-by: memoclaw <265580040+memoclaw@users.noreply.github.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
parent
e0cc247823
commit
ff32869d2f
2 changed files with 19 additions and 7 deletions
|
|
@ -627,28 +627,31 @@ func TestStopAndWait(t *testing.T) {
|
||||||
t.Run("repeated calls to stop, waiting for completion and after", func(*testing.T) {
|
t.Run("repeated calls to stop, waiting for completion and after", func(*testing.T) {
|
||||||
cron := newWithSeconds()
|
cron := newWithSeconds()
|
||||||
cron.AddFunc("* * * * * *", func() {})
|
cron.AddFunc("* * * * * *", func() {})
|
||||||
cron.AddFunc("* * * * * *", func() { time.Sleep(2 * time.Second) })
|
cron.AddFunc("* * * * * *", func() { time.Sleep(3 * time.Second) })
|
||||||
cron.Start()
|
cron.Start()
|
||||||
cron.AddFunc("* * * * * *", func() {})
|
cron.AddFunc("* * * * * *", func() {})
|
||||||
time.Sleep(time.Second)
|
time.Sleep(time.Second)
|
||||||
ctx := cron.Stop()
|
ctx := cron.Stop()
|
||||||
ctx2 := cron.Stop()
|
ctx2 := cron.Stop()
|
||||||
|
|
||||||
// Verify that it is not done for at least 1500ms
|
// Verify that it is not done for at least 1500ms.
|
||||||
|
// The 3-second sleep job starts within 1s of cron.Start(), so it always has
|
||||||
|
// at least 2s remaining when Stop() is called, well above this threshold.
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
t.Error("context was done too quickly immediately")
|
t.Error("context was done too quickly immediately")
|
||||||
case <-ctx2.Done():
|
case <-ctx2.Done():
|
||||||
t.Error("context2 was done too quickly immediately")
|
t.Error("context2 was done too quickly immediately")
|
||||||
case <-time.After(1500 * time.Millisecond):
|
case <-time.After(1500 * time.Millisecond):
|
||||||
// expected, because the job sleeping for 2 seconds is still running
|
// expected, because the job sleeping for 3 seconds is still running
|
||||||
}
|
}
|
||||||
|
|
||||||
// Verify that it IS done in the next 1s (giving 500ms buffer)
|
// Verify that it IS done in the next 2s (giving 500ms buffer).
|
||||||
|
// After the 1500ms wait above, at most 1500ms of the job remain.
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
// expected
|
// expected
|
||||||
case <-time.After(time.Second):
|
case <-time.After(2 * time.Second):
|
||||||
t.Error("context not done after job should have completed")
|
t.Error("context not done after job should have completed")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,15 @@ const (
|
||||||
Descriptor // Allow descriptors such as @monthly, @weekly, etc.
|
Descriptor // Allow descriptors such as @monthly, @weekly, etc.
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
secondIndex = iota
|
||||||
|
minuteIndex
|
||||||
|
hourIndex
|
||||||
|
domIndex
|
||||||
|
monthIndex
|
||||||
|
dowIndex
|
||||||
|
)
|
||||||
|
|
||||||
var places = []ParseOption{
|
var places = []ParseOption{
|
||||||
Second,
|
Second,
|
||||||
Minute,
|
Minute,
|
||||||
|
|
@ -193,9 +202,9 @@ func normalizeFields(fields []string, options ParseOption) ([]string, error) {
|
||||||
if min < max && len(fields) == min {
|
if min < max && len(fields) == min {
|
||||||
switch {
|
switch {
|
||||||
case options&DowOptional > 0:
|
case options&DowOptional > 0:
|
||||||
fields = append(fields, defaults[5]) // TODO: improve access to default
|
fields = append(fields, defaults[dowIndex])
|
||||||
case options&SecondOptional > 0:
|
case options&SecondOptional > 0:
|
||||||
fields = append([]string{defaults[0]}, fields...)
|
fields = append([]string{defaults[secondIndex]}, fields...)
|
||||||
default:
|
default:
|
||||||
return nil, errors.New("unexpected optional field")
|
return nil, errors.New("unexpected optional field")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue