support multi transaction

This commit is contained in:
hdt3213
2021-05-31 21:20:33 +08:00
parent 9d03314359
commit 67c385ee4a
50 changed files with 1919 additions and 1122 deletions

View File

@@ -11,29 +11,29 @@ import (
// basic add get and remove
func TestSAdd(t *testing.T) {
execFlushAll(testDB, [][]byte{})
testDB.Flush()
size := 100
// test sadd
key := utils.RandString(10)
for i := 0; i < size; i++ {
member := strconv.Itoa(i)
result := execSAdd(testDB, utils.ToBytesList(key, member))
result := testDB.Exec(nil, utils.ToCmdLine("sadd", key, member))
asserts.AssertIntReply(t, result, 1)
}
// test scard
result := execSCard(testDB, utils.ToBytesList(key))
result := testDB.Exec(nil, utils.ToCmdLine("SCard", key))
asserts.AssertIntReply(t, result, size)
// test is member
for i := 0; i < size; i++ {
member := strconv.Itoa(i)
result := execSIsMember(testDB, utils.ToBytesList(key, member))
result = testDB.Exec(nil, utils.ToCmdLine("SIsMember", key, member))
asserts.AssertIntReply(t, result, 1)
}
// test members
result = execSMembers(testDB, utils.ToBytesList(key))
result = testDB.Exec(nil, utils.ToCmdLine("SMembers", key))
multiBulk, ok := result.(*reply.MultiBulkReply)
if !ok {
t.Error(fmt.Sprintf("expected bulk reply, actually %s", result.ToBytes()))
@@ -46,25 +46,25 @@ func TestSAdd(t *testing.T) {
}
func TestSRem(t *testing.T) {
execFlushAll(testDB, [][]byte{})
testDB.Flush()
size := 100
// mock data
key := utils.RandString(10)
for i := 0; i < size; i++ {
member := strconv.Itoa(i)
execSAdd(testDB, utils.ToBytesList(key, member))
testDB.Exec(nil, utils.ToCmdLine("sadd", key, member))
}
for i := 0; i < size; i++ {
member := strconv.Itoa(i)
execSRem(testDB, utils.ToBytesList(key, member))
result := execSIsMember(testDB, utils.ToBytesList(key, member))
testDB.Exec(nil, utils.ToCmdLine("srem", key, member))
result := testDB.Exec(nil, utils.ToCmdLine("SIsMember", key, member))
asserts.AssertIntReply(t, result, 0)
}
}
func TestSInter(t *testing.T) {
execFlushAll(testDB, [][]byte{})
testDB.Flush()
size := 100
step := 10
@@ -75,39 +75,39 @@ func TestSInter(t *testing.T) {
keys = append(keys, key)
for j := start; j < size+start; j++ {
member := strconv.Itoa(j)
execSAdd(testDB, utils.ToBytesList(key, member))
testDB.Exec(nil, utils.ToCmdLine("sadd", key, member))
}
start += step
}
result := execSInter(testDB, utils.ToBytesList(keys...))
result := testDB.Exec(nil, utils.ToCmdLine2("sinter", keys...))
asserts.AssertMultiBulkReplySize(t, result, 70)
destKey := utils.RandString(10)
keysWithDest := []string{destKey}
keysWithDest = append(keysWithDest, keys...)
result = execSInterStore(testDB, utils.ToBytesList(keysWithDest...))
result = testDB.Exec(nil, utils.ToCmdLine2("SInterStore", keysWithDest...))
asserts.AssertIntReply(t, result, 70)
// test empty set
execFlushAll(testDB, [][]byte{})
testDB.Flush()
key0 := utils.RandString(10)
testDB.Remove(key0)
key1 := utils.RandString(10)
execSAdd(testDB, utils.ToBytesList(key1, "a", "b"))
testDB.Exec(nil, utils.ToCmdLine("sadd", key1, "a", "b"))
key2 := utils.RandString(10)
execSAdd(testDB, utils.ToBytesList(key2, "1", "2"))
result = execSInter(testDB, utils.ToBytesList(key0, key1, key2))
testDB.Exec(nil, utils.ToCmdLine("sadd", key1, "1", "2"))
result = testDB.Exec(nil, utils.ToCmdLine("sinter", key0, key1, key2))
asserts.AssertMultiBulkReplySize(t, result, 0)
result = execSInter(testDB, utils.ToBytesList(key1, key2))
result = testDB.Exec(nil, utils.ToCmdLine("sinter", key1, key2))
asserts.AssertMultiBulkReplySize(t, result, 0)
result = execSInterStore(testDB, utils.ToBytesList(utils.RandString(10), key0, key1, key2))
result = testDB.Exec(nil, utils.ToCmdLine("sinterstore", utils.RandString(10), key0, key1, key2))
asserts.AssertIntReply(t, result, 0)
result = execSInterStore(testDB, utils.ToBytesList(utils.RandString(10), key1, key2))
result = testDB.Exec(nil, utils.ToCmdLine("sinterstore", utils.RandString(10), key1, key2))
asserts.AssertIntReply(t, result, 0)
}
func TestSUnion(t *testing.T) {
execFlushAll(testDB, [][]byte{})
testDB.Flush()
size := 100
step := 10
@@ -118,22 +118,22 @@ func TestSUnion(t *testing.T) {
keys = append(keys, key)
for j := start; j < size+start; j++ {
member := strconv.Itoa(j)
execSAdd(testDB, utils.ToBytesList(key, member))
testDB.Exec(nil, utils.ToCmdLine("sadd", key, member))
}
start += step
}
result := execSUnion(testDB, utils.ToBytesList(keys...))
result := testDB.Exec(nil, utils.ToCmdLine2("sunion", keys...))
asserts.AssertMultiBulkReplySize(t, result, 130)
destKey := utils.RandString(10)
keysWithDest := []string{destKey}
keysWithDest = append(keysWithDest, keys...)
result = execSUnionStore(testDB, utils.ToBytesList(keysWithDest...))
result = testDB.Exec(nil, utils.ToCmdLine2("SUnionStore", keysWithDest...))
asserts.AssertIntReply(t, result, 130)
}
func TestSDiff(t *testing.T) {
execFlushAll(testDB, [][]byte{})
testDB.Flush()
size := 100
step := 20
@@ -144,52 +144,52 @@ func TestSDiff(t *testing.T) {
keys = append(keys, key)
for j := start; j < size+start; j++ {
member := strconv.Itoa(j)
execSAdd(testDB, utils.ToBytesList(key, member))
testDB.Exec(nil, utils.ToCmdLine("sadd", key, member))
}
start += step
}
result := execSDiff(testDB, utils.ToBytesList(keys...))
result := testDB.Exec(nil, utils.ToCmdLine2("SDiff", keys...))
asserts.AssertMultiBulkReplySize(t, result, step)
destKey := utils.RandString(10)
keysWithDest := []string{destKey}
keysWithDest = append(keysWithDest, keys...)
result = execSDiffStore(testDB, utils.ToBytesList(keysWithDest...))
result = testDB.Exec(nil, utils.ToCmdLine2("SDiffStore", keysWithDest...))
asserts.AssertIntReply(t, result, step)
// test empty set
execFlushAll(testDB, [][]byte{})
testDB.Flush()
key0 := utils.RandString(10)
testDB.Remove(key0)
key1 := utils.RandString(10)
execSAdd(testDB, utils.ToBytesList(key1, "a", "b"))
testDB.Exec(nil, utils.ToCmdLine("sadd", key1, "a", "b"))
key2 := utils.RandString(10)
execSAdd(testDB, utils.ToBytesList(key2, "a", "b"))
result = execSDiff(testDB, utils.ToBytesList(key0, key1, key2))
testDB.Exec(nil, utils.ToCmdLine("sadd", key2, "a", "b"))
result = testDB.Exec(nil, utils.ToCmdLine("sdiff", key0, key1, key2))
asserts.AssertMultiBulkReplySize(t, result, 0)
result = execSDiff(testDB, utils.ToBytesList(key1, key2))
result = testDB.Exec(nil, utils.ToCmdLine("sdiff", key1, key2))
asserts.AssertMultiBulkReplySize(t, result, 0)
result = execSDiffStore(testDB, utils.ToBytesList(utils.RandString(10), key0, key1, key2))
result = testDB.Exec(nil, utils.ToCmdLine("SDiffStore", utils.RandString(10), key0, key1, key2))
asserts.AssertIntReply(t, result, 0)
result = execSDiffStore(testDB, utils.ToBytesList(utils.RandString(10), key1, key2))
result = testDB.Exec(nil, utils.ToCmdLine("SDiffStore", utils.RandString(10), key1, key2))
asserts.AssertIntReply(t, result, 0)
}
func TestSRandMember(t *testing.T) {
execFlushAll(testDB, [][]byte{})
testDB.Flush()
key := utils.RandString(10)
for j := 0; j < 100; j++ {
member := strconv.Itoa(j)
execSAdd(testDB, utils.ToBytesList(key, member))
testDB.Exec(nil, utils.ToCmdLine("sadd", key, member))
}
result := execSRandMember(testDB, utils.ToBytesList(key))
result := testDB.Exec(nil, utils.ToCmdLine("SRandMember", key))
br, ok := result.(*reply.BulkReply)
if !ok && len(br.Arg) > 0 {
t.Error(fmt.Sprintf("expected bulk reply, actually %s", result.ToBytes()))
return
}
result = execSRandMember(testDB, utils.ToBytesList(key, "10"))
result = testDB.Exec(nil, utils.ToCmdLine("SRandMember", key, "10"))
asserts.AssertMultiBulkReplySize(t, result, 10)
multiBulk, ok := result.(*reply.MultiBulkReply)
if !ok {
@@ -205,12 +205,12 @@ func TestSRandMember(t *testing.T) {
return
}
result = execSRandMember(testDB, utils.ToBytesList(key, "110"))
result = testDB.Exec(nil, utils.ToCmdLine("SRandMember", key, "110"))
asserts.AssertMultiBulkReplySize(t, result, 100)
result = execSRandMember(testDB, utils.ToBytesList(key, "-10"))
result = testDB.Exec(nil, utils.ToCmdLine("SRandMember", key, "-10"))
asserts.AssertMultiBulkReplySize(t, result, 10)
result = execSRandMember(testDB, utils.ToBytesList(key, "-110"))
result = testDB.Exec(nil, utils.ToCmdLine("SRandMember", key, "-110"))
asserts.AssertMultiBulkReplySize(t, result, 110)
}