Skip to content

Commit

Permalink
更新
Browse files Browse the repository at this point in the history
Signed-off-by: guonaihong <guonaihong@qq.com>
  • Loading branch information
guonaihong committed Feb 16, 2024
1 parent cdf2966 commit 842da42
Show file tree
Hide file tree
Showing 13 changed files with 158 additions and 10 deletions.
3 changes: 3 additions & 0 deletions min_heap.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright 2020-2024 guonaihong, antlabs. All rights reserved.
//
// mit license
package timer

import (
Expand Down
3 changes: 3 additions & 0 deletions min_heap_node.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright 2020-2024 guonaihong, antlabs. All rights reserved.
//
// mit license
package timer

import (
Expand Down
4 changes: 4 additions & 0 deletions min_heap_node_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Copyright 2020-2024 guonaihong, antlabs. All rights reserved.
//
// mit license

package timer

import (
Expand Down
3 changes: 3 additions & 0 deletions min_heap_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright 2020-2024 guonaihong, antlabs. All rights reserved.
//
// mit license
package timer

import (
Expand Down
7 changes: 5 additions & 2 deletions option.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright 2020-2024 guonaihong, antlabs. All rights reserved.
//
// mit license
package timer

type option struct {
Expand All @@ -21,14 +24,14 @@ func WithMinHeap() Option {
}
}

//TODO
// TODO
func WithSkipList() Option {
return func(o *option) {
o.skiplist = true
}
}

//TODO
// TODO
func WithRbtree() Option {
return func(o *option) {
o.rbtree = true
Expand Down
3 changes: 3 additions & 0 deletions t_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright 2020-2024 guonaihong, antlabs. All rights reserved.
//
// mit license
package timer

import (
Expand Down
3 changes: 3 additions & 0 deletions time_wheel.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright 2020-2024 guonaihong, antlabs. All rights reserved.
//
// mit license
package timer

import (
Expand Down
5 changes: 4 additions & 1 deletion time_wheel_node.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright 2020-2024 guonaihong, antlabs. All rights reserved.
//
// mit license
package timer

import (
Expand Down Expand Up @@ -105,5 +108,5 @@ func (t *timeNode) Reset(expire time.Duration) {
expire = expire/(time.Millisecond*10) + time.Duration(jiffies)
t.expire = uint64(expire)

t.root.add(t, atomic.LoadUint64(&t.root.jiffies))
t.root.add(t, jiffies)
}
3 changes: 3 additions & 0 deletions time_wheel_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright 2020-2024 guonaihong, antlabs. All rights reserved.
//
// mit license
package timer

import (
Expand Down
3 changes: 3 additions & 0 deletions time_wheel_utils.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright 2020-2024 guonaihong, antlabs. All rights reserved.
//
// mit license
package timer

import "time"
Expand Down
6 changes: 5 additions & 1 deletion timer.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright 2020-2024 guonaihong, antlabs. All rights reserved.
//
// mit license
package timer

import "time"
Expand Down Expand Up @@ -27,7 +30,8 @@ type Timer interface {
// 停止单个定时器
type TimeNoder interface {
Stop()
Reset(d time.Duration)
// 重置时间器
Reset(expire time.Duration)
}

// 定时器构造函数
Expand Down
122 changes: 116 additions & 6 deletions timer_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
// Copyright 2020-2024 guonaihong, antlabs. All rights reserved.
//
// mit license
package timer

import (
"log"
"sync"
"sync/atomic"
"testing"
"time"
Expand Down Expand Up @@ -34,7 +38,7 @@ func Test_ScheduleFunc(t *testing.T) {

func Test_AfterFunc(t *testing.T) {
tm := NewTimer()

go tm.Run()
log.Printf("start\n")

count := uint32(0)
Expand All @@ -60,11 +64,9 @@ func Test_AfterFunc(t *testing.T) {
tm.AfterFunc(time.Hour*24*365*12, nil)
*/

go func() {
time.Sleep(time.Second + time.Millisecond*100)
tm.Stop()
}()
tm.Run()
time.Sleep(time.Second + time.Millisecond*100)
tm.Stop()

if count != 2 {
t.Errorf("count:%d != 2\n", count)
}
Expand Down Expand Up @@ -107,3 +109,111 @@ func Test_Node_Stop(t *testing.T) {
}

}

// 测试重置定时器
func Test_Reset(t *testing.T) {
t.Run("min heap reset", func(t *testing.T) {

tm := NewTimer(WithMinHeap())

go tm.Run()
count := int32(0)

tc := make(chan time.Duration, 2)

var mu sync.Mutex
isClose := false
now := time.Now()
node1 := tm.AfterFunc(time.Millisecond*100, func() {

mu.Lock()
atomic.AddInt32(&count, 1)
if atomic.LoadInt32(&count) <= 2 && !isClose {
tc <- time.Since(now)
}
mu.Unlock()
})

node2 := tm.AfterFunc(time.Millisecond*100, func() {
mu.Lock()
atomic.AddInt32(&count, 1)
if atomic.LoadInt32(&count) <= 2 && !isClose {
tc <- time.Since(now)
}
mu.Unlock()
})
node1.Reset(time.Millisecond)
node2.Reset(time.Millisecond)

time.Sleep(time.Millisecond * 3)
mu.Lock()
isClose = true
close(tc)
node1.Stop()
node2.Stop()
mu.Unlock()
for tv := range tc {
if tv < time.Millisecond || tv > 2*time.Millisecond {
t.Errorf("tc < time.Millisecond tc > 2*time.Millisecond")

}
}
if atomic.LoadInt32(&count) != 2 {
t.Errorf("count:%d != 2", atomic.LoadInt32(&count))
}

})

t.Run("time wheel reset", func(t *testing.T) {
tm := NewTimer()

go func() {
tm.Run()
}()

count := int32(0)

tc := make(chan time.Duration, 2)

var mu sync.Mutex
isClose := false
now := time.Now()
node1 := tm.AfterFunc(time.Millisecond*10, func() {

mu.Lock()
atomic.AddInt32(&count, 1)
if atomic.LoadInt32(&count) <= 2 && !isClose {
tc <- time.Since(now)
}
mu.Unlock()
})

node2 := tm.AfterFunc(time.Millisecond*10, func() {
mu.Lock()
atomic.AddInt32(&count, 1)
if atomic.LoadInt32(&count) <= 2 && !isClose {
tc <- time.Since(now)
}
mu.Unlock()
})

node1.Reset(time.Millisecond * 20)
node2.Reset(time.Millisecond * 20)

time.Sleep(time.Millisecond * 40)
mu.Lock()
isClose = true
close(tc)
node1.Stop()
node2.Stop()
mu.Unlock()
for tv := range tc {
if tv < time.Millisecond*20 || tv > 2*time.Millisecond*20 {
t.Errorf("tc < time.Millisecond tc > 2*time.Millisecond")
}
}
if atomic.LoadInt32(&count) != 2 {
t.Errorf("count:%d != 2", atomic.LoadInt32(&count))
}
})
}
3 changes: 3 additions & 0 deletions timer_wheel_utils_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright 2020-2024 guonaihong, antlabs. All rights reserved.
//
// mit license
package timer

import (
Expand Down

0 comments on commit 842da42

Please sign in to comment.