test: add test case

This commit is contained in:
fatedier 2017-03-27 23:46:38 +08:00
parent 30aeaf968e
commit a0c83bdb78
7 changed files with 86 additions and 9 deletions

View File

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

View File

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

View File

@ -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)
}

View File

@ -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")
}

View File

@ -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())
}

View File

@ -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
}

View File

@ -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())
}