ledisdb/server/scan_test.go

168 lines
3.6 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)
2014-08-27 06:29:17 +04:00
testHashScan(t, c)
testListScan(t, c)
testZSetScan(t, c)
testSetScan(t, c)
testBitScan(t, c)
2014-08-26 19:21:45 +04:00
}
func checkScanValues(t *testing.T, ay interface{}, values ...int) {
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 {
if string(v) != fmt.Sprintf("%d", values[i]) {
t.Fatal(fmt.Sprintf("%d %s != %d", string(v), values[i]))
}
}
}
2014-08-27 06:29:17 +04:00
func checkScan(t *testing.T, c *ledis.Client, cmd string) {
if ay, err := ledis.Values(c.Do(cmd, "", "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)
}
2014-08-27 06:29:17 +04:00
if ay, err := ledis.Values(c.Do(cmd, "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
}
2014-10-20 18:36:16 +04:00
func checkRevScan(t *testing.T, c *ledis.Client, cmd string) {
if ay, err := ledis.Values(c.Do(cmd, "", "count", 5)); err != nil {
t.Fatal(err)
} else if len(ay) != 2 {
t.Fatal(len(ay))
} else if n := ay[0].([]byte); string(n) != "5" {
t.Fatal(string(n))
} else {
checkScanValues(t, ay[1], 9, 8, 7, 6, 5)
}
if ay, err := ledis.Values(c.Do(cmd, "5", "count", 6)); err != nil {
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], 4, 3, 2, 1, 0)
}
}
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)
}
}
2014-09-09 05:53:26 +04:00
checkScan(t, c, "xscan")
2014-10-20 18:36:16 +04:00
checkRevScan(t, c, "xrevscan")
2014-08-27 06:29:17 +04:00
}
func testHashScan(t *testing.T, c *ledis.Client) {
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)
}
}
checkScan(t, c, "xhscan")
checkRevScan(t, c, "xhrevscan")
2014-08-27 06:29:17 +04:00
}
func testListScan(t *testing.T, c *ledis.Client) {
for i := 0; i < 10; i++ {
if _, err := c.Do("lpush", fmt.Sprintf("%d", i), fmt.Sprintf("%d", i)); err != nil {
t.Fatal(err)
}
}
checkScan(t, c, "xlscan")
checkRevScan(t, c, "xlrevscan")
2014-08-27 06:29:17 +04:00
}
func testZSetScan(t *testing.T, c *ledis.Client) {
for i := 0; i < 10; i++ {
if _, err := c.Do("zadd", fmt.Sprintf("%d", i), i, []byte("value")); err != nil {
t.Fatal(err)
}
}
2014-09-09 05:53:26 +04:00
checkScan(t, c, "zxscan")
2014-10-20 18:36:16 +04:00
checkRevScan(t, c, "zxrevscan")
2014-08-27 06:29:17 +04:00
}
func testSetScan(t *testing.T, c *ledis.Client) {
for i := 0; i < 10; i++ {
if _, err := c.Do("sadd", fmt.Sprintf("%d", i), fmt.Sprintf("%d", i)); err != nil {
t.Fatal(err)
}
}
checkScan(t, c, "xsscan")
checkRevScan(t, c, "xsrevscan")
2014-08-27 06:29:17 +04:00
}
func testBitScan(t *testing.T, c *ledis.Client) {
for i := 0; i < 10; i++ {
if _, err := c.Do("bsetbit", fmt.Sprintf("%d", i), 1024, 1); err != nil {
t.Fatal(err)
}
2014-08-26 19:21:45 +04:00
}
checkScan(t, c, "xbscan")
checkRevScan(t, c, "xbrevscan")
2014-08-26 19:21:45 +04:00
}