예제 #1
0
파일: proxy_test.go 프로젝트: YongMan/codis
func init() {
	conn = zkhelper.NewConn()
	conf = &Config{
		proxyId:     "proxy_test",
		productName: "test",
		zkAddr:      "localhost:2181",
		fact:        func(string, int) (zkhelper.Conn, error) { return conn, nil },
		proto:       "tcp4",
	}

	//init action path
	prefix := models.GetWatchActionPath(conf.productName)
	err := models.CreateActionRootPath(conn, prefix)
	assert.MustNoError(err)

	//init slot
	err = models.InitSlotSet(conn, conf.productName, 1024)
	assert.MustNoError(err)

	//init  server group
	g1 := models.NewServerGroup(conf.productName, 1)
	g1.Create(conn)
	g2 := models.NewServerGroup(conf.productName, 2)
	g2.Create(conn)

	redis1, _ = miniredis.Run()
	redis2, _ = miniredis.Run()

	s1 := models.NewServer(models.SERVER_TYPE_MASTER, redis1.Addr())
	s2 := models.NewServer(models.SERVER_TYPE_MASTER, redis2.Addr())

	g1.AddServer(conn, s1, "")
	g2.AddServer(conn, s2, "")

	//set slot range
	err = models.SetSlotRange(conn, conf.productName, 0, 511, 1, models.SLOT_STATUS_ONLINE)
	assert.MustNoError(err)

	err = models.SetSlotRange(conn, conf.productName, 512, 1023, 2, models.SLOT_STATUS_ONLINE)
	assert.MustNoError(err)

	s = New(":19000", ":11000", conf)

	err = models.SetProxyStatus(conn, conf.productName, conf.proxyId, models.PROXY_STATE_ONLINE)
	assert.MustNoError(err)
}
예제 #2
0
func TestRunner(t *testing.T) {
	r, err := miniredis.Run()
	if err != nil {
		t.Fatal(err)
	}

	defer r.Close()
	addr := r.Addr()

	c, err := redis.Dial("tcp", addr)
	if err != nil {
		t.Fatal(err)
	}
	defer c.Close()

	str := `
SET a 1
RET OK

GET b
RET nil

GET a
RET_LEN 1
RET "1"

MGET a b
RET ["1", nil]

SET a 1.2
RET OK

GET a
RET "1.2"

SET a 2
INCR a
RET 3

SET a -1
GET a
RET "-1"

SET a -1.2
GET a
RET "-1.2"
`

	s := &Scanner{}
	s.Init([]byte(str))

	runner := &ScriptRunner{}
	err = runner.Run(c, s)
	if err != nil {
		t.Fatal(err)
	}
}
예제 #3
0
파일: storage.go 프로젝트: xh3b4sd/anna
// NewStorage creates a new configured memory storage object. Therefore it
// manages an in-memory redis instance which can be shut down using the
// configured closer. This is used for local development.
func NewStorage(config StorageConfig) (spec.Storage, error) {
	addrChan := make(chan string, 1)
	closer := make(chan struct{}, 1)
	redisAddr := ""

	go func() {
		s, err := miniredis.Run()
		if err != nil {
			panic(err)
		}
		addrChan <- s.Addr()

		<-closer
		s.Close()
	}()

	select {
	case <-time.After(1 * time.Second):
		panic("starting miniredis timed out")
	case addr := <-addrChan:
		redisAddr = addr
	}

	newRedisStorageConfig := redis.DefaultStorageConfigWithAddr(redisAddr)
	newRedisStorageConfig.BackoffFactory = func() spec.Backoff {
		return backoff.NewExponentialBackOff()
	}
	newRedisStorage, err := redis.NewStorage(newRedisStorageConfig)
	if err != nil {
		return nil, maskAny(err)
	}

	newStorage := &storage{
		StorageConfig: config,

		Closer:       closer,
		ID:           id.MustNewID(),
		RedisStorage: newRedisStorage,
		ShutdownOnce: sync.Once{},
		Type:         ObjectType,
	}

	return newStorage, nil
}
예제 #4
0
func TestPingServer(t *testing.T) {
	rc := &redisChecker{
		defaultTimeout: 1 * time.Second,
	}

	errCh := make(chan interface{})
	go PingServer(rc, "context", errCh)
	if str := <-errCh; str.(string) != "context" {
		t.Error("should be error")
	}

	redis, _ := miniredis.Run()
	defer redis.Close()
	rc.addr = redis.Addr()
	go PingServer(rc, "context", errCh)
	if obj := <-errCh; obj != nil {
		t.Error("should be error")
	}
}
예제 #5
0
func TestRedisChecker(t *testing.T) {
	r, _ := miniredis.Run()
	defer r.Close()
	addr := r.Addr()
	rc := &redisChecker{
		addr:           addr,
		defaultTimeout: 5 * time.Second,
	}

	err := rc.CheckAlive()
	if err != nil {
		t.Error(err)
	}

	//test bad address
	rc.addr = "xxx"
	err = rc.CheckAlive()
	if err == nil {
		t.Error("should be error")
	}
}
예제 #6
0
func TestMgetResults(t *testing.T) {
	redisrv, err := miniredis.Run()
	if err != nil {
		t.Fatal("can not run miniredis")
	}
	defer redisrv.Close()

	moper := NewMultiOperator(redisrv.Addr())
	redisrv.Set("a", "a")
	redisrv.Set("b", "b")
	redisrv.Set("c", "c")
	buf, err := moper.mgetResults(&MulOp{
		op: "mget",
		keys: [][]byte{[]byte("a"),
			[]byte("b"), []byte("c"), []byte("x")}})
	if err != nil {
		t.Error(err)
	}

	res := string(buf)
	if !strings.Contains(res, "a") || !strings.Contains(res, "b") || !strings.Contains(res, "c") {
		t.Error("not match", res)
	}

	buf, err = moper.mgetResults(&MulOp{
		op: "mget",
		keys: [][]byte{[]byte("x"),
			[]byte("c"), []byte("x")}})
	if err != nil {
		t.Error(err)
	}

	buf, err = moper.mgetResults(&MulOp{
		op: "mget",
		keys: [][]byte{[]byte("x"),
			[]byte("y"), []byte("x")}})
	if err != nil {
		t.Error(err)
	}
}
예제 #7
0
func Example() {
	s, err := miniredis.Run()
	if err != nil {
		panic(err)
	}
	defer s.Close()

	// Configure you application to connect to redis at s.Addr()
	// Any redis client should work, as long as you use redis commands which
	// miniredis implements.
	c, err := redis.Dial("tcp", s.Addr())
	if err != nil {
		panic(err)
	}
	_, err = c.Do("SET", "foo", "bar")
	if err != nil {
		panic(err)
	}

	// You can ask miniredis about keys directly, without going over the network.
	if got, err := s.Get("foo"); err != nil || got != "bar" {
		panic("Didn't get 'bar' back")
	}
	// Or with a DB id
	if _, err := s.DB(42).Get("foo"); err != miniredis.ErrKeyNotFound {
		panic("Didn't use a different DB")
	}
	// Or use a Check* function which Fail()s if the key is not what we expect
	// (checks for existence, key type and the value)
	// s.CheckGet(t, "foo", "bar")

	// Check if there really was only one connection.
	if s.TotalConnectionCount() != 1 {
		panic("Too many connections made")
	}

	// Output:
}
예제 #8
0
파일: router_test.go 프로젝트: jcru/codis
func InitEnv() {
	go once.Do(func() {
		conn = zkhelper.NewConn()
		conf = &Config{
			proxyId:     "proxy_test",
			productName: "test",
			zkAddr:      "localhost:2181",
			fact:        func(string) (zkhelper.Conn, error) { return conn, nil },
			proto:       "tcp4",
		}

		//init action path
		prefix := models.GetWatchActionPath(conf.productName)
		err := models.CreateActionRootPath(conn, prefix)
		assert.MustNoError(err)

		//init slot
		err = models.InitSlotSet(conn, conf.productName, 1024)
		assert.MustNoError(err)

		//init  server group
		g1 := models.NewServerGroup(conf.productName, 1)
		g1.Create(conn)
		g2 := models.NewServerGroup(conf.productName, 2)
		g2.Create(conn)

		redis1, _ = miniredis.Run()
		redis2, _ = miniredis.Run()

		s1 := models.NewServer(models.SERVER_TYPE_MASTER, redis1.Addr())
		s2 := models.NewServer(models.SERVER_TYPE_MASTER, redis2.Addr())

		g1.AddServer(conn, s1, "")
		g2.AddServer(conn, s2, "")

		//set slot range
		err = models.SetSlotRange(conn, conf.productName, 0, 511, 1, models.SLOT_STATUS_ONLINE)
		assert.MustNoError(err)

		err = models.SetSlotRange(conn, conf.productName, 512, 1023, 2, models.SLOT_STATUS_ONLINE)
		assert.MustNoError(err)

		go func() { //set proxy online
			time.Sleep(3 * time.Second)
			err := models.SetProxyStatus(conn, conf.productName, conf.proxyId, models.PROXY_STATE_ONLINE)
			assert.MustNoError(err)

			time.Sleep(2 * time.Second)
			proxyMutex.Lock()
			defer proxyMutex.Unlock()
			assert.Must(s.info.State == models.PROXY_STATE_ONLINE)
		}()

		proxyMutex.Lock()
		s, err = NewServer(":19000", ":11000", conf)
		assert.MustNoError(err)
		proxyMutex.Unlock()
		s.Serve()
	})

	waitonce.Do(func() {
		time.Sleep(10 * time.Second)
	})
}
예제 #9
0
func InitEnv() {
	go once.Do(func() {
		conn = zkhelper.NewConn()
		conf = &Conf{
			proxyId:     "proxy_test",
			productName: "test",
			zkAddr:      "localhost:2181",
			netTimeout:  5,
			f:           func(string) (zkhelper.Conn, error) { return conn, nil },
			proto:       "tcp4",
		}

		//init action path
		prefix := models.GetWatchActionPath(conf.productName)
		err := models.CreateActionRootPath(conn, prefix)
		if err != nil {
			log.Fatal(err)
		}

		//init slot
		err = models.InitSlotSet(conn, conf.productName, 1024)
		if err != nil {
			log.Fatal(err)
		}

		//init  server group
		g1 := models.NewServerGroup(conf.productName, 1)
		g1.Create(conn)
		g2 := models.NewServerGroup(conf.productName, 2)
		g2.Create(conn)

		redis1, _ = miniredis.Run()
		redis2, _ = miniredis.Run()

		s1 := models.NewServer(models.SERVER_TYPE_MASTER, redis1.Addr())
		s2 := models.NewServer(models.SERVER_TYPE_MASTER, redis2.Addr())

		g1.AddServer(conn, s1)
		g2.AddServer(conn, s2)

		//set slot range
		err = models.SetSlotRange(conn, conf.productName, 0, 511, 1, models.SLOT_STATUS_ONLINE)
		if err != nil {
			log.Fatal(err)
		}

		err = models.SetSlotRange(conn, conf.productName, 512, 1023, 2, models.SLOT_STATUS_ONLINE)
		if err != nil {
			log.Fatal(err)
		}

		go func() { //set proxy online
			time.Sleep(3 * time.Second)
			err := models.SetProxyStatus(conn, conf.productName, conf.proxyId, models.PROXY_STATE_ONLINE)
			if err != nil {
				log.Fatal(errors.ErrorStack(err))
			}
			time.Sleep(2 * time.Second)
			proxyMutex.Lock()
			defer proxyMutex.Unlock()
			pi := s.getProxyInfo()
			if pi.State != models.PROXY_STATE_ONLINE {
				log.Fatalf("should be online, we got %s", pi.State)
			}
		}()

		proxyMutex.Lock()
		s = NewServer(":19000", ":11000",
			conf,
		)
		proxyMutex.Unlock()
		s.Run()
	})

	waitonce.Do(func() {
		time.Sleep(10 * time.Second)
	})
}
예제 #10
0
package main

import (
	"encoding/json"
	"fmt"

	"github.com/alicebob/miniredis"
	"github.com/wandoulabs/codis/pkg/models"
	"testing"
	"time"
)

const GROUP_ID = 1

var (
	redisServer, _ = miniredis.Run()
	groups1        = []models.ServerGroup{
		models.ServerGroup{
			Servers: []*models.Server{
				&models.Server{GroupId: GROUP_ID, Type: models.SERVER_TYPE_MASTER, Addr: "localhost:xxx"},
				&models.Server{GroupId: GROUP_ID, Type: models.SERVER_TYPE_SLAVE, Addr: redisServer.Addr()},
				&models.Server{GroupId: GROUP_ID, Type: models.SERVER_TYPE_SLAVE, Addr: "xx"},
				&models.Server{GroupId: GROUP_ID, Type: models.SERVER_TYPE_OFFLINE, Addr: "xx"},
			},
		},
	}
)

func TestGetServerGroups(t *testing.T) {
	callHttp = func(objPtr interface{}, url string, method string, arg interface{}) error {
		buf, _ := json.Marshal(groups1)