2019-02-02 16:18:25 +08:00
|
|
|
// 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,
|
2019-02-02 16:18:25 +08:00
|
|
|
// 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
|
|
|
|
2019-01-12 22:41:12 +08:00
|
|
|
package gtimer_test
|
2018-12-30 14:53:16 +08:00
|
|
|
|
|
|
|
import (
|
2019-02-02 16:18:25 +08:00
|
|
|
"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()
|
2019-02-01 22:00:58 +08:00
|
|
|
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)
|
2019-01-17 14:15:23 +08:00
|
|
|
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)
|
2019-01-17 14:15:23 +08:00
|
|
|
entry.Start()
|
2019-01-23 11:28:57 +08:00
|
|
|
time.Sleep(250*time.Millisecond)
|
2019-01-17 14:15:23 +08:00
|
|
|
gtest.Assert(array.Len(), 2)
|
|
|
|
entry.Close()
|
2019-01-23 11:28:57 +08:00
|
|
|
time.Sleep(250*time.Millisecond)
|
2019-01-17 14:15:23 +08:00
|
|
|
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()
|
2019-02-01 22:00:58 +08:00
|
|
|
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()
|
2019-02-01 22:00:58 +08:00
|
|
|
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()
|
2019-02-01 22:00:58 +08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|