Skip to content

Commit

Permalink
test: add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
fatedier committed Mar 27, 2017
1 parent 30aeaf9 commit a0c83bd
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 9 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ sudo: false
language: go

go:
- 1.6.4
- 1.7.5
- 1.8

Expand Down
3 changes: 0 additions & 3 deletions models/msg/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ package msg
import (
"bytes"
"encoding/binary"
"encoding/json"
"fmt"
"net"
"reflect"
"testing"

Expand Down
18 changes: 18 additions & 0 deletions models/proto/udp/udp_test.go
Original file line number Diff line number Diff line change
@@ -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 changes: 16 additions & 0 deletions utils/errors/errors_test.go
Original file line number Diff line number Diff line change
@@ -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 changes: 23 additions & 0 deletions utils/metric/counter_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
7 changes: 2 additions & 5 deletions utils/metric/date_counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
Expand All @@ -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
}
Expand Down
27 changes: 27 additions & 0 deletions utils/metric/date_counter_test.go
Original file line number Diff line number Diff line change
@@ -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())
}

0 comments on commit a0c83bd

Please sign in to comment.