2021-12-02 20:05:29 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
2021-08-23 23:12:16 +08:00
|
|
|
package gcron
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2021-09-03 19:35:19 +08:00
|
|
|
|
2021-11-09 16:25:44 +08:00
|
|
|
"github.com/gogf/gf/v2/test/gtest"
|
2021-08-23 23:12:16 +08:00
|
|
|
)
|
|
|
|
|
2021-08-26 10:58:54 +08:00
|
|
|
func TestSlash(t *testing.T) {
|
|
|
|
runs := []struct {
|
|
|
|
spec string
|
|
|
|
expected map[int]struct{}
|
|
|
|
}{
|
|
|
|
{"0 0 0 * Feb Mon/2", map[int]struct{}{1: struct{}{}, 3: struct{}{}, 5: struct{}{}}},
|
|
|
|
{"0 0 0 * Feb *", map[int]struct{}{1: struct{}{}, 2: struct{}{}, 3: struct{}{}, 4: struct{}{}, 5: struct{}{}, 6: struct{}{}, 0: struct{}{}}},
|
|
|
|
}
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
for _, c := range runs {
|
|
|
|
sched, err := newSchedule(c.spec)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
|
|
|
|
}
|
2022-07-13 20:20:38 +08:00
|
|
|
t.AssertEQ(sched.weekMap, c.expected)
|
2021-08-26 10:58:54 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-08-23 23:12:16 +08:00
|
|
|
func TestNext(t *testing.T) {
|
|
|
|
runs := []struct {
|
|
|
|
time, spec string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
// Simple cases
|
|
|
|
{"Mon Jul 9 14:45 2012", "0 0/15 * * * *", "Mon Jul 9 15:00 2012"},
|
|
|
|
{"Mon Jul 9 14:59 2012", "0 0/15 * * * *", "Mon Jul 9 15:00 2012"},
|
|
|
|
{"Mon Jul 9 14:59:59 2012", "0 0/15 * * * *", "Mon Jul 9 15:00 2012"},
|
|
|
|
|
|
|
|
// Wrap around hours
|
|
|
|
{"Mon Jul 9 15:45 2012", "0 20-35/15 * * * *", "Mon Jul 9 16:20 2012"},
|
|
|
|
|
|
|
|
// Wrap around days
|
|
|
|
{"Mon Jul 9 23:46 2012", "0 */15 * * * *", "Tue Jul 10 00:00 2012"},
|
|
|
|
{"Mon Jul 9 23:45 2012", "0 20-35/15 * * * *", "Tue Jul 10 00:20 2012"},
|
|
|
|
{"Mon Jul 9 23:35:51 2012", "15/35 20-35/15 * * * *", "Tue Jul 10 00:20:15 2012"},
|
|
|
|
{"Mon Jul 9 23:35:51 2012", "15/35 20-35/15 1/2 * * *", "Tue Jul 10 01:20:15 2012"},
|
|
|
|
{"Mon Jul 9 23:35:51 2012", "15/35 20-35/15 10-12 * * *", "Tue Jul 10 10:20:15 2012"},
|
|
|
|
|
|
|
|
{"Mon Jul 9 23:35:51 2012", "15/35 20-35/15 1/2 */2 * *", "Thu Jul 11 01:20:15 2012"},
|
|
|
|
{"Mon Jul 9 23:35:51 2012", "15/35 20-35/15 * 9-20 * *", "Wed Jul 10 00:20:15 2012"},
|
|
|
|
{"Mon Jul 9 23:35:51 2012", "15/35 20-35/15 * 9-20 Jul *", "Wed Jul 10 00:20:15 2012"},
|
|
|
|
|
|
|
|
// Wrap around months
|
|
|
|
{"Mon Jul 9 23:35 2012", "0 0 0 9 Apr-Oct ?", "Thu Aug 9 00:00 2012"},
|
|
|
|
{"Mon Jul 9 23:35 2012", "0 0 0 */5 Apr,Aug,Oct Mon", "Mon Aug 6 00:00 2012"},
|
|
|
|
{"Mon Jul 9 23:35 2012", "0 0 0 */5 Oct Mon", "Mon Oct 1 00:00 2012"},
|
|
|
|
|
|
|
|
// Wrap around years
|
|
|
|
{"Mon Jul 9 23:35 2012", "0 0 0 * Feb Mon", "Mon Feb 4 00:00 2013"},
|
|
|
|
{"Mon Jul 9 23:35 2012", "0 0 0 * Feb Mon/2", "Fri Feb 1 00:00 2013"},
|
|
|
|
|
|
|
|
// Wrap around minute, hour, day, month, and year
|
|
|
|
{"Mon Dec 31 23:59:45 2012", "0 * * * * *", "Tue Jan 1 00:00:00 2013"},
|
|
|
|
|
|
|
|
// Leap year
|
|
|
|
{"Mon Jul 9 23:35 2012", "0 0 0 29 Feb ?", "Mon Feb 29 00:00 2016"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range runs {
|
|
|
|
sched, err := newSchedule(c.spec)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// fmt.Printf("%+v", sched)
|
|
|
|
actual := sched.Next(getTime(c.time))
|
|
|
|
expected := getTime(c.expected)
|
|
|
|
if !(actual.Unix() == expected.Unix()) {
|
|
|
|
t.Errorf("%s, \"%s\": (expected) %v != %v (actual)", c.time, c.spec, expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func getTime(value string) time.Time {
|
|
|
|
if value == "" {
|
|
|
|
return time.Time{}
|
|
|
|
}
|
|
|
|
|
|
|
|
var location = time.Local
|
|
|
|
if strings.HasPrefix(value, "TZ=") {
|
|
|
|
parts := strings.Fields(value)
|
|
|
|
loc, err := time.LoadLocation(parts[0][len("TZ="):])
|
|
|
|
if err != nil {
|
|
|
|
panic("could not parse location:" + err.Error())
|
|
|
|
}
|
|
|
|
location = loc
|
|
|
|
value = parts[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
var layouts = []string{
|
|
|
|
"Mon Jan 2 15:04 2006",
|
|
|
|
"Mon Jan 2 15:04:05 2006",
|
|
|
|
}
|
|
|
|
for _, layout := range layouts {
|
|
|
|
if t, err := time.ParseInLocation(layout, value, location); err == nil {
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if t, err := time.ParseInLocation("2006-01-02T15:04:05-0700", value, location); err == nil {
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
panic("could not parse time value " + value)
|
|
|
|
}
|