github.com/hdt3213/godis@v1.2.9/cluster/rename_test.go (about) 1 package cluster 2 3 import ( 4 "github.com/hdt3213/godis/lib/utils" 5 "github.com/hdt3213/godis/redis/connection" 6 "github.com/hdt3213/godis/redis/protocol/asserts" 7 "testing" 8 ) 9 10 func TestRename(t *testing.T) { 11 conn := new(connection.FakeConn) 12 testNodeA.db.Exec(conn, utils.ToCmdLine("FlushALL")) 13 14 // cross node rename 15 key := testNodeA.self + utils.RandString(10) 16 value := utils.RandString(10) 17 newKey := testNodeB.self + utils.RandString(10) // route to testNodeB, see mockPicker.PickNode 18 testNodeA.db.Exec(conn, utils.ToCmdLine("SET", key, value, "ex", "1000")) 19 result := Rename(testNodeA, conn, utils.ToCmdLine("RENAME", key, newKey)) 20 asserts.AssertStatusReply(t, result, "OK") 21 result = testNodeA.db.Exec(conn, utils.ToCmdLine("EXISTS", key)) 22 asserts.AssertIntReply(t, result, 0) 23 result = testNodeB.db.Exec(conn, utils.ToCmdLine("EXISTS", newKey)) 24 asserts.AssertIntReply(t, result, 1) 25 result = testNodeB.db.Exec(conn, utils.ToCmdLine("TTL", newKey)) 26 asserts.AssertIntReplyGreaterThan(t, result, 0) 27 28 // same node rename 29 key = testNodeA.self + utils.RandString(10) 30 value = utils.RandString(10) 31 newKey = key + utils.RandString(2) 32 testNodeA.db.Exec(conn, utils.ToCmdLine("SET", key, value, "ex", "1000")) 33 result = Rename(testNodeA, conn, utils.ToCmdLine("RENAME", key, newKey)) 34 asserts.AssertStatusReply(t, result, "OK") 35 result = testNodeA.db.Exec(conn, utils.ToCmdLine("EXISTS", key)) 36 asserts.AssertIntReply(t, result, 0) 37 result = testNodeA.db.Exec(conn, utils.ToCmdLine("EXISTS", newKey)) 38 asserts.AssertIntReply(t, result, 1) 39 result = testNodeA.db.Exec(conn, utils.ToCmdLine("TTL", newKey)) 40 asserts.AssertIntReplyGreaterThan(t, result, 0) 41 42 // test src prepare failed 43 *simulateATimout = true 44 key = testNodeA.self + utils.RandString(10) 45 newKey = testNodeB.self + utils.RandString(10) // route to testNodeB, see mockPicker.PickNode 46 value = utils.RandString(10) 47 testNodeA.db.Exec(conn, utils.ToCmdLine("SET", key, value, "ex", "1000")) 48 result = Rename(testNodeB, conn, utils.ToCmdLine("RENAME", key, newKey)) 49 asserts.AssertErrReply(t, result, "ERR timeout") 50 result = testNodeA.db.Exec(conn, utils.ToCmdLine("EXISTS", key)) 51 asserts.AssertIntReply(t, result, 1) 52 result = testNodeA.db.Exec(conn, utils.ToCmdLine("TTL", key)) 53 asserts.AssertIntReplyGreaterThan(t, result, 0) 54 result = testNodeB.db.Exec(conn, utils.ToCmdLine("EXISTS", newKey)) 55 asserts.AssertIntReply(t, result, 0) 56 *simulateATimout = false 57 58 // test dest prepare failed 59 *simulateBTimout = true 60 key = testNodeA.self + utils.RandString(10) 61 newKey = testNodeB.self + utils.RandString(10) // route to testNodeB, see mockPicker.PickNode 62 value = utils.RandString(10) 63 testNodeA.db.Exec(conn, utils.ToCmdLine("SET", key, value, "ex", "1000")) 64 result = Rename(testNodeA, conn, utils.ToCmdLine("RENAME", key, newKey)) 65 asserts.AssertErrReply(t, result, "ERR timeout") 66 result = testNodeA.db.Exec(conn, utils.ToCmdLine("EXISTS", key)) 67 asserts.AssertIntReply(t, result, 1) 68 result = testNodeA.db.Exec(conn, utils.ToCmdLine("TTL", key)) 69 asserts.AssertIntReplyGreaterThan(t, result, 0) 70 result = testNodeB.db.Exec(conn, utils.ToCmdLine("EXISTS", newKey)) 71 asserts.AssertIntReply(t, result, 0) 72 *simulateBTimout = false 73 74 result = Rename(testNodeA, conn, utils.ToCmdLine("RENAME", key)) 75 asserts.AssertErrReply(t, result, "ERR wrong number of arguments for 'rename' command") 76 } 77 78 func TestRenameNx(t *testing.T) { 79 conn := new(connection.FakeConn) 80 testNodeA.db.Exec(conn, utils.ToCmdLine("FlushALL")) 81 // cross node rename 82 key := testNodeA.self + utils.RandString(10) 83 value := utils.RandString(10) 84 newKey := testNodeB.self + utils.RandString(10) // route to testNodeB, see mockPicker.PickNode 85 testNodeA.db.Exec(conn, utils.ToCmdLine("SET", key, value, "ex", "1000")) 86 result := RenameNx(testNodeA, conn, utils.ToCmdLine("RENAMENX", key, newKey)) 87 asserts.AssertIntReply(t, result, 1) 88 result = testNodeA.db.Exec(conn, utils.ToCmdLine("EXISTS", key)) 89 asserts.AssertIntReply(t, result, 0) 90 result = testNodeB.db.Exec(conn, utils.ToCmdLine("EXISTS", newKey)) 91 asserts.AssertIntReply(t, result, 1) 92 result = testNodeB.db.Exec(conn, utils.ToCmdLine("TTL", newKey)) 93 asserts.AssertIntReplyGreaterThan(t, result, 0) 94 95 // cross node rename, dest key exist 96 key = testNodeA.self + utils.RandString(10) 97 value = utils.RandString(10) 98 newKey = testNodeB.self + utils.RandString(10) // route to testNodeB, see mockPicker.PickNode 99 testNodeA.db.Exec(conn, utils.ToCmdLine("SET", key, value, "ex", "1000")) 100 testNodeB.db.Exec(conn, utils.ToCmdLine("SET", newKey, newKey)) 101 result = RenameNx(testNodeA, conn, utils.ToCmdLine("RENAMENX", key, newKey)) 102 asserts.AssertIntReply(t, result, 0) 103 result = testNodeA.db.Exec(conn, utils.ToCmdLine("EXISTS", key)) 104 asserts.AssertIntReply(t, result, 1) 105 result = testNodeA.db.Exec(conn, utils.ToCmdLine("TTL", key)) 106 asserts.AssertIntReplyGreaterThan(t, result, 0) 107 result = testNodeB.db.Exec(conn, utils.ToCmdLine("GET", newKey)) 108 asserts.AssertBulkReply(t, result, newKey) 109 110 // same node rename 111 key = testNodeA.self + utils.RandString(10) 112 value = utils.RandString(10) 113 newKey = key + utils.RandString(2) 114 testNodeA.db.Exec(conn, utils.ToCmdLine("SET", key, value, "ex", "1000")) 115 result = RenameNx(testNodeA, conn, utils.ToCmdLine("RENAMENX", key, newKey)) 116 asserts.AssertIntReply(t, result, 1) 117 result = testNodeA.db.Exec(conn, utils.ToCmdLine("EXISTS", key)) 118 asserts.AssertIntReply(t, result, 0) 119 result = testNodeA.db.Exec(conn, utils.ToCmdLine("EXISTS", newKey)) 120 asserts.AssertIntReply(t, result, 1) 121 result = testNodeA.db.Exec(conn, utils.ToCmdLine("TTL", newKey)) 122 asserts.AssertIntReplyGreaterThan(t, result, 0) 123 124 // test src prepare failed 125 *simulateATimout = true 126 key = testNodeA.self + utils.RandString(10) 127 newKey = testNodeB.self + utils.RandString(10) // route to testNodeB, see mockPicker.PickNode 128 value = utils.RandString(10) 129 testNodeA.db.Exec(conn, utils.ToCmdLine("SET", key, value, "ex", "1000")) 130 result = RenameNx(testNodeB, conn, utils.ToCmdLine("RENAMENX", key, newKey)) 131 asserts.AssertErrReply(t, result, "ERR timeout") 132 result = testNodeA.db.Exec(conn, utils.ToCmdLine("EXISTS", key)) 133 asserts.AssertIntReply(t, result, 1) 134 result = testNodeA.db.Exec(conn, utils.ToCmdLine("TTL", key)) 135 asserts.AssertIntReplyGreaterThan(t, result, 0) 136 result = testNodeB.db.Exec(conn, utils.ToCmdLine("EXISTS", newKey)) 137 asserts.AssertIntReply(t, result, 0) 138 *simulateATimout = false 139 140 // test dest prepare failed 141 *simulateBTimout = true 142 key = testNodeA.self + utils.RandString(10) 143 newKey = testNodeB.self + utils.RandString(10) // route to testNodeB, see mockPicker.PickNode 144 value = utils.RandString(10) 145 testNodeA.db.Exec(conn, utils.ToCmdLine("SET", key, value, "ex", "1000")) 146 result = RenameNx(testNodeA, conn, utils.ToCmdLine("RENAMENX", key, newKey)) 147 asserts.AssertErrReply(t, result, "ERR timeout") 148 result = testNodeA.db.Exec(conn, utils.ToCmdLine("EXISTS", key)) 149 asserts.AssertIntReply(t, result, 1) 150 result = testNodeA.db.Exec(conn, utils.ToCmdLine("TTL", key)) 151 asserts.AssertIntReplyGreaterThan(t, result, 0) 152 result = testNodeB.db.Exec(conn, utils.ToCmdLine("EXISTS", newKey)) 153 asserts.AssertIntReply(t, result, 0) 154 *simulateBTimout = false 155 156 result = RenameNx(testNodeA, conn, utils.ToCmdLine("RENAMENX", key)) 157 asserts.AssertErrReply(t, result, "ERR wrong number of arguments for 'renamenx' command") 158 159 }