forked from wangaoone/redeo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
238 lines (193 loc) · 4.3 KB
/
example_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
package resp_test
import (
"bytes"
"fmt"
"net"
"strings"
"github.com/mason-leap-lab/redeo/resp"
)
func Example_client() {
cn, _ := net.Dial("tcp", "127.0.0.1:6379")
defer cn.Close()
// Wrap connection
w := resp.NewRequestWriter(cn)
r := resp.NewResponseReader(cn)
// Write pipeline
w.WriteCmdString("PING")
w.WriteCmdString("ECHO", "HEllO")
w.WriteCmdString("GET", "key")
w.WriteCmdString("SET", "key", "value")
w.WriteCmdString("DEL", "key")
// Flush pipeline
if err := w.Flush(); err != nil {
panic(err)
}
// Consume responses
for i := 0; i < 5; i++ {
t, err := r.PeekType()
if err != nil {
return
}
switch t {
case resp.TypeInline:
s, _ := r.ReadInlineString()
fmt.Println(s)
case resp.TypeBulk:
s, _ := r.ReadBulkString()
fmt.Println(s)
case resp.TypeInt:
n, _ := r.ReadInt()
fmt.Println(n)
case resp.TypeNil:
_ = r.ReadNil()
fmt.Println(nil)
default:
panic("unexpected response type")
}
}
// Output:
// PONG
// HEllO
// <nil>
// OK
// 1
}
func ExampleRequestReader() {
cn := strings.NewReader("*1\r\n$4\r\nPING\r\n*2\r\n$4\r\nEcHO\r\n$5\r\nHeLLO\r\n")
r := resp.NewRequestReader(cn)
// read command
cmd, _ := r.ReadCmd(nil)
fmt.Println(cmd.Name)
for i := 0; i < len(cmd.Args); i++ {
fmt.Println(i, cmd.Args[i])
}
// read command, recycle previous instance
cmd, _ = r.ReadCmd(cmd)
fmt.Println(cmd.Name)
for i := 0; i < len(cmd.Args); i++ {
fmt.Println(i, cmd.Args[i])
}
// Output:
// PING
// EcHO
// 0 HeLLO
}
func ExampleResponseWriter() {
buf := new(bytes.Buffer)
w := resp.NewResponseWriter(buf)
// Append OK response
w.AppendOK()
// Append a number
w.AppendInt(33)
// Append an array
w.AppendArrayLen(3)
w.AppendBulkString("Adam")
w.AppendBulkString("Had'em")
w.AppendNil()
// Writer data must be flushed manually
fmt.Println(buf.Len(), w.Buffered())
if err := w.Flush(); err != nil {
panic(err)
}
// Once flushed, it will be sent to the underlying writer
// as a bulk
fmt.Println(buf.Len(), w.Buffered())
fmt.Printf("%q\n", buf.String())
// Output:
// 0 41
// 41 0
// "+OK\r\n:33\r\n*3\r\n$4\r\nAdam\r\n$6\r\nHad'em\r\n$-1\r\n"
}
func ExampleResponseWriter_AppendBulkString() {
b := new(bytes.Buffer)
w := resp.NewResponseWriter(b)
w.AppendBulkString("PONG")
w.Flush()
fmt.Printf("%q\n", b.String())
// Output:
// "$4\r\nPONG\r\n"
}
func ExampleResponseWriter_AppendInlineString() {
b := new(bytes.Buffer)
w := resp.NewResponseWriter(b)
w.AppendInlineString("PONG")
w.Flush()
fmt.Printf("%q\n", b.String())
// Output:
// "+PONG\r\n"
}
func ExampleResponseWriter_AppendNil() {
b := new(bytes.Buffer)
w := resp.NewResponseWriter(b)
w.AppendNil()
w.Flush()
fmt.Printf("%q\n", b.String())
// Output:
// "$-1\r\n"
}
func ExampleResponseWriter_AppendInt() {
b := new(bytes.Buffer)
w := resp.NewResponseWriter(b)
w.AppendInt(33)
w.Flush()
fmt.Printf("%q\n", b.String())
// Output:
// ":33\r\n"
}
func ExampleResponseWriter_AppendArrayLen() {
b := new(bytes.Buffer)
w := resp.NewResponseWriter(b)
w.AppendArrayLen(3)
w.AppendBulkString("item 1")
w.AppendNil()
w.AppendBulkString("item 2")
w.Flush()
fmt.Printf("%q\n", b.String())
// Output:
// "*3\r\n$6\r\nitem 1\r\n$-1\r\n$6\r\nitem 2\r\n"
}
func ExampleResponseWriter_CopyBulk() {
b := new(bytes.Buffer)
w := resp.NewResponseWriter(b)
w.CopyBulk(strings.NewReader("a streamer"), 8)
w.Flush()
fmt.Printf("%q\n", b.String())
// Output:
// "$8\r\na stream\r\n"
}
func ExampleResponseWriter_CopyBulk_in_array() {
b := new(bytes.Buffer)
w := resp.NewResponseWriter(b)
w.AppendArrayLen(2)
w.AppendBulkString("item 1")
w.CopyBulk(strings.NewReader("item 2"), 6)
w.Flush()
fmt.Printf("%q\n", b.String())
// Output:
// "*2\r\n$6\r\nitem 1\r\n$6\r\nitem 2\r\n"
}
func ExampleResponseReader_Scan() {
b := new(bytes.Buffer)
w := resp.NewResponseWriter(b)
w.AppendBulkString("foo")
w.AppendInt(33)
w.AppendInlineString("7.54")
w.AppendArrayLen(2)
w.AppendInlineString("bar")
w.AppendInt(14)
w.Flush()
var v struct {
String string
Number int
Decimal float64
Slice []string
}
r := resp.NewResponseReader(b)
if err := r.Scan(&v.String, &v.Number, &v.Decimal, &v.Slice); err != nil {
fmt.Printf("ERROR: %v\n", err)
return
}
fmt.Printf("%+v\n", v)
// Output:
// {String:foo Number:33 Decimal:7.54 Slice:[bar 14]}
}