예제 #1
0
func (s *testRedisHandlerSuite) TestServerServe(c *C) {
	h := &testHandler{make(map[string]int)}
	ss, err := NewServer(h)
	c.Assert(err, IsNil)

	resp, err := resp.Decode(bufio.NewReader(bytes.NewReader([]byte("*2\r\n$3\r\nset\r\n$3\r\nfoo\r\n"))))
	c.Assert(err, IsNil)

	_, err = ss.Dispatch(nil, resp)
	c.Assert(err, IsNil)

	s.testmapcount(c, h.c, map[string]int{"foo": 1})
}
예제 #2
0
파일: main_test.go 프로젝트: CowLeo/qdb
func (pc *testPoolConn) doCmd(c *C, cmd string, args ...interface{}) redis.Resp {
	r := bufio.NewReaderSize(pc.Conn, 32)
	w := bufio.NewWriterSize(pc.Conn, 32)

	req := redis.NewRequest(cmd, args...)
	err := redis.Encode(w, req)
	c.Assert(err, IsNil)

	err = w.Flush()
	c.Assert(err, IsNil)

	resp, err := redis.Decode(r)
	c.Assert(err, IsNil)

	return resp
}
예제 #3
0
파일: migrate.go 프로젝트: CowLeo/qdb
func (c *conn) decodeResp(timeout time.Duration) (redis.Resp, error) {
	if err := c.sock.SetReadDeadline(time.Now().Add(timeout)); err != nil {
		return nil, errors.Trace(err)
	}
	return redis.Decode(c.r)
}
예제 #4
0
func (s *testStoreSuite) checkSlotsMgrt(c *C, r *bufio.Reader, w *bufio.Writer, cc chan error, expect ...interface{}) {
	if len(expect) != 0 {
		req1, err := redis.Decode(r)
		c.Assert(err, IsNil)

		cmd1, args1, err := redis.ParseArgs(req1)
		c.Assert(err, IsNil)
		c.Assert(cmd1, Equals, "select")
		c.Assert(len(args1), Equals, 1)

		err = redis.Encode(w, redis.NewString("OK"))
		c.Assert(err, IsNil)

		err = w.Flush()
		c.Assert(err, IsNil)

		req2, err := redis.Decode(r)
		cmd2, args2, err := redis.ParseArgs(req2)
		c.Assert(err, IsNil)
		c.Assert(cmd2, Equals, "slotsrestore")
		c.Assert(len(args2), Equals, len(expect))

		m := make(map[string]*struct {
			key, value string
			ttlms      uint64
		})
		for i := 0; i < len(expect)/3; i++ {
			v := &struct {
				key, value string
				ttlms      uint64
			}{key: expect[i*3].(string), value: expect[i*3+2].(string)}
			v.ttlms, err = ParseUint(expect[i*3+1])
			c.Assert(err, IsNil)
			m[v.key] = v
		}

		for i := 0; i < len(expect)/3; i++ {
			key := args2[i*3]
			ttlms := args2[i*3+1]
			value := args2[i*3+2]

			v := m[string(key)]
			c.Assert(v, NotNil)
			c.Assert(string(key), Equals, v.key)

			b, err := rdb.DecodeDump(value)
			c.Assert(err, IsNil)
			c.Assert(string(b.(rdb.String)), Equals, v.value)

			x, err := strconv.Atoi(string(ttlms))
			c.Assert(err, IsNil)

			if v.ttlms == 0 {
				c.Assert(x, Equals, 0)
			} else {
				c.Assert(x, Not(Equals), 0)
				c.Assert(math.Abs(float64(x)-float64(v.ttlms)) < 1000, Equals, true)
			}
		}

		err = redis.Encode(w, redis.NewString("OK"))
		c.Assert(err, IsNil)

		err = w.Flush()
		c.Assert(err, IsNil)
	}

	select {
	case err := <-cc:
		c.Assert(err, IsNil)
	case <-time.After(time.Second):
		c.Fatal("timeout error")
	}
}