syncs: add ShardedMap.Mutate
To let callers do atomic/CAS-like operations. Updates tailscale/corp#7355 Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
committed by
Brad Fitzpatrick
parent
ab310a7f60
commit
cafd9a2bec
@@ -41,4 +41,41 @@ func TestShardedMap(t *testing.T) {
|
||||
if g, w := m.Len(), 0; g != w {
|
||||
t.Errorf("got Len %v; want %v", g, w)
|
||||
}
|
||||
|
||||
// Mutation adding an entry.
|
||||
if v := m.Mutate(1, func(was string, ok bool) (string, bool) {
|
||||
if ok {
|
||||
t.Fatal("was okay")
|
||||
}
|
||||
return "ONE", true
|
||||
}); v != 1 {
|
||||
t.Errorf("Mutate = %v; want 1", v)
|
||||
}
|
||||
if g, w := m.Get(1), "ONE"; g != w {
|
||||
t.Errorf("got %q; want %q", g, w)
|
||||
}
|
||||
// Mutation changing an entry.
|
||||
if v := m.Mutate(1, func(was string, ok bool) (string, bool) {
|
||||
if !ok {
|
||||
t.Fatal("wasn't okay")
|
||||
}
|
||||
return was + "-" + was, true
|
||||
}); v != 0 {
|
||||
t.Errorf("Mutate = %v; want 0", v)
|
||||
}
|
||||
if g, w := m.Get(1), "ONE-ONE"; g != w {
|
||||
t.Errorf("got %q; want %q", g, w)
|
||||
}
|
||||
// Mutation removing an entry.
|
||||
if v := m.Mutate(1, func(was string, ok bool) (string, bool) {
|
||||
if !ok {
|
||||
t.Fatal("wasn't okay")
|
||||
}
|
||||
return "", false
|
||||
}); v != -1 {
|
||||
t.Errorf("Mutate = %v; want -1", v)
|
||||
}
|
||||
if g, w := m.Get(1), ""; g != w {
|
||||
t.Errorf("got %q; want %q", g, w)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user