gf/g/os/gtimer/gtimer_z_unit_2_test.go

78 lines
1.9 KiB
Go
Raw Normal View History

// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
2018-12-30 14:53:16 +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.
2018-12-30 14:53:16 +08:00
2019-01-22 13:50:10 +08:00
// Entry Operations
2018-12-30 14:53:16 +08:00
package gtimer_test
2018-12-30 14:53:16 +08:00
import (
"github.com/gogf/gf/g/container/garray"
"github.com/gogf/gf/g/os/gtimer"
"github.com/gogf/gf/g/test/gtest"
2018-12-30 14:53:16 +08:00
"testing"
"time"
)
2019-01-22 13:50:10 +08:00
func TestEntry_Start_Stop_Close(t *testing.T) {
timer := New()
array := garray.New()
2019-01-23 11:28:57 +08:00
entry := timer.Add(200*time.Millisecond, func() {
2018-12-30 14:53:16 +08:00
array.Append(1)
})
2019-01-23 11:28:57 +08:00
time.Sleep(250*time.Millisecond)
2018-12-30 14:53:16 +08:00
gtest.Assert(array.Len(), 1)
entry.Stop()
2019-01-23 11:28:57 +08:00
time.Sleep(250*time.Millisecond)
2018-12-30 14:53:16 +08:00
gtest.Assert(array.Len(), 1)
entry.Start()
2019-01-23 11:28:57 +08:00
time.Sleep(250*time.Millisecond)
gtest.Assert(array.Len(), 2)
entry.Close()
2019-01-23 11:28:57 +08:00
time.Sleep(250*time.Millisecond)
gtest.Assert(array.Len(), 2)
2019-01-22 13:50:10 +08:00
gtest.Assert(entry.Status(), gtimer.STATUS_CLOSED)
2018-12-30 14:53:16 +08:00
}
2019-01-22 13:50:10 +08:00
func TestEntry_Singleton(t *testing.T) {
timer := New()
array := garray.New()
2019-01-23 11:28:57 +08:00
entry := timer.Add(200*time.Millisecond, func() {
2018-12-30 14:53:16 +08:00
array.Append(1)
time.Sleep(10*time.Second)
})
2019-01-22 13:50:10 +08:00
gtest.Assert(entry.IsSingleton(), false)
2019-01-03 19:11:54 +08:00
entry.SetSingleton(true)
2019-01-22 13:50:10 +08:00
gtest.Assert(entry.IsSingleton(), true)
2019-01-23 11:28:57 +08:00
time.Sleep(250*time.Millisecond)
2018-12-30 14:53:16 +08:00
gtest.Assert(array.Len(), 1)
2019-01-23 11:28:57 +08:00
time.Sleep(250*time.Millisecond)
2018-12-30 14:53:16 +08:00
gtest.Assert(array.Len(), 1)
}
2019-01-22 13:50:10 +08:00
func TestEntry_SetTimes(t *testing.T) {
timer := New()
array := garray.New()
2019-01-22 13:50:10 +08:00
entry := timer.Add(200*time.Millisecond, func() {
2018-12-30 14:53:16 +08:00
array.Append(1)
})
2019-01-22 13:50:10 +08:00
entry.SetTimes(2)
2019-01-01 19:43:31 +08:00
time.Sleep(1200*time.Millisecond)
2019-01-22 13:50:10 +08:00
gtest.Assert(array.Len(), 2)
2018-12-30 14:53:16 +08:00
}
2019-01-22 13:50:10 +08:00
2019-01-23 11:28:57 +08:00
func TestEntry_Run(t *testing.T) {
timer := New()
array := garray.New()
2019-01-23 11:28:57 +08:00
entry := timer.Add(1000*time.Millisecond, func() {
array.Append(1)
})
entry.Run()
gtest.Assert(array.Len(), 1)
}