ledisdb/server/cmd_scan_test.go

222 lines
4.4 KiB
Go
Raw Normal View History

2014-08-26 19:21:45 +04:00
package server
import (
"fmt"
"os"
"testing"
2015-05-04 17:42:28 +03:00
2023-02-15 10:41:45 +03:00
"git.internal/re/ledisdb/config"
2015-05-04 17:42:28 +03:00
"github.com/siddontang/goredis"
2014-08-26 19:21:45 +04:00
)
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()
2015-03-11 06:54:02 +03:00
c := goredis.NewClient(cfg.Addr, "")
c.SetMaxIdleConns(1)
2014-08-26 19:21:45 +04:00
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-26 19:21:45 +04:00
}
2015-03-02 09:30:04 +03:00
func checkScanValues(t *testing.T, ay interface{}, values ...interface{}) {
2015-03-11 06:54:02 +03:00
a, err := goredis.Strings(ay, nil)
2014-08-26 19:21:45 +04:00
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", i, string(v), values[i]))
2014-08-26 19:21:45 +04:00
}
}
}
2015-03-11 06:54:02 +03:00
func checkScan(t *testing.T, c *goredis.Client, tp string) {
if ay, err := goredis.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-11 06:54:02 +03:00
if ay, err := goredis.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
}
2015-03-11 06:54:02 +03:00
func testKVScan(t *testing.T, c *goredis.Client) {
2014-08-27 06:29:17 +04:00
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-11 06:54:02 +03:00
func testHashKeyScan(t *testing.T, c *goredis.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-11 06:54:02 +03:00
func testListKeyScan(t *testing.T, c *goredis.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-11 06:54:02 +03:00
func testZSetKeyScan(t *testing.T, c *goredis.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-11 06:54:02 +03:00
func testSetKeyScan(t *testing.T, c *goredis.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 TestXHashScan(t *testing.T) {
2015-03-02 09:30:04 +03:00
c := getTestConn()
defer c.Close()
key := "scan_hash"
c.Do("HMSET", key, "a", 1, "b", 2)
2015-03-11 06:54:02 +03:00
if ay, err := goredis.Values(c.Do("XHSCAN", key, "")); err != nil {
2015-03-02 09:30:04 +03:00
t.Fatal(err)
} else if len(ay) != 2 {
t.Fatal(len(ay))
} else {
checkScanValues(t, ay[1], "a", 1, "b", 2)
}
}
func TestHashScan(t *testing.T) {
c := getTestConn()
defer c.Close()
key := "scan_hash"
c.Do("HMSET", key, "a", 1, "b", 2)
if ay, err := goredis.Values(c.Do("HSCAN", key, "0")); err != nil {
t.Fatal(err)
} else if len(ay) != 2 {
t.Fatal(len(ay))
} else {
checkScanValues(t, ay[1], "a", 1, "b", 2)
}
}
func TestXSetScan(t *testing.T) {
2015-03-02 09:30:04 +03:00
c := getTestConn()
defer c.Close()
key := "scan_set"
c.Do("SADD", key, "a", "b")
2015-03-11 06:54:02 +03:00
if ay, err := goredis.Values(c.Do("XSSCAN", key, "")); err != nil {
2015-03-02 09:30:04 +03:00
t.Fatal(err)
} else if len(ay) != 2 {
t.Fatal(len(ay))
} else {
checkScanValues(t, ay[1], "a", "b")
}
}
func TestSetScan(t *testing.T) {
c := getTestConn()
defer c.Close()
2015-03-02 09:30:04 +03:00
key := "scan_set"
c.Do("SADD", key, "a", "b")
if ay, err := goredis.Values(c.Do("SSCAN", key, "0")); err != nil {
t.Fatal(err)
} else if len(ay) != 2 {
t.Fatal(len(ay))
} else {
checkScanValues(t, ay[1], "a", "b")
}
2015-03-02 09:30:04 +03:00
}
func TestXZSetScan(t *testing.T) {
2015-03-02 09:30:04 +03:00
c := getTestConn()
defer c.Close()
key := "scan_zset"
c.Do("ZADD", key, 1, "a", 2, "b")
2015-03-11 06:54:02 +03:00
if ay, err := goredis.Values(c.Do("XZSCAN", key, "")); err != nil {
2015-03-02 09:30:04 +03:00
t.Fatal(err)
} else if len(ay) != 2 {
t.Fatal(len(ay))
} else {
checkScanValues(t, ay[1], "a", 1, "b", 2)
}
}
func TestZSetScan(t *testing.T) {
c := getTestConn()
defer c.Close()
key := "scan_zset"
c.Do("ZADD", key, 1, "a", 2, "b")
if ay, err := goredis.Values(c.Do("XZSCAN", key, "0")); err != nil {
t.Fatal(err)
} else if len(ay) != 2 {
t.Fatal(len(ay))
} else {
checkScanValues(t, ay[1], "a", 1, "b", 2)
}
2015-03-02 09:30:04 +03:00
2014-08-27 06:29:17 +04:00
}