Example #1
0
func TestProxyOfflineInWaitActionReceiver(t *testing.T) {
	log.Infof("test proxy offline when waiting action response")
	fakeZkConn := zkhelper.NewConn()

	for i := 1; i <= 4; i++ {
		CreateProxyInfo(fakeZkConn, productName, &ProxyInfo{
			Id:    strconv.Itoa(i),
			State: PROXY_STATE_ONLINE,
		})
		go waitForProxyMarkOffline(fakeZkConn, strconv.Itoa(i))
	}

	lst, _ := ProxyList(fakeZkConn, productName, nil)
	assert.Must(len(lst) == 4)

	go func() {
		time.Sleep(500 * time.Millisecond)
		actionPath := path.Join(GetActionResponsePath(productName), fakeZkConn.Seq2Str(1))
		//create test response for proxy 4, means proxy 1,2,3 are timeout
		fakeZkConn.Create(path.Join(actionPath, "4"), nil,
			0, zkhelper.DefaultFileACLs())
	}()

	err := NewActionWithTimeout(fakeZkConn, productName, ACTION_TYPE_SLOT_CHANGED, nil, "desc", true, 3*1000)
	if err != nil {
		assert.Must(err.Error() == ErrReceiverTimeout.Error())
	}

	for i := 1; i <= 3; i++ {
		info, _ := GetProxyInfo(fakeZkConn, productName, strconv.Itoa(i))
		assert.Must(info.State == PROXY_STATE_OFFLINE)
	}
}
Example #2
0
func TestBytesizeError(t *testing.T) {
	var err error
	_, err = Parse("--1")
	assert.Must(errors.Equal(err, ErrBadBytesize))
	_, err = Parse("hello world")
	assert.Must(errors.Equal(err, ErrBadBytesize))
	_, err = Parse("123.132.32")
	assert.Must(errors.Equal(err, ErrBadBytesize))
}
Example #3
0
func TestDecodeBulkBytes(t *testing.T) {
	test := "*2\r\n$4\r\nLLEN\r\n$6\r\nmylist\r\n"
	resp, err := DecodeFromBytes([]byte(test))
	assert.MustNoError(err)
	assert.Must(len(resp.Array) == 2)
	s1 := resp.Array[0]
	assert.Must(bytes.Equal(s1.Value, []byte("LLEN")))
	s2 := resp.Array[1]
	assert.Must(bytes.Equal(s2.Value, []byte("mylist")))
}
Example #4
0
func TestForceRemoveLock(t *testing.T) {
	fakeZkConn := zkhelper.NewConn()
	zkLock := utils.GetZkLock(fakeZkConn, productName)
	assert.Must(zkLock != nil)

	zkLock.Lock("force remove lock")
	zkPath := fmt.Sprintf("/zk/codis/db_%s/LOCK", productName)
	children, _, err := fakeZkConn.Children(zkPath)
	assert.MustNoError(err)
	assert.Must(len(children) != 0)

	ForceRemoveLock(fakeZkConn, productName)
	children, _, err = fakeZkConn.Children(zkPath)
	assert.MustNoError(err)
	assert.Must(len(children) == 0)
}
Example #5
0
func TestDecodeInvalidRequests(t *testing.T) {
	test := []string{
		"*hello\r\n",
		"*-100\r\n",
		"*3\r\nhi",
		"*3\r\nhi\r\n",
		"*4\r\n$1",
		"*4\r\n$1\r",
		"*4\r\n$1\n",
		"*2\r\n$3\r\nget\r\n$what?\r\nx\r\n",
		"*4\r\n$3\r\nget\r\n$1\r\nx\r\n",
		"*2\r\n$3\r\nget\r\n$1\r\nx",
		"*2\r\n$3\r\nget\r\n$1\r\nx\r",
		"*2\r\n$3\r\nget\r\n$100\r\nx\r\n",
		"$6\r\nfoobar\r",
		"$0\rn\r\n",
		"$-1\n",
		"*0",
		"*2n$3\r\nfoo\r\n$3\r\nbar\r\n",
		"*-\r\n",
		"+OK\n",
		"-Error message\r",
	}
	for _, s := range test {
		_, err := DecodeFromBytes([]byte(s))
		assert.Must(err != nil)
	}
}
Example #6
0
func TestBtoi(t *testing.T) {
	for i, b := range tmap {
		v, err := btoi(b)
		assert.MustNoError(err)
		assert.Must(v == i)
	}
}
Example #7
0
func newConnPair() (*Conn, *Conn) {
	l, err := net.Listen("tcp", "127.0.0.1:0")
	assert.MustNoError(err)
	defer l.Close()

	const bufsize = 128 * 1024

	cc := make(chan *Conn, 1)
	go func() {
		defer close(cc)
		for {
			c, err := l.Accept()
			if err != nil {
				return
			}
			cc <- NewConnSize(c, bufsize)
		}
	}()

	conn1, err := DialTimeout(l.Addr().String(), bufsize, time.Millisecond*50)
	assert.MustNoError(err)

	conn2, ok := <-cc
	assert.Must(ok)
	return conn1, conn2
}
Example #8
0
func TestDecodeSimpleRequest3(t *testing.T) {
	test := []string{"\r", "\n", " \n"}
	for _, s := range test {
		_, err := DecodeFromBytes([]byte(s))
		assert.Must(err != nil)
	}
}
Example #9
0
func TestBackend(t *testing.T) {
	l, err := net.Listen("tcp", "127.0.0.1:0")
	assert.MustNoError(err)
	defer l.Close()

	addr := l.Addr().String()
	reqc := make(chan *Request, 16384)
	go func() {
		bc := NewBackendConn(addr, "")
		defer bc.Close()
		defer close(reqc)
		var resp = redis.NewBulkBytes(make([]byte, 4096))
		for i := 0; i < cap(reqc); i++ {
			r := &Request{
				Resp: resp,
				Wait: &sync.WaitGroup{},
			}
			bc.PushBack(r)
			reqc <- r
		}
	}()

	go func() {
		c, err := l.Accept()
		assert.MustNoError(err)
		defer c.Close()
		conn := redis.NewConn(c)
		time.Sleep(time.Millisecond * 300)
		for i := 0; i < cap(reqc); i++ {
			_, err := conn.Reader.Decode()
			assert.MustNoError(err)
			resp := redis.NewString([]byte(strconv.Itoa(i)))
			assert.MustNoError(conn.Writer.Encode(resp, true))
		}
	}()

	var n int
	for r := range reqc {
		r.Wait.Wait()
		assert.Must(string(r.Response.Resp.Value) == strconv.Itoa(n))
		n++
	}
	assert.Must(n == cap(reqc))
}
Example #10
0
func TestDecodeSimpleRequest2(t *testing.T) {
	test := []string{
		"hello world\r\n",
		"hello world    \r\n",
		"    hello world    \r\n",
		"    hello     world\r\n",
		"    hello     world    \r\n",
	}
	for _, s := range test {
		resp, err := DecodeFromBytes([]byte(s))
		assert.MustNoError(err)
		assert.Must(resp.IsArray())
		assert.Must(len(resp.Array) == 2)
		s1 := resp.Array[0]
		assert.Must(bytes.Equal(s1.Value, []byte("hello")))
		s2 := resp.Array[1]
		assert.Must(bytes.Equal(s2.Value, []byte("world")))
	}
}
Example #11
0
func TestAddSlaveToEmptyGroup(t *testing.T) {
	resetEnv()
	g := NewServerGroup(productName, 1)
	g.Create(conn)

	s1 := NewServer(SERVER_TYPE_SLAVE, "127.0.0.1:1111")
	err := g.AddServer(conn, s1, "")
	assert.MustNoError(err)
	assert.Must(g.Servers[0].Type == SERVER_TYPE_MASTER)
}
Example #12
0
func TestGetOpStr(t *testing.T) {
	var m = map[string]string{
		"get":     "GET",
		"aBc":     "ABC",
		"おはよ":     "おはよ",
		"ni hao!": "NI HAO!",
		"":        "",
	}
	for k, v := range m {
		resp := redis.NewArray([]*redis.Resp{redis.NewBulkBytes([]byte(k))})
		s, err := getOpStr(resp)
		if v != "" {
			assert.MustNoError(err)
			assert.Must(s == v)
		} else {
			assert.Must(err != nil)
		}
	}
}
Example #13
0
func TestConnReaderTimeout(t *testing.T) {
	resp := NewString([]byte("hello world"))

	conn1, conn2 := newConnPair()

	var wg sync.WaitGroup

	wg.Add(1)
	go func() {
		defer wg.Done()
		var err error

		conn1.ReaderTimeout = time.Millisecond * 10
		_, err = conn1.Reader.Decode()
		assert.Must(err != nil && IsTimeout(err))

		conn1.Reader.Err = nil
		conn1.ReaderTimeout = 0
		_, err = conn1.Reader.Decode()
		assert.MustNoError(err)

		_, err = conn1.Reader.Decode()
		assert.Must(err != nil && errors.Equal(err, io.EOF))
	}()

	wg.Add(1)
	go func() {
		defer wg.Done()
		var err error

		time.Sleep(time.Millisecond * 100)

		err = conn2.Writer.Encode(resp, true)
		assert.MustNoError(err)

		conn2.Close()
	}()

	wg.Wait()

	conn1.Close()
	conn2.Close()
}
Example #14
0
func TestProxy(t *testing.T) {
	fakeZkConn := zkhelper.NewConn()
	path := GetSlotBasePath(productName)
	children, _, _ := fakeZkConn.Children(path)
	assert.Must(len(children) == 0)

	g := NewServerGroup(productName, 1)
	g.Create(fakeZkConn)

	// test create new group
	_, err := ServerGroups(fakeZkConn, productName)
	assert.MustNoError(err)

	ok, err := g.Exists(fakeZkConn)
	assert.MustNoError(err)
	assert.Must(ok)

	s1 := NewServer(SERVER_TYPE_MASTER, "localhost:1111")

	g.AddServer(fakeZkConn, s1, "")

	err = InitSlotSet(fakeZkConn, productName, 1024)
	assert.MustNoError(err)

	children, _, _ = fakeZkConn.Children(path)
	assert.Must(len(children) == 1024)

	s, err := GetSlot(fakeZkConn, productName, 1)
	assert.MustNoError(err)
	assert.Must(s.GroupId == -1)

	err = SetSlotRange(fakeZkConn, productName, 0, 1023, 1, SLOT_STATUS_ONLINE)
	assert.MustNoError(err)

	pi := &ProxyInfo{
		Id:    "proxy_1",
		Addr:  "localhost:1234",
		State: PROXY_STATE_OFFLINE,
	}

	_, err = CreateProxyInfo(fakeZkConn, productName, pi)
	assert.MustNoError(err)

	ps, err := ProxyList(fakeZkConn, productName, nil)
	assert.MustNoError(err)

	assert.Must(len(ps) == 1)
	assert.Must(ps[0].Id == "proxy_1")

	err = SetProxyStatus(fakeZkConn, productName, pi.Id, PROXY_STATE_ONLINE)
	assert.MustNoError(err)

	p, err := GetProxyInfo(fakeZkConn, productName, pi.Id)
	assert.MustNoError(err)
	assert.Must(p.State == PROXY_STATE_ONLINE)
}
Example #15
0
func TestConnWriterTimeout(t *testing.T) {
	resp := NewString([]byte("hello world"))

	conn1, conn2 := newConnPair()

	var wg sync.WaitGroup

	var count atomic2.Int64

	wg.Add(1)
	go func() {
		defer wg.Done()
		defer conn2.Close()

		conn2.WriterTimeout = time.Millisecond * 50
		for {
			if err := conn2.Writer.Encode(resp, true); err != nil {
				assert.Must(IsTimeout(err))
				return
			}
			count.Incr()
		}
	}()

	wg.Wait()

	for i := count.Get(); i != 0; i-- {
		_, err := conn1.Reader.Decode()
		assert.MustNoError(err)
	}
	_, err := conn1.Reader.Decode()
	assert.Must(err != nil && errors.Equal(err, io.EOF))

	conn1.Close()
	conn2.Close()
}
Example #16
0
func TestNewAction(t *testing.T) {
	fakeZkConn := zkhelper.NewConn()
	err := NewAction(fakeZkConn, productName, ACTION_TYPE_SLOT_CHANGED, nil, "desc", false)
	assert.MustNoError(err)

	prefix := GetWatchActionPath(productName)
	exist, _, err := fakeZkConn.Exists(prefix)
	assert.MustNoError(err)
	assert.Must(exist)

	//test if response node exists
	d, _, err := fakeZkConn.Get(prefix + "/0000000001")
	assert.MustNoError(err)

	//test get action data
	d, _, err = fakeZkConn.Get(GetActionResponsePath(productName) + "/0000000001")
	assert.MustNoError(err)

	var action Action
	err = json.Unmarshal(d, &action)
	assert.MustNoError(err)
	assert.Must(action.Desc == "desc")
	assert.Must(action.Type == ACTION_TYPE_SLOT_CHANGED)
}
Example #17
0
func TestHashSlot(t *testing.T) {
	var m = map[string]string{
		"{abc}":           "abc",
		"{{{abc1}abc2}":   "{{abc1",
		"abc1{abc2{abc3}": "abc2{abc3",
		"{{{{abc":         "{{{{abc",
		"{{{{abc}":        "{{{abc",
		"{{}{{abc":        "{",
		"abc}{abc":        "abc}{abc",
		"abc}{123}456":    "123",
		"123{abc}456":     "abc",
		"{}abc":           "",
		"abc{}123":        "",
		"123{456}":        "456",
	}
	for k, v := range m {
		i := hashSlot([]byte(k))
		j := hashSlot([]byte(v))
		assert.Must(i == j)
	}
}
Example #18
0
func TestServerGroup(t *testing.T) {
	resetEnv()

	g := NewServerGroup(productName, 1)
	g.Create(conn)

	// test create new group
	groups, err := ServerGroups(conn, productName)
	assert.MustNoError(err)
	assert.Must(len(groups) != 0)

	ok, err := g.Exists(conn)
	assert.MustNoError(err)
	assert.Must(ok)

	gg, err := GetGroup(conn, productName, 1)
	assert.MustNoError(err)
	assert.Must(gg != nil && gg.Id == g.Id)

	s1 := NewServer(SERVER_TYPE_MASTER, "127.0.0.1:1111")
	s2 := NewServer(SERVER_TYPE_MASTER, "127.0.0.1:2222")

	err = g.AddServer(conn, s1, "")

	servers, err := g.GetServers(conn)
	assert.MustNoError(err)
	assert.Must(len(servers) == 1)

	g.AddServer(conn, s2, "")
	assert.Must(len(g.Servers) == 1)

	s2.Type = SERVER_TYPE_SLAVE
	g.AddServer(conn, s2, "")
	assert.Must(len(g.Servers) == 2)

	err = g.Promote(conn, s2.Addr, "")
	assert.MustNoError(err)

	s, err := g.Master(conn)
	assert.MustNoError(err)
	assert.Must(s.Addr == s2.Addr)
}
Example #19
0
func TestSlots(t *testing.T) {
	fakeZkConn := zkhelper.NewConn()
	path := GetSlotBasePath(productName)
	children, _, _ := fakeZkConn.Children(path)
	assert.Must(len(children) == 0)

	err := InitSlotSet(fakeZkConn, productName, 1024)
	assert.MustNoError(err)

	children, _, _ = fakeZkConn.Children(path)
	assert.Must(len(children) == 1024)

	s, err := GetSlot(fakeZkConn, productName, 1)
	assert.MustNoError(err)

	assert.Must(s.GroupId == -1)

	g := NewServerGroup(productName, 1)
	g.Create(fakeZkConn)

	// test create new group
	_, err = ServerGroups(fakeZkConn, productName)
	assert.MustNoError(err)

	ok, err := g.Exists(fakeZkConn)
	assert.MustNoError(err)
	assert.Must(ok)

	err = SetSlotRange(fakeZkConn, productName, 0, 1023, 1, SLOT_STATUS_ONLINE)
	assert.MustNoError(err)

	s, err = GetSlot(fakeZkConn, productName, 1)
	assert.MustNoError(err)
	assert.Must(s.GroupId == 1)

	err = s.SetMigrateStatus(fakeZkConn, 1, 2)
	assert.MustNoError(err)
	assert.Must(s.GroupId == 2)
	assert.Must(s.State.Status == SLOT_STATUS_MIGRATE)
}
Example #20
0
func testEncodeAndCheck(t *testing.T, resp *Resp, expect []byte) {
	b, err := EncodeToBytes(resp)
	assert.MustNoError(err)
	assert.Must(bytes.Equal(b, expect))
}
Example #21
0
func TestItoa(t *testing.T) {
	for i, b := range tmap {
		assert.Must(string(b) == itoa(i))
	}
}
Example #22
0
func TestDecodeSimpleRequest1(t *testing.T) {
	resp, err := DecodeFromBytes([]byte("\r\n"))
	assert.MustNoError(err)
	assert.Must(resp.IsArray())
	assert.Must(len(resp.Array) == 0)
}
Example #23
0
func TestGetOpStrCmd(t *testing.T) {
	var m = map[string]string{
		"del":              "DEL",
		"dump":             "DUMP",
		"exists":           "EXISTS",
		"expire":           "EXPIRE",
		"expireat":         "EXPIREAT",
		"persist":          "PERSIST",
		"pexpire":          "PEXPIRE",
		"pexpireat":        "PEXPIREAT",
		"pttl":             "PTTL",
		"restore":          "RESTORE",
		"sort":             "SORT",
		"ttl":              "TTL",
		"type":             "TYPE",
		"append":           "APPEND",
		"bitcount":         "BITCOUNT",
		"decr":             "DECR",
		"decrby":           "DECRBY",
		"get":              "GET",
		"getbit":           "GETBIT",
		"getrange":         "GETRANGE",
		"getset":           "GETSET",
		"incr":             "INCR",
		"incrby":           "INCRBY",
		"incrbyfloat":      "INCRBYFLOAT",
		"mget":             "MGET",
		"mset":             "MSET",
		"psetex":           "PSETEX",
		"set":              "SET",
		"setbit":           "SETBIT",
		"setex":            "SETEX",
		"setnx":            "SETNX",
		"setrange":         "SETRANGE",
		"strlen":           "STRLEN",
		"hdel":             "HDEL",
		"hexists":          "HEXISTS",
		"hget":             "HGET",
		"hgetall":          "HGETALL",
		"hincrby":          "HINCRBY",
		"hincrbyfloat":     "HINCRBYFLOAT",
		"hkeys":            "HKEYS",
		"hlen":             "HLEN",
		"hmget":            "HMGET",
		"hmset":            "HMSET",
		"hset":             "HSET",
		"hsetnx":           "HSETNX",
		"hvals":            "HVALS",
		"hscan":            "HSCAN",
		"lindex":           "LINDEX",
		"linsert":          "LINSERT",
		"llen":             "LLEN",
		"lpop":             "LPOP",
		"lpush":            "LPUSH",
		"lpushx":           "LPUSHX",
		"lrange":           "LRANGE",
		"lrem":             "LREM",
		"lset":             "LSET",
		"ltrim":            "LTRIM",
		"rpop":             "RPOP",
		"rpoplpush":        "RPOPLPUSH",
		"rpush":            "RPUSH",
		"rpushx":           "RPUSHX",
		"sadd":             "SADD",
		"scard":            "SCARD",
		"sdiff":            "SDIFF",
		"sdiffstore":       "SDIFFSTORE",
		"sinter":           "SINTER",
		"sinterstore":      "SINTERSTORE",
		"sismember":        "SISMEMBER",
		"smembers":         "SMEMBERS",
		"smove":            "SMOVE",
		"spop":             "SPOP",
		"srandmember":      "SRANDMEMBER",
		"srem":             "SREM",
		"sunion":           "SUNION",
		"sunionstore":      "SUNIONSTORE",
		"sscan":            "SSCAN",
		"zadd":             "ZADD",
		"zcard":            "ZCARD",
		"zcount":           "ZCOUNT",
		"zincrby":          "ZINCRBY",
		"zinterstore":      "ZINTERSTORE",
		"zlexcount":        "ZLEXCOUNT",
		"zrange":           "ZRANGE",
		"zrangebylex":      "ZRANGEBYLEX",
		"zrangebyscore":    "ZRANGEBYSCORE",
		"zrank":            "ZRANK",
		"zrem":             "ZREM",
		"zremrangebylex":   "ZREMRANGEBYLEX",
		"zremrangebyrank":  "ZREMRANGEBYRANK",
		"zremrangebyscore": "ZREMRANGEBYSCORE",
		"zrevrange":        "ZREVRANGE",
		"zrevrangebyscore": "ZREVRANGEBYSCORE",
		"zrevrank":         "ZREVRANK",
		"zscore":           "ZSCORE",
		"zunionstore":      "ZUNIONSTORE",
		"zscan":            "ZSCAN",
		"pfadd":            "PFADD",
		"pfcount":          "PFCOUNT",
		"pfmerge":          "PFMERGE",
		"eval":             "EVAL",
		"evalsha":          "EVALSHA",
	}
	for k, v := range m {
		resp := redis.NewArray([]*redis.Resp{redis.NewBulkBytes([]byte(k))})
		s, err := getOpStr(resp)
		assert.MustNoError(err)
		assert.Must(s == v)
	}
}
Example #24
0
func TestBytesize(t *testing.T) {
	assert.Must(MustParse("1") == 1)
	assert.Must(MustParse("1B") == 1)
	assert.Must(MustParse("1K") == KB)
	assert.Must(MustParse("1M") == MB)
	assert.Must(MustParse("1G") == GB)
	assert.Must(MustParse("1T") == TB)
	assert.Must(MustParse("1P") == PB)

	assert.Must(MustParse(" -1") == -1)
	assert.Must(MustParse(" -1 b") == -1)
	assert.Must(MustParse(" -1 kb ") == -1*KB)
	assert.Must(MustParse(" -1 mb ") == -1*MB)
	assert.Must(MustParse(" -1 gb ") == -1*GB)
	assert.Must(MustParse(" -1 tb ") == -1*TB)
	assert.Must(MustParse(" -1 pb ") == -1*PB)

	assert.Must(MustParse(" 1.5") == 1)
	assert.Must(MustParse(" 1.5 kb ") == 1.5*KB)
	assert.Must(MustParse(" 1.5 mb ") == 1.5*MB)
	assert.Must(MustParse(" 1.5 gb ") == 1.5*GB)
	assert.Must(MustParse(" 1.5 tb ") == 1.5*TB)
	assert.Must(MustParse(" 1.5 pb ") == 1.5*PB)
}