示例#1
0
文件: migrate.go 项目: CowLeo/qdb
func (c *conn) encodeResp(resp redis.Resp, timeout time.Duration) error {
	if err := c.sock.SetWriteDeadline(time.Now().Add(timeout)); err != nil {
		return errors.Trace(err)
	}
	if err := redis.Encode(c.w, resp); err != nil {
		return errors.Trace(err)
	}
	return errors.Trace(c.w.Flush())
}
示例#2
0
文件: conn.go 项目: cuiwm/reborn
func (c *conn) writeRESP(resp redis.Resp) error {
	c.wLock.Lock()
	defer c.wLock.Unlock()

	if err := redis.Encode(c.w, resp); err != nil {
		return errors.Trace(err)
	}

	return errors.Trace(c.w.Flush())
}
示例#3
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
}
示例#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")
	}
}