-
Notifications
You must be signed in to change notification settings - Fork 4
/
queue_chan_test.go
121 lines (101 loc) · 2.34 KB
/
queue_chan_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
package yiyidb
import (
"fmt"
"github.com/stretchr/testify/assert"
"strconv"
"testing"
"time"
)
func TestChanQueue_EnqueueObject(t *testing.T) {
file := fmt.Sprintf("test_db_%d", time.Now().UnixNano())
q, err := OpenChanQueue(file, 10)
if err != nil {
t.Error(err)
}
defer q.Close()
type Object struct {
Value []byte
Key string
}
msg := &Object{}
msg.Key = "dkfjdkf"
msg.Value = []byte("ddddd")
for i := 1; i <= 5; i++ {
msg := Object{
Key: "dkfjdkf" + strconv.Itoa(i),
Value: []byte("ddddd" + strconv.Itoa(i)),
}
_, err := q.EnqueueObject("jac", msg)
assert.NoError(t, err)
}
vals, err := q.PeekStart("jac")
assert.NoError(t, err)
remsg := Object{}
err = vals[0].ToObject(&remsg)
assert.NoError(t, err)
assert.Equal(t, remsg.Value, []byte("ddddd1"))
q.Drop()
}
func TestQueueChan_Enqueue(t *testing.T) {
file := fmt.Sprintf("test_db_%d", time.Now().UnixNano())
q, err := OpenChanQueue(file, 10)
if err != nil {
t.Error(err)
}
defer q.Close()
for i := 1; i <= 5; i++ {
_, err = q.Enqueue("jac", []byte(fmt.Sprintf("value for item %d", i)))
assert.NoError(t, err)
}
for i := 1; i <= 8; i++ {
_, err = q.Enqueue("quy", []byte(fmt.Sprintf("value for item %d", i)))
assert.NoError(t, err)
}
for i := 0; i < 5; i++ {
_, err = q.Dequeue("jac")
assert.NoError(t, err)
}
for i := 0; i < 8; i++ {
_, err := q.Dequeue("quy")
assert.NoError(t, err)
}
q.Drop()
}
func TestChanQueue_Clear(t *testing.T) {
file := fmt.Sprintf("test_db_%d", time.Now().UnixNano())
q, err := OpenChanQueue(file, 10)
if err != nil {
t.Error(err)
}
defer q.Close()
for i := 1; i <= 5; i++ {
if _, err = q.Enqueue("jac", []byte(fmt.Sprintf("value for item %d", i))); err != nil {
t.Error(err)
}
}
q.Clear("jac")
_, err = q.Dequeue("jac")
assert.Equal(t, err.Error(), "queue is empty")
//fmt.Println("deq:",deqItem.ID, string(deqItem.Value))
}
func BenchmarkQueueChan_Dequeue(b *testing.B) {
// Open test database
file := fmt.Sprintf("test_db_%d", time.Now().UnixNano())
q, err := OpenChanQueue(file, 3)
if err != nil {
b.Error(err)
}
defer q.Drop()
// Fill with dummy data
for n := 0; n < b.N; n++ {
if _, err = q.Enqueue("jac", []byte("value")); err != nil {
b.Error(err)
}
}
// Start benchmark
b.ResetTimer()
b.ReportAllocs()
for n := 0; n < b.N; n++ {
_, _ = q.Dequeue("jac")
}
}