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{}
|
|
|
|
}{
|
2023-03-17 17:54:14 +08:00
|
|
|
{"0 0 0 * Feb Mon/2", map[int]struct{}{1: {}, 3: {}, 5: {}}},
|
|
|
|
{"0 0 0 * Feb *", map[int]struct{}{1: {}, 2: {}, 3: {}, 4: {}, 5: {}, 6: {}, 0: {}}},
|
2021-08-26 10:58:54 +08:00
|
|
|
}
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
for _, c := range runs {
|
2024-02-07 15:38:24 +08:00
|
|
|
s, err := newSchedule(c.spec)
|
2021-08-26 10:58:54 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
|
|
|
|
}
|
2024-02-07 15:38:24 +08:00
|
|
|
t.AssertEQ(s.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"},
|
2022-11-10 19:59:09 +08:00
|
|
|
|
|
|
|
// Predefined pattern map.
|
|
|
|
{"Mon Jul 9 23:35 2012", "@yearly", "Sun Jan 1 00:00:00 2013"},
|
|
|
|
{"Mon Jul 9 23:35 2012", "@annually", "Sun Jan 1 00:00:00 2013"},
|
|
|
|
{"Mon Jul 9 23:35 2012", "@monthly", "Mon Aug 1 00:00:00 2012"},
|
|
|
|
{"Mon Jul 9 23:35 2012", "@weekly", "Sun Jul 15 00:00:00 2012"},
|
|
|
|
{"Mon Jul 9 23:35 2012", "@daily", "Tue Jul 10 00:00:00 2012"},
|
|
|
|
{"Mon Jul 9 23:35 2012", "@midnight", "Tue Jul 10 00:00:00 2012"},
|
|
|
|
{"Mon Jul 9 23:35 2012", "@hourly", "Tue Jul 10 00:00:00 2012"},
|
2024-02-07 15:38:24 +08:00
|
|
|
|
|
|
|
// Ignore seconds.
|
|
|
|
{"Mon Jul 9 23:35 2012", "# * * * * *", "Mon Jul 9 23:36 2012"},
|
|
|
|
{"Mon Jul 9 23:35 2012", "# */2 * * * *", "Mon Jul 9 23:36 2012"},
|
2021-08-23 23:12:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range runs {
|
2024-02-07 15:38:24 +08:00
|
|
|
s, err := newSchedule(c.spec)
|
2021-08-23 23:12:16 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// fmt.Printf("%+v", sched)
|
2024-02-07 15:38:24 +08:00
|
|
|
actual := s.Next(getTime(c.time))
|
2021-08-23 23:12:16 +08:00
|
|
|
expected := getTime(c.expected)
|
|
|
|
if !(actual.Unix() == expected.Unix()) {
|
2024-02-07 15:38:24 +08:00
|
|
|
t.Errorf(
|
|
|
|
"%s, \"%s\": (expected) %v != %v (actual)",
|
|
|
|
c.time, c.spec, expected, actual,
|
|
|
|
)
|
2021-08-23 23:12:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-11-01 20:12:21 +08:00
|
|
|
|
2021-08-23 23:12:16 +08:00
|
|
|
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)
|
|
|
|
}
|