github.com/hdt3213/godis@v1.2.9/cluster/tcc_test.go (about) 1 package cluster 2 3 import ( 4 "github.com/hdt3213/godis/redis/connection" 5 "github.com/hdt3213/godis/redis/protocol/asserts" 6 "math/rand" 7 "strconv" 8 "testing" 9 ) 10 11 func TestRollback(t *testing.T) { 12 // rollback uncommitted transaction 13 conn := new(connection.FakeConn) 14 FlushAll(testNodeA, conn, toArgs("FLUSHALL")) 15 txID := rand.Int63() 16 txIDStr := strconv.FormatInt(txID, 10) 17 keys := []string{"a", "b"} 18 groupMap := testNodeA.groupBy(keys) 19 args := []string{txIDStr, "DEL"} 20 args = append(args, keys...) 21 testNodeA.Exec(conn, toArgs("SET", "a", "a")) 22 ret := execPrepare(testNodeA, conn, makeArgs("Prepare", args...)) 23 asserts.AssertNotError(t, ret) 24 requestRollback(testNodeA, conn, txID, groupMap) 25 ret = testNodeA.Exec(conn, toArgs("GET", "a")) 26 asserts.AssertBulkReply(t, ret, "a") 27 28 // rollback committed transaction 29 FlushAll(testNodeA, conn, toArgs("FLUSHALL")) 30 txID = rand.Int63() 31 txIDStr = strconv.FormatInt(txID, 10) 32 args = []string{txIDStr, "DEL"} 33 args = append(args, keys...) 34 testNodeA.Exec(conn, toArgs("SET", "a", "a")) 35 ret = execPrepare(testNodeA, conn, makeArgs("Prepare", args...)) 36 asserts.AssertNotError(t, ret) 37 _, err := requestCommit(testNodeA, conn, txID, groupMap) 38 if err != nil { 39 t.Errorf("del failed %v", err) 40 return 41 } 42 ret = testNodeA.Exec(conn, toArgs("GET", "a")) 43 asserts.AssertNullBulk(t, ret) 44 requestRollback(testNodeA, conn, txID, groupMap) 45 ret = testNodeA.Exec(conn, toArgs("GET", "a")) 46 asserts.AssertBulkReply(t, ret, "a") 47 }