Skip to content

Commit

Permalink
Merge pull request weaveworks#3095 from weaveworks/blank-latestmap-in…
Browse files Browse the repository at this point in the history
…sert

Blank out value on LatestMap decode insert
  • Loading branch information
rade authored Feb 26, 2018
2 parents adc46e8 + f176406 commit 306a744
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions extras/generate_latest_map
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ function generate_latest_map() {
if i == len(*m) || (*m)[i].key != key {
*m = append(*m, ${entry_type}{})
copy((*m)[i+1:], (*m)[i:])
(*m)[i] = ${entry_type}{}
}
return i
}
Expand Down
2 changes: 2 additions & 0 deletions report/latest_map_generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ func (m *StringLatestMap) locate(key string) int {
if i == len(*m) || (*m)[i].key != key {
*m = append(*m, stringLatestEntry{})
copy((*m)[i+1:], (*m)[i:])
(*m)[i] = stringLatestEntry{}
}
return i
}
Expand Down Expand Up @@ -332,6 +333,7 @@ func (m *NodeControlDataLatestMap) locate(key string) int {
if i == len(*m) || (*m)[i].key != key {
*m = append(*m, nodeControlDataLatestEntry{})
copy((*m)[i+1:], (*m)[i:])
(*m)[i] = nodeControlDataLatestEntry{}
}
return i
}
Expand Down
31 changes: 30 additions & 1 deletion report/latest_map_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,36 @@ func BenchmarkLatestMapDecode(b *testing.B) {
}
}

func TestLatestMapDecoding(t *testing.T) {
ts, _ := time.Parse(time.RFC3339Nano, "2018-02-26T09:50:43Z")
want := MakeStringLatestMap().
Set("foo", ts, "bar").
Set("bar", ts, "baz").
Set("emptyval", ts, "")
// The following string is carefully constructed to have 'emptyval' not in alphabetical order
data := `
{
"bar": {
"timestamp": "2018-02-26T09:50:43Z",
"value": "baz"
},
"foo": {
"timestamp": "2018-02-26T09:50:43Z",
"value": "bar"
},
"emptyval": {
"timestamp": "2018-02-26T09:50:43Z"
}
}`
h := &codec.JsonHandle{}
decoder := codec.NewDecoder(bytes.NewBufferString(data), h)
have := MakeStringLatestMap()
have.CodecDecodeSelf(decoder)
if !reflect.DeepEqual(want, have) {
t.Error(test.Diff(want, have))
}
}

func TestLatestMapEncoding(t *testing.T) {
now := time.Now()
want := MakeStringLatestMap().
Expand All @@ -188,7 +218,6 @@ func TestLatestMapEncoding(t *testing.T) {
t.Error(test.Diff(want, have))
}
}

}

func TestLatestMapEncodingNil(t *testing.T) {
Expand Down

0 comments on commit 306a744

Please sign in to comment.