From bc8ca912ce0d90eaca9df5e5b991695adcdd6a47 Mon Sep 17 00:00:00 2001 From: HaiLaz <739476267@qq.com> Date: Thu, 10 Nov 2022 19:59:09 +0800 Subject: [PATCH] fix: gcron check if the predefined patterns fail (#2288) --- os/gcron/gcron_schedule.go | 3 ++- os/gcron/gcron_z_unit_schedule_test.go | 9 +++++++++ os/gcron/gcron_z_unit_test.go | 16 ++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/os/gcron/gcron_schedule.go b/os/gcron/gcron_schedule.go index 14c595a84..e926e748e 100644 --- a/os/gcron/gcron_schedule.go +++ b/os/gcron/gcron_schedule.go @@ -123,8 +123,9 @@ func newSchedule(pattern string) (*cronSchedule, error) { pattern: pattern, lastTimestamp: gtype.NewInt64(currentTimestamp), }, nil + } else { + return nil, gerror.NewCodef(gcode.CodeInvalidParameter, `invalid pattern: "%s"`, pattern) } - return nil, gerror.NewCodef(gcode.CodeInvalidParameter, `invalid pattern: "%s"`, pattern) } // Handle the common cron pattern, like: // 0 0 0 1 1 2 diff --git a/os/gcron/gcron_z_unit_schedule_test.go b/os/gcron/gcron_z_unit_schedule_test.go index 3b406c41c..277389a6a 100644 --- a/os/gcron/gcron_z_unit_schedule_test.go +++ b/os/gcron/gcron_z_unit_schedule_test.go @@ -73,6 +73,15 @@ func TestNext(t *testing.T) { // Leap year {"Mon Jul 9 23:35 2012", "0 0 0 29 Feb ?", "Mon Feb 29 00:00 2016"}, + + // 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"}, } for _, c := range runs { diff --git a/os/gcron/gcron_z_unit_test.go b/os/gcron/gcron_z_unit_test.go index 47a743b5f..d84d904e9 100644 --- a/os/gcron/gcron_z_unit_test.go +++ b/os/gcron/gcron_z_unit_test.go @@ -67,6 +67,22 @@ func TestCron_Basic(t *testing.T) { t.AssertNE(entry1, nil) t.Assert(entry2, nil) }) + + // test @ error + gtest.C(t, func(t *gtest.T) { + cron := gcron.New() + defer cron.Close() + _, err := cron.Add(ctx, "@aaa", func(ctx context.Context) {}, "add") + t.AssertNE(err, nil) + }) + + // test @every error + gtest.C(t, func(t *gtest.T) { + cron := gcron.New() + defer cron.Close() + _, err := cron.Add(ctx, "@every xxx", func(ctx context.Context) {}, "add") + t.AssertNE(err, nil) + }) } func TestCron_Remove(t *testing.T) {