diff --git a/cluster_test.go b/cluster_test.go index 8b8fff2..b6b952b 100644 --- a/cluster_test.go +++ b/cluster_test.go @@ -181,28 +181,28 @@ func TestCluster_Add(t *testing.T) { // TODO: verify the job is added to the cluster and can be executed after the specified duration } -func TestMain(m *testing.M) { - client := redis.NewClient(&redis.Options{ - Addr: "127.0.0.1" + ":" + "6379", - Password: "", // no password set - DB: 0, // use default DB - }) - if client == nil { - fmt.Println("redis init error") - return - } - // Redis = client +// func TestMain(m *testing.M) { +// client := redis.NewClient(&redis.Options{ +// Addr: "127.0.0.1" + ":" + "6379", +// Password: "", // no password set +// DB: 0, // use default DB +// }) +// if client == nil { +// fmt.Println("redis init error") +// return +// } +// // Redis = client -} +// } -func TestRedis(t *testing.T) { - fmt.Println("6666") - t.Log("fffff") - // t.Fail() - // t.Error("ffff") - // Redis.Set(context.Background(), "dddd", "dddd", 0) - // str, err := Redis.Get(context.Background(), "dddd").Result() - // fmt.Println("ssss", str, err) - // t.Log(str, err) - // t.Fail() -} +// func TestRedis(t *testing.T) { +// fmt.Println("6666") +// t.Log("fffff") +// // t.Fail() +// // t.Error("ffff") +// // Redis.Set(context.Background(), "dddd", "dddd", 0) +// // str, err := Redis.Get(context.Background(), "dddd").Result() +// // fmt.Println("ssss", str, err) +// // t.Log(str, err) +// // t.Fail() +// } diff --git a/go.mod b/go.mod index 78150e6..572917a 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/yuninks/timerx -go 1.19 +go 1.21 require ( github.com/go-redis/redis/v8 v8.11.5 diff --git a/go.sum b/go.sum index f8e2ba0..18a3ecf 100644 --- a/go.sum +++ b/go.sum @@ -5,6 +5,7 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= +github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI= github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo= github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI= @@ -13,8 +14,11 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE= +github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU= github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE= +github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU= github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE= +github.com/onsi/gomega v1.18.1/go.mod h1:0q+aL8jAiMXy9hbwj2mr5GziHiwhAIQpFmmtT5hitRs= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww= @@ -26,12 +30,17 @@ github.com/yuninks/cachex v1.0.5/go.mod h1:5357qz18UvHTJSgZzkMamUzZoFzGeKG9+4tIU github.com/yuninks/lockx v1.0.2 h1:p0n791WmsU8D7YF2tQaNLwPE75jdd774unlJZRTNfaw= github.com/yuninks/lockx v1.0.2/go.mod h1:J6wvuUELLcMn6FCmiZFt7K5w1QQAh1myL7h3JrZaQiQ= golang.org/x/net v0.0.0-20210428140749-89ef3d95e781 h1:DzZ89McO9/gWPsQXS/FVKAlG02ZjaQ6AlZRBimEYOd0= +golang.org/x/net v0.0.0-20210428140749-89ef3d95e781/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk= golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e h1:fLOSk5Q00efkSvAm+4xcoXD+RRmLmmulPn5I3Y9F2EM= +golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/next_time.go b/next_time.go index e1e45d2..9e9d2b6 100644 --- a/next_time.go +++ b/next_time.go @@ -111,12 +111,14 @@ func calculateNextInterval(t time.Time, job JobData) (*time.Time, error) { } func calculateNextMonthTime(t time.Time, job JobData) (*time.Time, error) { - // 尝试光剑本月的执行四件 + // 尝试当月是否有这个天数 + // time.Date(2025, 2, 30, 0, 0, 0, 0, t.Location()) => 2025-03-02 00:00:00 +0800 CST 当月不足往后补 currentMonthTime := time.Date(t.Year(), t.Month(), job.Day, job.Hour, job.Minute, job.Second, 0, t.Location()) // 如果日期无效(比如2月30号),则调整到该月最后一天 if currentMonthTime.Day() != job.Day { - // 获取该月的最后一天 + // 获取该月的最后一天(0日就是上个月最后一天) + // time.Date(2025,2,0,0,0,0,0,time.Local) => 2025-01-31 00:00:00 +0800 CST lastDay := time.Date(t.Year(), t.Month()+1, 0, 0, 0, 0, 0, t.Location()).Day() if job.Day > lastDay { currentMonthTime = time.Date(t.Year(), t.Month(), lastDay, job.Hour, job.Minute, job.Second, 0, t.Location()) diff --git a/next_time_test.go b/next_time_test.go index dd94bbc..2e9b53c 100644 --- a/next_time_test.go +++ b/next_time_test.go @@ -9,6 +9,9 @@ import ( ) func TestGetNextTime(t *testing.T) { + + tt := time.Now() + // Test cases tests := []struct { name string @@ -25,7 +28,7 @@ func TestGetNextTime(t *testing.T) { Minute: 0, Second: 0, }, - expectedTime: time.Date(2022, 3, 15, 10, 0, 0, 0, time.Local), + expectedTime: time.Date(tt.Year(), tt.Month()+1, 15, 10, 0, 0, 0, time.Local), expectedError: nil, }, { @@ -37,7 +40,7 @@ func TestGetNextTime(t *testing.T) { Minute: 0, Second: 0, }, - expectedTime: time.Date(2022, 3, 8, 10, 0, 0, 0, time.Local), // Assuming current date is March 7, 2022 + expectedTime: time.Date(2025, 9, 23, 10, 0, 0, 0, time.Local), // Assuming current date is March 7, 2022 expectedError: nil, }, { @@ -48,7 +51,7 @@ func TestGetNextTime(t *testing.T) { Minute: 0, Second: 0, }, - expectedTime: time.Date(2022, 3, 8, 10, 0, 0, 0, time.Local), // Assuming current date is March 7, 2022 + expectedTime: time.Date(2025, 9, 18, 10, 0, 0, 0, time.Local), // Assuming current date is March 7, 2022 expectedError: nil, }, { @@ -58,25 +61,26 @@ func TestGetNextTime(t *testing.T) { Minute: 0, Second: 0, }, - expectedTime: time.Date(2022, 3, 7, 11, 0, 0, 0, time.Local), // Assuming current date is March 7, 2022, 10:30 AM + expectedTime: time.Date(2025, 9, 17, 16, 0, 0, 0, time.Local), // Assuming current date is March 7, 2022, 10:30 AM expectedError: nil, }, { name: "Test JobTypeEveryMinute", job: JobData{ JobType: JobTypeEveryMinute, - Second: 0, + Second: 12, }, - expectedTime: time.Date(2022, 3, 7, 10, 31, 0, 0, time.Local), // Assuming current date is March 7, 2022, 10:30 AM + expectedTime: time.Date(tt.Year(), tt.Month(), tt.Day(), tt.Hour(), tt.Minute()+1, 12, 0, time.Local), // Assuming current date is March 7, 2022, 10:30 AM expectedError: nil, }, { name: "Test JobTypeInterval", job: JobData{ JobType: JobTypeInterval, + CreateTime: tt, IntervalTime: 1 * time.Hour, }, - expectedTime: time.Date(2022, 3, 7, 11, 30, 0, 0, time.Local), // Assuming current date is March 7, 2022, 10:30 AM + expectedTime: tt.Add(1 * time.Hour), // Assuming current date is March 7, 2022, 10:30 AM expectedError: nil, }, { @@ -99,7 +103,7 @@ func TestGetNextTime(t *testing.T) { t.Errorf("Expected error: %v, Got error: %v", test.expectedError, err) } } else { - if nextTime.IsZero() != (test.expectedTime == time.Time{}) || (nextTime != &test.expectedTime) { + if nextTime.IsZero() != (test.expectedTime == time.Time{}) || (!nextTime.Equal(test.expectedTime)) { t.Errorf("Expected time: %v, Got time: %v", test.expectedTime, nextTime) } } @@ -297,6 +301,8 @@ func TestCalculateNextInterval(t *testing.T) { } } + + // 测试月任务 func TestCalculateNextMonthTime(t *testing.T) { baseTime := time.Date(2023, 6, 15, 12, 30, 45, 0, time.UTC) @@ -340,7 +346,7 @@ func TestCalculateNextMonthTime(t *testing.T) { Minute: 30, Second: 45, }, - currentTime: time.Date(2023, 1, 15, 12, 30, 45, 0, time.UTC), + currentTime: time.Date(2023, 2, 15, 12, 30, 45, 0, time.UTC), expected: time.Date(2023, 2, 28, 12, 30, 45, 0, time.UTC), }, { @@ -352,7 +358,7 @@ func TestCalculateNextMonthTime(t *testing.T) { Minute: 30, Second: 45, }, - currentTime: time.Date(2024, 1, 15, 12, 30, 45, 0, time.UTC), // 2024是闰年 + currentTime: time.Date(2024, 2, 15, 12, 30, 45, 0, time.UTC), // 2024是闰年 expected: time.Date(2024, 2, 29, 12, 30, 45, 0, time.UTC), }, { @@ -378,6 +384,38 @@ func TestCalculateNextMonthTime(t *testing.T) { } } +func TestCalculateNextMonthTimeOnce(t *testing.T){ + // baseTime := time.Date(2023, 6, 15, 12, 30, 45, 0, time.UTC) + + tests := []struct { + name string + job JobData + currentTime time.Time + expected time.Time + }{ + { + name: "2月30日调整到2月28日", + job: JobData{ + JobType: JobTypeEveryMonth, + Day: 30, + Hour: 12, + Minute: 30, + Second: 45, + }, + currentTime: time.Date(2023, 1, 31, 12, 30, 45, 0, time.UTC), + expected: time.Date(2023, 2, 28, 12, 30, 45, 0, time.UTC), + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + result, err := calculateNextMonthTime(tt.currentTime, tt.job) + assert.NoError(t, err) + assert.Equal(t, tt.expected, *result) + }) + } +} + // 测试周任务 func TestCalculateNextWeekTime(t *testing.T) { baseTime := time.Date(2023, 6, 15, 12, 30, 45, 0, time.UTC) // 星期四 @@ -670,7 +708,7 @@ func TestGetNextTime_ErrorCases(t *testing.T) { { name: "无效月任务日期", job: JobData{ - JobType: "每月", + JobType: JobTypeEveryMonth, Day: 32, // 无效日期 Hour: 12, Minute: 30, @@ -708,6 +746,17 @@ func TestGetNextTime_EdgeCases(t *testing.T) { currentTime: time.Date(2023, 6, 15, 12, 30, 45, 0, time.UTC), expected: time.Date(2023, 6, 16, 12, 30, 45, 0, time.UTC), }, + { + name: "刚好在执行时间点上-应该到下一个周期-秒", + job: JobData{ + JobType: JobTypeEveryDay, + Hour: 12, + Minute: 30, + Second: 45, + }, + currentTime: time.Date(2023, 6, 16, 12, 30, 44, 0, time.UTC), + expected: time.Date(2023, 6, 16, 12, 30, 45, 0, time.UTC), + }, { name: "闰年2月29日", job: JobData{ @@ -717,7 +766,7 @@ func TestGetNextTime_EdgeCases(t *testing.T) { Minute: 30, Second: 45, }, - currentTime: time.Date(2024, 1, 15, 12, 30, 45, 0, time.UTC), // 闰年 + currentTime: time.Date(2024, 2, 15, 12, 30, 45, 0, time.UTC), // 闰年 expected: time.Date(2024, 2, 29, 12, 30, 45, 0, time.UTC), }, { @@ -729,7 +778,7 @@ func TestGetNextTime_EdgeCases(t *testing.T) { Minute: 30, Second: 45, }, - currentTime: time.Date(2023, 1, 15, 12, 30, 45, 0, time.UTC), // 非闰年 + currentTime: time.Date(2023, 2, 15, 12, 30, 45, 0, time.UTC), // 非闰年 expected: time.Date(2023, 2, 28, 12, 30, 45, 0, time.UTC), }, }