示例#1
0
// Dial dials the local Redis server and selects database 9. To prevent
// stomping on real data, DialTestDB fails if database 9 contains data. The
// returned connection flushes database 9 on close.
func Dial() (redis.Conn, error) {
	c, err := redis.DialTimeout("tcp", ":6379", 0, 1*time.Second, 1*time.Second)
	if err != nil {
		return nil, err
	}

	_, err = c.Do("SELECT", "9")
	if err != nil {
		c.Close()
		return nil, err
	}

	n, err := redis.Int(c.Do("DBSIZE"))
	if err != nil {
		c.Close()
		return nil, err
	}

	if n != 0 {
		c.Close()
		return nil, errors.New("database #9 is not empty, test can not continue")
	}

	return testConn{c}, nil
}
示例#2
0
func ExampleInt() {
	c, err := dial()
	if err != nil {
		panic(err)
	}
	defer c.Close()

	c.Do("SET", "k1", 1)
	n, _ := redis.Int(c.Do("GET", "k1"))
	fmt.Printf("%#v\n", n)
	n, _ = redis.Int(c.Do("INCR", "k1"))
	fmt.Printf("%#v\n", n)
	// Output:
	// 1
	// 2
}
示例#3
0
// GetVoteComics returns two comics that should face off against each other in
// a vote-duel. One comic is chosen randomly, the other is approximately chosen
// from the 1% least voted-on comics.
func GetVoteComics() (c1, c2 Comic, err error) {
	C := pool.Get()
	defer C.Close()

	var (
		candidates []string
		votes      int
	)

	candidates, err = redis.Strings(C.Do("SRANDMEMBER", "strips", 100))
	if err != nil {
		return
	}

	// Choose one comic at random (this is needed because Redis' SRANDMEMBER
	// function isn't random enough)
	idx := rand.Intn(len(candidates))
	c1, err = GetComic(candidates[idx])
	if err != nil {
		return
	}

	// Remove the chosen comic from the rotation, so we don't duel a comic
	// against itself
	candidates[idx] = candidates[len(candidates)-1]
	candidates = candidates[:len(candidates)-1]

	// Find the comic with the least totalvotes among the remaining candidates
	minVotes := 1<<31 - 1
	bestStrip := ""
	for _, strip := range candidates {
		votes, err = redis.Int(C.Do("HGET", fmt.Sprintf(stripsFmt, strip), "totalvotes"))

		if votes < minVotes {
			bestStrip = strip
			minVotes = votes
		}
	}

	c2, err = GetComic(bestStrip)
	if err != nil {
		return
	}

	return
}
示例#4
0
// GetPage returns the contents of the requested page number
func GetPage(page string) (List, error) {
	C := pool.Get()
	defer C.Close()

	p, err := strconv.Atoi(page)
	if err != nil || p < 1 {
		return List{}, errors.New("invalid page: " + page)
	}

	comics, err := redis.Strings(C.Do("ZREVRANGE", "ranked", (p-1)*pageSize, (p-1)*pageSize+pageSize-1))
	if err != nil {
		return List{}, errors.New("could not retrieve comics for page " + page)
	}

	if len(comics) == 0 {
		return List{}, errors.New("invalid page: " + page)
	}

	numPages := 0
	numComics, err := redis.Int(C.Do("SCARD", "strips"))
	if err != nil {
		numPages = p
	} else {
		numPages = numComics / pageSize
		if numComics%pageSize != 0 {
			numPages++
		}
	}

	result := List{
		CurPage:   p,
		LastPage:  numPages,
		ListStart: (p-1)*pageSize + 1,
		Comics:    make([]Comic, 0, pageSize),
	}

	for _, c := range comics {
		comic, err := GetComic(c)
		if err == nil {
			result.Comics = append(result.Comics, comic)
		}
	}

	return result, nil
}
示例#5
0
func TestDialURL(t *testing.T) {
	for _, d := range dialErrors {
		_, err := redis.DialURL(d.rawurl)
		if err == nil || !strings.Contains(err.Error(), d.expectedError) {
			t.Errorf("DialURL did not return expected error (expected %v to contain %s)", err, d.expectedError)
		}
	}

	checkPort := func(network, address string) (net.Conn, error) {
		if address != "localhost:6379" {
			t.Errorf("DialURL did not set port to 6379 by default (got %v)", address)
		}
		return net.Dial(network, address)
	}
	c, err := redis.DialURL("redis://localhost", redis.DialNetDial(checkPort))
	if err != nil {
		t.Error("dial error:", err)
	}
	c.Close()

	checkHost := func(network, address string) (net.Conn, error) {
		if address != "localhost:6379" {
			t.Errorf("DialURL did not set host to localhost by default (got %v)", address)
		}
		return net.Dial(network, address)
	}
	c, err = redis.DialURL("redis://:6379", redis.DialNetDial(checkHost))
	if err != nil {
		t.Error("dial error:", err)
	}
	c.Close()

	// Check that the database is set correctly
	c1, err := redis.DialURL("redis://:6379/8")
	defer c1.Close()
	if err != nil {
		t.Error("Dial error:", err)
	}
	dbSize, _ := redis.Int(c1.Do("DBSIZE"))
	if dbSize > 0 {
		t.Fatal("DB 8 has existing keys; aborting test to avoid overwriting data")
	}
	c1.Do("SET", "var", "val")

	c2, err := redis.Dial("tcp", ":6379")
	defer c2.Close()
	if err != nil {
		t.Error("dial error:", err)
	}
	_, err = c2.Do("SELECT", "8")
	if err != nil {
		t.Error(err)
	}
	got, err := redis.String(c2.Do("GET", "var"))
	if err != nil {
		t.Error(err)
	}
	if got != "val" {
		t.Error("DialURL did not correctly set the db.")
	}
	_, err = c2.Do("DEL", "var")
}