gf/g/os/gcron/gcron_unit_2_test.go

64 lines
1.9 KiB
Go
Raw Normal View History

// 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,
// 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 (
"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()
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)
})
cron := gcron.New()
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")
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)
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
})
}