-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpcountmap_test.go
More file actions
171 lines (143 loc) · 3.44 KB
/
Copy pathpcountmap_test.go
File metadata and controls
171 lines (143 loc) · 3.44 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
import (
"fmt"
"sync"
"testing"
"testing/synctest"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/stretchr/testify/require"
)
func TestProviderMapConcurrentAddSameProvider(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
const (
goroutines = 64
addsPerGoroutine = 250
)
pm := NewProviderMap(1)
provider := randomPeerID(t)
var wg sync.WaitGroup
for range goroutines {
wg.Go(func() {
for range addsPerGoroutine {
pm.Add(provider)
}
})
}
wg.Wait()
synctest.Wait()
top := pm.Top()
require.Len(t, top, 1)
require.Equal(t, provider, top[0].Provider)
require.Equal(t, int64(goroutines*addsPerGoroutine), top[0].Count)
})
}
func TestProviderMapConcurrentAddDistinctProviders(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
const (
providerCount = 64
goroutines = 64
addsPerGoroutine = 100
)
pm := NewProviderMap(providerCount)
providers := make([]peer.ID, providerCount)
for i := range providers {
providers[i] = randomPeerID(t)
}
var wg sync.WaitGroup
for g := range goroutines {
provider := providers[g]
wg.Go(func() {
for range addsPerGoroutine {
pm.Add(provider)
}
})
}
wg.Wait()
synctest.Wait()
counts := providerCountMap(pm, providerCount)
require.Len(t, counts, providerCount)
for i, provider := range providers {
require.Equal(t, int64(addsPerGoroutine), counts[provider], "provider %d", i)
}
})
}
func TestProviderMapConcurrentAddOverlappingProviders(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
const (
providerCount = 32
goroutines = 96
addsPerGoroutine = 200
)
pm := NewProviderMap(providerCount)
providers := make([]peer.ID, providerCount)
for i := range providers {
providers[i] = randomPeerID(t)
}
var wg sync.WaitGroup
for g := range goroutines {
wg.Go(func() {
for j := range addsPerGoroutine {
pm.Add(providers[(g+j)%providerCount])
}
})
}
wg.Wait()
synctest.Wait()
counts := providerCountMap(pm, providerCount)
require.Len(t, counts, providerCount)
var total int64
for _, count := range counts {
total += count
}
require.Equal(t, int64(goroutines*addsPerGoroutine), total)
})
}
func providerCountMap(pm *ProviderMap, cardinality int) map[peer.ID]int64 {
orig := pm.cardinality
pm.cardinality = cardinality
defer func() { pm.cardinality = orig }()
counts := make(map[peer.ID]int64, cardinality)
for _, rcrd := range pm.Top() {
counts[rcrd.Provider] = rcrd.Count
}
return counts
}
func TestProviderMapConcurrentStress(t *testing.T) {
if testing.Short() {
t.Skip("skipping stress test in short mode")
}
synctest.Test(t, func(t *testing.T) {
const (
providerCount = 64
goroutines = 128
operations = 2000
)
pm := NewProviderMap(10)
providers := make([]peer.ID, providerCount)
for i := range providers {
providers[i] = peer.ID(fmt.Appendf(nil, "provider-%d-%s", i, randomPeerID(t)))
}
var wg sync.WaitGroup
for g := range goroutines {
wg.Go(func() {
for op := range operations {
switch op % 3 {
case 0, 1:
pm.Add(providers[(g+op)%providerCount])
case 2:
_ = pm.Top()
}
}
})
}
wg.Wait()
synctest.Wait()
counts := providerCountMap(pm, providerCount)
require.NotEmpty(t, counts)
top := pm.Top()
require.Len(t, top, 10)
for i := 1; i < len(top); i++ {
require.GreaterOrEqual(t, top[i-1].Count, top[i].Count)
}
})
}