2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2019-01-23 11:28:57 +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,
|
2019-02-02 16:18:25 +08:00
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2019-01-23 11:28:57 +08:00
|
|
|
|
|
|
|
// Package functions
|
|
|
|
|
|
|
|
package gtimer_test
|
|
|
|
|
|
|
|
import (
|
2019-06-19 09:06:52 +08:00
|
|
|
"testing"
|
|
|
|
"time"
|
2019-07-29 21:01:19 +08:00
|
|
|
|
|
|
|
"github.com/gogf/gf/container/garray"
|
|
|
|
"github.com/gogf/gf/os/gtimer"
|
|
|
|
"github.com/gogf/gf/test/gtest"
|
2019-01-23 11:28:57 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSetTimeout(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-23 23:20:27 +08:00
|
|
|
array := garray.New(true)
|
2019-06-19 09:06:52 +08:00
|
|
|
gtimer.SetTimeout(200*time.Millisecond, func() {
|
|
|
|
array.Append(1)
|
|
|
|
})
|
|
|
|
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-23 11:28:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetInterval(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-23 23:20:27 +08:00
|
|
|
array := garray.New(true)
|
2021-01-18 21:20:10 +08:00
|
|
|
gtimer.SetInterval(300*time.Millisecond, func() {
|
2019-06-19 09:06:52 +08:00
|
|
|
array.Append(1)
|
|
|
|
})
|
2021-01-18 21:20:10 +08:00
|
|
|
time.Sleep(1000 * time.Millisecond)
|
|
|
|
t.Assert(array.Len(), 3)
|
2019-06-19 09:06:52 +08:00
|
|
|
})
|
2019-01-23 11:28:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddEntry(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-23 23:20:27 +08:00
|
|
|
array := garray.New(true)
|
2019-06-19 09:06:52 +08:00
|
|
|
gtimer.AddEntry(200*time.Millisecond, func() {
|
|
|
|
array.Append(1)
|
2020-12-14 13:26:48 +08:00
|
|
|
}, false, 2, gtimer.StatusReady)
|
2019-06-19 09:06:52 +08:00
|
|
|
time.Sleep(1100 * 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
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddSingleton(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-23 23:20:27 +08:00
|
|
|
array := garray.New(true)
|
2019-06-19 09:06:52 +08:00
|
|
|
gtimer.AddSingleton(200*time.Millisecond, func() {
|
|
|
|
array.Append(1)
|
|
|
|
time.Sleep(10000 * time.Millisecond)
|
|
|
|
})
|
|
|
|
time.Sleep(1100 * 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
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddTimes(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-23 23:20:27 +08:00
|
|
|
array := garray.New(true)
|
2019-06-19 09:06:52 +08:00
|
|
|
gtimer.AddTimes(200*time.Millisecond, 2, func() {
|
|
|
|
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-23 11:28:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDelayAdd(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-23 23:20:27 +08:00
|
|
|
array := garray.New(true)
|
2021-01-18 21:20:10 +08:00
|
|
|
gtimer.DelayAdd(500*time.Millisecond, 500*time.Millisecond, func() {
|
2019-06-19 09:06:52 +08:00
|
|
|
array.Append(1)
|
|
|
|
})
|
2021-01-18 21:20:10 +08:00
|
|
|
time.Sleep(600 * time.Millisecond)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(array.Len(), 0)
|
2021-01-18 21:20:10 +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
|
|
|
})
|
2019-01-23 11:28:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDelayAddEntry(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-23 23:20:27 +08:00
|
|
|
array := garray.New(true)
|
2019-06-19 09:06:52 +08:00
|
|
|
gtimer.DelayAddEntry(200*time.Millisecond, 200*time.Millisecond, func() {
|
|
|
|
array.Append(1)
|
2020-12-14 13:26:48 +08:00
|
|
|
}, false, 2, gtimer.StatusReady)
|
2019-06-19 09:06:52 +08:00
|
|
|
time.Sleep(300 * 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(), 2)
|
2019-06-19 09:06:52 +08:00
|
|
|
})
|
2019-01-23 11:28:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDelayAddSingleton(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-23 23:20:27 +08:00
|
|
|
array := garray.New(true)
|
2019-06-19 09:06:52 +08:00
|
|
|
gtimer.DelayAddSingleton(200*time.Millisecond, 200*time.Millisecond, func() {
|
|
|
|
array.Append(1)
|
|
|
|
time.Sleep(10000 * time.Millisecond)
|
|
|
|
})
|
|
|
|
time.Sleep(300 * 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-23 11:28:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDelayAddOnce(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-23 23:20:27 +08:00
|
|
|
array := garray.New(true)
|
2019-06-19 09:06:52 +08:00
|
|
|
gtimer.DelayAddOnce(200*time.Millisecond, 200*time.Millisecond, func() {
|
|
|
|
array.Append(1)
|
|
|
|
})
|
|
|
|
time.Sleep(300 * 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-23 11:28:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDelayAddTimes(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-23 23:20:27 +08:00
|
|
|
array := garray.New(true)
|
2019-06-19 09:06:52 +08:00
|
|
|
gtimer.DelayAddTimes(200*time.Millisecond, 200*time.Millisecond, 2, func() {
|
|
|
|
array.Append(1)
|
|
|
|
})
|
|
|
|
time.Sleep(300 * 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(), 2)
|
2019-06-19 09:06:52 +08:00
|
|
|
})
|
2019-01-23 11:28:57 +08:00
|
|
|
}
|