-
Notifications
You must be signed in to change notification settings - Fork 39
/
example_test.go
166 lines (136 loc) · 3.32 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
package redeo_test
import (
"net"
"sync"
"github.com/bsm/redeo/v2"
"github.com/bsm/redeo/v2/resp"
)
func ExampleServer() {
srv := redeo.NewServer(nil)
// Define handlers
srv.HandleFunc("ping", func(w resp.ResponseWriter, _ *resp.Command) {
w.AppendInlineString("PONG")
})
srv.HandleFunc("info", func(w resp.ResponseWriter, _ *resp.Command) {
w.AppendBulkString(srv.Info().String())
})
// More handlers; demo usage of redeo.WrapperFunc
srv.Handle("echo", redeo.WrapperFunc(func(c *resp.Command) interface{} {
if c.ArgN() != 1 {
return redeo.ErrWrongNumberOfArgs(c.Name)
}
return c.Arg(0)
}))
// Open a new listener
lis, err := net.Listen("tcp", ":9736")
if err != nil {
panic(err)
}
defer lis.Close()
// Start serving (blocking)
if err := srv.Serve(lis); err != nil {
panic(err)
}
}
func ExampleClient() {
srv := redeo.NewServer(nil)
srv.HandleFunc("myip", func(w resp.ResponseWriter, cmd *resp.Command) {
client := redeo.GetClient(cmd.Context())
if client == nil {
w.AppendNil()
return
}
w.AppendInlineString(client.RemoteAddr().String())
})
}
func ExamplePing() {
srv := redeo.NewServer(nil)
srv.Handle("ping", redeo.Ping())
}
func ExampleInfo() {
srv := redeo.NewServer(nil)
srv.Handle("info", redeo.Info(srv))
}
func ExampleCommandDescriptions() {
srv := redeo.NewServer(nil)
srv.Handle("command", redeo.CommandDescriptions{
{Name: "get", Arity: 2, Flags: []string{"readonly", "fast"}, FirstKey: 1, LastKey: 1, KeyStepCount: 1},
{Name: "randomkey", Arity: 1, Flags: []string{"readonly", "random"}},
{Name: "mset", Arity: -3, Flags: []string{"write", "denyoom"}, FirstKey: 1, LastKey: -1, KeyStepCount: 2},
{Name: "quit", Arity: 1},
})
}
func ExampleSubCommands() {
srv := redeo.NewServer(nil)
srv.Handle("custom", redeo.SubCommands{
"ping": redeo.Ping(),
"echo": redeo.Echo(),
})
}
func ExamplePubSubBroker() {
broker := redeo.NewPubSubBroker()
srv := redeo.NewServer(nil)
srv.Handle("publish", broker.Publish())
srv.Handle("subscribe", broker.Subscribe())
}
func ExampleHandlerFunc() {
mu := sync.RWMutex{}
data := make(map[string]string)
srv := redeo.NewServer(nil)
srv.HandleFunc("set", func(w resp.ResponseWriter, c *resp.Command) {
if c.ArgN() != 2 {
w.AppendError(redeo.WrongNumberOfArgs(c.Name))
return
}
key := c.Arg(0).String()
val := c.Arg(1).String()
mu.Lock()
data[key] = val
mu.Unlock()
w.AppendInt(1)
})
srv.HandleFunc("get", func(w resp.ResponseWriter, c *resp.Command) {
if c.ArgN() != 1 {
w.AppendError(redeo.WrongNumberOfArgs(c.Name))
return
}
key := c.Arg(0).String()
mu.RLock()
val, ok := data[key]
mu.RUnlock()
if ok {
w.AppendBulkString(val)
return
}
w.AppendNil()
})
}
func ExampleWrapperFunc() {
mu := sync.RWMutex{}
data := make(map[string]string)
srv := redeo.NewServer(nil)
srv.Handle("set", redeo.WrapperFunc(func(c *resp.Command) interface{} {
if c.ArgN() != 2 {
return redeo.ErrWrongNumberOfArgs(c.Name)
}
key := c.Arg(0).String()
val := c.Arg(1).String()
mu.Lock()
data[key] = val
mu.Unlock()
return 1
}))
srv.Handle("get", redeo.WrapperFunc(func(c *resp.Command) interface{} {
if c.ArgN() != 1 {
return redeo.ErrWrongNumberOfArgs(c.Name)
}
key := c.Arg(0).String()
mu.RLock()
val, ok := data[key]
mu.RUnlock()
if ok {
return val
}
return nil
}))
}