2018-12-30 11:08:07 +08:00
|
|
|
// Copyright 2018 gf Author(https://gitee.com/johng/gf). All Rights Reserved.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
// You can obtain one at https://gitee.com/johng/gf.
|
|
|
|
|
|
|
|
|
|
|
|
package gcron_test
|
|
|
|
|
|
|
|
import (
|
2019-01-01 19:43:31 +08:00
|
|
|
"fmt"
|
2018-12-30 11:08:07 +08:00
|
|
|
"gitee.com/johng/gf/g/container/garray"
|
|
|
|
"gitee.com/johng/gf/g/os/gcron"
|
2019-01-06 11:09:50 +08:00
|
|
|
"gitee.com/johng/gf/g/os/glog"
|
2018-12-30 11:08:07 +08:00
|
|
|
"gitee.com/johng/gf/g/util/gtest"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCron_Add_Close(t *testing.T) {
|
2019-01-02 10:18:00 +08:00
|
|
|
gtest.Case(t, func() {
|
2018-12-30 18:56:21 +08:00
|
|
|
cron := gcron.New()
|
|
|
|
array := garray.New(0, 0)
|
|
|
|
_, err1 := cron.Add("* * * * * *", func() {
|
2019-01-06 11:09:50 +08:00
|
|
|
glog.Println("cron1")
|
2018-12-30 18:56:21 +08:00
|
|
|
array.Append(1)
|
|
|
|
})
|
|
|
|
_, err2 := cron.Add("* * * * * *", func() {
|
2019-01-06 11:09:50 +08:00
|
|
|
glog.Println("cron2")
|
2018-12-30 18:56:21 +08:00
|
|
|
array.Append(1)
|
|
|
|
}, "test")
|
|
|
|
_, err3 := cron.Add("* * * * * *", func() {
|
|
|
|
array.Append(1)
|
|
|
|
}, "test")
|
|
|
|
_, err4 := cron.Add("@every 2s", func() {
|
2019-01-06 11:09:50 +08:00
|
|
|
glog.Println("cron3")
|
2018-12-30 18:56:21 +08:00
|
|
|
array.Append(1)
|
|
|
|
})
|
|
|
|
gtest.Assert(err1, nil)
|
|
|
|
gtest.Assert(err2, nil)
|
|
|
|
gtest.AssertNE(err3, nil)
|
|
|
|
gtest.Assert(err4, nil)
|
2018-12-31 17:46:04 +08:00
|
|
|
gtest.Assert(cron.Size(), 3)
|
2019-01-06 11:09:50 +08:00
|
|
|
time.Sleep(1100*time.Millisecond)
|
2018-12-30 18:56:21 +08:00
|
|
|
gtest.Assert(array.Len(), 2)
|
2019-01-06 11:09:50 +08:00
|
|
|
time.Sleep(1100*time.Millisecond)
|
2018-12-30 18:56:21 +08:00
|
|
|
gtest.Assert(array.Len(), 5)
|
|
|
|
cron.Close()
|
2019-01-06 11:09:50 +08:00
|
|
|
time.Sleep(1100*time.Millisecond)
|
2018-12-30 18:56:21 +08:00
|
|
|
fixedLength := array.Len()
|
2019-01-06 11:09:50 +08:00
|
|
|
time.Sleep(1100*time.Millisecond)
|
2018-12-30 18:56:21 +08:00
|
|
|
gtest.Assert(array.Len(), fixedLength)
|
2018-12-30 11:08:07 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-01-16 22:34:22 +08:00
|
|
|
func TestCron_Basic(t *testing.T) {
|
2019-01-02 10:18:00 +08:00
|
|
|
gtest.Case(t, func() {
|
2018-12-30 18:56:21 +08:00
|
|
|
cron := gcron.New()
|
|
|
|
cron.Add("* * * * * *", func() {}, "add")
|
2019-01-01 19:43:31 +08:00
|
|
|
fmt.Println("start", time.Now())
|
2018-12-30 18:56:21 +08:00
|
|
|
cron.DelayAdd(1, "* * * * * *", func() {}, "delay_add")
|
2018-12-31 17:46:04 +08:00
|
|
|
gtest.Assert(cron.Size(), 1)
|
2019-01-01 19:43:31 +08:00
|
|
|
time.Sleep(1200*time.Millisecond)
|
2018-12-31 17:46:04 +08:00
|
|
|
gtest.Assert(cron.Size(), 2)
|
2018-12-30 11:08:07 +08:00
|
|
|
|
2018-12-30 18:56:21 +08:00
|
|
|
cron.Remove("delay_add")
|
2018-12-31 17:46:04 +08:00
|
|
|
gtest.Assert(cron.Size(), 1)
|
2018-12-30 11:08:07 +08:00
|
|
|
|
2018-12-30 18:56:21 +08:00
|
|
|
entry1 := cron.Search("add")
|
|
|
|
entry2 := cron.Search("test-none")
|
|
|
|
gtest.AssertNE(entry1, nil)
|
|
|
|
gtest.Assert(entry2, nil)
|
|
|
|
})
|
2018-12-30 11:08:07 +08:00
|
|
|
}
|
2019-01-16 22:34:22 +08:00
|
|
|
|
|
|
|
func TestCron_AddSingleton(t *testing.T) {
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
cron := gcron.New()
|
|
|
|
array := garray.New(0, 0)
|
|
|
|
cron.AddSingleton("* * * * * *", func() {
|
|
|
|
array.Append(1)
|
|
|
|
time.Sleep(5*time.Second)
|
|
|
|
|
|
|
|
})
|
|
|
|
gtest.Assert(cron.Size(), 1)
|
|
|
|
time.Sleep(3500*time.Millisecond)
|
|
|
|
gtest.Assert(array.Len(), 1)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCron_AddOnce(t *testing.T) {
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
cron := gcron.New()
|
|
|
|
array := garray.New(0, 0)
|
|
|
|
cron.AddOnce("* * * * * *", func() {
|
|
|
|
array.Append(1)
|
|
|
|
})
|
|
|
|
cron.AddOnce("* * * * * *", func() {
|
|
|
|
array.Append(1)
|
|
|
|
})
|
|
|
|
gtest.Assert(cron.Size(), 2)
|
|
|
|
time.Sleep(2500*time.Millisecond)
|
|
|
|
gtest.Assert(array.Len(), 2)
|
|
|
|
gtest.Assert(cron.Size(), 0)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCron_AddTimes(t *testing.T) {
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
cron := gcron.New()
|
|
|
|
array := garray.New(0, 0)
|
|
|
|
cron.AddTimes("* * * * * *", 2, func() {
|
|
|
|
array.Append(1)
|
|
|
|
})
|
|
|
|
time.Sleep(3500*time.Millisecond)
|
|
|
|
gtest.Assert(array.Len(), 2)
|
|
|
|
gtest.Assert(cron.Size(), 0)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCron_DelayAdd(t *testing.T) {
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
cron := gcron.New()
|
|
|
|
array := garray.New(0, 0)
|
|
|
|
cron.DelayAdd(1, "* * * * * *", func() {
|
|
|
|
array.Append(1)
|
|
|
|
})
|
|
|
|
gtest.Assert(cron.Size(), 0)
|
|
|
|
time.Sleep(1200*time.Millisecond)
|
|
|
|
gtest.Assert(array.Len(), 0)
|
|
|
|
gtest.Assert(cron.Size(), 1)
|
|
|
|
time.Sleep(1200*time.Millisecond)
|
|
|
|
gtest.Assert(array.Len(), 1)
|
|
|
|
gtest.Assert(cron.Size(), 1)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCron_DelayAddSingleton(t *testing.T) {
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
cron := gcron.New()
|
|
|
|
array := garray.New(0, 0)
|
|
|
|
cron.DelayAddSingleton(1, "* * * * * *", func() {
|
|
|
|
array.Append(1)
|
|
|
|
time.Sleep(10*time.Second)
|
|
|
|
})
|
|
|
|
gtest.Assert(cron.Size(), 0)
|
|
|
|
time.Sleep(2200*time.Millisecond)
|
|
|
|
gtest.Assert(array.Len(), 1)
|
|
|
|
gtest.Assert(cron.Size(), 1)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCron_DelayAddOnce(t *testing.T) {
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
cron := gcron.New()
|
|
|
|
array := garray.New(0, 0)
|
|
|
|
cron.DelayAddOnce(1, "* * * * * *", func() {
|
|
|
|
array.Append(1)
|
|
|
|
})
|
|
|
|
gtest.Assert(cron.Size(), 0)
|
|
|
|
time.Sleep(1200*time.Millisecond)
|
|
|
|
gtest.Assert(array.Len(), 0)
|
|
|
|
gtest.Assert(cron.Size(), 1)
|
|
|
|
time.Sleep(1200*time.Millisecond)
|
|
|
|
gtest.Assert(array.Len(), 1)
|
|
|
|
gtest.Assert(cron.Size(), 0)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCron_DelayAddTimes(t *testing.T) {
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
cron := gcron.New()
|
|
|
|
array := garray.New(0, 0)
|
|
|
|
cron.DelayAddTimes(1, "* * * * * *", 2, func() {
|
|
|
|
array.Append(1)
|
|
|
|
})
|
|
|
|
gtest.Assert(cron.Size(), 0)
|
|
|
|
time.Sleep(5000*time.Millisecond)
|
|
|
|
gtest.Assert(array.Len(), 2)
|
|
|
|
gtest.Assert(cron.Size(), 0)
|
|
|
|
})
|
|
|
|
}
|