crontabex/crontab_test.go

158 lines
3.7 KiB
Go
Raw Normal View History

2018-12-06 03:07:16 +00:00
package crontab
import (
"fmt"
2018-12-20 20:18:40 +00:00
"log"
2018-12-06 03:07:16 +00:00
"runtime"
"testing"
"time"
2018-12-25 02:17:15 +00:00
"github.com/satori/go.uuid"
2018-12-06 03:07:16 +00:00
)
// type LRValue struct {
// left, right int
// }
func TestParseCrontab(t *testing.T) {
// crontab := "0-5/2,7-30/3,30,35,40-^1 * * * *" //(秒) 分 时 号(每月的多少号, 要注意月可可能性) 星期几(每个星期的) /每 ,列表 -范围
crontab := "* * * * *"
PrintMemUsage()
ty := newTrieYear()
cron := NewCrontab(crontab)
ty.FromCrontab(cron)
if len(ty.GetPlanTime(cron, time.Now(), 10)) != 10 {
t.Error("GetPlanTime error len != 10")
}
cron.createYearPlan()
2018-12-06 20:42:53 +00:00
if cron.TimeUp() {
t.Error("timeup error", cron.WillPlans[0:2])
2018-12-06 03:07:16 +00:00
}
PrintMemUsage()
}
func TestParseInterval(t *testing.T) {
//crontab := "0-5/2,7-30/3,30,35,40-^1 * * * *" //(秒) 分 时 号(每月的多少号, 要注意月可可能性) 星期几(每个星期的) /每 ,列表 -范围
2018-12-20 10:42:18 +00:00
//crontab := "f1-2|1-3|5-8x5,f1|10m,10-15,f22?15-12"
2018-12-20 20:18:40 +00:00
crontab := "2s"
2018-12-06 03:07:16 +00:00
cron := NewCrontab(crontab)
now := time.Now()
2018-12-20 20:18:40 +00:00
for i := 0; i <= 3; i++ {
log.Println(cron.NextTime())
2018-12-06 03:07:16 +00:00
if cron.TimeUp() {
sec := time.Since(now).Seconds()
if i != 0 {
if 2.0 <= sec && sec <= 2.1 {
t.Log(sec)
} else {
t.Error("interval time is ", sec)
}
}
now = time.Now()
}
time.Sleep(time.Second)
}
}
2018-12-21 14:51:52 +00:00
func isInRangeTime(sec, t float64) error {
if sec >= t-0.05 && sec <= t+0.05 {
return nil
}
return fmt.Errorf("计算错误 范围 %f-%f interval time is %f", t-0.05, t+0.05, sec)
}
2018-12-21 15:30:21 +00:00
func TestParseIntervalOnline1(t *testing.T) {
2018-12-25 02:17:15 +00:00
uid, _ := uuid.NewV4()
log.Println(string(uid.Bytes()))
2018-12-21 15:30:21 +00:00
2018-12-25 02:17:15 +00:00
crontab := "f[c > 2]=3|s[c <= 2]"
log.Println(crontab)
2018-12-21 15:30:21 +00:00
}
2018-12-06 03:07:16 +00:00
func TestParseIntervalPlus(t *testing.T) {
// crontab := "0-5/2,7-30/3,30,35,40-^1 * * * *" //(秒) 分 时 号(每月的多少号, 要注意月可可能性) 星期几(每个星期的) /每 ,列表 -范围
// crondata := NewCrontab("*22 * * * *")
//crontab := "0-5/2,7-30/3,30,35,40-^1 * * * *" //(秒) 分 时 号(每月的多少号, 要注意月可可能性) 星期几(每个星期的) /每 ,列表 -范围
2018-12-20 10:42:18 +00:00
//crontab := "f1-2|1-3|5-8x5,f1|10m,10-15,f1" ^代表往后算 ^5 2月份就代表 28-4=24 f代表失败后1-2秒设置 由SetStatus控制 t 相反默认不写前缀为t
2018-12-06 03:07:16 +00:00
2018-12-21 14:51:52 +00:00
log.SetFlags(log.Llongfile)
2018-12-21 20:04:12 +00:00
crontab := "f=2|f2>=3|f3>=4|1|s4>=5"
2018-12-20 20:18:40 +00:00
cron := NewCrontab(crontab)
2018-12-21 14:51:52 +00:00
i := 0
2018-12-20 20:18:40 +00:00
now := time.Now()
2018-12-21 14:51:52 +00:00
lasttime := now
for j := 0; j <= 2500; j++ {
nexttime := cron.NextTime()
if j >= 5 && !nexttime.Equal(lasttime) {
lasttime = nexttime
}
2018-12-20 20:18:40 +00:00
if cron.TimeUp() {
sec := time.Since(now).Seconds()
2018-12-21 14:51:52 +00:00
switch i {
case 0:
2018-12-20 20:18:40 +00:00
2018-12-21 14:51:52 +00:00
case 1:
if err := isInRangeTime(sec, 2.0); err != nil {
t.Error(err.Error(), "status = ", cron.lastStatus)
2018-12-20 20:18:40 +00:00
}
2018-12-21 14:51:52 +00:00
case 2:
if err := isInRangeTime(sec, 3.0); err != nil {
t.Error(err.Error(), "status = ", cron.lastStatus)
2018-12-21 14:51:52 +00:00
}
case 3:
if err := isInRangeTime(sec, 4.0); err != nil {
t.Error(err.Error(), "status = ", cron.lastStatus)
2018-12-21 14:51:52 +00:00
}
case 5:
if err := isInRangeTime(sec, 1); err != nil {
t.Error(err.Error(), "status = ", cron.lastStatus)
2018-12-20 20:18:40 +00:00
}
2018-12-21 14:51:52 +00:00
default:
if i >= 10 {
if err := isInRangeTime(sec, 5.0); err != nil {
t.Error(err.Error(), "status = ", cron.lastStatus)
2018-12-21 14:51:52 +00:00
}
}
}
if i <= 3 {
cron.SetStatus(SExecuteOK, false)
2018-12-21 14:51:52 +00:00
} else {
cron.SetStatus(SExecuteOK, true)
2018-12-20 20:18:40 +00:00
}
now = time.Now()
2018-12-21 14:51:52 +00:00
i++
2018-12-20 20:18:40 +00:00
}
2018-12-21 14:51:52 +00:00
time.Sleep(time.Millisecond * 10)
2018-12-20 20:18:40 +00:00
}
2018-12-06 03:07:16 +00:00
}
func PrintMemUsage() {
var m runtime.MemStats
runtime.ReadMemStats(&m)
// For info on each, see: https://golang.org/pkg/runtime/#MemStats
fmt.Printf("Alloc = %v MiB", bToMb(m.Alloc))
fmt.Printf("\tTotalAlloc = %v MiB", bToMb(m.TotalAlloc))
fmt.Printf("\tSys = %v MiB", bToMb(m.Sys))
fmt.Printf("\tNumGC = %v\n", m.NumGC)
}
func bToMb(b uint64) uint64 {
return b / 1024 / 1024
}