conn_test.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. // Copyright 2012 Gary Burd
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License"): you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  11. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  12. // License for the specific language governing permissions and limitations
  13. // under the License.
  14. package redis_test
  15. import (
  16. "bufio"
  17. "bytes"
  18. "errors"
  19. "github.com/garyburd/redigo/redis"
  20. "reflect"
  21. "strings"
  22. "testing"
  23. "time"
  24. )
  25. var writeTests = []struct {
  26. args []interface{}
  27. expected string
  28. }{
  29. {
  30. []interface{}{"SET", "foo", "bar"},
  31. "*3\r\n$3\r\nSET\r\n$3\r\nfoo\r\n$3\r\nbar\r\n",
  32. },
  33. {
  34. []interface{}{"SET", "foo", "bar"},
  35. "*3\r\n$3\r\nSET\r\n$3\r\nfoo\r\n$3\r\nbar\r\n",
  36. },
  37. {
  38. []interface{}{"SET", "foo", 100},
  39. "*3\r\n$3\r\nSET\r\n$3\r\nfoo\r\n$3\r\n100\r\n",
  40. },
  41. {
  42. []interface{}{"SET", "", []byte("foo")},
  43. "*3\r\n$3\r\nSET\r\n$0\r\n\r\n$3\r\nfoo\r\n",
  44. },
  45. {
  46. []interface{}{"SET", nil, []byte("foo")},
  47. "*3\r\n$3\r\nSET\r\n$0\r\n\r\n$3\r\nfoo\r\n",
  48. },
  49. }
  50. func TestWrite(t *testing.T) {
  51. for _, tt := range writeTests {
  52. var buf bytes.Buffer
  53. rw := bufio.ReadWriter{Writer: bufio.NewWriter(&buf)}
  54. c := redis.NewConnBufio(rw)
  55. err := c.Send(tt.args[0].(string), tt.args[1:]...)
  56. if err != nil {
  57. t.Errorf("Send(%v) returned error %v", tt.args, err)
  58. continue
  59. }
  60. rw.Flush()
  61. actual := buf.String()
  62. if actual != tt.expected {
  63. t.Errorf("Send(%v) = %q, want %q", tt.args, actual, tt.expected)
  64. }
  65. }
  66. }
  67. var errorSentinel = &struct{}{}
  68. var readTests = []struct {
  69. reply string
  70. expected interface{}
  71. }{
  72. {
  73. "+OK\r\n",
  74. "OK",
  75. },
  76. {
  77. "@OK\r\n",
  78. errorSentinel,
  79. },
  80. {
  81. "$6\r\nfoobar\r\n",
  82. []byte("foobar"),
  83. },
  84. {
  85. "$-1\r\n",
  86. nil,
  87. },
  88. {
  89. ":1\r\n",
  90. int64(1),
  91. },
  92. {
  93. "*0\r\n",
  94. []interface{}{},
  95. },
  96. {
  97. "*-1\r\n",
  98. nil,
  99. },
  100. {
  101. "*4\r\n$3\r\nfoo\r\n$3\r\nbar\r\n$5\r\nHello\r\n$5\r\nWorld\r\n",
  102. []interface{}{[]byte("foo"), []byte("bar"), []byte("Hello"), []byte("World")},
  103. },
  104. {
  105. "*3\r\n$3\r\nfoo\r\n$-1\r\n$3\r\nbar\r\n",
  106. []interface{}{[]byte("foo"), nil, []byte("bar")},
  107. },
  108. }
  109. func TestRead(t *testing.T) {
  110. for _, tt := range readTests {
  111. rw := bufio.ReadWriter{
  112. Reader: bufio.NewReader(strings.NewReader(tt.reply)),
  113. Writer: bufio.NewWriter(nil), // writer need to support Flush
  114. }
  115. c := redis.NewConnBufio(rw)
  116. actual, err := c.Receive()
  117. if tt.expected == errorSentinel {
  118. if err == nil {
  119. t.Errorf("Receive(%q) did not return expected error", tt.reply)
  120. }
  121. } else {
  122. if err != nil {
  123. t.Errorf("Receive(%q) returned error %v", tt.reply, err)
  124. continue
  125. }
  126. if !reflect.DeepEqual(actual, tt.expected) {
  127. t.Errorf("Receive(%q) = %v, want %v", tt.reply, actual, tt.expected)
  128. }
  129. }
  130. }
  131. }
  132. type testConn struct {
  133. redis.Conn
  134. }
  135. func (t testConn) Close() error {
  136. _, err := t.Conn.Do("SELECT", "9")
  137. if err != nil {
  138. return nil
  139. }
  140. _, err = t.Conn.Do("FLUSHDB")
  141. if err != nil {
  142. return err
  143. }
  144. return t.Conn.Close()
  145. }
  146. func dial() (redis.Conn, error) {
  147. c, err := redis.DialTimeout("tcp", ":6379", 0, 1*time.Second, 1*time.Second)
  148. if err != nil {
  149. return nil, err
  150. }
  151. _, err = c.Do("SELECT", "9")
  152. if err != nil {
  153. return nil, err
  154. }
  155. n, err := redis.Int(c.Do("DBSIZE"))
  156. if err != nil {
  157. return nil, err
  158. }
  159. if n != 0 {
  160. return nil, errors.New("Database #9 is not empty, test can not continue")
  161. }
  162. return testConn{c}, nil
  163. }
  164. func dialt(t *testing.T) redis.Conn {
  165. c, err := dial()
  166. if err != nil {
  167. t.Fatalf("error connection to database, %v", err)
  168. }
  169. return c
  170. }
  171. var testCommands = []struct {
  172. args []interface{}
  173. expected interface{}
  174. }{
  175. {
  176. []interface{}{"PING"},
  177. "PONG",
  178. },
  179. {
  180. []interface{}{"SET", "foo", "bar"},
  181. "OK",
  182. },
  183. {
  184. []interface{}{"GET", "foo"},
  185. []byte("bar"),
  186. },
  187. {
  188. []interface{}{"GET", "nokey"},
  189. nil,
  190. },
  191. {
  192. []interface{}{"MGET", "nokey", "foo"},
  193. []interface{}{nil, []byte("bar")},
  194. },
  195. {
  196. []interface{}{"INCR", "mycounter"},
  197. int64(1),
  198. },
  199. {
  200. []interface{}{"LPUSH", "mylist", "foo"},
  201. int64(1),
  202. },
  203. {
  204. []interface{}{"LPUSH", "mylist", "bar"},
  205. int64(2),
  206. },
  207. {
  208. []interface{}{"LRANGE", "mylist", 0, -1},
  209. []interface{}{[]byte("bar"), []byte("foo")},
  210. },
  211. {
  212. []interface{}{"MULTI"},
  213. "OK",
  214. },
  215. {
  216. []interface{}{"LRANGE", "mylist", 0, -1},
  217. "QUEUED",
  218. },
  219. {
  220. []interface{}{"PING"},
  221. "QUEUED",
  222. },
  223. {
  224. []interface{}{"EXEC"},
  225. []interface{}{
  226. []interface{}{[]byte("bar"), []byte("foo")},
  227. "PONG",
  228. },
  229. },
  230. }
  231. func TestDoCommands(t *testing.T) {
  232. c := dialt(t)
  233. defer c.Close()
  234. for _, cmd := range testCommands {
  235. actual, err := c.Do(cmd.args[0].(string), cmd.args[1:]...)
  236. if err != nil {
  237. t.Errorf("Do(%v) returned error %v", cmd.args, err)
  238. continue
  239. }
  240. if !reflect.DeepEqual(actual, cmd.expected) {
  241. t.Errorf("Do(%v) = %v, want %v", cmd.args, actual, cmd.expected)
  242. }
  243. }
  244. }
  245. func TestPipelineCommands(t *testing.T) {
  246. c := dialt(t)
  247. defer c.Close()
  248. for _, cmd := range testCommands {
  249. if err := c.Send(cmd.args[0].(string), cmd.args[1:]...); err != nil {
  250. t.Errorf("Send(%v) returned error %v", cmd.args, err)
  251. continue
  252. }
  253. }
  254. if err := c.Flush(); err != nil {
  255. t.Errorf("Flush() returned error %v", err)
  256. }
  257. for _, cmd := range testCommands {
  258. actual, err := c.Receive()
  259. if err != nil {
  260. t.Errorf("Receive(%v) returned error %v", cmd.args, err)
  261. continue
  262. }
  263. if !reflect.DeepEqual(actual, cmd.expected) {
  264. t.Errorf("Receive(%v) = %v, want %v", cmd.args, actual, cmd.expected)
  265. }
  266. }
  267. }
  268. func TestError(t *testing.T) {
  269. c := dialt(t)
  270. defer c.Close()
  271. c.Do("SET", "key", "val")
  272. _, err := c.Do("HSET", "key", "fld", "val")
  273. if err == nil {
  274. t.Errorf("Expected err for HSET on string key.")
  275. }
  276. if c.Err() != nil {
  277. t.Errorf("Conn has Err()=%v, expect nil", c.Err())
  278. }
  279. _, err = c.Do("SET", "key", "val")
  280. if err != nil {
  281. t.Errorf("Do(SET, key, val) returned error %v, expected nil.", err)
  282. }
  283. }