Skip to content

Commit

Permalink
去除依赖 (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
guonaihong authored Feb 2, 2024
1 parent f3de2a2 commit 8c8bf64
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 62 deletions.
12 changes: 1 addition & 11 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,4 @@ module github.com/antlabs/timer

go 1.19

require (
github.com/antlabs/stl v0.0.1
github.com/stretchr/testify v1.6.1
gopkg.in/go-playground/assert.v1 v1.2.1
)

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.0 // indirect
)
require github.com/antlabs/stl v0.0.2
19 changes: 2 additions & 17 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,17 +1,2 @@
github.com/antlabs/stl v0.0.1 h1:TRD3csCrjREeLhLoQ/supaoCvFhNLBTNIwuRGrDIs6Q=
github.com/antlabs/stl v0.0.1/go.mod h1:wvVwP1loadLG3cRjxUxK8RL4Co5xujGaZlhbztmUEqQ=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/go-playground/assert.v1 v1.2.1 h1:xoYuJVE7KT85PYWrN730RguIQO0ePzVRfFMXadIrXTM=
gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0 h1:hjy8E9ON/egN1tAYqKb61G10WtihqetD4sz2H+8nIeA=
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
github.com/antlabs/stl v0.0.2 h1:sna1AXR5yIkNE9lWhCcKbheFJSVfCa3vugnGyakI79s=
github.com/antlabs/stl v0.0.2/go.mod h1:kKrO4xrn9cfS1mJVo+/BqePZjAYMXqD0amGF2Ouq7ac=
55 changes: 42 additions & 13 deletions min_heap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"sync/atomic"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

// 测试AfterFunc有没有运行以及时间间隔可对
Expand All @@ -31,10 +29,14 @@ func Test_MinHeap_AfterFunc_Run(t *testing.T) {
close(tc)
for tv := range tc {
if tv < time.Millisecond || tv > 2*time.Millisecond {
assert.Fail(t, "tc < time.Millisecond tc > 2*time.Millisecond")
t.Errorf("tc < time.Millisecond tc > 2*time.Millisecond")

}
}
assert.Equal(t, atomic.LoadInt32(&count), int32(2))
if atomic.LoadInt32(&count) != 2 {
t.Errorf("count != 2")
}

})

t.Run("10ms", func(t *testing.T) {
Expand Down Expand Up @@ -63,7 +65,10 @@ func Test_MinHeap_AfterFunc_Run(t *testing.T) {
}
// cnt++
}
assert.Equal(t, atomic.LoadInt32(&count), int32(2))
if atomic.LoadInt32(&count) != 2 {
t.Errorf("count != 2")
}

})

t.Run("90ms", func(t *testing.T) {
Expand All @@ -74,7 +79,10 @@ func Test_MinHeap_AfterFunc_Run(t *testing.T) {
tm.AfterFunc(time.Millisecond*90, func() { atomic.AddInt32(&count, 2) })

time.Sleep(time.Millisecond * 180)
assert.Equal(t, atomic.LoadInt32(&count), int32(3))
if atomic.LoadInt32(&count) != 3 {
t.Errorf("count != 3")
}

})
}

Expand All @@ -99,7 +107,10 @@ func Test_MinHeap_ScheduleFunc_Run(t *testing.T) {
}()

time.Sleep(time.Millisecond * 5)
assert.Equal(t, atomic.LoadInt32(&count), int32(2))
if atomic.LoadInt32(&count) != 2 {
t.Errorf("count != 2")
}

})

t.Run("10ms", func(t *testing.T) {
Expand Down Expand Up @@ -133,7 +144,11 @@ func Test_MinHeap_ScheduleFunc_Run(t *testing.T) {
}
cnt++
}
assert.Equal(t, atomic.LoadInt32(&count), int32(2))

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

})

t.Run("30ms", func(t *testing.T) {
Expand All @@ -154,7 +169,10 @@ func Test_MinHeap_ScheduleFunc_Run(t *testing.T) {
}()

time.Sleep(time.Millisecond * 70)
assert.Equal(t, atomic.LoadInt32(&count), int32(2))
if atomic.LoadInt32(&count) != 2 {
t.Errorf("count != 2")
}

})
}

Expand All @@ -170,7 +188,9 @@ func Test_Run_Stop(t *testing.T) {
tm.Stop()
}()
tm.Run()
assert.Equal(t, atomic.LoadUint32(&count), uint32(2))
if atomic.LoadUint32(&count) != 2 {
t.Errorf("count != 2")
}
})
}

Expand Down Expand Up @@ -219,8 +239,14 @@ func Test_CustomFunc(t *testing.T) {
}
cnt++
}
assert.Equal(t, atomic.LoadUint32(&count), uint32(2))
assert.Equal(t, mh.runCount, uint32(1))
if atomic.LoadUint32(&count) != 2 {
t.Errorf("count != 2")
}

if mh.runCount != uint32(1) {
t.Errorf("mh.runCount != 1")
}

})
}

