Add tests for counters

This commit is contained in:
Tom Wilkie
2016-01-24 14:04:08 -08:00
parent f2df789503
commit e125fb5e88
2 changed files with 97 additions and 0 deletions

View File

@@ -0,0 +1,97 @@
package report
import (
"testing"
"github.com/weaveworks/scope/test"
"github.com/weaveworks/scope/test/reflect"
)
func TestCountersAdd(t *testing.T) {
want := EmptyCounters.
Add("foo", 3)
have := EmptyCounters.
Add("foo", 1).
Add("foo", 2)
if !reflect.DeepEqual(want, have) {
t.Errorf(test.Diff(want, have))
}
if v, ok := have.Lookup("foo"); !ok || v != 3 {
t.Errorf("foo != 3")
}
if v, ok := have.Lookup("bar"); ok || v != 0 {
t.Errorf("bar != nil")
}
}
func TestCountersMerge(t *testing.T) {
for name, c := range map[string]struct {
a, b, want Counters
}{
"Empty a": {
a: EmptyCounters,
b: EmptyCounters.
Add("foo", 1),
want: EmptyCounters.
Add("foo", 1),
},
"Empty b": {
a: EmptyCounters.
Add("foo", 1),
b: EmptyCounters,
want: EmptyCounters.
Add("foo", 1),
},
"Disparate keys": {
a: EmptyCounters.
Add("foo", 1),
b: EmptyCounters.
Add("bar", 2),
want: EmptyCounters.
Add("foo", 1).
Add("bar", 2),
},
"Key merge": {
a: EmptyCounters.
Add("foo", 1),
b: EmptyCounters.
Add("foo", 2),
want: EmptyCounters.
Add("foo", 3),
},
} {
if have := c.a.Merge(c.b); !reflect.DeepEqual(c.want, have) {
t.Errorf("%s:\n%s", name, test.Diff(c.want, have))
}
}
}
func TestCountersEncoding(t *testing.T) {
want := EmptyCounters.
Add("foo", 1).
Add("bar", 2)
{
gobs, err := want.GobEncode()
if err != nil {
t.Fatal(err)
}
have := EmptyCounters
have.GobDecode(gobs)
if !reflect.DeepEqual(want, have) {
t.Error(test.Diff(want, have))
}
}
{
json, err := want.MarshalJSON()
if err != nil {
t.Fatal(err)
}
have := EmptyCounters
have.UnmarshalJSON(json)
if !reflect.DeepEqual(want, have) {
t.Error(test.Diff(want, have))
}
}
}