From e125fb5e8844bff98730ae6273492f85999be030 Mon Sep 17 00:00:00 2001 From: Tom Wilkie Date: Sun, 24 Jan 2016 14:04:08 -0800 Subject: [PATCH] Add tests for counters --- report/counters_internal_test.go | 97 ++++++++++++++++++++++++++ report/{merge_test.go => node_test.go} | 0 2 files changed, 97 insertions(+) create mode 100644 report/counters_internal_test.go rename report/{merge_test.go => node_test.go} (100%) diff --git a/report/counters_internal_test.go b/report/counters_internal_test.go new file mode 100644 index 0000000000..157d72cebf --- /dev/null +++ b/report/counters_internal_test.go @@ -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)) + } + } +} diff --git a/report/merge_test.go b/report/node_test.go similarity index 100% rename from report/merge_test.go rename to report/node_test.go