Files
mlsr/tests/goCron_test.go
T

182 lines
4.5 KiB
Go

// Package tests
// @Author: Wang Feng
// @Description:
// @Version: 0.1.0
// @Date: 2022/4/15 15:08
// @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
//
package tests
import (
"fmt"
"github.com/go-co-op/gocron"
"testing"
"time"
)
func printHHH() {
println("hello,gocron.",time.Now().String())
}
//
// Test_Loop
// @Description: 测试循环执行某个函数
// @param t
//
func Test_Loop(t *testing.T) {
// Scheduler: 调度器跟踪分配给它的所有作业,
// 并确保它们在准备好运行时传递给执行器。
// 调度程序能够管理作业行为(Job)的整体方面,例如限制一次运行的作业数量。
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()})
// Executor:顾名思义,executor 只负责调用Scheduler(调度程序)发送时,Job(作业)交给它的任务(go 函数)。
fmt.Println(j.IsRunning())
s.StartAsync()
for true {
time.Sleep(10 * time.Second)
}
}
//
// Test_IsRunning
// @Description: 测试Job运行状态 job.IsRunning()
// @param 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()})
// Executor:顾名思义,executor 只负责调用Scheduler(调度程序)发送时,Job(作业)交给它的任务(go 函数)。
fmt.Println(j.IsRunning())
s.StartAsync()
time.Sleep(time.Second)
fmt.Println(j.IsRunning())
time.Sleep(time.Second)
s.Stop()
time.Sleep(1 * time.Second)
fmt.Println(j.IsRunning())
time.Sleep(1 * time.Second)
fmt.Println(j.IsRunning())
}
var task = func() {}
//
// Test_RunCount
// @Description: 测试job.RunCount()
// @param t
//
func Test_RunCount(t *testing.T) {
s := gocron.NewScheduler(time.UTC)
job, _ := s.Every(1).Second().Do(task)
go func() {
for {
fmt.Println("Run count", job.RunCount())
time.Sleep(time.Second)
}
}()
s.StartAsync()
// 保活
for true {
time.Sleep(10 * time.Second)
}
}
//
// Test_LastRun
// @Description: 测试获取job上次运行时间 job.LastRun() job.NextRun())
// 返回0001-01-01 00:00:00 +0000 UTC时,说明事件尚未被触发
// @param t
//
func Test_LastRun_NextRun(t *testing.T) {
s := gocron.NewScheduler(time.UTC)
job, _ := s.Every(1).Second().Do(task)
s.StartAsync()
fmt.Println("Last run:", job.LastRun())
fmt.Println("Next run", job.NextRun())
// 保活
for true {
time.Sleep(3 * time.Second)
fmt.Println("Last run: ", job.LastRun())
fmt.Println("Next run: ", job.NextRun())
}
}
//
// Test_LimitRunsTo
// @Description: 测试对预约的循环触发事件进行次数限制
// @param t
//
func Test_LimitRunsTo(t *testing.T) {
s := gocron.NewScheduler(time.UTC)
job, _ := s.Every(1).Second().Do(printHHH)
job.LimitRunsTo(2)
s.StartAsync()
// 保活
for true {
time.Sleep(3 * time.Second)
fmt.Println("Last run:", job.LastRun())
}
}
//
// Test_ScheduleAtTimes
// @Description: 设置事件在指定时间节点被触发
// @param t
//
func Test_ScheduleAtTimes(t *testing.T) {
// 设置一个触发时间
s := gocron.NewScheduler(time.UTC)
job, _ := s.Every(1).Day().At("20:02").Do(func() {printHHH()})
s.StartAsync()
fmt.Println(job.ScheduledAtTime())
// 设置多个触发时间。当取时间时,返回最早的那个
job1, _ := s.Every(1).Day().At("10:30;08:00").Do(task)
fmt.Println(job1.ScheduledAtTime())
// Output:
// 10:30
// 8:0
// 当取所有时间时,返回一个时间列表
fmt.Println(job1.ScheduledAtTimes())
// Output:
// [8:0 10:30]
// 保活
for true {
time.Sleep(3 * time.Second)
fmt.Println("Last run:", job.LastRun())
}
}
//
// Test_SingletonMode
// @Description: 单例模式
// @param t
//
func Test_SingletonMode(t *testing.T) {
s := gocron.NewScheduler(time.UTC)
job, _ := s.Every(1).Second().Do(task)
// a long running job will not be rescheduled until the current run is completed
job.SingletonMode()
// 保活
for true {
time.Sleep(3 * time.Second)
fmt.Println("Last run:", job.LastRun())
}
}