gf/os/gcron/gcron_unit_2_test.go
2021-10-30 20:47:38 +08:00

68 lines
1.7 KiB
Go

// Copyright GoFrame Author(https://goframe.org). 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://github.com/gogf/gf.
package gcron_test
import (
"context"
"github.com/gogf/gf/v2/frame/g"
"testing"
"time"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/os/gcron"
"github.com/gogf/gf/v2/test/gtest"
)
func TestCron_Entry_Operations(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
cron = gcron.New()
array = garray.New(true)
)
cron.DelayAddTimes(ctx, 500*time.Millisecond, "* * * * * *", 2, func(ctx context.Context) {
g.Log().Print(ctx, "add times")
array.Append(1)
})
t.Assert(cron.Size(), 0)
time.Sleep(800 * time.Millisecond)
t.Assert(array.Len(), 0)
t.Assert(cron.Size(), 1)
time.Sleep(3000 * time.Millisecond)
t.Assert(array.Len(), 2)
t.Assert(cron.Size(), 0)
})
gtest.C(t, func(t *gtest.T) {
var (
cron = gcron.New()
array = garray.New(true)
)
entry, err1 := cron.Add(ctx, "* * * * * *", func(ctx context.Context) {
g.Log().Print(ctx, "add")
array.Append(1)
})
t.Assert(err1, nil)
t.Assert(array.Len(), 0)
t.Assert(cron.Size(), 1)
time.Sleep(1300 * time.Millisecond)
t.Assert(array.Len(), 1)
t.Assert(cron.Size(), 1)
entry.Stop()
time.Sleep(5000 * time.Millisecond)
t.Assert(array.Len(), 1)
t.Assert(cron.Size(), 1)
entry.Start()
g.Log().Print(ctx, "start")
time.Sleep(1000 * time.Millisecond)
t.Assert(array.Len(), 2)
t.Assert(cron.Size(), 1)
entry.Close()
time.Sleep(1200 * time.Millisecond)
t.Assert(cron.Size(), 0)
})
}