测试复杂的时间计划成功

This commit is contained in:
eson 2018-12-21 22:51:52 +08:00
parent 0dce1192d2
commit 2904c8f38c
2 changed files with 85 additions and 63 deletions

View File

@ -23,9 +23,6 @@ type hInterval struct {
PlanFail []randLR PlanFail []randLR
PlanTrue []randLR PlanTrue []randLR
TrueCount int
FailCount int
Count int Count int
ConstCount int ConstCount int
} }
@ -61,7 +58,10 @@ type Crontab struct {
interval *clinked.CircularLinked interval *clinked.CircularLinked
lastStatus bool lastStatus bool
isCalculated bool isCalculated bool
nextTime time.Time
trueCount int
failCount int
nextTime time.Time
} }
// NewCrontab create 一个crontab // NewCrontab create 一个crontab
@ -222,34 +222,19 @@ func (cron *Crontab) linuxTimeUp() bool {
// IntervalCalculateNextTime 计算时间间隔的下次时间 // IntervalCalculateNextTime 计算时间间隔的下次时间
func (cron *Crontab) intervalCalculateNextTime(now time.Time) { func (cron *Crontab) intervalCalculateNextTime(now time.Time) {
iv := cron.interval.Cursor().GetValue().(hInterval) iv := cron.interval.Cursor().GetValue().(*hInterval)
isecond := 0 isecond := 0
if cron.lastStatus { if iv.PlanFailCount.Size() == 0 && len(iv.PlanFail) == 0 {
iv.TrueCount++ cron.lastStatus = true
iv.FailCount = 0
} else {
iv.FailCount++
iv.TrueCount = 0
} }
if cron.lastStatus == false { if cron.lastStatus {
isecond = intervalPriorityListISecond(&iv.PlanFailCount, iv.FailCount) cron.trueCount++
if isecond == -1 { cron.failCount = 0
if len(iv.PlanFail) > 0 {
idx := randomdata.Number(len(iv.PlanFail))
lr := iv.PlanFail[idx]
isecond = randomdata.Number(lr.left, lr.right+1)
} else {
isecond = 0
}
}
log.Println("fail:", iv.FailCount, "count time wait:", isecond, "s") isecond = intervalPriorityListISecond(&iv.PlanTrueCount, cron.trueCount)
} else {
isecond = intervalPriorityListISecond(&iv.PlanTrueCount, iv.TrueCount)
if isecond == -1 { if isecond == -1 {
if len(iv.PlanTrue) > 0 { if len(iv.PlanTrue) > 0 {
idx := randomdata.Number(len(iv.PlanTrue)) idx := randomdata.Number(len(iv.PlanTrue))
@ -261,8 +246,26 @@ func (cron *Crontab) intervalCalculateNextTime(now time.Time) {
} }
} else { } else {
log.Println("success:", iv.TrueCount, "count time wait:", isecond, "s") log.Println("success:", cron.trueCount, "count time wait:", isecond, "s")
} }
} else {
cron.failCount++
cron.trueCount = 0
isecond = intervalPriorityListISecond(&iv.PlanFailCount, cron.failCount)
if isecond == -1 {
if len(iv.PlanFail) > 0 {
idx := randomdata.Number(len(iv.PlanFail))
lr := iv.PlanFail[idx]
isecond = randomdata.Number(lr.left, lr.right+1)
} else {
isecond = 0
}
}
log.Println("fail:", cron.failCount, "count time wait:", isecond, "s")
} }
iv.Count-- iv.Count--
@ -381,7 +384,8 @@ func parseIntervalString(crontab string) []interface{} {
values := strings.Split(crontab, ",") values := strings.Split(crontab, ",")
for _, value := range values { for _, value := range values {
interval := hInterval{} interval := &hInterval{}
// 次数 // 次数
valuesCounts := strings.Split(value, "x") valuesCounts := strings.Split(value, "x")
switch len(valuesCounts) { switch len(valuesCounts) {

View File

@ -59,6 +59,13 @@ func TestParseInterval(t *testing.T) {
} }
} }
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)
}
func TestParseIntervalPlus(t *testing.T) { func TestParseIntervalPlus(t *testing.T) {
// crontab := "0-5/2,7-30/3,30,35,40-^1 * * * *" //(秒) 分 时 号(每月的多少号, 要注意月可可能性) 星期几(每个星期的) /每 ,列表 -范围 // crontab := "0-5/2,7-30/3,30,35,40-^1 * * * *" //(秒) 分 时 号(每月的多少号, 要注意月可可能性) 星期几(每个星期的) /每 ,列表 -范围
// crondata := NewCrontab("*22 * * * *") // crondata := NewCrontab("*22 * * * *")
@ -66,51 +73,62 @@ func TestParseIntervalPlus(t *testing.T) {
//crontab := "0-5/2,7-30/3,30,35,40-^1 * * * *" //(秒) 分 时 号(每月的多少号, 要注意月可可能性) 星期几(每个星期的) /每 ,列表 -范围 //crontab := "0-5/2,7-30/3,30,35,40-^1 * * * *" //(秒) 分 时 号(每月的多少号, 要注意月可可能性) 星期几(每个星期的) /每 ,列表 -范围
//crontab := "f1-2|1-3|5-8x5,f1|10m,10-15,f1" ^代表往后算 ^5 2月份就代表 28-4=24 f代表失败后1-2秒设置 由SetStatus控制 t 相反默认不写前缀为t //crontab := "f1-2|1-3|5-8x5,f1|10m,10-15,f1" ^代表往后算 ^5 2月份就代表 28-4=24 f代表失败后1-2秒设置 由SetStatus控制 t 相反默认不写前缀为t
crontab := "f2|f2?3|f3?4|1" log.SetFlags(log.Llongfile)
crontab := "f2|f2?3|f3?4|1|t4?5"
cron := NewCrontab(crontab) cron := NewCrontab(crontab)
i := 0
now := time.Now() now := time.Now()
t.Error("") lasttime := now
for i := 0; i <= 5*15; i++ {
cron.NextTime() for j := 0; j <= 2500; j++ {
nexttime := cron.NextTime()
if j >= 5 && !nexttime.Equal(lasttime) {
lasttime = nexttime
}
if cron.TimeUp() { if cron.TimeUp() {
sec := time.Since(now).Seconds() sec := time.Since(now).Seconds()
cron.SetStatus(false)
t.Error(i, sec) switch i {
if i == 0 { case 0:
if 1.0 <= sec && sec <= 1.1 {
t.Log(sec) case 1:
} else { if err := isInRangeTime(sec, 2.0); err != nil {
t.Error(i, "interval time is ", sec) t.Error(err.Error(), "status = ", cron.GetStatus())
}
case 2:
if err := isInRangeTime(sec, 3.0); err != nil {
t.Error(err.Error(), "status = ", cron.GetStatus())
}
case 3:
if err := isInRangeTime(sec, 4.0); err != nil {
t.Error(err.Error(), "status = ", cron.GetStatus())
}
case 5:
if err := isInRangeTime(sec, 1); err != nil {
t.Error(err.Error(), "status = ", cron.GetStatus())
}
default:
if i >= 10 {
if err := isInRangeTime(sec, 5.0); err != nil {
t.Error(err.Error(), "status = ", cron.GetStatus())
}
} }
} }
if i <= 3 {
if i == 1 { cron.SetStatus(false)
if 2.0 <= sec && sec <= 2.1 { } else {
t.Log(sec) cron.SetStatus(true)
} else {
t.Error(i, "interval time is ", sec)
}
}
if i == 2 {
if 3.0 <= sec && sec <= 3.1 {
t.Log(sec)
} else {
t.Error(i, "interval time is ", sec)
}
}
if i == 3 {
if 4.0 <= sec && sec <= 4.1 {
t.Log(sec)
} else {
t.Error(i, "interval time is ", sec)
}
} }
now = time.Now() now = time.Now()
i++
} }
time.Sleep(time.Millisecond * 200)
time.Sleep(time.Millisecond * 10)
} }
} }