2024-05-18 21:54:07 +08:00
|
|
|
package timerx_test
|
2023-08-06 01:07:29 +08:00
|
|
|
|
|
|
|
|
import (
|
2024-05-18 21:54:07 +08:00
|
|
|
"context"
|
2023-08-06 01:07:29 +08:00
|
|
|
"fmt"
|
2023-08-27 23:39:58 +08:00
|
|
|
"testing"
|
2024-05-18 21:54:07 +08:00
|
|
|
"time"
|
2023-08-27 23:39:58 +08:00
|
|
|
|
2025-10-04 22:00:08 +08:00
|
|
|
"github.com/redis/go-redis/v9"
|
2024-05-18 21:54:07 +08:00
|
|
|
"github.com/yuninks/timerx"
|
2023-08-06 01:07:29 +08:00
|
|
|
)
|
|
|
|
|
|
2025-10-04 21:33:57 +08:00
|
|
|
func redisInit() *redis.Client {
|
|
|
|
|
return redis.NewClient(&redis.Options{
|
2025-09-24 14:50:30 +08:00
|
|
|
Addr: "localhost:6379",
|
2025-09-18 09:56:24 +08:00
|
|
|
Password: "123456",
|
2025-09-24 14:50:30 +08:00
|
|
|
DB: 0,
|
2024-05-18 21:54:07 +08:00
|
|
|
})
|
2025-10-04 21:33:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestCluster_AddEveryMonth(t *testing.T) {
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
redis := redisInit()
|
2024-05-18 21:54:07 +08:00
|
|
|
defer redis.Close()
|
|
|
|
|
|
2025-10-04 21:33:57 +08:00
|
|
|
cluster, err := timerx.InitCluster(ctx, redis, "test")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Errorf("InitCluster failed, err: %v", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
defer cluster.Stop()
|
2024-05-18 21:54:07 +08:00
|
|
|
|
|
|
|
|
taskId := "testTask"
|
|
|
|
|
hour := 2
|
|
|
|
|
minute := 3
|
|
|
|
|
second := 4
|
|
|
|
|
callback := func(ctx context.Context, data interface{}) error {
|
|
|
|
|
// do something
|
|
|
|
|
fmt.Println("Task executed:", data)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
extendData := "testData"
|
|
|
|
|
|
2025-10-04 21:33:57 +08:00
|
|
|
err = cluster.EveryMonth(ctx, taskId, 1, hour, minute, second, callback, extendData)
|
2024-05-18 21:54:07 +08:00
|
|
|
if err != nil {
|
|
|
|
|
t.Errorf("AddEveryMonth failed, err: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-18 09:56:24 +08:00
|
|
|
time.Sleep(time.Second * 10)
|
|
|
|
|
|
2024-05-18 21:54:07 +08:00
|
|
|
// TODO: verify the job is added to the cluster and can be executed at the specified time
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestCluster_AddEveryWeek(t *testing.T) {
|
|
|
|
|
ctx := context.Background()
|
2025-10-04 21:33:57 +08:00
|
|
|
redis := redisInit()
|
2024-05-18 21:54:07 +08:00
|
|
|
defer redis.Close()
|
|
|
|
|
|
2025-10-04 21:33:57 +08:00
|
|
|
cluster, _ := timerx.InitCluster(ctx, redis, "test")
|
2024-05-18 21:54:07 +08:00
|
|
|
|
|
|
|
|
taskId := "testTask"
|
|
|
|
|
week := time.Sunday
|
|
|
|
|
hour := 2
|
|
|
|
|
minute := 3
|
|
|
|
|
second := 4
|
|
|
|
|
callback := func(ctx context.Context, data interface{}) error {
|
|
|
|
|
// do something
|
|
|
|
|
fmt.Println("Task executed:", data)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
extendData := "testData"
|
|
|
|
|
|
2024-05-31 13:05:51 +08:00
|
|
|
err := cluster.EveryWeek(ctx, taskId, week, hour, minute, second, callback, extendData)
|
2024-05-18 21:54:07 +08:00
|
|
|
if err != nil {
|
|
|
|
|
t.Errorf("AddEveryWeek failed, err: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: verify the job is added to the cluster and can be executed at the specified time
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestCluster_AddEveryDay(t *testing.T) {
|
|
|
|
|
ctx := context.Background()
|
2025-10-04 21:33:57 +08:00
|
|
|
redis := redisInit()
|
2024-05-18 21:54:07 +08:00
|
|
|
defer redis.Close()
|
|
|
|
|
|
2025-10-04 21:33:57 +08:00
|
|
|
cluster, _ := timerx.InitCluster(ctx, redis, "test")
|
2024-05-18 21:54:07 +08:00
|
|
|
|
|
|
|
|
taskId := "testTask"
|
|
|
|
|
hour := 2
|
|
|
|
|
minute := 3
|
|
|
|
|
second := 4
|
|
|
|
|
callback := func(ctx context.Context, data interface{}) error {
|
|
|
|
|
// do something
|
|
|
|
|
fmt.Println("Task executed:", data)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
extendData := "testData"
|
|
|
|
|
|
2024-05-31 13:05:51 +08:00
|
|
|
err := cluster.EveryDay(ctx, taskId, hour, minute, second, callback, extendData)
|
2024-05-18 21:54:07 +08:00
|
|
|
if err != nil {
|
|
|
|
|
t.Errorf("AddEveryDay failed, err: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: verify the job is added to the cluster and can be executed at the specified time
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestCluster_AddEveryHour(t *testing.T) {
|
|
|
|
|
ctx := context.Background()
|
2025-10-04 21:33:57 +08:00
|
|
|
redis := redisInit()
|
2024-05-18 21:54:07 +08:00
|
|
|
defer redis.Close()
|
|
|
|
|
|
2025-10-04 21:33:57 +08:00
|
|
|
cluster, _ := timerx.InitCluster(ctx, redis, "test")
|
2024-05-18 21:54:07 +08:00
|
|
|
|
|
|
|
|
taskId := "testTask"
|
|
|
|
|
minute := 3
|
|
|
|
|
second := 4
|
2025-09-24 14:50:30 +08:00
|
|
|
callback := func(ctx context.Context, data interface{}) error {
|
2024-05-18 21:54:07 +08:00
|
|
|
// do something
|
|
|
|
|
fmt.Println("Task executed:", data)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
extendData := "testData"
|
|
|
|
|
|
2024-05-31 13:05:51 +08:00
|
|
|
err := cluster.EveryHour(ctx, taskId, minute, second, callback, extendData)
|
2024-05-18 21:54:07 +08:00
|
|
|
if err != nil {
|
|
|
|
|
t.Errorf("AddEveryHour failed, err: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: verify the job is added to the cluster and can be executed at the specified time
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestCluster_AddEveryMinute(t *testing.T) {
|
|
|
|
|
ctx := context.Background()
|
2025-10-04 21:33:57 +08:00
|
|
|
redis := redisInit()
|
2024-05-18 21:54:07 +08:00
|
|
|
defer redis.Close()
|
|
|
|
|
|
2025-10-04 21:33:57 +08:00
|
|
|
cluster, _ := timerx.InitCluster(ctx, redis, "test")
|
2023-08-06 01:07:29 +08:00
|
|
|
|
2024-05-18 21:54:07 +08:00
|
|
|
taskId := "testTask"
|
|
|
|
|
second := 4
|
2025-09-24 14:50:30 +08:00
|
|
|
callback := func(ctx context.Context, data interface{}) error {
|
2024-05-18 21:54:07 +08:00
|
|
|
// do something
|
|
|
|
|
fmt.Println("Task executed:", data)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
extendData := "testData"
|
|
|
|
|
|
2024-05-31 13:05:51 +08:00
|
|
|
err := cluster.EveryMinute(ctx, taskId, second, callback, extendData)
|
2024-05-18 21:54:07 +08:00
|
|
|
if err != nil {
|
|
|
|
|
t.Errorf("AddEveryMinute failed, err: %v", err)
|
|
|
|
|
}
|
2023-08-27 23:39:58 +08:00
|
|
|
|
2024-05-18 21:54:07 +08:00
|
|
|
// TODO: verify the job is added to the cluster and can be executed at the specified time
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestCluster_Add(t *testing.T) {
|
|
|
|
|
fmt.Println("66666")
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
fmt.Println("66666")
|
2025-10-04 21:33:57 +08:00
|
|
|
redis := redisInit()
|
2024-05-18 21:54:07 +08:00
|
|
|
defer redis.Close()
|
|
|
|
|
|
|
|
|
|
t.Log("6666")
|
|
|
|
|
|
2025-10-04 21:33:57 +08:00
|
|
|
cluster, _ := timerx.InitCluster(ctx, redis, "test")
|
2024-05-18 21:54:07 +08:00
|
|
|
|
|
|
|
|
taskId := "testTask"
|
|
|
|
|
dur := time.Second
|
|
|
|
|
callback := func(ctx context.Context, data interface{}) error {
|
|
|
|
|
// do something
|
|
|
|
|
fmt.Println("Task executed:", data)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
extendData := "testData"
|
|
|
|
|
|
2024-05-31 13:05:51 +08:00
|
|
|
err := cluster.EverySpace(ctx, taskId, dur, callback, extendData)
|
2024-05-18 21:54:07 +08:00
|
|
|
if err != nil {
|
2025-09-18 09:56:24 +08:00
|
|
|
t.Errorf("Add failed,1 err: %v", err)
|
2024-05-18 21:54:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
time.Sleep(time.Second * 20)
|
|
|
|
|
|
|
|
|
|
// TODO: verify the job is added to the cluster and can be executed after the specified duration
|
|
|
|
|
}
|