-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmightymap_concurency_test.go
More file actions
55 lines (47 loc) · 1.05 KB
/
mightymap_concurency_test.go
File metadata and controls
55 lines (47 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package mightymap_test
import (
"context"
"sync"
"testing"
"github.com/thisisdevelopment/mightymap"
)
func TestMightyMap_Concurrency(t *testing.T) {
// Testing concurrent access to MightyMap
ctx := context.Background()
cm := mightymap.New[int, int](true)
var wg sync.WaitGroup
t.Run("Concurrent Store", func(t *testing.T) {
for i := 0; i < 1000; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
cm.Store(ctx, i, i*i)
}(i)
}
wg.Wait()
if cm.Len(ctx) != 1000 {
t.Errorf("Expected 1000 items, got %d", cm.Len(ctx))
}
err := cm.Close(ctx)
if err != nil {
t.Errorf("Error closing map: %v", err)
}
})
t.Run("Concurrent Load and Delete", func(t *testing.T) {
for i := 0; i < 1000; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
value, ok := cm.Load(ctx, i)
if !ok || value != i*i {
t.Errorf("Expected to load %d, got %d", i*i, value)
}
cm.Delete(ctx, i)
}(i)
}
wg.Wait()
if cm.Len(ctx) != 0 {
t.Errorf("Expected map to be empty after deletes, got %d", cm.Len(ctx))
}
})
}