Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix WithLatests() fixup on duplicate keys #3281

Merged
merged 1 commit into from
Jul 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions report/map_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ func (m StringLatestMap) Len() int { return len(m) }
func (m StringLatestMap) Swap(i, j int) { m[i], m[j] = m[j], m[i] }
func (m StringLatestMap) Less(i, j int) bool { return m[i].key < m[j].key }

// sort entries and shuffle down any duplicates
func (m StringLatestMap) fixup() {
// sort entries and shuffle down any duplicates. NOTE: may modify contents of m.
func (m StringLatestMap) sortedAndDeduplicated() StringLatestMap {
sort.Sort(m)
for i := 1; i < len(m); {
if m[i-1].key == m[i].key {
Expand All @@ -161,6 +161,7 @@ func (m StringLatestMap) fixup() {
i++
}
}
return m
}

// add several entries at the same timestamp
Expand All @@ -170,8 +171,7 @@ func (m StringLatestMap) addMapEntries(ts time.Time, n map[string]string) String
for k, v := range n {
out = append(out, stringLatestEntry{key: k, Value: v, Timestamp: ts})
}
out.fixup()
return out
return out.sortedAndDeduplicated()
}

// Propagate a set of latest values from one set to another.
Expand All @@ -183,6 +183,5 @@ func (m StringLatestMap) Propagate(from StringLatestMap, keys ...string) StringL
out = append(out, stringLatestEntry{key: k, Value: v, Timestamp: ts})
}
}
out.fixup()
return out
return out.sortedAndDeduplicated()
}
17 changes: 17 additions & 0 deletions report/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/weaveworks/common/mtime"
"github.com/weaveworks/common/test"
"github.com/weaveworks/scope/report"
Expand All @@ -16,6 +17,22 @@ const (
Domain = "domain"
)

func TestWithLatest(t *testing.T) {
mtime.NowForce(time.Now())
defer mtime.NowReset()

latests1 := map[string]string{Name: "x"}
latests2 := map[string]string{PID: "123"}
node1 := report.MakeNode("node1").WithLatests(latests1)
assert.Equal(t, 1, node1.Latest.Len())
node2 := node1.WithLatests(latests1)
assert.Equal(t, node1, node2)
node3 := node1.WithLatests(latests2)
assert.Equal(t, 2, node3.Latest.Len())
node4 := node1.WithLatests(latests2)
assert.Equal(t, node3, node4)
}

func TestMergeNodes(t *testing.T) {
mtime.NowForce(time.Now())
defer mtime.NowReset()
Expand Down