Files
redis-go/cluster/transaction_test.go
2021-05-04 20:51:55 +08:00

46 lines
1.4 KiB
Go

package cluster
import (
"github.com/hdt3213/godis/redis/reply/asserts"
"math/rand"
"strconv"
"testing"
)
func TestRollback(t *testing.T) {
// rollback uncommitted transaction
FlushAll(testCluster, nil, toArgs("FLUSHALL"))
txId := rand.Int63()
txIdStr := strconv.FormatInt(txId, 10)
keys := []string{"a", "b"}
groupMap := testCluster.groupBy(keys)
args := []string{txIdStr}
args = append(args, keys...)
testCluster.Exec(nil, toArgs("SET", "a", "a"))
ret := PrepareDel(testCluster, nil, makeArgs("PrepareDel", args...))
asserts.AssertNotError(t, ret)
RequestRollback(testCluster, nil, txId, groupMap)
ret = testCluster.Exec(nil, toArgs("GET", "a"))
asserts.AssertBulkReply(t, ret, "a")
// rollback committed transaction
FlushAll(testCluster, nil, toArgs("FLUSHALL"))
txId = rand.Int63()
txIdStr = strconv.FormatInt(txId, 10)
args = []string{txIdStr}
args = append(args, keys...)
testCluster.Exec(nil, toArgs("SET", "a", "a"))
ret = PrepareDel(testCluster, nil, makeArgs("PrepareDel", args...))
asserts.AssertNotError(t, ret)
_, err := RequestCommit(testCluster, nil, txId, groupMap)
if err != nil {
t.Errorf("del failed %v", err)
return
}
ret = testCluster.Exec(nil, toArgs("GET", "a"))
asserts.AssertNullBulk(t, ret)
RequestRollback(testCluster, nil, txId, groupMap)
ret = testCluster.Exec(nil, toArgs("GET", "a"))
asserts.AssertBulkReply(t, ret, "a")
}