2019-02-02 16:18:25 +08:00
|
|
|
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
2018-12-30 11:08:07 +08:00
|
|
|
//
|
|
|
|
// 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,
|
2019-02-02 16:18:25 +08:00
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2018-12-30 11:08:07 +08:00
|
|
|
|
2019-01-16 22:34:22 +08:00
|
|
|
|
2018-12-30 11:08:07 +08:00
|
|
|
package gcron_test
|
|
|
|
|
|
|
|
import (
|
2019-02-02 16:18:25 +08:00
|
|
|
"github.com/gogf/gf/g/container/garray"
|
|
|
|
"github.com/gogf/gf/g/os/gcron"
|
|
|
|
"github.com/gogf/gf/g/os/glog"
|
|
|
|
"github.com/gogf/gf/g/test/gtest"
|
2018-12-30 11:08:07 +08:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2019-01-16 22:34:22 +08:00
|
|
|
func TestCron_Entry_Operations(t *testing.T) {
|
2019-01-02 10:18:00 +08:00
|
|
|
gtest.Case(t, func() {
|
2019-01-22 13:50:10 +08:00
|
|
|
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
cron := gcron.New()
|
2019-02-01 22:00:58 +08:00
|
|
|
array := garray.New()
|
2019-01-22 13:50:10 +08:00
|
|
|
cron.DelayAddTimes(500*time.Millisecond, "* * * * * *", 2, func() {
|
2019-01-22 22:07:46 +08:00
|
|
|
glog.Println("add times")
|
2019-01-22 13:50:10 +08:00
|
|
|
array.Append(1)
|
|
|
|
})
|
|
|
|
gtest.Assert(cron.Size(), 0)
|
|
|
|
time.Sleep(800*time.Millisecond)
|
|
|
|
gtest.Assert(array.Len(), 0)
|
|
|
|
gtest.Assert(cron.Size(), 1)
|
|
|
|
time.Sleep(3000*time.Millisecond)
|
|
|
|
gtest.Assert(array.Len(), 2)
|
|
|
|
gtest.Assert(cron.Size(), 0)
|
|
|
|
})
|
|
|
|
|
2018-12-30 18:56:21 +08:00
|
|
|
cron := gcron.New()
|
2019-02-01 22:00:58 +08:00
|
|
|
array := garray.New()
|
2019-01-16 22:34:22 +08:00
|
|
|
entry, err1 := cron.Add("* * * * * *", func() {
|
2019-01-22 13:50:10 +08:00
|
|
|
glog.Println("add")
|
2018-12-30 18:56:21 +08:00
|
|
|
array.Append(1)
|
|
|
|
})
|
2019-01-16 22:34:22 +08:00
|
|
|
gtest.Assert(err1, nil)
|
|
|
|
gtest.Assert(array.Len(), 0)
|
|
|
|
gtest.Assert(cron.Size(), 1)
|
|
|
|
time.Sleep(1200*time.Millisecond)
|
|
|
|
gtest.Assert(array.Len(), 1)
|
2018-12-31 17:46:04 +08:00
|
|
|
gtest.Assert(cron.Size(), 1)
|
2019-01-16 22:34:22 +08:00
|
|
|
entry.Stop()
|
2019-01-22 13:50:10 +08:00
|
|
|
time.Sleep(2000*time.Millisecond)
|
2018-12-30 18:56:21 +08:00
|
|
|
gtest.Assert(array.Len(), 1)
|
2019-01-16 22:34:22 +08:00
|
|
|
gtest.Assert(cron.Size(), 1)
|
|
|
|
entry.Start()
|
2019-01-22 13:50:10 +08:00
|
|
|
glog.Println("start")
|
2019-01-22 22:07:46 +08:00
|
|
|
time.Sleep(1200*time.Millisecond)
|
2019-01-16 22:34:22 +08:00
|
|
|
gtest.Assert(array.Len(), 2)
|
|
|
|
gtest.Assert(cron.Size(), 1)
|
|
|
|
entry.Close()
|
|
|
|
time.Sleep(1200*time.Millisecond)
|
|
|
|
gtest.Assert(cron.Size(), 0)
|
2018-12-30 11:08:07 +08:00
|
|
|
})
|
|
|
|
}
|