From a0c83bdb7819e8e3b842147d9b3aab723d029f84 Mon Sep 17 00:00:00 2001 From: fatedier Date: Mon, 27 Mar 2017 23:46:38 +0800 Subject: [PATCH] test: add test case --- .travis.yml | 1 - models/msg/process_test.go | 3 --- models/proto/udp/udp_test.go | 18 ++++++++++++++++++ utils/errors/errors_test.go | 16 ++++++++++++++++ utils/metric/counter_test.go | 23 +++++++++++++++++++++++ utils/metric/date_counter.go | 7 ++----- utils/metric/date_counter_test.go | 27 +++++++++++++++++++++++++++ 7 files changed, 86 insertions(+), 9 deletions(-) create mode 100644 models/proto/udp/udp_test.go create mode 100644 utils/errors/errors_test.go create mode 100644 utils/metric/counter_test.go create mode 100644 utils/metric/date_counter_test.go diff --git a/.travis.yml b/.travis.yml index fd3bc7c..3570be0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,7 +2,6 @@ sudo: false language: go go: - - 1.6.4 - 1.7.5 - 1.8 diff --git a/models/msg/process_test.go b/models/msg/process_test.go index f8b677f..149b7db 100644 --- a/models/msg/process_test.go +++ b/models/msg/process_test.go @@ -17,9 +17,6 @@ package msg import ( "bytes" "encoding/binary" - "encoding/json" - "fmt" - "net" "reflect" "testing" diff --git a/models/proto/udp/udp_test.go b/models/proto/udp/udp_test.go new file mode 100644 index 0000000..6f364b8 --- /dev/null +++ b/models/proto/udp/udp_test.go @@ -0,0 +1,18 @@ +package udp + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestUdpPacket(t *testing.T) { + assert := assert.New(t) + + buf := []byte("hello world") + udpMsg := NewUdpPacket(buf, nil, nil) + + newBuf, err := GetContent(udpMsg) + assert.NoError(err) + assert.EqualValues(buf, newBuf) +} diff --git a/utils/errors/errors_test.go b/utils/errors/errors_test.go new file mode 100644 index 0000000..0edfa45 --- /dev/null +++ b/utils/errors/errors_test.go @@ -0,0 +1,16 @@ +package errors + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestPanicToError(t *testing.T) { + assert := assert.New(t) + + err := PanicToError(func() { + panic("test error") + }) + assert.Contains(err.Error(), "test error") +} diff --git a/utils/metric/counter_test.go b/utils/metric/counter_test.go new file mode 100644 index 0000000..4925c25 --- /dev/null +++ b/utils/metric/counter_test.go @@ -0,0 +1,23 @@ +package metric + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestCounter(t *testing.T) { + assert := assert.New(t) + c := NewCounter() + c.Inc(10) + assert.EqualValues(10, c.Count()) + + c.Dec(5) + assert.EqualValues(5, c.Count()) + + cTmp := c.Snapshot() + assert.EqualValues(5, cTmp.Count()) + + c.Clear() + assert.EqualValues(0, c.Count()) +} diff --git a/utils/metric/date_counter.go b/utils/metric/date_counter.go index e839cd8..4524fec 100644 --- a/utils/metric/date_counter.go +++ b/utils/metric/date_counter.go @@ -94,10 +94,7 @@ func (c *StandardDateCounter) Dec(count int64) { func (c *StandardDateCounter) Snapshot() DateCounter { c.mu.Lock() defer c.mu.Unlock() - tmp := &StandardDateCounter{ - reserveDays: c.reserveDays, - counts: make([]int64, c.reserveDays), - } + tmp := newStandardDateCounter(c.reserveDays) for i := 0; i < int(c.reserveDays); i++ { tmp.counts[i] = c.counts[i] } @@ -124,7 +121,7 @@ func (c *StandardDateCounter) rotate(now time.Time) { if days <= 0 { return - } else if days >= 7 { + } else if days >= int(c.reserveDays) { c.counts = make([]int64, c.reserveDays) return } diff --git a/utils/metric/date_counter_test.go b/utils/metric/date_counter_test.go new file mode 100644 index 0000000..c9997c7 --- /dev/null +++ b/utils/metric/date_counter_test.go @@ -0,0 +1,27 @@ +package metric + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestDateCounter(t *testing.T) { + assert := assert.New(t) + + dc := NewDateCounter(3) + dc.Inc(10) + assert.EqualValues(10, dc.TodayCount()) + + dc.Dec(5) + assert.EqualValues(5, dc.TodayCount()) + + counts := dc.GetLastDaysCount(3) + assert.EqualValues(3, len(counts)) + assert.EqualValues(5, counts[0]) + assert.EqualValues(0, counts[1]) + assert.EqualValues(0, counts[2]) + + dcTmp := dc.Snapshot() + assert.EqualValues(5, dcTmp.TodayCount()) +}