gf/g/os/gtimer/gtimer_z_unit_2_test.go

56 lines
1.4 KiB
Go
Raw Normal View History

2018-12-30 14:53:16 +08:00
// Copyright 2018 gf Author(https://gitee.com/johng/gf). 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://gitee.com/johng/gf.
// Entry操作
package gtimer_test
2018-12-30 14:53:16 +08:00
import (
"gitee.com/johng/gf/g/container/garray"
"gitee.com/johng/gf/g/util/gtest"
"testing"
"time"
)
2019-01-12 22:47:07 +08:00
func TestTimer_Entry_Operation(t *testing.T) {
2019-01-11 13:46:40 +08:00
wheel := New()
2019-01-09 13:26:59 +08:00
array := garray.New(0, 0)
entry := wheel.Add(time.Second, func() {
2018-12-30 14:53:16 +08:00
array.Append(1)
})
2019-01-01 19:43:31 +08:00
time.Sleep(1200*time.Millisecond)
2018-12-30 14:53:16 +08:00
gtest.Assert(array.Len(), 1)
2019-01-03 19:11:54 +08:00
entry.Close()
2019-01-01 19:43:31 +08:00
time.Sleep(1200*time.Millisecond)
2018-12-30 14:53:16 +08:00
gtest.Assert(array.Len(), 1)
}
2019-01-12 22:47:07 +08:00
func TestTimer_Entry_Singleton(t *testing.T) {
2019-01-11 13:46:40 +08:00
wheel := New()
2019-01-03 19:11:54 +08:00
array := garray.New(0, 0)
2019-01-09 13:26:59 +08:00
entry := wheel.Add(time.Second, func() {
2018-12-30 14:53:16 +08:00
array.Append(1)
time.Sleep(10*time.Second)
})
2019-01-03 19:11:54 +08:00
entry.SetSingleton(true)
2019-01-01 19:43:31 +08:00
time.Sleep(1200*time.Millisecond)
2018-12-30 14:53:16 +08:00
gtest.Assert(array.Len(), 1)
2019-01-01 19:43:31 +08:00
time.Sleep(1200*time.Millisecond)
2018-12-30 14:53:16 +08:00
gtest.Assert(array.Len(), 1)
}
2019-01-12 22:47:07 +08:00
func TestTimer_Entry_Once(t *testing.T) {
2019-01-11 13:46:40 +08:00
wheel := New()
2019-01-09 13:26:59 +08:00
array := garray.New(0, 0)
entry := wheel.Add(time.Second, func() {
2018-12-30 14:53:16 +08:00
array.Append(1)
})
2019-01-03 19:11:54 +08:00
entry.SetTimes(1)
2019-01-01 19:43:31 +08:00
time.Sleep(1200*time.Millisecond)
2018-12-30 14:53:16 +08:00
gtest.Assert(array.Len(), 1)
}