-
Notifications
You must be signed in to change notification settings - Fork 1
/
slice_test.go
249 lines (197 loc) · 4.18 KB
/
slice_test.go
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package freesync
import (
"math/rand"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestSlice_Append(t *testing.T) {
var slice Slice
for i := 0; i < 102400; i++ {
index := slice.Append(i)
if !assert.Equal(t, i, index) {
t.Fatal()
}
}
length := slice.Length()
assert.Equal(t, 102400, length)
for i := 0; i < 102400; i++ {
got, _ := slice.Load(i).(int)
if !assert.Equal(t, i, got) {
t.Fatal()
}
}
index1 := slice.Append("1")
got, _ := slice.Load(index1).(string)
assert.Equal(t, "1", got)
index2 := slice.Append("2")
got, _ = slice.Load(index2).(string)
assert.Equal(t, "2", got)
index3 := slice.Append("3")
got, _ = slice.Load(index3).(string)
assert.Equal(t, "3", got)
}
func TestSlice_ConcurrentlyAppend(t *testing.T) {
var slice Slice
var wg sync.WaitGroup
wg.Add(500)
letGo := make(chan int)
for i := 0; i < 500; i++ {
go func() {
defer wg.Done()
r := rand.New(rand.NewSource(time.Now().UnixNano()))
<-letGo
for j := 0; j < 10000; j++ {
num := r.Int()
index := slice.Append(num)
p := slice.Load(index)
assert.NotNil(t, p)
got, _ := p.(int)
assert.Equal(t, num, got)
}
}()
}
time.Sleep(time.Millisecond * 200)
close(letGo)
wg.Wait()
length := slice.Length()
assert.Equal(t, 500*10000, length)
}
func TestSlice_Range(t *testing.T) {
var slice Slice
for i := 0; i < 10240; i++ {
slice.Append(i)
}
length := slice.Length()
assert.Equal(t, 10240, length)
var count int
slice.Range(func(index int, p interface{}) (stopIteration bool) {
assert.Equal(t, count, index)
num, ok := p.(int)
assert.True(t, ok, "Failed to load p by index %d", index)
if !assert.Equal(t, count, num) {
return true
}
count++
return false
})
}
func TestSlice_UpdateAt(t *testing.T) {
var slice Slice
for i := 0; i < 10240; i++ {
slice.Append(i)
}
for i := 0; i < 10240; i++ {
old := slice.UpdateAt(i, i*2)
assert.Equal(t, i, old)
}
length := slice.Length()
assert.Equal(t, 10240, length)
for i := 0; i < 10240; i++ {
num, _ := slice.Load(i).(int)
assert.Equal(t, i*2, num)
}
}
func TestSlice_ConcurrentlyUpdateAt(t *testing.T) {
var slice Slice
for i := 0; i < 2000; i++ {
slice.Append(i)
}
var wg sync.WaitGroup
wg.Add(500)
letGo := make(chan int)
for i := 0; i < 500; i++ {
go func() {
defer wg.Done()
<-letGo
// 每个位置并发更新100次
for j := 0; j < 2000; j++ {
for k := 1; k <= 100; k++ {
slice.UpdateAt(j, j*k)
}
}
}()
}
time.Sleep(time.Millisecond * 200)
close(letGo)
wg.Wait()
// 检查
for i := 0; i < 2000; i++ {
num, _ := slice.Load(i).(int)
assert.Equal(t, i*100, num)
}
}
func TestSlice_ConcurrentlyAppendAndUpdateAt(t *testing.T) {
var slice Slice
var wg sync.WaitGroup
wg.Add(500)
letGo := make(chan int)
indexChann := make(chan int, 1000000)
for i := 0; i < 500; i++ {
go func() {
defer wg.Done()
<-letGo
for j := 0; j < 10000; j++ {
// 随便塞个数据
index := slice.Append(j)
// 先自己更新一次
slice.UpdateAt(index, index*2)
// 请其它goroutine更新一次
indexChann <- index
// 更新其它的goroutine的数据一次
index = <-indexChann
num, _ := slice.Load(index).(int)
slice.UpdateAt(index, num*5)
}
}()
}
time.Sleep(time.Millisecond * 200)
close(letGo)
wg.Wait()
// 检查
length := slice.Length()
assert.Equal(t, 500*10000, length)
slice.Range(func(index int, p interface{}) (stopIteration bool) {
num, _ := p.(int)
assert.Equal(t, index*10, num)
return false
})
}
func TestSlice_concurrentlyAppendAndRange(t *testing.T) {
var slice Slice
big := 50000
var wg sync.WaitGroup
for i := 0; i < 100; i++ {
wg.Add(1)
go func() {
defer wg.Done()
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for _, num := range r.Perm(big) {
slice.Append(num)
}
}()
}
var done = false
go func() {
wg.Wait()
done = true
}()
var wg2 sync.WaitGroup
for i := 0; i < 100; i++ {
wg2.Add(1)
go func() {
defer wg2.Done()
for {
if done {
return
}
slice.Range(func(index int, p interface{}) (stopIteration bool) {
_ = p.(int)
return false
})
}
}()
}
wg2.Wait()
}