Files
gf/os/gtimer/gtimer_z_unit_internal_test.go

86 lines
2.0 KiB
Go
Raw Permalink Normal View History

2021-01-18 21:20:10 +08:00
// 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 gtimer
import (
"context"
2021-01-18 21:20:10 +08:00
"testing"
"time"
2021-11-15 20:49:02 +08:00
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/test/gtest"
2021-01-18 21:20:10 +08:00
)
2021-01-19 11:27:23 +08:00
func TestTimer_Proceed(t *testing.T) {
2021-01-19 00:12:22 +08:00
gtest.C(t, func(t *gtest.T) {
array := garray.New(true)
2021-05-15 18:13:51 +08:00
timer := New(TimerOptions{
Interval: time.Hour,
2021-01-19 00:12:22 +08:00
})
timer.Add(ctx, 10000*time.Hour, func(ctx context.Context) {
2021-05-15 18:13:51 +08:00
array.Append(1)
2021-01-19 00:12:22 +08:00
})
2021-05-15 18:13:51 +08:00
timer.proceed(10001)
time.Sleep(10 * time.Millisecond)
t.Assert(array.Len(), 1)
timer.proceed(20001)
time.Sleep(10 * time.Millisecond)
t.Assert(array.Len(), 2)
})
gtest.C(t, func(t *gtest.T) {
array := garray.New(true)
timer := New(TimerOptions{
Interval: time.Millisecond * 100,
2021-01-19 00:12:22 +08:00
})
timer.Add(ctx, 10000*time.Hour, func(ctx context.Context) {
2021-05-15 18:13:51 +08:00
array.Append(1)
2021-01-19 00:12:22 +08:00
})
2021-05-15 18:13:51 +08:00
ticks := int64((10000 * time.Hour) / (time.Millisecond * 100))
timer.proceed(ticks + 1)
time.Sleep(10 * time.Millisecond)
t.Assert(array.Len(), 1)
timer.proceed(2*ticks + 1)
time.Sleep(10 * time.Millisecond)
t.Assert(array.Len(), 2)
2021-01-19 00:12:22 +08:00
})
2021-01-18 21:20:10 +08:00
}
func TestTimer_PriorityQueue(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
queue := newPriorityQueue()
queue.Push(1, 1)
queue.Push(4, 4)
queue.Push(5, 5)
queue.Push(2, 2)
queue.Push(3, 3)
t.Assert(queue.Pop(), 1)
t.Assert(queue.Pop(), 2)
t.Assert(queue.Pop(), 3)
t.Assert(queue.Pop(), 4)
t.Assert(queue.Pop(), 5)
})
}
func TestTimer_PriorityQueue_FirstOneInArrayIsTheLeast(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
size = 1000000
array = garray.NewIntArrayRange(0, size, 1)
)
array.Shuffle()
queue := newPriorityQueue()
array.Iterator(func(k int, v int) bool {
queue.Push(v, int64(v))
return true
})
for i := 0; i < size; i++ {
t.Assert(queue.Pop(), i)
t.Assert(queue.heap.array[0].priority, i+1)
}
})
}