From aa86d201919f1222fbd0cd210561f97256d8b1a5 Mon Sep 17 00:00:00 2001 From: free will <2647778488@qq.com> Date: Wed, 18 May 2022 17:33:14 +0800 Subject: [PATCH] =?UTF-8?q?=E6=AD=A3=E5=9C=A8=E9=87=8D=E6=96=B0=E5=B0=81?= =?UTF-8?q?=E8=A3=85goCron...?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/goCron_test.go | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/tests/goCron_test.go b/tests/goCron_test.go index ab44355..df80a81 100644 --- a/tests/goCron_test.go +++ b/tests/goCron_test.go @@ -16,7 +16,7 @@ import ( ) func printHHH() { - println("hello,gocron.",time.Now().String()) + println("hello,gocron.", time.Now().String()) } // @@ -31,7 +31,7 @@ func Test_Loop(t *testing.T) { s := gocron.NewScheduler(time.UTC) // Job:只知道它提供的任务(task, go函数),因此只能执行与该任务相关的操作,例如防止自己超出需要很长时间的先前任务。 //j, _ := s.Every(10).Seconds().Do(func() { time.Sleep(2 * time.Second); printHHH()}) - j, _ := s.Every(1).Seconds().Do(func() { printHHH()}) + j, _ := s.Every(1).Seconds().Do(func() { printHHH() }) // Executor:顾名思义,executor 只负责调用Scheduler(调度程序)发送时,Job(作业)交给它的任务(go 函数)。 fmt.Println(j.IsRunning()) @@ -50,7 +50,7 @@ func Test_Loop(t *testing.T) { func Test_IsRunning(t *testing.T) { s := gocron.NewScheduler(time.UTC) // Job:只知道它提供的任务(task, go函数),因此只能执行与该任务相关的操作,例如防止自己超出需要很长时间的先前任务。 - j, _ := s.Every(10).Seconds().Do(func() { time.Sleep(2 * time.Second); printHHH()}) + j, _ := s.Every(10).Seconds().Do(func() { time.Sleep(2 * time.Second); printHHH() }) // Executor:顾名思义,executor 只负责调用Scheduler(调度程序)发送时,Job(作业)交给它的任务(go 函数)。 fmt.Println(j.IsRunning()) @@ -71,6 +71,29 @@ func Test_IsRunning(t *testing.T) { var task = func() {} +func Test_Run1Time(t *testing.T) { + // 触发时间设置到五秒后 + timeRun := time.Now() + //dur := 5 * time.Second + //timeRun.Add(dur) + + // 设置触发事件 + s := gocron.NewScheduler(time.UTC) + _, _ = s.At(timeRun).Do(printHHH) + //go func() { + // for { + // fmt.Println("Run count", job.RunCount()) + // time.Sleep(time.Second) + // } + //}() + s.StartAsync() + + // 保活 + for true { + time.Sleep(10 * time.Second) + } +} + // // Test_RunCount // @Description: 测试job.RunCount() @@ -141,7 +164,7 @@ func Test_LimitRunsTo(t *testing.T) { func Test_ScheduleAtTimes(t *testing.T) { // 设置一个触发时间 s := gocron.NewScheduler(time.UTC) - job, _ := s.Every(1).Day().At("20:02").Do(func() {printHHH()}) + job, _ := s.Every(1).Day().At("20:02").Do(func() { printHHH() }) s.StartAsync() fmt.Println(job.ScheduledAtTime()) @@ -179,4 +202,4 @@ func Test_SingletonMode(t *testing.T) { time.Sleep(3 * time.Second) fmt.Println("Last run:", job.LastRun()) } -} \ No newline at end of file +}