ledisdb/server/cmd_scan_test.go

178 lines
3.5 KiB
Go
Raw Normal View History

2014-08-26 19:21:45 +04:00
package server
import (
"fmt"
"github.com/siddontang/ledisdb/client/go/ledis"
"github.com/siddontang/ledisdb/config"
"os"
"testing"
)
func TestScan(t *testing.T) {
2014-10-11 12:00:59 +04:00
cfg := config.NewConfigDefault()
2014-08-26 19:21:45 +04:00
cfg.DataDir = "/tmp/test_scan"
cfg.Addr = "127.0.0.1:11185"
os.RemoveAll(cfg.DataDir)
s, err := NewApp(cfg)
if err != nil {
t.Fatal(err)
}
go s.Run()
defer s.Close()
cc := new(ledis.Config)
cc.Addr = cfg.Addr
cc.MaxIdleConns = 1
c := ledis.NewClient(cc)
defer c.Close()
testKVScan(t, c)
2015-03-02 09:30:04 +03:00
testHashKeyScan(t, c)
testListKeyScan(t, c)
testZSetKeyScan(t, c)
testSetKeyScan(t, c)
2014-08-27 06:29:17 +04:00
2014-08-26 19:21:45 +04:00
}
2015-03-02 09:30:04 +03:00
func checkScanValues(t *testing.T, ay interface{}, values ...interface{}) {
2014-08-26 19:21:45 +04:00
a, err := ledis.Strings(ay, nil)
if err != nil {
t.Fatal(err)
}
if len(a) != len(values) {
t.Fatal(fmt.Sprintf("len %d != %d", len(a), len(values)))
}
for i, v := range a {
2015-03-02 09:30:04 +03:00
if string(v) != fmt.Sprintf("%v", values[i]) {
t.Fatal(fmt.Sprintf("%d %s != %v", string(v), values[i]))
2014-08-26 19:21:45 +04:00
}
}
}
2015-03-02 06:10:54 +03:00
func checkScan(t *testing.T, c *ledis.Client, tp string) {
if ay, err := ledis.Values(c.Do("XSCAN", tp, "", "count", 5)); err != nil {
2014-08-26 19:21:45 +04:00
t.Fatal(err)
} else if len(ay) != 2 {
t.Fatal(len(ay))
} else if n := ay[0].([]byte); string(n) != "4" {
t.Fatal(string(n))
} else {
checkScanValues(t, ay[1], 0, 1, 2, 3, 4)
}
2015-03-02 06:10:54 +03:00
if ay, err := ledis.Values(c.Do("XSCAN", tp, "4", "count", 6)); err != nil {
2014-08-26 19:21:45 +04:00
t.Fatal(err)
} else if len(ay) != 2 {
t.Fatal(len(ay))
} else if n := ay[0].([]byte); string(n) != "" {
t.Fatal(string(n))
} else {
checkScanValues(t, ay[1], 5, 6, 7, 8, 9)
}
2014-08-27 06:29:17 +04:00
}
func testKVScan(t *testing.T, c *ledis.Client) {
for i := 0; i < 10; i++ {
if _, err := c.Do("set", fmt.Sprintf("%d", i), []byte("value")); err != nil {
t.Fatal(err)
}
}
2015-03-02 06:10:54 +03:00
checkScan(t, c, "KV")
2014-08-27 06:29:17 +04:00
}
2015-03-02 09:30:04 +03:00
func testHashKeyScan(t *testing.T, c *ledis.Client) {
2014-08-27 06:29:17 +04:00
for i := 0; i < 10; i++ {
if _, err := c.Do("hset", fmt.Sprintf("%d", i), fmt.Sprintf("%d", i), []byte("value")); err != nil {
t.Fatal(err)
}
}
2015-03-02 06:10:54 +03:00
checkScan(t, c, "HASH")
2014-08-27 06:29:17 +04:00
}
2015-03-02 09:30:04 +03:00
func testListKeyScan(t *testing.T, c *ledis.Client) {
2014-08-27 06:29:17 +04:00
for i := 0; i < 10; i++ {
if _, err := c.Do("lpush", fmt.Sprintf("%d", i), fmt.Sprintf("%d", i)); err != nil {
t.Fatal(err)
}
}
2015-03-02 06:10:54 +03:00
checkScan(t, c, "LIST")
2014-08-27 06:29:17 +04:00
}
2015-03-02 09:30:04 +03:00
func testZSetKeyScan(t *testing.T, c *ledis.Client) {
2014-08-27 06:29:17 +04:00
for i := 0; i < 10; i++ {
if _, err := c.Do("zadd", fmt.Sprintf("%d", i), i, []byte("value")); err != nil {
t.Fatal(err)
}
}
2015-03-02 06:10:54 +03:00
checkScan(t, c, "ZSET")
2014-08-27 06:29:17 +04:00
}
2015-03-02 09:30:04 +03:00
func testSetKeyScan(t *testing.T, c *ledis.Client) {
2014-08-27 06:29:17 +04:00
for i := 0; i < 10; i++ {
if _, err := c.Do("sadd", fmt.Sprintf("%d", i), fmt.Sprintf("%d", i)); err != nil {
t.Fatal(err)
}
}
2015-03-02 06:10:54 +03:00
checkScan(t, c, "SET")
2015-03-02 09:30:04 +03:00
}
func TestHashScan(t *testing.T) {
c := getTestConn()
defer c.Close()
key := "scan_hash"
c.Do("HMSET", key, "a", 1, "b", 2)
if ay, err := ledis.Values(c.Do("XHSCAN", key, "")); err != nil {
t.Fatal(err)
} else if len(ay) != 2 {
t.Fatal(len(ay))
} else {
checkScanValues(t, ay[1], "a", 1, "b", 2)
}
}
func TestSetScan(t *testing.T) {
c := getTestConn()
defer c.Close()
key := "scan_set"
c.Do("SADD", key, "a", "b")
if ay, err := ledis.Values(c.Do("XSSCAN", key, "")); err != nil {
t.Fatal(err)
} else if len(ay) != 2 {
t.Fatal(len(ay))
} else {
checkScanValues(t, ay[1], "a", "b")
}
}
func TestZSetScan(t *testing.T) {
c := getTestConn()
defer c.Close()
key := "scan_zset"
c.Do("ZADD", key, 1, "a", 2, "b")
if ay, err := ledis.Values(c.Do("XZSCAN", key, "")); err != nil {
t.Fatal(err)
} else if len(ay) != 2 {
t.Fatal(len(ay))
} else {
checkScanValues(t, ay[1], "a", 1, "b", 2)
}
2014-08-27 06:29:17 +04:00
}