-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue_test.go
More file actions
162 lines (155 loc) · 4.13 KB
/
queue_test.go
File metadata and controls
162 lines (155 loc) · 4.13 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
package LFQueue
import (
"fmt"
"sync"
"testing"
)
var inCapacity = 1024
func TestNewQue(t *testing.T) {
que := NewQue(inCapacity)
capacity := uint64(getCapacity(inCapacity))
if que.capacity != capacity {
t.Error("que's capacity is wrong")
}
if que.endIndex != capacity-1 {
t.Error("que's endIndex is wrong")
}
if que.writeCursor != 0 {
t.Error("que's writeCursor is wrong")
}
if que.readCursor != 0 {
t.Error("que's readCursor is wrong")
}
if uint64(len(que.ringBuffer)) != capacity {
t.Error("que's len of ringBuffer is wrong")
}
for _, value := range que.availableBuffer {
if value != -1 {
t.Error("que's value of available is not -1")
}
}
}
func TestLFQueue_Push(t *testing.T) {
que := NewQue(inCapacity)
capacity := uint64(getCapacity(inCapacity))
var wg sync.WaitGroup
for item := uint64(1); item <= capacity+2; item++ {
wg.Add(1)
go func(i uint64) {
defer wg.Done()
var err *QueError
err = que.Push(i)
if err != nil {
if err.StatusCode == QueueIsFull {
fmt.Printf("err: %s, value: %d\n", err, i)
} else {
t.Errorf("que's push method run with wrong:%+v", err)
}
}
}(item)
}
wg.Wait()
checkMap := make(map[uint64]int)
for index, value := range que.ringBuffer {
if value.value == nil {
continue
}
if checkMap[value.value.(uint64)] == 0 {
checkMap[value.value.(uint64)] = index
} else {
t.Errorf("data error occurs!index:%d, value:%d", index, value.value.(uint64))
}
}
}
func TestLFQueue_Pop(t *testing.T) {
que := NewQue(inCapacity)
var wg sync.WaitGroup
item := 1
for ; item <= inCapacity; item++ {
wg.Add(1)
go func(item int) {
defer wg.Done()
err := que.Push(item)
if err != nil {
t.Errorf("que's push method run with wrong:%s", err)
}
}(item)
}
wg.Wait()
var checkMap sync.Map
index := 0
flag := true
for flag {
wg.Add(1)
go func(i int) {
defer wg.Done()
value, err := que.Pop()
if err != nil {
if err.StatusCode == QueueIsEmpty {
flag = false
return
}
}
_, ok := checkMap.Load(value.(int))
if ok {
t.Errorf("data error occurs!index:%d, value:%d", i, value.(int))
} else {
checkMap.Store(value.(int), 1)
}
}(index)
index++
}
wg.Wait()
}
func BenchmarkLFQueue_Push(b *testing.B) {
var wg sync.WaitGroup
que := NewQue(b.N)
capacity := uint64(b.N)
for item := uint64(1); item <= capacity; item++ {
wg.Add(1)
go func(i uint64) {
defer wg.Done()
var err *QueError
err = que.Push(i)
if err != nil {
if err.StatusCode == QueueIsFull {
fmt.Printf("err: %s, value: %d\n", err, i)
} else {
b.Errorf("que's push method run with wrong:%+v", err)
}
}
}(item)
}
wg.Wait()
}
func BenchmarkLFQueue_Pop(b *testing.B) {
b.StopTimer()
que := NewQue(b.N)
var wg sync.WaitGroup
item := 1
for ; item <= b.N; item++ {
wg.Add(1)
go func(item int) {
defer wg.Done()
err := que.Push(item)
if err != nil {
b.Errorf("que's push method run with wrong:%s", err)
}
}(item)
}
wg.Wait()
b.StartTimer()
for index := 0; index < b.N; index++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
_, err := que.Pop()
if err != nil {
if err.StatusCode != QueueIsEmpty {
b.Errorf("error: %+v", err)
}
}
}(index)
}
wg.Wait()
}