Expand All @@ -242,6 +268,9 @@ func Test_RunCount(t *testing.T) {

time.Sleep(time.Millisecond * 15)
tm.Stop()
assert.Equal(t, count, uint32(max))
if count != uint32(max) {
t.Errorf("count != %d", max)
}

})
}
57 changes: 42 additions & 15 deletions time_wheel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,43 @@ import (
"sync/atomic"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func Test_maxVal(t *testing.T) {
assert.Equal(t, maxVal(), uint64(math.MaxUint32))

if maxVal() != uint64(math.MaxUint32) {
t.Error("maxVal() != uint64(math.MaxUint32)")
}
}

func Test_LevelMax(t *testing.T) {
assert.Equal(t, levelMax(1), uint64(1<<(nearShift+levelShift)))
assert.Equal(t, levelMax(2), uint64(1<<(nearShift+2*levelShift)))
assert.Equal(t, levelMax(3), uint64(1<<(nearShift+3*levelShift)))
assert.Equal(t, levelMax(4), uint64(1<<(nearShift+4*levelShift)))
if levelMax(1) != uint64(1<<(nearShift+levelShift)) {
t.Error("levelMax(1) != uint64(1<<(nearShift+levelShift))")
}

if levelMax(2) != uint64(1<<(nearShift+2*levelShift)) {
t.Error("levelMax(2) != uint64(1<<(nearShift+2*levelShift))")
}

if levelMax(3) != uint64(1<<(nearShift+3*levelShift)) {
t.Error("levelMax(3) != uint64(1<<(nearShift+3*levelShift))")
}

if levelMax(4) != uint64(1<<(nearShift+4*levelShift)) {
t.Error("levelMax(4) != uint64(1<<(nearShift+4*levelShift))")
}

}

func Test_GenVersion(t *testing.T) {
assert.Equal(t, genVersionHeight(1, 0xf), uint64(0x0001000f00000000))
assert.Equal(t, genVersionHeight(1, 64), uint64(0x0001004000000000))
if genVersionHeight(1, 0xf) != uint64(0x0001000f00000000) {
t.Error("genVersionHeight(1, 0xf) != uint64(0x0001000f00000000)")
}

if genVersionHeight(1, 64) != uint64(0x0001004000000000) {
t.Error("genVersionHeight(2, 0xf) != uint64(0x0001004000000000)")
}

}

// 测试1小时
Expand All @@ -49,7 +68,11 @@ func Test_hour(t *testing.T) {
case <-done:
case <-time.After(time.Second / 100):
}
assert.True(t, *testHour)

if *testHour == false {
t.Error("testHour == false")
}

}

// 测试周期性定时器, 5s
Expand Down Expand Up @@ -84,8 +107,9 @@ func Test_ScheduleFunc_5s(t *testing.T) {
case <-time.After(time.Second / 100):
}

assert.Equal(t, total, first5)

if total != first5 {
t.Errorf("total:%d != first5:%d\n", total, first5)
}
}

// 测试周期性定时器, 1hour
Expand Down Expand Up @@ -119,7 +143,9 @@ func Test_ScheduleFunc_hour(t *testing.T) {
case <-time.After(time.Second / 100):
}

assert.Equal(t, total, first5)
if total != first5 {
t.Errorf("total:%d != first5:%d\n", total, first5)
}

}

Expand Down Expand Up @@ -154,6 +180,7 @@ func Test_ScheduleFunc_day(t *testing.T) {
case <-time.After(time.Second / 100):
}

assert.Equal(t, total, first5)

if total != first5 {
t.Errorf("total:%d != first5:%d\n", total, first5)
}
}
21 changes: 15 additions & 6 deletions timer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"sync/atomic"
"testing"
"time"

"gopkg.in/go-playground/assert.v1"
)

func Test_ScheduleFunc(t *testing.T) {
Expand All @@ -28,7 +26,10 @@ func Test_ScheduleFunc(t *testing.T) {
}()

tm.Run()
assert.Equal(t, atomic.LoadUint32(&count), uint32(5))
if count != 5 {
t.Errorf("count:%d != 5\n", count)
}

}

func Test_AfterFunc(t *testing.T) {
Expand Down Expand Up @@ -64,7 +65,10 @@ func Test_AfterFunc(t *testing.T) {
tm.Stop()
}()
tm.Run()
assert.Equal(t, atomic.LoadUint32(&count), uint32(2))
if count != 2 {
t.Errorf("count:%d != 2\n", count)
}

}

func Test_Node_Stop_1(t *testing.T) {
Expand All @@ -80,7 +84,9 @@ func Test_Node_Stop_1(t *testing.T) {
}()

tm.Run()
assert.NotEqual(t, count, 1)
if count != 1 {
t.Errorf("count:%d == 1\n", count)
}
}

func Test_Node_Stop(t *testing.T) {
Expand All @@ -96,5 +102,8 @@ func Test_Node_Stop(t *testing.T) {
}()
tm.Run()

assert.NotEqual(t, count, 1)
if count == 1 {
t.Errorf("count:%d == 1\n", count)
}

}

0 comments on commit 8c8bf64

Please sign in to comment.