Browse Source

test: add test case

fatedier 8 years ago
parent
commit
a0c83bdb78

+ 0 - 1
.travis.yml

@@ -2,7 +2,6 @@ sudo: false
 language: go
 
 go:
-    - 1.6.4
     - 1.7.5
     - 1.8
 

+ 0 - 3
models/msg/process_test.go

@@ -17,9 +17,6 @@ package msg
 import (
 	"bytes"
 	"encoding/binary"
-	"encoding/json"
-	"fmt"
-	"net"
 	"reflect"
 	"testing"
 

+ 18 - 0
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)
+}

+ 16 - 0
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")
+}

+ 23 - 0
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())
+}

+ 2 - 5
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
 	}

+ 27 - 0
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())
+}