From fc3915074080d2e8f1170f2ea5ff55286ca8fda2 Mon Sep 17 00:00:00 2001 From: hdt3213 Date: Fri, 7 May 2021 00:04:38 +0800 Subject: [PATCH] add some unittest --- cluster/rename.go | 2 +- cluster/rename_test.go | 64 ++++++++++++++++++++++++++++++ datastruct/dict/concurrent_test.go | 12 ++++++ datastruct/dict/simple_test.go | 39 ++++++++++++++++++ db/geo_test.go | 15 +++++++ 5 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 cluster/rename_test.go create mode 100644 datastruct/dict/simple_test.go diff --git a/cluster/rename.go b/cluster/rename.go index 454a04b..d91d8f0 100644 --- a/cluster/rename.go +++ b/cluster/rename.go @@ -5,7 +5,7 @@ import ( "github.com/hdt3213/godis/redis/reply" ) -// TODO: support multiplex slots +// Rename renames a key, the origin and the destination must within the same slot func Rename(cluster *Cluster, c redis.Connection, args [][]byte) redis.Reply { if len(args) != 3 { return reply.MakeErrReply("ERR wrong number of arguments for 'rename' command") diff --git a/cluster/rename_test.go b/cluster/rename_test.go new file mode 100644 index 0000000..20e85e8 --- /dev/null +++ b/cluster/rename_test.go @@ -0,0 +1,64 @@ +package cluster + +import ( + "fmt" + "github.com/hdt3213/godis/db" + "github.com/hdt3213/godis/lib/utils" + "github.com/hdt3213/godis/redis/reply" + "github.com/hdt3213/godis/redis/reply/asserts" + "testing" +) + +func TestRename(t *testing.T) { + testDB := testCluster.db + db.FlushAll(testDB, [][]byte{}) + key := utils.RandString(10) + value := utils.RandString(10) + newKey := key + utils.RandString(2) + db.Set(testDB, utils.ToBytesList(key, value, "ex", "1000")) + result := Rename(testCluster, nil, utils.ToBytesList("RENAME", key, newKey)) + if _, ok := result.(*reply.OkReply); !ok { + t.Error("expect ok") + return + } + result = db.Exists(testDB, utils.ToBytesList(key)) + asserts.AssertIntReply(t, result, 0) + result = db.Exists(testDB, utils.ToBytesList(newKey)) + asserts.AssertIntReply(t, result, 1) + // check ttl + result = db.TTL(testDB, utils.ToBytesList(newKey)) + intResult, ok := result.(*reply.IntReply) + if !ok { + t.Error(fmt.Sprintf("expected int reply, actually %s", result.ToBytes())) + return + } + if intResult.Code <= 0 { + t.Errorf("expected ttl more than 0, actual: %d", intResult.Code) + return + } +} + +func TestRenameNx(t *testing.T) { + testDB := testCluster.db + db.FlushAll(testDB, [][]byte{}) + key := utils.RandString(10) + value := utils.RandString(10) + newKey := key + utils.RandString(2) + db.Set(testCluster.db, utils.ToBytesList(key, value, "ex", "1000")) + result := RenameNx(testCluster, nil, utils.ToBytesList("RENAMENX", key, newKey)) + asserts.AssertIntReply(t, result, 1) + result = db.Exists(testDB, utils.ToBytesList(key)) + asserts.AssertIntReply(t, result, 0) + result = db.Exists(testDB, utils.ToBytesList(newKey)) + asserts.AssertIntReply(t, result, 1) + result = db.TTL(testDB, utils.ToBytesList(newKey)) + intResult, ok := result.(*reply.IntReply) + if !ok { + t.Error(fmt.Sprintf("expected int reply, actually %s", result.ToBytes())) + return + } + if intResult.Code <= 0 { + t.Errorf("expected ttl more than 0, actual: %d", intResult.Code) + return + } +} diff --git a/datastruct/dict/concurrent_test.go b/datastruct/dict/concurrent_test.go index e5234bf..eb3671b 100644 --- a/datastruct/dict/concurrent_test.go +++ b/datastruct/dict/concurrent_test.go @@ -1,6 +1,7 @@ package dict import ( + "github.com/hdt3213/godis/lib/utils" "strconv" "sync" "testing" @@ -267,3 +268,14 @@ func TestConcurrentRandomKey(t *testing.T) { t.Errorf("get duplicated keys in result") } } + +func TestConcurrentDict_Keys(t *testing.T) { + d := MakeConcurrent(0) + size := 10 + for i := 0; i < size; i++ { + d.Put(utils.RandString(5), utils.RandString(5)) + } + if len(d.Keys()) != size { + t.Errorf("expect %d keys, actual: %d", size, len(d.Keys())) + } +} \ No newline at end of file diff --git a/datastruct/dict/simple_test.go b/datastruct/dict/simple_test.go new file mode 100644 index 0000000..9a52bfd --- /dev/null +++ b/datastruct/dict/simple_test.go @@ -0,0 +1,39 @@ +package dict + +import ( + "github.com/hdt3213/godis/lib/utils" + "testing" +) + +func TestSimpleDict_Keys(t *testing.T) { + d := MakeSimple() + size := 10 + for i := 0; i < size; i++ { + d.Put(utils.RandString(5), utils.RandString(5)) + } + if len(d.Keys()) != size { + t.Errorf("expect %d keys, actual: %d", size, len(d.Keys())) + } +} + +func TestSimpleDict_PutIfExists(t *testing.T) { + d := MakeSimple() + key := utils.RandString(5) + val := key + "1" + ret := d.PutIfExists(key, val) + if ret != 0 { + t.Error("expect 0") + return + } + d.Put(key, val) + val = key + "2" + ret = d.PutIfExists(key, val) + if ret != 1 { + t.Error("expect 1") + return + } + if v, _ := d.Get(key); v != val { + t.Error("wrong value") + return + } +} diff --git a/db/geo_test.go b/db/geo_test.go index 3725960..ef66b5e 100644 --- a/db/geo_test.go +++ b/db/geo_test.go @@ -85,4 +85,19 @@ func TestGeoDist(t *testing.T) { if dist < 166.274 || dist > 166.275 { t.Errorf("expected 166.274, actual: %f", dist) } + + result = GeoDist(testDB, utils.ToBytesList(key, pos1, pos2, "m")) + bulkReply, ok = result.(*reply.BulkReply) + if !ok { + t.Error(fmt.Sprintf("expected bulk reply, actually %s", result.ToBytes())) + return + } + dist, err = strconv.ParseFloat(string(bulkReply.Arg), 10) + if err != nil { + t.Error(err) + return + } + if dist < 166274 || dist > 166275 { + t.Errorf("expected 166274, actual: %f", dist) + } }