diff options
author | Bryan C. Mills <bcmills@google.com> | 2017-09-08 14:36:43 -0400 |
---|---|---|
committer | Russ Cox <rsc@golang.org> | 2017-10-25 20:22:50 +0000 |
commit | 8c7fa95ad3420779ecffc9c72afc82bf074c6a88 (patch) | |
tree | 3559a22145f1406849fe425f8c84f3f6f1ba800b | |
parent | ccd5abc10555ed39b6b8b1e5c12e5ecce734b72c (diff) | |
download | go-8c7fa95ad3420779ecffc9c72afc82bf074c6a88.tar.gz go-8c7fa95ad3420779ecffc9c72afc82bf074c6a88.zip |
[release-branch.go1.9] expvar: make (*Map).Init clear existing keys
fixes #21619
Change-Id: I5bb513dfc8cac875b06a262eec40b5863ae23a4c
Reviewed-on: https://go-review.googlesource.com/62370
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-on: https://go-review.googlesource.com/70973
Run-TryBot: Russ Cox <rsc@golang.org>
-rw-r--r-- | src/expvar/expvar.go | 12 | ||||
-rw-r--r-- | src/expvar/expvar_test.go | 22 |
2 files changed, 33 insertions, 1 deletions
diff --git a/src/expvar/expvar.go b/src/expvar/expvar.go index 64dae70c62..8290e0bd72 100644 --- a/src/expvar/expvar.go +++ b/src/expvar/expvar.go @@ -125,7 +125,17 @@ func (v *Map) String() string { return b.String() } -func (v *Map) Init() *Map { return v } +// Init removes all keys from the map. +func (v *Map) Init() *Map { + v.keysMu.Lock() + defer v.keysMu.Unlock() + v.keys = v.keys[:0] + v.m.Range(func(k, _ interface{}) bool { + v.m.Delete(k) + return true + }) + return v +} // updateKeys updates the sorted list of keys in v.keys. func (v *Map) addKey(key string) { diff --git a/src/expvar/expvar_test.go b/src/expvar/expvar_test.go index 7014063d4f..728e763896 100644 --- a/src/expvar/expvar_test.go +++ b/src/expvar/expvar_test.go @@ -161,6 +161,28 @@ func BenchmarkStringSet(b *testing.B) { }) } +func TestMapInit(t *testing.T) { + RemoveAll() + colors := NewMap("bike-shed-colors") + colors.Add("red", 1) + colors.Add("blue", 1) + colors.Add("chartreuse", 1) + + n := 0 + colors.Do(func(KeyValue) { n++ }) + if n != 3 { + t.Errorf("after three Add calls with distinct keys, Do should invoke f 3 times; got %v", n) + } + + colors.Init() + + n = 0 + colors.Do(func(KeyValue) { n++ }) + if n != 0 { + t.Errorf("after Init, Do should invoke f 0 times; got %v", n) + } +} + func TestMapCounter(t *testing.T) { RemoveAll() colors := NewMap("bike-shed-colors") |