gf/os/gcron/gcron_z_unit_test.go

231 lines
5.8 KiB
Go
Raw Normal View History

2021-01-17 21:46:25 +08:00
// Copyright GoFrame Author(https://goframe.org). 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
package gcron_test
import (
2021-09-28 19:04:36 +08:00
"context"
2019-06-22 23:06:44 +08:00
"testing"
"time"
2021-10-11 21:41:56 +08:00
"github.com/gogf/gf/v2/container/garray"
2021-11-15 20:26:31 +08:00
"github.com/gogf/gf/v2/frame/g"
2021-10-11 21:41:56 +08:00
"github.com/gogf/gf/v2/os/gcron"
"github.com/gogf/gf/v2/test/gtest"
2018-12-30 11:08:07 +08:00
)
2021-09-28 19:04:36 +08:00
var (
ctx = context.TODO()
)
2018-12-30 11:08:07 +08:00
func TestCron_Add_Close(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-19 09:06:52 +08:00
cron := gcron.New()
array := garray.New(true)
_, err1 := cron.Add(ctx, "* * * * * *", func(ctx context.Context) {
g.Log().Print(ctx, "cron1")
2019-06-19 09:06:52 +08:00
array.Append(1)
})
_, err2 := cron.Add(ctx, "* * * * * *", func(ctx context.Context) {
g.Log().Print(ctx, "cron2")
2019-06-19 09:06:52 +08:00
array.Append(1)
}, "test")
2020-03-19 22:56:12 +08:00
t.Assert(err1, nil)
t.Assert(err2, nil)
t.Assert(cron.Size(), 2)
time.Sleep(1300 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 2)
time.Sleep(1300 * time.Millisecond)
t.Assert(array.Len(), 4)
2019-06-19 09:06:52 +08:00
cron.Close()
time.Sleep(1300 * time.Millisecond)
2019-06-19 09:06:52 +08:00
fixedLength := array.Len()
time.Sleep(1300 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), fixedLength)
2019-06-19 09:06:52 +08:00
})
2018-12-30 11:08:07 +08:00
}
2019-01-16 22:34:22 +08:00
func TestCron_Basic(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-19 09:06:52 +08:00
cron := gcron.New()
cron.Add(ctx, "* * * * * *", func(ctx context.Context) {}, "add")
2021-11-15 20:26:31 +08:00
// fmt.Println("start", time.Now())
cron.DelayAdd(ctx, time.Second, "* * * * * *", func(ctx context.Context) {}, "delay_add")
2020-03-19 22:56:12 +08:00
t.Assert(cron.Size(), 1)
2019-06-19 09:06:52 +08:00
time.Sleep(1200 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(cron.Size(), 2)
2019-06-19 09:06:52 +08:00
cron.Remove("delay_add")
2020-03-19 22:56:12 +08:00
t.Assert(cron.Size(), 1)
2019-06-19 09:06:52 +08:00
entry1 := cron.Search("add")
entry2 := cron.Search("test-none")
2020-03-19 22:56:12 +08:00
t.AssertNE(entry1, nil)
t.Assert(entry2, nil)
2019-06-19 09:06:52 +08:00
})
2018-12-30 11:08:07 +08:00
}
2019-01-16 22:34:22 +08:00
func TestCron_Remove(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-19 09:06:52 +08:00
cron := gcron.New()
array := garray.New(true)
cron.Add(ctx, "* * * * * *", func(ctx context.Context) {
2019-06-19 09:06:52 +08:00
array.Append(1)
}, "add")
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 0)
2019-06-19 09:06:52 +08:00
time.Sleep(1200 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 1)
2019-06-19 09:06:52 +08:00
cron.Remove("add")
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 1)
2019-06-19 09:06:52 +08:00
time.Sleep(1200 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 1)
2019-06-19 09:06:52 +08:00
})
}
2019-01-16 22:34:22 +08:00
func TestCron_AddSingleton(t *testing.T) {
2019-06-19 09:06:52 +08:00
// un used, can be removed
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-19 09:06:52 +08:00
cron := gcron.New()
cron.Add(ctx, "* * * * * *", func(ctx context.Context) {}, "add")
cron.DelayAdd(ctx, time.Second, "* * * * * *", func(ctx context.Context) {}, "delay_add")
2020-03-19 22:56:12 +08:00
t.Assert(cron.Size(), 1)
2019-06-19 09:06:52 +08:00
time.Sleep(1200 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(cron.Size(), 2)
2019-06-19 09:06:52 +08:00
cron.Remove("delay_add")
2020-03-19 22:56:12 +08:00
t.Assert(cron.Size(), 1)
2019-06-19 09:06:52 +08:00
entry1 := cron.Search("add")
entry2 := cron.Search("test-none")
2020-03-19 22:56:12 +08:00
t.AssertNE(entry1, nil)
t.Assert(entry2, nil)
2019-06-19 09:06:52 +08:00
})
// keep this
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-19 09:06:52 +08:00
cron := gcron.New()
array := garray.New(true)
cron.AddSingleton(ctx, "* * * * * *", func(ctx context.Context) {
2019-06-19 09:06:52 +08:00
array.Append(1)
time.Sleep(50 * time.Second)
})
2020-03-19 22:56:12 +08:00
t.Assert(cron.Size(), 1)
2019-06-19 09:06:52 +08:00
time.Sleep(3500 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 1)
2019-06-19 09:06:52 +08:00
})
2019-01-21 22:09:51 +08:00
2019-01-16 22:34:22 +08:00
}
func TestCron_AddOnce1(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-19 09:06:52 +08:00
cron := gcron.New()
array := garray.New(true)
cron.AddOnce(ctx, "* * * * * *", func(ctx context.Context) {
2019-06-19 09:06:52 +08:00
array.Append(1)
})
cron.AddOnce(ctx, "* * * * * *", func(ctx context.Context) {
2019-06-19 09:06:52 +08:00
array.Append(1)
})
2020-03-19 22:56:12 +08:00
t.Assert(cron.Size(), 2)
2019-06-19 09:06:52 +08:00
time.Sleep(2500 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 2)
t.Assert(cron.Size(), 0)
2019-06-19 09:06:52 +08:00
})
2019-01-16 22:34:22 +08:00
}
func TestCron_AddOnce2(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-19 09:06:52 +08:00
cron := gcron.New()
array := garray.New(true)
cron.AddOnce(ctx, "@every 2s", func(ctx context.Context) {
array.Append(1)
})
2020-03-19 22:56:12 +08:00
t.Assert(cron.Size(), 1)
2019-06-22 23:06:44 +08:00
time.Sleep(3000 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 1)
t.Assert(cron.Size(), 0)
})
}
2019-01-16 22:34:22 +08:00
func TestCron_AddTimes(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-19 09:06:52 +08:00
cron := gcron.New()
array := garray.New(true)
cron.AddTimes(ctx, "* * * * * *", 2, func(ctx context.Context) {
2019-06-19 09:06:52 +08:00
array.Append(1)
})
time.Sleep(3500 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 2)
t.Assert(cron.Size(), 0)
2019-06-19 09:06:52 +08:00
})
2019-01-16 22:34:22 +08:00
}
func TestCron_DelayAdd(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-19 09:06:52 +08:00
cron := gcron.New()
array := garray.New(true)
cron.DelayAdd(ctx, 500*time.Millisecond, "* * * * * *", func(ctx context.Context) {
2019-06-19 09:06:52 +08:00
array.Append(1)
})
2020-03-19 22:56:12 +08:00
t.Assert(cron.Size(), 0)
2019-06-19 09:06:52 +08:00
time.Sleep(800 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 0)
t.Assert(cron.Size(), 1)
2019-06-19 09:06:52 +08:00
time.Sleep(1000 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 1)
t.Assert(cron.Size(), 1)
2019-06-19 09:06:52 +08:00
})
2019-01-16 22:34:22 +08:00
}
func TestCron_DelayAddSingleton(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-19 09:06:52 +08:00
cron := gcron.New()
array := garray.New(true)
cron.DelayAddSingleton(ctx, 500*time.Millisecond, "* * * * * *", func(ctx context.Context) {
2019-06-19 09:06:52 +08:00
array.Append(1)
time.Sleep(10 * time.Second)
})
2020-03-19 22:56:12 +08:00
t.Assert(cron.Size(), 0)
2019-06-19 09:06:52 +08:00
time.Sleep(2200 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 1)
t.Assert(cron.Size(), 1)
2019-06-19 09:06:52 +08:00
})
2019-01-16 22:34:22 +08:00
}
func TestCron_DelayAddOnce(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-19 09:06:52 +08:00
cron := gcron.New()
array := garray.New(true)
cron.DelayAddOnce(ctx, 500*time.Millisecond, "* * * * * *", func(ctx context.Context) {
2019-06-19 09:06:52 +08:00
array.Append(1)
})
2020-03-19 22:56:12 +08:00
t.Assert(cron.Size(), 0)
2019-06-19 09:06:52 +08:00
time.Sleep(800 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 0)
t.Assert(cron.Size(), 1)
2019-06-19 09:06:52 +08:00
time.Sleep(2200 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 1)
t.Assert(cron.Size(), 0)
2019-06-19 09:06:52 +08:00
})
2019-01-16 22:34:22 +08:00
}
func TestCron_DelayAddTimes(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-19 09:06:52 +08:00
cron := gcron.New()
array := garray.New(true)
cron.DelayAddTimes(ctx, 500*time.Millisecond, "* * * * * *", 2, func(ctx context.Context) {
2019-06-19 09:06:52 +08:00
array.Append(1)
})
2020-03-19 22:56:12 +08:00
t.Assert(cron.Size(), 0)
2019-06-19 09:06:52 +08:00
time.Sleep(800 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 0)
t.Assert(cron.Size(), 1)
2019-06-19 09:06:52 +08:00
time.Sleep(3000 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 2)
t.Assert(cron.Size(), 0)
2019-06-19 09:06:52 +08:00
})
}