| 1 |
// Copyright 2016 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package sync_test |
| 6 |
|
| 7 |
import ( |
| 8 |
isync "internal/sync" |
| 9 |
"internal/testenv" |
| 10 |
"math/rand" |
| 11 |
"reflect" |
| 12 |
"runtime" |
| 13 |
"sync" |
| 14 |
"sync/atomic" |
| 15 |
"testing" |
| 16 |
"testing/quick" |
| 17 |
) |
| 18 |
|
| 19 |
type mapOp string |
| 20 |
|
| 21 |
const ( |
| 22 |
opLoad = mapOp("Load") |
| 23 |
opStore = mapOp("Store") |
| 24 |
opLoadOrStore = mapOp("LoadOrStore") |
| 25 |
opLoadAndDelete = mapOp("LoadAndDelete") |
| 26 |
opDelete = mapOp("Delete") |
| 27 |
opSwap = mapOp("Swap") |
| 28 |
opCompareAndSwap = mapOp("CompareAndSwap") |
| 29 |
opCompareAndDelete = mapOp("CompareAndDelete") |
| 30 |
opClear = mapOp("Clear") |
| 31 |
) |
| 32 |
|
| 33 |
var mapOps = [...]mapOp{ |
| 34 |
opLoad, |
| 35 |
opStore, |
| 36 |
opLoadOrStore, |
| 37 |
opLoadAndDelete, |
| 38 |
opDelete, |
| 39 |
opSwap, |
| 40 |
opCompareAndSwap, |
| 41 |
opCompareAndDelete, |
| 42 |
opClear, |
| 43 |
} |
| 44 |
|
| 45 |
// mapCall is a quick.Generator for calls on mapInterface. |
| 46 |
type mapCall struct { |
| 47 |
op mapOp |
| 48 |
k, v any |
| 49 |
} |
| 50 |
|
| 51 |
func (c mapCall) apply(m mapInterface) (any, bool) { |
| 52 |
switch c.op { |
| 53 |
case opLoad: |
| 54 |
return m.Load(c.k) |
| 55 |
case opStore: |
| 56 |
m.Store(c.k, c.v) |
| 57 |
return nil, false |
| 58 |
case opLoadOrStore: |
| 59 |
return m.LoadOrStore(c.k, c.v) |
| 60 |
case opLoadAndDelete: |
| 61 |
return m.LoadAndDelete(c.k) |
| 62 |
case opDelete: |
| 63 |
m.Delete(c.k) |
| 64 |
return nil, false |
| 65 |
case opSwap: |
| 66 |
return m.Swap(c.k, c.v) |
| 67 |
case opCompareAndSwap: |
| 68 |
if m.CompareAndSwap(c.k, c.v, rand.Int()) { |
| 69 |
m.Delete(c.k) |
| 70 |
return c.v, true |
| 71 |
} |
| 72 |
return nil, false |
| 73 |
case opCompareAndDelete: |
| 74 |
if m.CompareAndDelete(c.k, c.v) { |
| 75 |
if _, ok := m.Load(c.k); !ok { |
| 76 |
return nil, true |
| 77 |
} |
| 78 |
} |
| 79 |
return nil, false |
| 80 |
case opClear: |
| 81 |
m.Clear() |
| 82 |
return nil, false |
| 83 |
default: |
| 84 |
panic("invalid mapOp") |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
type mapResult struct { |
| 89 |
value any |
| 90 |
ok bool |
| 91 |
} |
| 92 |
|
| 93 |
func randValue(r *rand.Rand) any { |
| 94 |
b := make([]byte, r.Intn(4)) |
| 95 |
for i := range b { |
| 96 |
b[i] = 'a' + byte(rand.Intn(26)) |
| 97 |
} |
| 98 |
return string(b) |
| 99 |
} |
| 100 |
|
| 101 |
func (mapCall) Generate(r *rand.Rand, size int) reflect.Value { |
| 102 |
c := mapCall{op: mapOps[rand.Intn(len(mapOps))], k: randValue(r)} |
| 103 |
switch c.op { |
| 104 |
case opStore, opLoadOrStore: |
| 105 |
c.v = randValue(r) |
| 106 |
} |
| 107 |
return reflect.ValueOf(c) |
| 108 |
} |
| 109 |
|
| 110 |
func applyCalls(m mapInterface, calls []mapCall) (results []mapResult, final map[any]any) { |
| 111 |
for _, c := range calls { |
| 112 |
v, ok := c.apply(m) |
| 113 |
results = append(results, mapResult{v, ok}) |
| 114 |
} |
| 115 |
|
| 116 |
final = make(map[any]any) |
| 117 |
m.Range(func(k, v any) bool { |
| 118 |
final[k] = v |
| 119 |
return true |
| 120 |
}) |
| 121 |
|
| 122 |
return results, final |
| 123 |
} |
| 124 |
|
| 125 |
func applyMap(calls []mapCall) ([]mapResult, map[any]any) { |
| 126 |
return applyCalls(new(sync.Map), calls) |
| 127 |
} |
| 128 |
|
| 129 |
func applyRWMutexMap(calls []mapCall) ([]mapResult, map[any]any) { |
| 130 |
return applyCalls(new(RWMutexMap), calls) |
| 131 |
} |
| 132 |
|
| 133 |
func applyDeepCopyMap(calls []mapCall) ([]mapResult, map[any]any) { |
| 134 |
return applyCalls(new(DeepCopyMap), calls) |
| 135 |
} |
| 136 |
|
| 137 |
func applyHashTrieMap(calls []mapCall) ([]mapResult, map[any]any) { |
| 138 |
return applyCalls(new(isync.HashTrieMap[any, any]), calls) |
| 139 |
} |
| 140 |
|
| 141 |
func TestMapMatchesRWMutex(t *testing.T) { |
| 142 |
if err := quick.CheckEqual(applyMap, applyRWMutexMap, nil); err != nil { |
| 143 |
t.Error(err) |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
func TestMapMatchesDeepCopy(t *testing.T) { |
| 148 |
if err := quick.CheckEqual(applyMap, applyDeepCopyMap, nil); err != nil { |
| 149 |
t.Error(err) |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
func TestMapMatchesHashTrieMap(t *testing.T) { |
| 154 |
if err := quick.CheckEqual(applyMap, applyHashTrieMap, nil); err != nil { |
| 155 |
t.Error(err) |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
func TestConcurrentRange(t *testing.T) { |
| 160 |
const mapSize = 1 << 10 |
| 161 |
|
| 162 |
m := new(sync.Map) |
| 163 |
for n := int64(1); n <= mapSize; n++ { |
| 164 |
m.Store(n, n) |
| 165 |
} |
| 166 |
|
| 167 |
done := make(chan struct{}) |
| 168 |
var wg sync.WaitGroup |
| 169 |
defer func() { |
| 170 |
close(done) |
| 171 |
wg.Wait() |
| 172 |
}() |
| 173 |
for g := int64(runtime.GOMAXPROCS(0)); g > 0; g-- { |
| 174 |
r := rand.New(rand.NewSource(g)) |
| 175 |
wg.Add(1) |
| 176 |
go func(g int64) { |
| 177 |
defer wg.Done() |
| 178 |
for i := int64(0); ; i++ { |
| 179 |
select { |
| 180 |
case <-done: |
| 181 |
return |
| 182 |
default: |
| 183 |
} |
| 184 |
for n := int64(1); n < mapSize; n++ { |
| 185 |
if r.Int63n(mapSize) == 0 { |
| 186 |
m.Store(n, n*i*g) |
| 187 |
} else { |
| 188 |
m.Load(n) |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
}(g) |
| 193 |
} |
| 194 |
|
| 195 |
iters := 1 << 10 |
| 196 |
if testing.Short() { |
| 197 |
iters = 16 |
| 198 |
} |
| 199 |
for n := iters; n > 0; n-- { |
| 200 |
seen := make(map[int64]bool, mapSize) |
| 201 |
|
| 202 |
m.Range(func(ki, vi any) bool { |
| 203 |
k, v := ki.(int64), vi.(int64) |
| 204 |
if v%k != 0 { |
| 205 |
t.Fatalf("while Storing multiples of %v, Range saw value %v", k, v) |
| 206 |
} |
| 207 |
if seen[k] { |
| 208 |
t.Fatalf("Range visited key %v twice", k) |
| 209 |
} |
| 210 |
seen[k] = true |
| 211 |
return true |
| 212 |
}) |
| 213 |
|
| 214 |
if len(seen) != mapSize { |
| 215 |
t.Fatalf("Range visited %v elements of %v-element Map", len(seen), mapSize) |
| 216 |
} |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
func TestIssue40999(t *testing.T) { |
| 221 |
var m sync.Map |
| 222 |
|
| 223 |
// Since the miss-counting in missLocked (via Delete) |
| 224 |
// compares the miss count with len(m.dirty), |
| 225 |
// add an initial entry to bias len(m.dirty) above the miss count. |
| 226 |
m.Store(nil, struct{}{}) |
| 227 |
|
| 228 |
var cleanedUp uint32 |
| 229 |
|
| 230 |
// Add cleanups that count for collected keys. A non-zero count |
| 231 |
// indicates that keys have not been leaked. |
| 232 |
for atomic.LoadUint32(&cleanedUp) == 0 { |
| 233 |
p := new(int) |
| 234 |
runtime.AddCleanup(p, func(c *uint32) { atomic.AddUint32(c, 1) }, &cleanedUp) |
| 235 |
m.Store(p, struct{}{}) |
| 236 |
m.Delete(p) |
| 237 |
runtime.GC() |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
func TestMapRangeNestedCall(t *testing.T) { // Issue 46399 |
| 242 |
var m sync.Map |
| 243 |
for i, v := range [3]string{"hello", "world", "Go"} { |
| 244 |
m.Store(i, v) |
| 245 |
} |
| 246 |
m.Range(func(key, value any) bool { |
| 247 |
m.Range(func(key, value any) bool { |
| 248 |
// We should be able to load the key offered in the Range callback, |
| 249 |
// because there are no concurrent Delete involved in this tested map. |
| 250 |
if v, ok := m.Load(key); !ok || !reflect.DeepEqual(v, value) { |
| 251 |
t.Fatalf("Nested Range loads unexpected value, got %+v want %+v", v, value) |
| 252 |
} |
| 253 |
|
| 254 |
// We didn't keep 42 and a value into the map before, if somehow we loaded |
| 255 |
// a value from such a key, meaning there must be an internal bug regarding |
| 256 |
// nested range in the Map. |
| 257 |
if _, loaded := m.LoadOrStore(42, "dummy"); loaded { |
| 258 |
t.Fatalf("Nested Range loads unexpected value, want store a new value") |
| 259 |
} |
| 260 |
|
| 261 |
// Try to Store then LoadAndDelete the corresponding value with the key |
| 262 |
// 42 to the Map. In this case, the key 42 and associated value should be |
| 263 |
// removed from the Map. Therefore any future range won't observe key 42 |
| 264 |
// as we checked in above. |
| 265 |
val := "sync.Map" |
| 266 |
m.Store(42, val) |
| 267 |
if v, loaded := m.LoadAndDelete(42); !loaded || !reflect.DeepEqual(v, val) { |
| 268 |
t.Fatalf("Nested Range loads unexpected value, got %v, want %v", v, val) |
| 269 |
} |
| 270 |
return true |
| 271 |
}) |
| 272 |
|
| 273 |
// Remove key from Map on-the-fly. |
| 274 |
m.Delete(key) |
| 275 |
return true |
| 276 |
}) |
| 277 |
|
| 278 |
// After a Range of Delete, all keys should be removed and any |
| 279 |
// further Range won't invoke the callback. Hence length remains 0. |
| 280 |
length := 0 |
| 281 |
m.Range(func(key, value any) bool { |
| 282 |
length++ |
| 283 |
return true |
| 284 |
}) |
| 285 |
|
| 286 |
if length != 0 { |
| 287 |
t.Fatalf("Unexpected sync.Map size, got %v want %v", length, 0) |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
func TestCompareAndSwap_NonExistingKey(t *testing.T) { |
| 292 |
m := &sync.Map{} |
| 293 |
if m.CompareAndSwap(m, nil, 42) { |
| 294 |
// See https://go.dev/issue/51972#issuecomment-1126408637. |
| 295 |
t.Fatalf("CompareAndSwap on a non-existing key succeeded") |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
func TestMapRangeNoAllocations(t *testing.T) { // Issue 62404 |
| 300 |
testenv.SkipIfOptimizationOff(t) |
| 301 |
var m sync.Map |
| 302 |
allocs := testing.AllocsPerRun(10, func() { |
| 303 |
m.Range(func(key, value any) bool { |
| 304 |
return true |
| 305 |
}) |
| 306 |
}) |
| 307 |
if allocs > 0 { |
| 308 |
t.Errorf("AllocsPerRun of m.Range = %v; want 0", allocs) |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
// TestConcurrentClear tests concurrent behavior of sync.Map properties to ensure no data races. |
| 313 |
// Checks for proper synchronization between Clear, Store, Load operations. |
| 314 |
func TestConcurrentClear(t *testing.T) { |
| 315 |
var m sync.Map |
| 316 |
|
| 317 |
wg := sync.WaitGroup{} |
| 318 |
wg.Add(30) // 10 goroutines for writing, 10 goroutines for reading, 10 goroutines for waiting |
| 319 |
|
| 320 |
// Writing data to the map concurrently |
| 321 |
for i := 0; i < 10; i++ { |
| 322 |
go func(k, v int) { |
| 323 |
defer wg.Done() |
| 324 |
m.Store(k, v) |
| 325 |
}(i, i*10) |
| 326 |
} |
| 327 |
|
| 328 |
// Reading data from the map concurrently |
| 329 |
for i := 0; i < 10; i++ { |
| 330 |
go func(k int) { |
| 331 |
defer wg.Done() |
| 332 |
if value, ok := m.Load(k); ok { |
| 333 |
t.Logf("Key: %v, Value: %v\n", k, value) |
| 334 |
} else { |
| 335 |
t.Logf("Key: %v not found\n", k) |
| 336 |
} |
| 337 |
}(i) |
| 338 |
} |
| 339 |
|
| 340 |
// Clearing data from the map concurrently |
| 341 |
for i := 0; i < 10; i++ { |
| 342 |
go func() { |
| 343 |
defer wg.Done() |
| 344 |
m.Clear() |
| 345 |
}() |
| 346 |
} |
| 347 |
|
| 348 |
wg.Wait() |
| 349 |
|
| 350 |
m.Clear() |
| 351 |
|
| 352 |
m.Range(func(k, v any) bool { |
| 353 |
t.Errorf("after Clear, Map contains (%v, %v); expected to be empty", k, v) |
| 354 |
|
| 355 |
return true |
| 356 |
}) |
| 357 |
} |
| 358 |
|
| 359 |
func TestMapClearOneAllocation(t *testing.T) { |
| 360 |
testenv.SkipIfOptimizationOff(t) |
| 361 |
var m sync.Map |
| 362 |
allocs := testing.AllocsPerRun(10, func() { |
| 363 |
m.Clear() |
| 364 |
}) |
| 365 |
if allocs > 1 { |
| 366 |
t.Errorf("AllocsPerRun of m.Clear = %v; want 1", allocs) |
| 367 |
} |
| 368 |
} |