conn_test.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. )
  24. var sendTests = []struct {
  25. args []interface{}
  26. expected string
  27. }{
  28. {
  29. []interface{}{"SET", "foo", "bar"},
  30. "*3\r\n$3\r\nSET\r\n$3\r\nfoo\r\n$3\r\nbar\r\n",
  31. },
  32. {
  33. []interface{}{"SET", "foo", "bar"},
  34. "*3\r\n$3\r\nSET\r\n$3\r\nfoo\r\n$3\r\nbar\r\n",
  35. },
  36. {
  37. []interface{}{"SET", "foo", 100},
  38. "*3\r\n$3\r\nSET\r\n$3\r\nfoo\r\n$3\r\n100\r\n",
  39. },
  40. {
  41. []interface{}{"SET", "", []byte("foo")},
  42. "*3\r\n$3\r\nSET\r\n$0\r\n\r\n$3\r\nfoo\r\n",
  43. },
  44. {
  45. []interface{}{"SET", nil, []byte("foo")},
  46. "*3\r\n$3\r\nSET\r\n$0\r\n\r\n$3\r\nfoo\r\n",
  47. },
  48. }
  49. func TestSend(t *testing.T) {
  50. for _, tt := range sendTests {
  51. var buf bytes.Buffer
  52. rw := bufio.ReadWriter{Writer: bufio.NewWriter(&buf)}
  53. c := redis.NewConnBufio(rw)
  54. err := c.Send(tt.args[0].(string), tt.args[1:]...)
  55. if err != nil {
  56. t.Errorf("Send(%v) returned error %v", tt.args, err)
  57. continue
  58. }
  59. rw.Flush()
  60. actual := buf.String()
  61. if actual != tt.expected {
  62. t.Errorf("Send(%v) = %q, want %q", tt.args, actual, tt.expected)
  63. }
  64. }
  65. }
  66. var errorSentinel = &struct{}{}
  67. var receiveTests = []struct {
  68. reply string
  69. expected interface{}
  70. }{
  71. {
  72. "+OK\r\n",
  73. "OK",
  74. },
  75. {
  76. "@OK\r\n",
  77. errorSentinel,
  78. },
  79. {
  80. "$6\r\nfoobar\r\n",
  81. []byte("foobar"),
  82. },
  83. {
  84. "$-1\r\n",
  85. nil,
  86. },
  87. {
  88. ":1\r\n",
  89. int64(1),
  90. },
  91. {
  92. "*0\r\n",
  93. []interface{}{},
  94. },
  95. {
  96. "*-1\r\n",
  97. nil,
  98. },
  99. {
  100. "*4\r\n$3\r\nfoo\r\n$3\r\nbar\r\n$5\r\nHello\r\n$5\r\nWorld\r\n",
  101. []interface{}{[]byte("foo"), []byte("bar"), []byte("Hello"), []byte("World")},
  102. },
  103. {
  104. "*3\r\n$3\r\nfoo\r\n$-1\r\n$3\r\nbar\r\n",
  105. []interface{}{[]byte("foo"), nil, []byte("bar")},
  106. },
  107. }
  108. func TestReceive(t *testing.T) {
  109. for _, tt := range receiveTests {
  110. rw := bufio.ReadWriter{
  111. Reader: bufio.NewReader(strings.NewReader(tt.reply)),
  112. Writer: bufio.NewWriter(nil), // writer need to support Flush
  113. }
  114. c := redis.NewConnBufio(rw)
  115. actual, err := c.Receive()
  116. if tt.expected == errorSentinel {
  117. if err == nil {
  118. t.Errorf("Receive(%q) did not return expected error", tt.reply)
  119. }
  120. } else {
  121. if err != nil {
  122. t.Errorf("Receive(%q) returned error %v", tt.reply, err)
  123. continue
  124. }
  125. if !reflect.DeepEqual(actual, tt.expected) {
  126. t.Errorf("Receive(%q) = %v, want %v", tt.reply, actual, tt.expected)
  127. }
  128. }
  129. }
  130. }
  131. func connect() (redis.Conn, error) {
  132. c, err := redis.Dial("tcp", ":6379")
  133. if err != nil {
  134. return nil, err
  135. }
  136. reply, err := c.Do("SELECT", "9")
  137. if err != nil {
  138. return nil, err
  139. }
  140. reply, err = c.Do("DBSIZE")
  141. if err != nil {
  142. return nil, err
  143. }
  144. if reply, ok := reply.(int); !ok && reply != 0 {
  145. return nil, errors.New("Database #9 is not empty, test can not continue")
  146. }
  147. return c, nil
  148. }
  149. func disconnect(c redis.Conn) error {
  150. _, err := c.Do("SELECT", "9")
  151. if err != nil {
  152. return nil
  153. }
  154. _, err = c.Do("FLUSHDB")
  155. return err
  156. }
  157. var testCommands = []struct {
  158. args []interface{}
  159. expected interface{}
  160. }{
  161. {
  162. []interface{}{"PING"},
  163. "PONG",
  164. },
  165. {
  166. []interface{}{"SET", "foo", "bar"},
  167. "OK",
  168. },
  169. {
  170. []interface{}{"GET", "foo"},
  171. []byte("bar"),
  172. },
  173. {
  174. []interface{}{"GET", "nokey"},
  175. nil,
  176. },
  177. {
  178. []interface{}{"MGET", "nokey", "foo"},
  179. []interface{}{nil, []byte("bar")},
  180. },
  181. {
  182. []interface{}{"INCR", "mycounter"},
  183. int64(1),
  184. },
  185. {
  186. []interface{}{"LPUSH", "mylist", "foo"},
  187. int64(1),
  188. },
  189. {
  190. []interface{}{"LPUSH", "mylist", "bar"},
  191. int64(2),
  192. },
  193. {
  194. []interface{}{"LRANGE", "mylist", 0, -1},
  195. []interface{}{[]byte("bar"), []byte("foo")},
  196. },
  197. {
  198. []interface{}{"MULTI"},
  199. "OK",
  200. },
  201. {
  202. []interface{}{"LRANGE", "mylist", 0, -1},
  203. "QUEUED",
  204. },
  205. {
  206. []interface{}{"PING"},
  207. "QUEUED",
  208. },
  209. {
  210. []interface{}{"EXEC"},
  211. []interface{}{
  212. []interface{}{[]byte("bar"), []byte("foo")},
  213. "PONG",
  214. },
  215. },
  216. }
  217. func TestDoCommands(t *testing.T) {
  218. c, err := connect()
  219. if err != nil {
  220. t.Fatalf("Error connection to database, %v", err)
  221. }
  222. defer disconnect(c)
  223. for _, cmd := range testCommands {
  224. actual, err := c.Do(cmd.args[0].(string), cmd.args[1:]...)
  225. if err != nil {
  226. t.Errorf("Do(%v) returned error %v", cmd.args, err)
  227. continue
  228. }
  229. if !reflect.DeepEqual(actual, cmd.expected) {
  230. t.Errorf("Do(%v) = %v, want %v", cmd.args, actual, cmd.expected)
  231. }
  232. }
  233. }
  234. func TestPipelineCommands(t *testing.T) {
  235. c, err := connect()
  236. if err != nil {
  237. t.Fatalf("Error connection to database, %v", err)
  238. }
  239. defer disconnect(c)
  240. for _, cmd := range testCommands {
  241. err := c.Send(cmd.args[0].(string), cmd.args[1:]...)
  242. if err != nil {
  243. t.Errorf("Send(%v) returned error %v", cmd.args, err)
  244. continue
  245. }
  246. }
  247. for _, cmd := range testCommands {
  248. actual, err := c.Receive()
  249. if err != nil {
  250. t.Errorf("Receive(%v) returned error %v", cmd.args, err)
  251. continue
  252. }
  253. if !reflect.DeepEqual(actual, cmd.expected) {
  254. t.Errorf("Receive(%v) = %v, want %v", cmd.args, actual, cmd.expected)
  255. }
  256. }
  257. }