forked from alicebob/miniredis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd_connection_test.go
112 lines (88 loc) · 2.1 KB
/
cmd_connection_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
package miniredis
import (
"testing"
"github.com/gomodule/redigo/redis"
)
func TestAuth(t *testing.T) {
s, err := Run()
ok(t, err)
defer s.Close()
c, err := redis.Dial("tcp", s.Addr())
ok(t, err)
_, err = c.Do("AUTH", "foo", "bar")
mustFail(t, err, "ERR wrong number of arguments for 'auth' command")
s.RequireAuth("nocomment")
_, err = c.Do("PING", "foo", "bar")
mustFail(t, err, "NOAUTH Authentication required.")
_, err = c.Do("AUTH", "wrongpasswd")
mustFail(t, err, "ERR invalid password")
_, err = c.Do("AUTH", "nocomment")
ok(t, err)
_, err = c.Do("PING")
ok(t, err)
}
func TestPing(t *testing.T) {
s, err := Run()
ok(t, err)
defer s.Close()
c, err := redis.Dial("tcp", s.Addr())
ok(t, err)
r, err := redis.String(c.Do("PING"))
ok(t, err)
equals(t, "PONG", r)
r, err = redis.String(c.Do("PING", "hi"))
ok(t, err)
equals(t, "hi", r)
_, err = c.Do("PING", "foo", "bar")
mustFail(t, err, errWrongNumber("ping"))
}
func TestEcho(t *testing.T) {
s, err := Run()
ok(t, err)
defer s.Close()
c, err := redis.Dial("tcp", s.Addr())
ok(t, err)
r, err := redis.String(c.Do("ECHO", "hello\nworld"))
ok(t, err)
equals(t, "hello\nworld", r)
}
func TestSelect(t *testing.T) {
s, err := Run()
ok(t, err)
defer s.Close()
c, err := redis.Dial("tcp", s.Addr())
ok(t, err)
_, err = redis.String(c.Do("SET", "foo", "bar"))
ok(t, err)
_, err = redis.String(c.Do("SELECT", "5"))
ok(t, err)
_, err = redis.String(c.Do("SET", "foo", "baz"))
ok(t, err)
// Direct access.
got, err := s.Get("foo")
ok(t, err)
equals(t, "bar", got)
s.Select(5)
got, err = s.Get("foo")
ok(t, err)
equals(t, "baz", got)
// Another connection should have its own idea of the db:
c2, err := redis.Dial("tcp", s.Addr())
ok(t, err)
v, err := redis.String(c2.Do("GET", "foo"))
ok(t, err)
equals(t, "bar", v)
}
func TestQuit(t *testing.T) {
s, err := Run()
ok(t, err)
defer s.Close()
c, err := redis.Dial("tcp", s.Addr())
ok(t, err)
v, err := redis.String(c.Do("QUIT"))
ok(t, err)
equals(t, "OK", v)
v, err = redis.String(c.Do("PING"))
assert(t, err != nil, "QUIT closed the client")
equals(t, "", v)
}