github.com/cellofellow/gopkg@v0.0.0-20140722061823-eec0544a62ad/database/redis/client_test.go (about)

     1  // Copyright 2014 <chaishushan{AT}gmail.com>. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package redis
     6  
     7  import (
     8  	"bufio"
     9  	"bytes"
    10  	"time"
    11  
    12  	. "chai2010.gopkg/check"
    13  )
    14  
    15  type ClientSuite struct {
    16  	c *Client
    17  }
    18  
    19  func init() {
    20  	Suite(&ClientSuite{})
    21  }
    22  
    23  func (s *ClientSuite) SetUpTest(c *C) {
    24  	var err error
    25  	s.c, err = DialTimeout("tcp", "127.0.0.1:6379", time.Duration(10)*time.Second)
    26  	c.Assert(err, IsNil)
    27  
    28  	// select database
    29  	r := s.c.Cmd("select", 8)
    30  	c.Assert(r.Err, IsNil)
    31  }
    32  
    33  func (s *ClientSuite) TearDownTest(c *C) {
    34  	s.c.Close()
    35  }
    36  
    37  func (s *ClientSuite) TestCmd(c *C) {
    38  	v, _ := s.c.Cmd("echo", "Hello, World!").Str()
    39  	c.Assert(v, Equals, "Hello, World!")
    40  }
    41  
    42  func (s *ClientSuite) TestPipeline(c *C) {
    43  	s.c.Append("echo", "foo")
    44  	s.c.Append("echo", "bar")
    45  	s.c.Append("echo", "zot")
    46  
    47  	v, _ := s.c.GetReply().Str()
    48  	c.Assert(v, Equals, "foo")
    49  
    50  	v, _ = s.c.GetReply().Str()
    51  	c.Assert(v, Equals, "bar")
    52  
    53  	v, _ = s.c.GetReply().Str()
    54  	c.Assert(v, Equals, "zot")
    55  
    56  	r := s.c.GetReply()
    57  	c.Assert(r.Type, Equals, ErrorReply)
    58  	c.Assert(r.Err, Equals, PipelineQueueEmptyError)
    59  }
    60  
    61  func (s *ClientSuite) TestParse(c *C) {
    62  	parseString := func(b string) *Reply {
    63  		s.c.reader = bufio.NewReader(bytes.NewBufferString(b))
    64  		return s.c.parse()
    65  	}
    66  
    67  	// missing \n trailing
    68  	r := parseString("foo")
    69  	c.Check(r.Type, Equals, ErrorReply)
    70  	c.Check(r.Err, NotNil)
    71  
    72  	// error reply
    73  	r = parseString("-ERR unknown command 'foobar'\r\n")
    74  	c.Check(r.Type, Equals, ErrorReply)
    75  	c.Check(r.Err.Error(), Equals, "ERR unknown command 'foobar'")
    76  
    77  	// LOADING error
    78  	r = parseString("-LOADING Redis is loading the dataset in memory\r\n")
    79  	c.Check(r.Type, Equals, ErrorReply)
    80  	c.Check(r.Err, Equals, LoadingError)
    81  
    82  	// status reply
    83  	r = parseString("+OK\r\n")
    84  	c.Check(r.Type, Equals, StatusReply)
    85  	c.Check(r.buf, DeepEquals, []byte("OK"))
    86  
    87  	// integer reply
    88  	r = parseString(":1337\r\n")
    89  	c.Check(r.Type, Equals, IntegerReply)
    90  	c.Check(r.int, Equals, int64(1337))
    91  
    92  	// null bulk reply
    93  	r = parseString("$-1\r\n")
    94  	c.Check(r.Type, Equals, NilReply)
    95  
    96  	// bulk reply
    97  	r = parseString("$6\r\nfoobar\r\n")
    98  	c.Check(r.Type, Equals, BulkReply)
    99  	c.Check(r.buf, DeepEquals, []byte("foobar"))
   100  
   101  	// null multi bulk reply
   102  	r = parseString("*-1\r\n")
   103  	c.Check(r.Type, Equals, NilReply)
   104  
   105  	// multi bulk reply
   106  	r = parseString("*5\r\n:1\r\n:2\r\n:3\r\n:4\r\n$6\r\nfoobar\r\n")
   107  	c.Check(r.Type, Equals, MultiReply)
   108  	c.Assert(len(r.Elems), Equals, 5)
   109  	c.Check(r.Elems[0].int, Equals, int64(1))
   110  	c.Check(r.Elems[1].int, Equals, int64(2))
   111  	c.Check(r.Elems[2].int, Equals, int64(3))
   112  	c.Check(r.Elems[3].int, Equals, int64(4))
   113  	c.Check(r.Elems[4].buf, DeepEquals, []byte("foobar"))
   114  
   115  	// invalid multi bulk reply
   116  	r = parseString("*-2\r\n")
   117  	c.Check(r.Type, Equals, ErrorReply)
   118  	c.Check(r.Err, Equals, ParseError)
   119  
   120  	// invalid reply
   121  	r = parseString("@foo\r\n")
   122  	c.Check(r.Type, Equals, ErrorReply)
   123  	c.Check(r.Err, Equals, ParseError)
   124  }