1
0
Fork 0

Stats group tests

This commit is contained in:
Gregory Eremin 2015-10-26 13:22:01 +03:00
parent 3918b26043
commit 98ff2625a4
1 changed files with 70 additions and 0 deletions

70
stats/group_test.go Normal file
View File

@ -0,0 +1,70 @@
package stats
import (
"testing"
"time"
)
func TestNewGroup(t *testing.T) {
g := NewGroup(newGroupItemMock(), newGroupItemMock())
if bc := len(g.backends); bc != 2 {
t.Fatalf("Expected backends to contain 2 items, got %d", bc)
}
}
func TestGroupAdd(t *testing.T) {
g := NewGroup(newGroupItemMock(), newGroupItemMock())
dur := 5 * time.Second
g.Add("", dur)
for _, m := range g.backends {
select {
case dur1 := <-m.(*groupItemMock).addCalls:
if dur1 != dur {
t.Errorf("Expected a 5s duration, got %d", dur1)
}
default:
t.Error("Mock item didn't receive an Add call")
}
}
}
func TestGroupError(t *testing.T) {
g := NewGroup(newGroupItemMock(), newGroupItemMock())
g.Error("")
for _, m := range g.backends {
select {
case <-m.(*groupItemMock).errorCalls:
default:
t.Error("Mock item didn't receive an Error call")
}
}
}
//
// Mock
//
type groupItemMock struct {
addCalls chan time.Duration
errorCalls chan struct{}
}
func (g *groupItemMock) Add(_ string, dur time.Duration) {
g.addCalls <- dur
}
func (g *groupItemMock) Error(_ string) {
g.errorCalls <- struct{}{}
}
func newGroupItemMock() *groupItemMock {
return &groupItemMock{
addCalls: make(chan time.Duration, 1),
errorCalls: make(chan struct{}, 1),
}
}