-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathqueue_test.go
143 lines (114 loc) · 2.52 KB
/
queue_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
package bigqueue
import (
"math/rand"
"strings"
"sync"
"testing"
"time"
"github.com/spf13/cast"
"github.com/stretchr/testify/assert"
)
func init() {
rand.Seed(time.Now().Unix())
}
func TestBigQueueChain1(t *testing.T) {
payload := []byte("aabbccdd")
qc := NewQueueChains(10, 2)
// ok
err := qc.Push(payload)
assert.Equal(t, err, nil)
assert.Equal(t, qc.BucketLength(), 1)
// ok
err = qc.Push(payload)
assert.Equal(t, err, nil)
assert.Equal(t, qc.BucketLength(), 2)
// fail
err = qc.Push(payload)
assert.Equal(t, err, ErrOverflowQueue)
// ok
data, err := qc.Pop()
assert.Equal(t, err, nil)
assert.Equal(t, data, payload)
assert.Equal(t, qc.BucketLength(), 2)
// ok
qc.Pop()
assert.Equal(t, qc.chains.Len(), 1)
// fail
bs, err := qc.Pop()
assert.Equal(t, len(bs), 0)
assert.Equal(t, qc.chains.Len(), 1)
assert.Equal(t, err, ErrEmptyQueue)
for i := 0; i < 5; i++ {
err := qc.Push([]byte(cast.ToString(i)))
assert.Equal(t, err, nil)
}
for i := 0; i < 5; i++ {
val, err := qc.Pop()
assert.Equal(t, err, nil)
assert.Equal(t, val, []byte(cast.ToString(i)))
}
}
func TestBigQueueChain2(t *testing.T) {
qc := NewQueueChains(1*1024*1024, 10)
count := 10000
for i := 0; i < count; i++ {
length := rand.Intn(1024)
bs := "{{" + randString(length) + "}}"
qc.Push([]byte(bs))
}
incr := 0
for i := 0; i < count; i++ {
val, err := qc.Pop()
if err != nil {
continue
}
str := string(val)
if strings.HasSuffix(str, "}}") && strings.HasPrefix(str, "{{") {
incr++
continue
}
t.Fatal("bad")
}
assert.Equal(t, incr, count)
t.Log(incr)
}
func TestBigQueueChain3(t *testing.T) {
qc := NewQueueChains(10*1024*1024, 10) // 10mb * 10 = 100mb
count := 100000 // 10w
wg := sync.WaitGroup{}
incr := 0
wg.Add(1)
go func() {
defer wg.Done()
for i := 0; i < count; i++ {
time.Sleep(time.Duration(rand.Intn(5)) * time.Microsecond) // < 10us
val, err := qc.Pop()
for err == ErrEmptyQueue {
time.Sleep(1 * time.Microsecond)
val, err = qc.Pop()
}
str := string(val)
if strings.HasSuffix(str, "}}") && strings.HasPrefix(str, "{{") {
incr++
continue
}
return
}
}()
wg.Add(1)
go func() {
defer wg.Done()
for i := 0; i < count; i++ {
time.Sleep(time.Duration(rand.Intn(5)) * time.Microsecond) // < 10us
length := rand.Intn(1024)
bs := "{{" + randString(length) + "}}"
qc.Push([]byte(bs))
}
}()
wg.Wait()
assert.Equal(t, incr, count)
t.Log(incr)
}
func randString(n int) string {
return strings.Repeat(".", n)
}