mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-04 12:52:28 +08:00
test: update test for Schedule
This commit is contained in:
@@ -164,16 +164,18 @@ func Throttle(fn func(), interval time.Duration) func() {
|
|||||||
|
|
||||||
// Schedule invoke function every duration time, util close the returned bool channel.
|
// Schedule invoke function every duration time, util close the returned bool channel.
|
||||||
// Play: https://go.dev/play/p/hbON-Xeyn5N
|
// Play: https://go.dev/play/p/hbON-Xeyn5N
|
||||||
func Schedule(d time.Duration, fn any, args ...any) chan bool {
|
func Schedule(duration time.Duration, fn any, args ...any) chan bool {
|
||||||
// Catch programming error while constructing the closure
|
// Catch programming error while constructing the closure
|
||||||
mustBeFunction(fn)
|
mustBeFunction(fn)
|
||||||
|
|
||||||
quit := make(chan bool)
|
quit := make(chan bool)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
unsafeInvokeFunc(fn, args...)
|
unsafeInvokeFunc(fn, args...)
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-time.After(d):
|
case <-time.After(duration):
|
||||||
case <-quit:
|
case <-quit:
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -268,23 +268,35 @@ func TestThrottle(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestSchedule(t *testing.T) {
|
func TestSchedule(t *testing.T) {
|
||||||
// assert := internal.NewAssert(t, "TestSchedule")
|
assert := internal.NewAssert(t, "TestSchedule")
|
||||||
|
|
||||||
var res []string
|
t.Run("Single call", func(t *testing.T) {
|
||||||
appendStr := func(s string) {
|
res := []string{}
|
||||||
res = append(res, s)
|
appendStr := func(s string) {
|
||||||
}
|
res = append(res, s)
|
||||||
|
}
|
||||||
|
stop := Schedule(200*time.Millisecond, appendStr, "*")
|
||||||
|
close(stop)
|
||||||
|
|
||||||
stop := Schedule(1*time.Second, appendStr, "*")
|
time.Sleep(400 * time.Millisecond)
|
||||||
time.Sleep(5 * time.Second)
|
|
||||||
close(stop)
|
|
||||||
|
|
||||||
t.Log(res)
|
assert.Equal([]string{"*"}, res)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("Multiple calls", func(t *testing.T) {
|
||||||
|
res := []string{}
|
||||||
|
appendStr := func(s string) {
|
||||||
|
res = append(res, s)
|
||||||
|
}
|
||||||
|
stop := Schedule(200*time.Millisecond, appendStr, "*")
|
||||||
|
|
||||||
|
time.Sleep(800 * time.Millisecond)
|
||||||
|
|
||||||
|
close(stop)
|
||||||
|
|
||||||
|
assert.Equal([]string{"*", "*", "*", "*"}, res)
|
||||||
|
})
|
||||||
|
|
||||||
// todo: in github action, for now, this test is not working sometimes
|
|
||||||
// res maybe [* * * * *] or [* * * * * *]
|
|
||||||
// expected := []string{"*", "*", "*", "*", "*"}
|
|
||||||
// assert.Equal(expected, res)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPipeline(t *testing.T) {
|
func TestPipeline(t *testing.T) {
|
||||||
|
|||||||
Reference in New Issue
Block a user