You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
startimer/time_test.go

284 lines
6.2 KiB
Go

2 years ago
package startimer
import (
"fmt"
"testing"
"time"
)
func TestTimer(t *testing.T) {
2 years ago
tk := StarTimer{
base: time.Now(),
repeat: []*Repeats{
2 years ago
{
2 years ago
Every: false,
2 years ago
Repeat: []Repeat{
{
Unit: STAR_MINUTE,
Value: 7,
},
2 years ago
{
Unit: STAR_HOUR,
Value: 19,
},
2 years ago
{
Unit: STAR_HOUR,
Value: 18,
},
},
},
{
2 years ago
Every: false,
2 years ago
Repeat: []Repeat{
{
Unit: STAR_MINUTE,
Value: 15,
},
{
Unit: STAR_HOUR,
Value: 14,
},
},
},
{
2 years ago
Every: true,
2 years ago
Repeat: []Repeat{
{
Unit: STAR_HOUR,
Value: 5,
},
},
},
},
}
2 years ago
base := tk.base
for i := 0; i < 50; i++ {
base = tk.parseNextDate(base, true)
fmt.Println(base)
}
}
func TestPrepareCronMax(t *testing.T) {
var rpt = new(Repeats)
for i := 0; i < 60; i++ {
if i%2 != 0 {
continue
}
rpt.Repeat = append(rpt.Repeat, Repeat{
Unit: STAR_SECOND,
Value: uint32(i),
}, Repeat{
Unit: STAR_MINUTE,
Value: uint32(i),
})
}
/*
for i := 0; i < 24; i++ {
rpt.Repeat = append(rpt.Repeat, Repeat{
Unit: STAR_HOUR,
Value: uint32(i),
})
}
*/
for i := 1; i < 7; i++ {
rpt.Repeat = append(rpt.Repeat, Repeat{
Unit: STAR_MONTH,
Value: uint32(i),
})
}
tk := StarTimer{
base: time.Now(),
repeat: []*Repeats{
{
Every: false,
Repeat: rpt.Repeat,
},
},
}
base := tk.base
for i := 0; i < 10; i++ {
base = tk.parseNextDate(base, true)
fmt.Println(base)
}
}
func TestPrepareCron(t *testing.T) {
tk := StarTimer{
base: time.Now(),
repeat: []*Repeats{
{
//*/3,5,6 */2,4,6 6 1-5 5
Every: false,
Repeat: []Repeat{
{Unit: STAR_MINUTE, Value: 0}, {Unit: STAR_MINUTE, Value: 3}, {Unit: STAR_MINUTE, Value: 6}, {Unit: STAR_MINUTE, Value: 9}, {Unit: STAR_MINUTE, Value: 12}, {Unit: STAR_MINUTE, Value: 15}, {Unit: STAR_MINUTE, Value: 18}, {Unit: STAR_MINUTE, Value: 21},
{Unit: STAR_MINUTE, Value: 24}, {Unit: STAR_MINUTE, Value: 27}, {Unit: STAR_MINUTE, Value: 30}, {Unit: STAR_MINUTE, Value: 33}, {Unit: STAR_MINUTE, Value: 36}, {Unit: STAR_MINUTE, Value: 39}, {Unit: STAR_MINUTE, Value: 42}, {Unit: STAR_MINUTE, Value: 45},
{Unit: STAR_MINUTE, Value: 48}, {Unit: STAR_MINUTE, Value: 51}, {Unit: STAR_MINUTE, Value: 54}, {Unit: STAR_MINUTE, Value: 57}, {Unit: STAR_MINUTE, Value: 5},
{Unit: STAR_HOUR, Value: 2}, {Unit: STAR_HOUR, Value: 4}, {Unit: STAR_HOUR, Value: 6}, {Unit: STAR_HOUR, Value: 8}, {Unit: STAR_HOUR, Value: 10}, {Unit: STAR_HOUR, Value: 12}, {Unit: STAR_HOUR, Value: 14}, {Unit: STAR_HOUR, Value: 16}, {Unit: STAR_HOUR, Value: 18}, {Unit: STAR_HOUR, Value: 20}, {Unit: STAR_HOUR, Value: 22}, {Unit: STAR_HOUR, Value: 0},
{Unit: STAR_DAY, Value: 6},
{Unit: STAR_MONTH, Value: 1}, {Unit: STAR_MONTH, Value: 2}, {Unit: STAR_MONTH, Value: 3}, {Unit: STAR_MONTH, Value: 4}, {Unit: STAR_MONTH, Value: 5},
{Unit: STAR_WEEK, Value: 5},
},
},
},
}
base := tk.base
for i := 0; i < 10; i++ {
base = tk.parseNextDate(base, true)
fmt.Println(base)
}
}
func TestPrepareCronSimple(t *testing.T) {
tk := StarTimer{
base: time.Now(),
repeat: []*Repeats{
{
Every: false,
Repeat: []Repeat{
{Unit: STAR_MINUTE, Value: 0}, {Unit: STAR_MINUTE, Value: 3}, {Unit: STAR_MINUTE, Value: 6},
{Unit: STAR_HOUR, Value: 2}, {Unit: STAR_HOUR, Value: 4}, {Unit: STAR_HOUR, Value: 6},
{Unit: STAR_DAY, Value: 6},
{Unit: STAR_MONTH, Value: 1}, {Unit: STAR_MONTH, Value: 5},
},
},
},
}
base := tk.base
for i := 0; i < 1; i++ {
base = tk.parseNextDate(base, true)
2 years ago
fmt.Println(base)
2 years ago
fmt.Println("")
2 years ago
}
}
2 years ago
func TestInvalidCron(t *testing.T) {
tk := StarTimer{
base: time.Now(),
repeat: []*Repeats{
{
Every: false,
Repeat: []Repeat{
{Unit: STAR_MINUTE, Value: 0},
{Unit: STAR_HOUR, Value: 2},
{Unit: STAR_DAY, Value: 29}, {Unit: STAR_DAY, Value: 30}, {Unit: STAR_DAY, Value: 31},
{Unit: STAR_MONTH, Value: 4},
},
},
},
}
base := tk.base
for i := 0; i < 10; i++ {
base = tk.parseNextDate(base, true)
fmt.Println(base)
fmt.Println("")
}
}
func TestParseCronMax(t *testing.T) {
tk, err := NewTimerWithSecCron("*/5 40-59 * * * *")
if err != nil {
t.Fatal(err)
}
tk.AddTask(func() {
fmt.Println("现在是", time.Now())
})
for i := 0; i < 20; i++ {
fmt.Println()
}
/*
err = tk.Run()
if err != nil {
t.Fatal(err)
}
time.Sleep(time.Second * 300)
*/
}
2 years ago
func TestPrepareCron2(t *testing.T) {
tk := StarTimer{
base: time.Now(),
repeat: []*Repeats{
{
Every: false,
Repeat: []Repeat{
//{Unit: STAR_MINUTE, Value: 0},
{Unit: STAR_HOUR, Value: 11},
{Unit: STAR_WEEK, Value: 5}, {Unit: STAR_WEEK, Value: 6}, {Unit: STAR_WEEK, Value: 0},
},
},
},
}
base := tk.base
for i := 0; i < 10; i++ {
base = tk.parseNextDate(base, true)
fmt.Println(base)
}
}
2 years ago
func TestRun(t *testing.T) {
now := time.Now().Add(time.Second * 2)
2 years ago
tmr, _ := NewTimer(time.Now(), WithStaticDate(now), WithRunCountLimit(1))
2 years ago
c := make(chan int)
tmr.AddTask(func() {
fmt.Println("hello world")
c <- 1
})
if err := tmr.Run(); err != nil {
t.Fatal(err)
}
fmt.Println(tmr.IsRunning())
select {
case <-c:
fmt.Println(tmr.IsRunning())
return
case <-time.After(time.Second * 10):
fmt.Println(tmr.IsRunning())
t.FailNow()
}
}
2 years ago
func TestInvalid(t *testing.T) {
exp := `[{"repeat":[{"unit":3,"value":19},{"unit":2,"value":8},{"unit":1,"value":15},{"unit":0,"value":0}],"every":false}]`
2 years ago
tmr, _ := NewTimer(time.Now())
2 years ago
err := tmr.ImportRepeats(exp)
if err != nil {
t.Fatal(err)
}
fmt.Println(tmr.NextTimer())
}
func TestDate(t *testing.T) {
n := time.Now().Add(time.Minute)
var rp = Repeats{
Repeat: []Repeat{
{
Unit: STAR_HOUR,
Value: uint32(n.Hour()),
},
{
Unit: STAR_MINUTE,
Value: uint32(n.Minute()),
},
{
Unit: STAR_SECOND,
Value: 0,
},
},
Every: false,
}
2 years ago
tmr, _ := NewTimer(time.Now(), WithRepeats(&rp))
2 years ago
tmr.AddTask(func() {
time.Sleep(time.Second * 2)
fmt.Println(tmr.NextTimer())
})
err := tmr.Run()
if err != nil {
t.Fatal(err)
}
fmt.Println(tmr.NextTimer())
fmt.Println(tmr.NextTimerAfterDate(time.Now().Add(time.Hour)))
time.Sleep(time.Hour)
}