From 6cc47479650a93353cc44300ebbe65f0117b6c71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=B3=96=E6=B0=B4=E4=B8=8D=E5=8A=A0=E7=B3=96?= <641008175@qq.com> Date: Wed, 29 Jul 2020 11:22:46 +0800 Subject: [PATCH 1/3] add func gtimer.Entry.Reset() --- os/gtimer/gtimer.go | 1 + os/gtimer/gtimer_entry.go | 7 +++++++ os/gtimer/gtimer_loop.go | 4 ++++ os/gtimer/gtimer_z_unit_1_test.go | 14 ++++++++++++++ 4 files changed, 26 insertions(+) diff --git a/os/gtimer/gtimer.go b/os/gtimer/gtimer.go index c938b946f..0f146be3c 100644 --- a/os/gtimer/gtimer.go +++ b/os/gtimer/gtimer.go @@ -31,6 +31,7 @@ const ( STATUS_READY = 0 // Job is ready for running. STATUS_RUNNING = 1 // Job is already running. STATUS_STOPPED = 2 // Job is stopped. + STATUS_RESET = 3 // Job is reset. STATUS_CLOSED = -1 // Job is closed and waiting to be deleted. gPANIC_EXIT = "exit" // Internal usage for custom job exit function with panic. gDEFAULT_TIMES = math.MaxInt32 // Default limit running times, a big number. diff --git a/os/gtimer/gtimer_entry.go b/os/gtimer/gtimer_entry.go index 3d1cfe763..3d296f012 100644 --- a/os/gtimer/gtimer_entry.go +++ b/os/gtimer/gtimer_entry.go @@ -106,6 +106,11 @@ func (entry *Entry) Stop() { entry.status.Set(STATUS_STOPPED) } +//Reset reset the job. +func (entry *Entry) Reset() { + entry.status.Set(STATUS_RESET) +} + // Close closes the job, and then it will be removed from the timer. func (entry *Entry) Close() { entry.status.Set(STATUS_CLOSED) @@ -138,6 +143,8 @@ func (entry *Entry) check(nowTicks int64, nowMs int64) (runnable, addable bool) return false, true case STATUS_CLOSED: return false, false + case STATUS_RESET: + return false, true } // Firstly checks using the ticks, this may be low precision as one tick is a little bit long. if diff := nowTicks - entry.create; diff > 0 && diff%entry.interval == 0 { diff --git a/os/gtimer/gtimer_loop.go b/os/gtimer/gtimer_loop.go index 5dc652fc2..9fac8add0 100644 --- a/os/gtimer/gtimer_loop.go +++ b/os/gtimer/gtimer_loop.go @@ -74,6 +74,10 @@ func (w *wheel) proceed() { } // If rolls on the job. if addable { + //If STATUS_RESET , reset to runnable state. + if entry.Status() == STATUS_RESET { + entry.SetStatus(STATUS_READY) + } entry.wheel.timer.doAddEntryByParent(entry.rawIntervalMs, entry) } } diff --git a/os/gtimer/gtimer_z_unit_1_test.go b/os/gtimer/gtimer_z_unit_1_test.go index 1aadd161e..5a57651fb 100644 --- a/os/gtimer/gtimer_z_unit_1_test.go +++ b/os/gtimer/gtimer_z_unit_1_test.go @@ -9,6 +9,7 @@ package gtimer_test import ( + "github.com/gogf/gf/os/glog" "testing" "time" @@ -73,6 +74,19 @@ func TestTimer_Start_Stop_Close(t *testing.T) { }) } +func TestTimer_Reset(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + glog.Printf("start time:%d", time.Now().Unix()) + singleton := timer.AddSingleton(2*time.Second, func() { + glog.Println(time.Now().Unix()) + }) + time.Sleep(5 * time.Second) + glog.Printf("reset time:%d", time.Now().Unix()) + singleton.Reset() + select {} + }) +} + func TestTimer_AddSingleton(t *testing.T) { gtest.C(t, func(t *gtest.T) { timer := New() From 5d24f702be27830b438945a470cedba2fa7f5393 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=B3=96=E6=B0=B4=E4=B8=8D=E5=8A=A0=E7=B3=96?= <641008175@qq.com> Date: Thu, 30 Jul 2020 10:37:48 +0800 Subject: [PATCH 2/3] adjust gtimer.Entry.Reset() Test Code. --- os/gtimer/gtimer_z_unit_1_test.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/os/gtimer/gtimer_z_unit_1_test.go b/os/gtimer/gtimer_z_unit_1_test.go index 5a57651fb..09e595dde 100644 --- a/os/gtimer/gtimer_z_unit_1_test.go +++ b/os/gtimer/gtimer_z_unit_1_test.go @@ -76,14 +76,18 @@ func TestTimer_Start_Stop_Close(t *testing.T) { func TestTimer_Reset(t *testing.T) { gtest.C(t, func(t *gtest.T) { + array := garray.New(true) glog.Printf("start time:%d", time.Now().Unix()) singleton := timer.AddSingleton(2*time.Second, func() { - glog.Println(time.Now().Unix()) + timestamp := time.Now().Unix() + glog.Println(timestamp) + array.Append(timestamp) }) time.Sleep(5 * time.Second) glog.Printf("reset time:%d", time.Now().Unix()) singleton.Reset() - select {} + time.Sleep(10 * time.Second) + t.Assert(array.Len(), 6) }) } From 9e1fb93e08ddbf952c0ad50ea37ae920adef6d5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=B3=96=E6=B0=B4=E4=B8=8D=E5=8A=A0=E7=B3=96?= <641008175@qq.com> Date: Thu, 30 Jul 2020 11:00:19 +0800 Subject: [PATCH 3/3] adjust gtimer.Entry.Reset() Test Code. --- os/gtimer/gtimer_z_unit_1_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/os/gtimer/gtimer_z_unit_1_test.go b/os/gtimer/gtimer_z_unit_1_test.go index 09e595dde..00010dc05 100644 --- a/os/gtimer/gtimer_z_unit_1_test.go +++ b/os/gtimer/gtimer_z_unit_1_test.go @@ -76,6 +76,7 @@ func TestTimer_Start_Stop_Close(t *testing.T) { func TestTimer_Reset(t *testing.T) { gtest.C(t, func(t *gtest.T) { + timer := New() array := garray.New(true) glog.Printf("start time:%d", time.Now().Unix()) singleton := timer.AddSingleton(2*time.Second, func() {