-
Notifications
You must be signed in to change notification settings - Fork 13
/
pipeline_test.go
135 lines (124 loc) · 2.47 KB
/
pipeline_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
package gore
import (
"os"
"testing"
)
func init() {
if os.Getenv("TEST_REDIS_CLIENT") != "" {
shouldTest = true
}
}
func TestPipeline(t *testing.T) {
if !shouldTest {
return
}
conn, err := Dial("localhost:6379")
if err != nil {
t.Fatal(err)
}
defer conn.Close()
p := NewPipeline()
for i := 0; i < 100; i++ {
p.Add(NewCommand("SET", i, i))
}
replies, err := p.Run(conn)
if err != nil || len(replies) != 100 {
t.Fatal(err, len(replies))
}
for _, rep := range replies {
if !rep.IsOk() {
t.Fatal("not ok", rep)
}
}
p = NewPipeline()
for i := 0; i < 100; i++ {
p.Add(NewCommand("GET", i))
}
replies, err = p.Run(conn)
if err != nil || len(replies) != 100 {
t.Fatal(err, len(replies))
}
for i, rep := range replies {
x, err := rep.Int()
if err != nil || x != int64(i) {
t.Fatal(err, i, x)
}
}
rep, err := NewCommand("FLUSHALL").Run(conn)
if err != nil || !rep.IsOk() {
t.Fatal(err, "not ok")
}
}
func TestPipelineGoroutine(t *testing.T) {
if !shouldTest {
return
}
conn, err := Dial("localhost:6379")
if err != nil {
t.Fatal(err)
}
defer conn.Close()
c := make(chan bool, 20)
for i := 0; i < 1000; i++ {
go func(conn *Conn, c chan bool, x int64) {
defer func() {
c <- true
}()
_, err := NewCommand("SET", x, x).Run(conn)
if err != nil {
t.Fatal(err)
}
rep, err := NewCommand("GET", x).Run(conn)
if err != nil {
t.Fatal(err)
}
y, err := rep.Int()
if err != nil || y != x {
t.Fatal(err, x, y)
}
}(conn, c, int64(i))
}
for i := 0; i < 100; i++ {
go func(conn *Conn, c chan bool, x int64) {
defer func() {
c <- true
}()
p := NewPipeline()
for j := 0; j < 100; j++ {
val := 1000 + x*100 + int64(j)
p.Add(NewCommand("SET", val, val))
}
replies, err := p.Run(conn)
if err != nil || len(replies) != 100 {
t.Fatal(err, len(replies))
}
for _, r := range replies {
if !r.IsOk() {
t.Fatal("not ok", r)
}
}
p = NewPipeline()
for j := 0; j < 100; j++ {
val := 1000 + x*100 + int64(j)
p.Add(NewCommand("GET", val))
}
replies, err = p.Run(conn)
if err != nil || len(replies) != 100 {
t.Fatal(err, len(replies))
}
for j, r := range replies {
y, err := r.Int()
if err != nil || y != 1000+x*100+int64(j) {
t.Fatal(err, x, j, y)
}
}
}(conn, c, int64(i))
}
for i := 0; i < 1100; i++ {
<-c
}
rep, err := NewCommand("FLUSHALL").Run(conn)
if err != nil || !rep.IsOk() {
t.Fatal(err, "not ok")
}
}