mirror of
https://gitee.com/johng/gf.git
synced 2024-11-30 11:18:02 +08:00
231 lines
5.8 KiB
Go
231 lines
5.8 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"
|
|
)
|
|
|
|
var (
|
|
ctx = context.TODO()
|
|
)
|
|
|
|
func TestCron_Add_Close(t *testing.T) {
|
|
gtest.C(t, func(t *gtest.T) {
|
|
cron := gcron.New()
|
|
array := garray.New(true)
|
|
_, err1 := cron.Add(ctx, "* * * * * *", func(ctx context.Context) {
|
|
g.Log().Print(ctx, "cron1")
|
|
array.Append(1)
|
|
})
|
|
_, err2 := cron.Add(ctx, "* * * * * *", func(ctx context.Context) {
|
|
g.Log().Print(ctx, "cron2")
|
|
array.Append(1)
|
|
}, "test")
|
|
t.Assert(err1, nil)
|
|
t.Assert(err2, nil)
|
|
t.Assert(cron.Size(), 2)
|
|
time.Sleep(1300 * time.Millisecond)
|
|
t.Assert(array.Len(), 2)
|
|
time.Sleep(1300 * time.Millisecond)
|
|
t.Assert(array.Len(), 4)
|
|
cron.Close()
|
|
time.Sleep(1300 * time.Millisecond)
|
|
fixedLength := array.Len()
|
|
time.Sleep(1300 * time.Millisecond)
|
|
t.Assert(array.Len(), fixedLength)
|
|
})
|
|
}
|
|
|
|
func TestCron_Basic(t *testing.T) {
|
|
gtest.C(t, func(t *gtest.T) {
|
|
cron := gcron.New()
|
|
cron.Add(ctx, "* * * * * *", func(ctx context.Context) {}, "add")
|
|
//fmt.Println("start", time.Now())
|
|
cron.DelayAdd(ctx, time.Second, "* * * * * *", func(ctx context.Context) {}, "delay_add")
|
|
t.Assert(cron.Size(), 1)
|
|
time.Sleep(1200 * time.Millisecond)
|
|
t.Assert(cron.Size(), 2)
|
|
|
|
cron.Remove("delay_add")
|
|
t.Assert(cron.Size(), 1)
|
|
|
|
entry1 := cron.Search("add")
|
|
entry2 := cron.Search("test-none")
|
|
t.AssertNE(entry1, nil)
|
|
t.Assert(entry2, nil)
|
|
})
|
|
}
|
|
|
|
func TestCron_Remove(t *testing.T) {
|
|
gtest.C(t, func(t *gtest.T) {
|
|
cron := gcron.New()
|
|
array := garray.New(true)
|
|
cron.Add(ctx, "* * * * * *", func(ctx context.Context) {
|
|
array.Append(1)
|
|
}, "add")
|
|
t.Assert(array.Len(), 0)
|
|
time.Sleep(1200 * time.Millisecond)
|
|
t.Assert(array.Len(), 1)
|
|
|
|
cron.Remove("add")
|
|
t.Assert(array.Len(), 1)
|
|
time.Sleep(1200 * time.Millisecond)
|
|
t.Assert(array.Len(), 1)
|
|
})
|
|
}
|
|
|
|
func TestCron_AddSingleton(t *testing.T) {
|
|
// un used, can be removed
|
|
gtest.C(t, func(t *gtest.T) {
|
|
cron := gcron.New()
|
|
cron.Add(ctx, "* * * * * *", func(ctx context.Context) {}, "add")
|
|
cron.DelayAdd(ctx, time.Second, "* * * * * *", func(ctx context.Context) {}, "delay_add")
|
|
t.Assert(cron.Size(), 1)
|
|
time.Sleep(1200 * time.Millisecond)
|
|
t.Assert(cron.Size(), 2)
|
|
|
|
cron.Remove("delay_add")
|
|
t.Assert(cron.Size(), 1)
|
|
|
|
entry1 := cron.Search("add")
|
|
entry2 := cron.Search("test-none")
|
|
t.AssertNE(entry1, nil)
|
|
t.Assert(entry2, nil)
|
|
})
|
|
// keep this
|
|
gtest.C(t, func(t *gtest.T) {
|
|
cron := gcron.New()
|
|
array := garray.New(true)
|
|
cron.AddSingleton(ctx, "* * * * * *", func(ctx context.Context) {
|
|
array.Append(1)
|
|
time.Sleep(50 * time.Second)
|
|
})
|
|
t.Assert(cron.Size(), 1)
|
|
time.Sleep(3500 * time.Millisecond)
|
|
t.Assert(array.Len(), 1)
|
|
})
|
|
|
|
}
|
|
|
|
func TestCron_AddOnce1(t *testing.T) {
|
|
gtest.C(t, func(t *gtest.T) {
|
|
cron := gcron.New()
|
|
array := garray.New(true)
|
|
cron.AddOnce(ctx, "* * * * * *", func(ctx context.Context) {
|
|
array.Append(1)
|
|
})
|
|
cron.AddOnce(ctx, "* * * * * *", func(ctx context.Context) {
|
|
array.Append(1)
|
|
})
|
|
t.Assert(cron.Size(), 2)
|
|
time.Sleep(2500 * time.Millisecond)
|
|
t.Assert(array.Len(), 2)
|
|
t.Assert(cron.Size(), 0)
|
|
})
|
|
}
|
|
|
|
func TestCron_AddOnce2(t *testing.T) {
|
|
gtest.C(t, func(t *gtest.T) {
|
|
cron := gcron.New()
|
|
array := garray.New(true)
|
|
cron.AddOnce(ctx, "@every 2s", func(ctx context.Context) {
|
|
array.Append(1)
|
|
})
|
|
t.Assert(cron.Size(), 1)
|
|
time.Sleep(3000 * time.Millisecond)
|
|
t.Assert(array.Len(), 1)
|
|
t.Assert(cron.Size(), 0)
|
|
})
|
|
}
|
|
|
|
func TestCron_AddTimes(t *testing.T) {
|
|
gtest.C(t, func(t *gtest.T) {
|
|
cron := gcron.New()
|
|
array := garray.New(true)
|
|
cron.AddTimes(ctx, "* * * * * *", 2, func(ctx context.Context) {
|
|
array.Append(1)
|
|
})
|
|
time.Sleep(3500 * time.Millisecond)
|
|
t.Assert(array.Len(), 2)
|
|
t.Assert(cron.Size(), 0)
|
|
})
|
|
}
|
|
|
|
func TestCron_DelayAdd(t *testing.T) {
|
|
gtest.C(t, func(t *gtest.T) {
|
|
cron := gcron.New()
|
|
array := garray.New(true)
|
|
cron.DelayAdd(ctx, 500*time.Millisecond, "* * * * * *", func(ctx context.Context) {
|
|
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(1000 * time.Millisecond)
|
|
t.Assert(array.Len(), 1)
|
|
t.Assert(cron.Size(), 1)
|
|
})
|
|
}
|
|
|
|
func TestCron_DelayAddSingleton(t *testing.T) {
|
|
gtest.C(t, func(t *gtest.T) {
|
|
cron := gcron.New()
|
|
array := garray.New(true)
|
|
cron.DelayAddSingleton(ctx, 500*time.Millisecond, "* * * * * *", func(ctx context.Context) {
|
|
array.Append(1)
|
|
time.Sleep(10 * time.Second)
|
|
})
|
|
t.Assert(cron.Size(), 0)
|
|
time.Sleep(2200 * time.Millisecond)
|
|
t.Assert(array.Len(), 1)
|
|
t.Assert(cron.Size(), 1)
|
|
})
|
|
}
|
|
|
|
func TestCron_DelayAddOnce(t *testing.T) {
|
|
gtest.C(t, func(t *gtest.T) {
|
|
cron := gcron.New()
|
|
array := garray.New(true)
|
|
cron.DelayAddOnce(ctx, 500*time.Millisecond, "* * * * * *", func(ctx context.Context) {
|
|
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(2200 * time.Millisecond)
|
|
t.Assert(array.Len(), 1)
|
|
t.Assert(cron.Size(), 0)
|
|
})
|
|
}
|
|
|
|
func TestCron_DelayAddTimes(t *testing.T) {
|
|
gtest.C(t, func(t *gtest.T) {
|
|
cron := gcron.New()
|
|
array := garray.New(true)
|
|
cron.DelayAddTimes(ctx, 500*time.Millisecond, "* * * * * *", 2, func(ctx context.Context) {
|
|
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)
|
|
})
|
|
}
|