From f2df789503ebcaca00c9242907dd27bda79c03e6 Mon Sep 17 00:00:00 2001 From: Tom Wilkie Date: Sun, 24 Jan 2016 10:06:03 -0800 Subject: [PATCH] Add tests for latest map. --- report/latest_map.go | 9 ++- report/latest_map_internal_test.go | 118 +++++++++++++++++++++++++++++ 2 files changed, 124 insertions(+), 3 deletions(-) create mode 100644 report/latest_map_internal_test.go diff --git a/report/latest_map.go b/report/latest_map.go index 996b5da62..20ded1b07 100644 --- a/report/latest_map.go +++ b/report/latest_map.go @@ -5,7 +5,6 @@ import ( "encoding/gob" "encoding/json" "fmt" - "reflect" "sort" "time" @@ -25,7 +24,11 @@ type LatestEntry struct { } func (e LatestEntry) String() string { - return fmt.Sprintf("\"%s\" (%d)", e.Value, e.Timestamp) + return fmt.Sprintf("\"%s\" (%d)", e.Value, e.Timestamp.String()) +} + +func (e LatestEntry) Equal(e2 LatestEntry) bool { + return e.Timestamp.Equal(e2.Timestamp) && e.Value == e2.Value } // EmptyLatestMap is an empty LatestMap. Start with this. @@ -120,7 +123,7 @@ func (m LatestMap) DeepEqual(i interface{}) bool { if otherValue, ok := n.Map.Lookup(k); !ok { equal = false } else { - equal = equal && reflect.DeepEqual(val, otherValue) + equal = equal && val.(LatestEntry).Equal(otherValue.(LatestEntry)) } }) return equal diff --git a/report/latest_map_internal_test.go b/report/latest_map_internal_test.go new file mode 100644 index 000000000..ae49b0792 --- /dev/null +++ b/report/latest_map_internal_test.go @@ -0,0 +1,118 @@ +package report + +import ( + "testing" + "time" + + "github.com/weaveworks/scope/test" + "github.com/weaveworks/scope/test/reflect" +) + +func TestLatestMapAdd(t *testing.T) { + now := time.Now() + want := EmptyLatestMap. + Set("foo", now, "Bar") + have := EmptyLatestMap. + Set("foo", now.Add(-1), "Baz"). + Set("foo", now, "Bar") + if !reflect.DeepEqual(want, have) { + t.Errorf(test.Diff(want, have)) + } + if v, ok := have.Lookup("foo"); !ok || v != "Bar" { + t.Errorf("v != Bar") + } + if v, ok := have.Lookup("bar"); ok || v != "" { + t.Errorf("v != nil") + } + have.ForEach(func(k, v string) { + if k != "foo" || v != "Bar" { + t.Errorf("v != Bar") + } + }) +} +func TestLatestMapDelete(t *testing.T) { + now := time.Now() + want := EmptyLatestMap + have := EmptyLatestMap. + Set("foo", now, "Baz"). + Delete("foo") + if !reflect.DeepEqual(want, have) { + t.Errorf(test.Diff(want, have)) + } +} + +func TestLatestMapMerge(t *testing.T) { + now := time.Now() + then := now.Add(-1) + + for name, c := range map[string]struct { + a, b, want LatestMap + }{ + "Empty a": { + a: EmptyLatestMap, + b: EmptyLatestMap. + Set("foo", now, "bar"), + want: EmptyLatestMap. + Set("foo", now, "bar"), + }, + "Empty b": { + a: EmptyLatestMap. + Set("foo", now, "bar"), + b: EmptyLatestMap, + want: EmptyLatestMap. + Set("foo", now, "bar"), + }, + "Host merge": { + a: EmptyLatestMap. + Set("foo", now, "bar"), + b: EmptyLatestMap. + Set("baz", now, "bop"), + want: EmptyLatestMap. + Set("foo", now, "bar"). + Set("baz", now, "bop"), + }, + "Edge merge": { + a: EmptyLatestMap. + Set("foo", now, "bar"), + b: EmptyLatestMap. + Set("foo", then, "baz"), + want: EmptyLatestMap. + Set("foo", now, "bar"), + }, + } { + if have := c.a.Merge(c.b); !reflect.DeepEqual(c.want, have) { + t.Errorf("%s:\n%s", name, test.Diff(c.want, have)) + } + } +} + +func TestLatestMapEncoding(t *testing.T) { + now := time.Now() + want := EmptyLatestMap. + Set("foo", now, "bar"). + Set("bar", now, "baz") + + { + gobs, err := want.GobEncode() + if err != nil { + t.Fatal(err) + } + have := EmptyLatestMap + 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 := EmptyLatestMap + have.UnmarshalJSON(json) + if !reflect.DeepEqual(want, have) { + t.Error(test.Diff(want, have)) + } + } +}