gf/os/gtimer/gtimer_z_unit_timer_test.go

307 lines
7.7 KiB
Go
Raw Normal View History

2021-01-17 21:46:25 +08:00
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
2019-01-01 19:43:31 +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.
2019-01-01 19:43:31 +08:00
2019-01-22 13:50:10 +08:00
// Timer Operations
2019-01-01 19:43:31 +08:00
package gtimer_test
2019-01-01 19:43:31 +08:00
import (
"context"
2019-06-19 09:06:52 +08:00
"testing"
"time"
2019-07-29 21:01:19 +08:00
2021-10-11 21:41:56 +08:00
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/os/gtimer"
"github.com/gogf/gf/v2/test/gtest"
2019-01-01 19:43:31 +08:00
)
2019-01-12 22:47:07 +08:00
func TestTimer_Add_Close(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
timer := gtimer.New()
array := garray.New(true)
2019-06-19 09:06:52 +08:00
//fmt.Println("start", time.Now())
timer.Add(ctx, 200*time.Millisecond, func(ctx context.Context) {
2021-05-15 18:13:51 +08:00
//fmt.Println("job1", time.Now())
2019-06-19 09:06:52 +08:00
array.Append(1)
})
timer.Add(ctx, 200*time.Millisecond, func(ctx context.Context) {
2021-05-15 18:13:51 +08:00
//fmt.Println("job2", time.Now())
2019-06-19 09:06:52 +08:00
array.Append(1)
})
timer.Add(ctx, 400*time.Millisecond, func(ctx context.Context) {
2021-05-15 18:13:51 +08:00
//fmt.Println("job3", time.Now())
2019-06-19 09:06:52 +08:00
array.Append(1)
})
time.Sleep(250 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 2)
2019-06-19 09:06:52 +08:00
time.Sleep(250 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 5)
2019-06-19 09:06:52 +08:00
timer.Close()
time.Sleep(250 * time.Millisecond)
fixedLength := array.Len()
time.Sleep(250 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), fixedLength)
2019-06-19 09:06:52 +08:00
})
2019-01-01 19:43:31 +08:00
}
2019-01-22 13:50:10 +08:00
func TestTimer_Start_Stop_Close(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
timer := gtimer.New()
array := garray.New(true)
timer.Add(ctx, 1000*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(array.Len(), 0)
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
timer.Stop()
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
timer.Start()
time.Sleep(1200 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 2)
2019-06-19 09:06:52 +08:00
timer.Close()
time.Sleep(1200 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 2)
2019-06-19 09:06:52 +08:00
})
2019-01-22 13:50:10 +08:00
}
2021-05-15 18:13:51 +08:00
func TestJob_Reset(t *testing.T) {
2020-07-29 11:22:46 +08:00
gtest.C(t, func(t *gtest.T) {
timer := gtimer.New()
2020-07-30 10:37:48 +08:00
array := garray.New(true)
job := timer.AddSingleton(ctx, 500*time.Millisecond, func(ctx context.Context) {
2021-05-15 18:13:51 +08:00
array.Append(1)
2020-07-29 11:22:46 +08:00
})
2021-05-15 18:13:51 +08:00
time.Sleep(300 * time.Millisecond)
job.Reset()
time.Sleep(300 * time.Millisecond)
job.Reset()
time.Sleep(300 * time.Millisecond)
job.Reset()
time.Sleep(600 * time.Millisecond)
t.Assert(array.Len(), 1)
2020-07-29 11:22:46 +08:00
})
}
2019-01-22 13:50:10 +08:00
func TestTimer_AddSingleton(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
timer := gtimer.New()
array := garray.New(true)
timer.AddSingleton(ctx, 200*time.Millisecond, func(ctx context.Context) {
2019-06-19 09:06:52 +08:00
array.Append(1)
time.Sleep(10 * time.Second)
})
time.Sleep(250 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 1)
2019-06-19 09:06:52 +08:00
time.Sleep(500 * 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-01 19:43:31 +08:00
}
2023-03-20 10:00:55 +08:00
func TestTimer_AddSingletonWithQuick(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
timer := gtimer.New(gtimer.TimerOptions{
Interval: 100 * time.Millisecond,
Quick: true,
})
array := garray.New(true)
timer.AddSingleton(ctx, 5*time.Second, func(ctx context.Context) {
array.Append(1)
time.Sleep(10 * time.Second)
})
time.Sleep(250 * time.Millisecond)
t.Assert(array.Len(), 1)
time.Sleep(500 * time.Millisecond)
t.Assert(array.Len(), 1)
})
}
func TestTimer_AddSingletonWithoutQuick(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
timer := gtimer.New(gtimer.TimerOptions{
Interval: 100 * time.Millisecond,
Quick: false,
})
array := garray.New(true)
timer.AddSingleton(ctx, 5*time.Second, func(ctx context.Context) {
array.Append(1)
time.Sleep(10 * time.Second)
})
time.Sleep(250 * time.Millisecond)
t.Assert(array.Len(), 0)
time.Sleep(500 * time.Millisecond)
t.Assert(array.Len(), 0)
})
}
2019-01-22 13:50:10 +08:00
func TestTimer_AddOnce(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
timer := gtimer.New()
array := garray.New(true)
timer.AddOnce(ctx, 200*time.Millisecond, func(ctx context.Context) {
2019-06-19 09:06:52 +08:00
array.Append(1)
})
timer.AddOnce(ctx, 200*time.Millisecond, func(ctx context.Context) {
2019-06-19 09:06:52 +08:00
array.Append(1)
})
time.Sleep(250 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 2)
2019-06-19 09:06:52 +08:00
time.Sleep(250 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 2)
2019-06-19 09:06:52 +08:00
timer.Close()
time.Sleep(250 * time.Millisecond)
fixedLength := array.Len()
time.Sleep(250 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), fixedLength)
2019-06-19 09:06:52 +08:00
})
2019-01-01 19:43:31 +08:00
}
2019-01-22 13:50:10 +08:00
func TestTimer_AddTimes(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
timer := gtimer.New()
array := garray.New(true)
timer.AddTimes(ctx, 200*time.Millisecond, 2, func(ctx context.Context) {
2019-06-19 09:06:52 +08:00
array.Append(1)
})
time.Sleep(1000 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 2)
2019-06-19 09:06:52 +08:00
})
2019-01-22 13:50:10 +08:00
}
2019-01-12 22:47:07 +08:00
func TestTimer_DelayAdd(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
timer := gtimer.New()
array := garray.New(true)
timer.DelayAdd(ctx, 200*time.Millisecond, 200*time.Millisecond, func(ctx context.Context) {
2019-06-19 09:06:52 +08:00
array.Append(1)
})
time.Sleep(250 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 0)
2019-06-19 09:06:52 +08:00
time.Sleep(250 * 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-01 19:43:31 +08:00
}
2021-05-15 18:13:51 +08:00
func TestTimer_DelayAddJob(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
timer := gtimer.New()
array := garray.New(true)
timer.DelayAddEntry(ctx, 200*time.Millisecond, 200*time.Millisecond, func(ctx context.Context) {
2019-06-19 09:06:52 +08:00
array.Append(1)
2020-12-14 13:26:48 +08:00
}, false, 100, gtimer.StatusReady)
2019-06-19 09:06:52 +08:00
time.Sleep(250 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 0)
2019-06-19 09:06:52 +08:00
time.Sleep(250 * 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-23 11:28:57 +08:00
}
2019-01-22 13:50:10 +08:00
func TestTimer_DelayAddSingleton(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
timer := gtimer.New()
array := garray.New(true)
timer.DelayAddSingleton(ctx, 200*time.Millisecond, 200*time.Millisecond, func(ctx context.Context) {
2019-06-19 09:06:52 +08:00
array.Append(1)
time.Sleep(10 * time.Second)
})
time.Sleep(250 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 0)
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)
2019-06-19 09:06:52 +08:00
})
2019-01-01 19:43:31 +08:00
}
2019-01-22 13:50:10 +08:00
func TestTimer_DelayAddOnce(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
timer := gtimer.New()
array := garray.New(true)
timer.DelayAddOnce(ctx, 200*time.Millisecond, 200*time.Millisecond, func(ctx context.Context) {
2019-06-19 09:06:52 +08:00
array.Append(1)
})
time.Sleep(250 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 0)
2019-06-19 09:06:52 +08:00
time.Sleep(250 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 1)
2019-06-19 09:06:52 +08:00
time.Sleep(500 * 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-01 19:43:31 +08:00
}
2019-01-22 13:50:10 +08:00
func TestTimer_DelayAddTimes(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
timer := gtimer.New()
array := garray.New(true)
timer.DelayAddTimes(ctx, 200*time.Millisecond, 500*time.Millisecond, 2, func(ctx context.Context) {
2019-06-19 09:06:52 +08:00
array.Append(1)
})
time.Sleep(200 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 0)
2019-06-19 09:06:52 +08:00
time.Sleep(600 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 1)
2019-06-19 09:06:52 +08:00
time.Sleep(600 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 2)
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(), 2)
2019-06-19 09:06:52 +08:00
})
2019-01-22 13:50:10 +08:00
}
2019-01-23 11:28:57 +08:00
func TestTimer_AddLessThanInterval(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2021-05-15 18:13:51 +08:00
timer := gtimer.New(gtimer.TimerOptions{
Interval: 100 * time.Millisecond,
})
array := garray.New(true)
timer.Add(ctx, 20*time.Millisecond, func(ctx context.Context) {
2019-06-19 09:06:52 +08:00
array.Append(1)
})
time.Sleep(50 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 0)
2019-06-19 09:06:52 +08:00
time.Sleep(110 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 1)
2019-06-19 09:06:52 +08:00
time.Sleep(110 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 2)
2019-06-19 09:06:52 +08:00
})
2019-01-23 11:28:57 +08:00
}
2021-05-15 18:13:51 +08:00
func TestTimer_AddLeveledJob1(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
timer := gtimer.New()
array := garray.New(true)
timer.DelayAdd(ctx, 1000*time.Millisecond, 1000*time.Millisecond, func(ctx context.Context) {
2019-06-19 09:06:52 +08:00
array.Append(1)
})
time.Sleep(1500 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), 0)
2019-06-19 09:06:52 +08:00
time.Sleep(1300 * 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-23 11:28:57 +08:00
}
2019-01-22 13:50:10 +08:00
func TestTimer_Exit(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
timer := gtimer.New()
array := garray.New(true)
timer.Add(ctx, 200*time.Millisecond, func(ctx context.Context) {
2019-06-19 09:06:52 +08:00
array.Append(1)
gtimer.Exit()
})
time.Sleep(1000 * 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-01 19:43:31 +08:00
}