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 (
|
|
|
|
"gitee.com/johng/gf/g/container/garray"
|
|
|
|
"gitee.com/johng/gf/g/os/gcron"
|
|
|
|
"gitee.com/johng/gf/g/util/gtest"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCron_Add_Close(t *testing.T) {
|
2018-12-30 14:53:16 +08:00
|
|
|
cron := gcron.New()
|
2018-12-30 11:08:07 +08:00
|
|
|
array := garray.New(0, 0)
|
2018-12-30 14:53:16 +08:00
|
|
|
_, err1 := cron.Add("* * * * * *", func() {
|
2018-12-30 11:08:07 +08:00
|
|
|
array.Append(1)
|
|
|
|
})
|
2018-12-30 14:53:16 +08:00
|
|
|
_, err2 := cron.Add("* * * * * *", func() {
|
2018-12-30 11:08:07 +08:00
|
|
|
array.Append(1)
|
|
|
|
}, "test")
|
2018-12-30 14:53:16 +08:00
|
|
|
_, err3 := cron.Add("* * * * * *", func() {
|
2018-12-30 11:08:07 +08:00
|
|
|
array.Append(1)
|
|
|
|
}, "test")
|
2018-12-30 14:53:16 +08:00
|
|
|
_, err4 := cron.Add("@every 2s", func() {
|
2018-12-30 11:08:07 +08:00
|
|
|
array.Append(1)
|
|
|
|
})
|
|
|
|
gtest.Assert(err1, nil)
|
|
|
|
gtest.Assert(err2, nil)
|
|
|
|
gtest.AssertNE(err3, nil)
|
|
|
|
gtest.Assert(err4, nil)
|
2018-12-30 14:53:16 +08:00
|
|
|
gtest.Assert(len(cron.Entries()), 3)
|
2018-12-30 11:08:07 +08:00
|
|
|
time.Sleep(1100*time.Millisecond)
|
|
|
|
gtest.Assert(array.Len(), 2)
|
|
|
|
time.Sleep(1100*time.Millisecond)
|
|
|
|
gtest.Assert(array.Len(), 5)
|
2018-12-30 14:53:16 +08:00
|
|
|
cron.Close()
|
2018-12-30 11:08:07 +08:00
|
|
|
time.Sleep(1100*time.Millisecond)
|
|
|
|
fixedLength := array.Len()
|
|
|
|
time.Sleep(1100*time.Millisecond)
|
|
|
|
gtest.Assert(array.Len(), fixedLength)
|
|
|
|
}
|
|
|
|
|
2018-12-30 14:53:16 +08:00
|
|
|
func TestCron_Mathod(t *testing.T) {
|
|
|
|
cron := gcron.New()
|
|
|
|
cron.Add("* * * * * *", func() {}, "add")
|
|
|
|
cron.DelayAdd(1, "* * * * * *", func() {}, "delay_add")
|
|
|
|
gtest.Assert(len(cron.Entries()), 1)
|
2018-12-30 11:08:07 +08:00
|
|
|
time.Sleep(1100*time.Millisecond)
|
2018-12-30 14:53:16 +08:00
|
|
|
gtest.Assert(len(cron.Entries()), 2)
|
2018-12-30 11:08:07 +08:00
|
|
|
|
2018-12-30 14:53:16 +08:00
|
|
|
cron.Remove("delay_add")
|
|
|
|
gtest.Assert(len(cron.Entries()), 1)
|
2018-12-30 11:08:07 +08:00
|
|
|
|
2018-12-30 14:53:16 +08:00
|
|
|
entry1 := cron.Search("add")
|
|
|
|
entry2 := cron.Search("test-none")
|
2018-12-30 11:08:07 +08:00
|
|
|
gtest.AssertNE(entry1, nil)
|
|
|
|
gtest.Assert(entry2, nil)
|
|
|
|
}
|