diff options
author | Michael Fraenkel <michael.fraenkel@gmail.com> | 2018-10-03 20:42:05 -0400 |
---|---|---|
committer | Ian Lance Taylor <iant@golang.org> | 2018-10-04 01:16:52 +0000 |
commit | c6483b61a9096e3076439079c88eab84c9e6d859 (patch) | |
tree | a2525d0a5b2d26e3a2b360b90d7dafcccfd5b361 /src/expvar | |
parent | f1973f3164e5b570b8601c18fa484be7e97171f4 (diff) | |
download | go-c6483b61a9096e3076439079c88eab84c9e6d859.tar.gz go-c6483b61a9096e3076439079c88eab84c9e6d859.zip |
expvar: add Map.Delete
Fixes #13491
Change-Id: Ic0525d8ee90f47d0d23c1485919aee13d2400494
Reviewed-on: https://go-review.googlesource.com/c/139537
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Diffstat (limited to 'src/expvar')
-rw-r--r-- | src/expvar/expvar.go | 13 | ||||
-rw-r--r-- | src/expvar/expvar_test.go | 37 |
2 files changed, 49 insertions, 1 deletions
diff --git a/src/expvar/expvar.go b/src/expvar/expvar.go index b7928aab17..976b300d63 100644 --- a/src/expvar/expvar.go +++ b/src/expvar/expvar.go @@ -137,7 +137,7 @@ func (v *Map) Init() *Map { return v } -// updateKeys updates the sorted list of keys in v.keys. +// addKey updates the sorted list of keys in v.keys. func (v *Map) addKey(key string) { v.keysMu.Lock() defer v.keysMu.Unlock() @@ -199,6 +199,17 @@ func (v *Map) AddFloat(key string, delta float64) { } } +// Deletes the given key from the map. +func (v *Map) Delete(key string) { + v.keysMu.Lock() + defer v.keysMu.Unlock() + i := sort.SearchStrings(v.keys, key) + if i < len(v.keys) && key == v.keys[i] { + v.keys = append(v.keys[:i], v.keys[i+1:]...) + v.m.Delete(key) + } +} + // Do calls f for each entry in the map. // The map is locked during the iteration, // but existing entries may be concurrently updated. diff --git a/src/expvar/expvar_test.go b/src/expvar/expvar_test.go index 728e763896..804b56c1aa 100644 --- a/src/expvar/expvar_test.go +++ b/src/expvar/expvar_test.go @@ -183,6 +183,43 @@ func TestMapInit(t *testing.T) { } } +func TestMapDelete(t *testing.T) { + RemoveAll() + colors := NewMap("bike-shed-colors") + + colors.Add("red", 1) + colors.Add("red", 2) + colors.Add("blue", 4) + + n := 0 + colors.Do(func(KeyValue) { n++ }) + if n != 2 { + t.Errorf("after two Add calls with distinct keys, Do should invoke f 2 times; got %v", n) + } + + colors.Delete("red") + n = 0 + colors.Do(func(KeyValue) { n++ }) + if n != 1 { + t.Errorf("removed red, Do should invoke f 1 times; got %v", n) + } + + colors.Delete("notfound") + n = 0 + colors.Do(func(KeyValue) { n++ }) + if n != 1 { + t.Errorf("attempted to remove notfound, Do should invoke f 1 times; got %v", n) + } + + colors.Delete("blue") + colors.Delete("blue") + n = 0 + colors.Do(func(KeyValue) { n++ }) + if n != 0 { + t.Errorf("all keys removed, Do should invoke f 0 times; got %v", n) + } +} + func TestMapCounter(t *testing.T) { RemoveAll() colors := NewMap("bike-shed-colors") |