func TestBytesList(t *T) { r := &Reply{Type: MultiReply} r.Elems = make([]*Reply, 3) r.Elems[0] = &Reply{Type: BulkReply, buf: []byte("0")} r.Elems[1] = &Reply{Type: NilReply} r.Elems[2] = &Reply{Type: BulkReply, buf: []byte("2")} l, err := r.ListBytes() assert.Nil(t, err) assert.Equal(t, 3, len(l)) assert.Equal(t, []byte("0"), l[0]) assert.Nil(t, l[1]) assert.Equal(t, []byte("2"), l[2]) }
func TestLoadGame(t *testing.T) { StartGame(aGameId, aFirstPlayerId) game, error := LoadGame(aGameId) assert.Nil(t, error) assert.NotNil(t, game) assert.Equal(t, game.GameId, aGameId) assert.Equal(t, game.GamerOneId, aFirstPlayerId) assert.Equal(t, game.LastPlayer, false) assert.Equal(t, len(game.State), 7, "Table should have 7 columns") assert.Equal(t, len(game.State[0]), 6, "Table should have 6 columns") }
func TestStartGame(t *testing.T) { game, error := StartGame(aGameId, aFirstPlayerId) assert.Nil(t, error) assert.NotNil(t, game) assert.Equal(t, len(game.State), 7, "Table should have 7 columns") assert.Equal(t, len(game.State[0]), 6, "Table should have 6 columns") for i := range game.State { for j := range game.State[i] { assert.Equal(t, game.State[i][j], uint8(0), "Table should be 0 initialized") } } }
func TestHash(t *T) { r := &Reply{Type: MultiReply} r.Elems = make([]*Reply, 6) r.Elems[0] = &Reply{Type: BulkReply, buf: []byte("a")} r.Elems[1] = &Reply{Type: BulkReply, buf: []byte("0")} r.Elems[2] = &Reply{Type: BulkReply, buf: []byte("b")} r.Elems[3] = &Reply{Type: NilReply} r.Elems[4] = &Reply{Type: BulkReply, buf: []byte("c")} r.Elems[5] = &Reply{Type: BulkReply, buf: []byte("2")} h, err := r.Hash() assert.Nil(t, err) assert.Equal(t, "0", h["a"]) assert.Equal(t, "", h["b"]) assert.Equal(t, "2", h["c"]) }
func TestFloat64(t *T) { r := &Reply{Type: ErrorReply, Err: LoadingError} _, err := r.Float64() assert.Equal(t, LoadingError, err) r = &Reply{Type: IntegerReply, int: 5} _, err = r.Float64() assert.NotNil(t, err) r = &Reply{Type: BulkReply, buf: []byte("5.1")} b, err := r.Float64() assert.Nil(t, err) assert.Equal(t, float64(5.1), b) r = &Reply{Type: BulkReply, buf: []byte("foo")} _, err = r.Float64() assert.NotNil(t, err) }
func TestStr(t *T) { r := &Reply{Type: ErrorReply, Err: LoadingError} _, err := r.Str() assert.Equal(t, LoadingError, err) r = &Reply{Type: IntegerReply} _, err = r.Str() assert.NotNil(t, err) r = &Reply{Type: StatusReply, buf: []byte("foo")} b, err := r.Str() assert.Nil(t, err) assert.Equal(t, "foo", b) r = &Reply{Type: BulkReply, buf: []byte("foo")} b, err = r.Str() assert.Nil(t, err) assert.Equal(t, "foo", b) }
func TestPipeline(t *T) { c := dial(t) c.Append("echo", "foo") c.Append("echo", "bar") c.Append("echo", "zot") v, _ := c.GetReply().Str() assert.Equal(t, "foo", v) v, _ = c.GetReply().Str() assert.Equal(t, "bar", v) v, _ = c.GetReply().Str() assert.Equal(t, "zot", v) r := c.GetReply() assert.Equal(t, ErrorReply, r.Type) assert.Equal(t, PipelineQueueEmptyError, r.Err) }
func TestWriteArbitraryAsFlattenedStrings(t *T) { var err error buf := bytes.NewBuffer([]byte{}) for _, test := range arbitraryAsFlattenedStringsTests { t.Logf("Checking test %v", test) buf.Reset() err = WriteArbitraryAsFlattenedStrings(buf, test.val) assert.Nil(t, err) assert.Equal(t, test.expect, buf.Bytes()) } }
func TestBool(t *T) { r := &Reply{Type: IntegerReply, int: 0} b, err := r.Bool() assert.Nil(t, err) assert.Equal(t, false, b) r = &Reply{Type: StatusReply, buf: []byte("0")} b, err = r.Bool() assert.Nil(t, err) assert.Equal(t, false, b) r = &Reply{Type: IntegerReply, int: 2} b, err = r.Bool() assert.Nil(t, err) assert.Equal(t, true, b) r = &Reply{Type: NilReply} _, err = r.Bool() assert.NotNil(t, err) }
func TestMessageWrite(t *T) { var err error var m *Message buf := bytes.NewBuffer([]byte{}) for _, test := range arbitraryTests { t.Logf("Checking test; %v", test) buf.Reset() m, err = NewMessage(test.expect) assert.Nil(t, err) err = WriteMessage(buf, m) assert.Nil(t, err) assert.Equal(t, test.expect, buf.Bytes()) } }
func TestCmd(t *T) { c := dial(t) v, _ := c.Cmd("echo", "Hello, World!").Str() assert.Equal(t, "Hello, World!", v) // Test that a bad command properly returns a *CmdError err := c.Cmd("non-existant-cmd").Err assert.NotEqual(t, "", err.(*CmdError).Error()) // Test that application level errors propagate correctly c.Cmd("sadd", "foo", "bar") _, err = c.Cmd("get", "foo").Str() assert.NotEqual(t, "", err.(*CmdError).Error()) }
func TestRead(t *T) { var m *Message var err error _, err = NewMessage(nil) assert.NotNil(t, err) _, err = NewMessage([]byte{}) assert.NotNil(t, err) // Simple string m, _ = NewMessage([]byte("+ohey\r\n")) assert.Equal(t, SimpleStr, m.Type) assert.Equal(t, []byte("ohey"), m.val) // Empty simple string m, _ = NewMessage([]byte("+\r\n")) assert.Equal(t, SimpleStr, m.Type) assert.Equal(t, []byte(""), m.val.([]byte)) // Error m, _ = NewMessage([]byte("-ohey\r\n")) assert.Equal(t, Err, m.Type) assert.Equal(t, []byte("ohey"), m.val.([]byte)) // Empty error m, _ = NewMessage([]byte("-\r\n")) assert.Equal(t, Err, m.Type) assert.Equal(t, []byte(""), m.val.([]byte)) // Int m, _ = NewMessage([]byte(":1024\r\n")) assert.Equal(t, Int, m.Type) assert.Equal(t, int64(1024), m.val.(int64)) // Bulk string m, _ = NewMessage([]byte("$3\r\nfoo\r\n")) assert.Equal(t, BulkStr, m.Type) assert.Equal(t, []byte("foo"), m.val.([]byte)) // Empty bulk string m, _ = NewMessage([]byte("$0\r\n\r\n")) assert.Equal(t, BulkStr, m.Type) assert.Equal(t, []byte(""), m.val.([]byte)) // Nil bulk string m, _ = NewMessage([]byte("$-1\r\n")) assert.Equal(t, Nil, m.Type) // Array m, _ = NewMessage([]byte("*2\r\n+foo\r\n+bar\r\n")) assert.Equal(t, Array, m.Type) assert.Equal(t, 2, len(m.val.([]*Message))) assert.Equal(t, SimpleStr, m.val.([]*Message)[0].Type) assert.Equal(t, []byte("foo"), m.val.([]*Message)[0].val.([]byte)) assert.Equal(t, SimpleStr, m.val.([]*Message)[1].Type) assert.Equal(t, []byte("bar"), m.val.([]*Message)[1].val.([]byte)) // Empty array m, _ = NewMessage([]byte("*0\r\n")) assert.Equal(t, Array, m.Type) assert.Equal(t, 0, len(m.val.([]*Message))) // Nil Array m, _ = NewMessage([]byte("*-1\r\n")) assert.Equal(t, Nil, m.Type) // Embedded Array m, _ = NewMessage([]byte("*3\r\n+foo\r\n+bar\r\n*2\r\n+foo\r\n+bar\r\n")) assert.Equal(t, Array, m.Type) assert.Equal(t, 3, len(m.val.([]*Message))) assert.Equal(t, SimpleStr, m.val.([]*Message)[0].Type) assert.Equal(t, []byte("foo"), m.val.([]*Message)[0].val.([]byte)) assert.Equal(t, SimpleStr, m.val.([]*Message)[1].Type) assert.Equal(t, []byte("bar"), m.val.([]*Message)[1].val.([]byte)) m = m.val.([]*Message)[2] assert.Equal(t, 2, len(m.val.([]*Message))) assert.Equal(t, SimpleStr, m.val.([]*Message)[0].Type) assert.Equal(t, []byte("foo"), m.val.([]*Message)[0].val.([]byte)) assert.Equal(t, SimpleStr, m.val.([]*Message)[1].Type) assert.Equal(t, []byte("bar"), m.val.([]*Message)[1].val.([]byte)) // Test that two bulks in a row read correctly m, _ = NewMessage([]byte("*2\r\n$3\r\nfoo\r\n$3\r\nbar\r\n")) assert.Equal(t, Array, m.Type) assert.Equal(t, 2, len(m.val.([]*Message))) assert.Equal(t, BulkStr, m.val.([]*Message)[0].Type) assert.Equal(t, []byte("foo"), m.val.([]*Message)[0].val.([]byte)) assert.Equal(t, BulkStr, m.val.([]*Message)[1].Type) assert.Equal(t, []byte("bar"), m.val.([]*Message)[1].val.([]byte)) }
func TestInt(t *T) { r := &Reply{Type: IntegerReply, int: 5} b, err := r.Int() assert.Nil(t, err) assert.Equal(t, 5, b) }
func TestBytes(t *T) { r := &Reply{Type: BulkReply, buf: []byte("foo")} b, err := r.Bytes() assert.Nil(t, err) assert.Equal(t, []byte("foo"), b) }
func TestParse(t *T) { c := dial(t) parseString := func(b string) *Reply { c.reader = bufio.NewReader(bytes.NewBufferString(b)) return c.parse() } // missing \n trailing r := parseString("foo") assert.Equal(t, ErrorReply, r.Type) assert.NotNil(t, r.Err) // error reply r = parseString("-ERR unknown command 'foobar'\r\n") assert.Equal(t, ErrorReply, r.Type) assert.Equal(t, "ERR unknown command 'foobar'", r.Err.Error()) // LOADING error r = parseString("-LOADING Redis is loading the dataset in memory\r\n") assert.Equal(t, ErrorReply, r.Type) assert.Equal(t, LoadingError, r.Err) // status reply r = parseString("+OK\r\n") assert.Equal(t, StatusReply, r.Type) assert.Equal(t, []byte("OK"), r.buf) // integer reply r = parseString(":1337\r\n") assert.Equal(t, IntegerReply, r.Type) assert.Equal(t, int64(1337), r.int) // null bulk reply r = parseString("$-1\r\n") assert.Equal(t, NilReply, r.Type) // bulk reply r = parseString("$6\r\nfoobar\r\n") assert.Equal(t, BulkReply, r.Type) assert.Equal(t, []byte("foobar"), r.buf) // null multi bulk reply r = parseString("*-1\r\n") assert.Equal(t, NilReply, r.Type) // multi bulk reply r = parseString("*5\r\n:0\r\n:1\r\n:2\r\n:3\r\n$6\r\nfoobar\r\n") assert.Equal(t, MultiReply, r.Type) assert.Equal(t, 5, len(r.Elems)) for i := 0; i < 4; i++ { assert.Equal(t, int64(i), r.Elems[i].int) } assert.Equal(t, []byte("foobar"), r.Elems[4].buf) }