github.com/hdt3213/godis@v1.2.9/database/keys_test.go (about) 1 package database 2 3 import ( 4 "fmt" 5 "github.com/hdt3213/godis/lib/utils" 6 "github.com/hdt3213/godis/redis/connection" 7 "github.com/hdt3213/godis/redis/protocol" 8 "github.com/hdt3213/godis/redis/protocol/asserts" 9 "strconv" 10 "testing" 11 "time" 12 ) 13 14 func TestExists(t *testing.T) { 15 testDB.Flush() 16 key := utils.RandString(10) 17 value := utils.RandString(10) 18 testDB.Exec(nil, utils.ToCmdLine("set", key, value)) 19 result := testDB.Exec(nil, utils.ToCmdLine("exists", key)) 20 asserts.AssertIntReply(t, result, 1) 21 key = utils.RandString(10) 22 result = testDB.Exec(nil, utils.ToCmdLine("exists", key)) 23 asserts.AssertIntReply(t, result, 0) 24 } 25 26 func TestType(t *testing.T) { 27 testDB.Flush() 28 key := utils.RandString(10) 29 value := utils.RandString(10) 30 testDB.Exec(nil, utils.ToCmdLine("set", key, value)) 31 result := testDB.Exec(nil, utils.ToCmdLine("type", key)) 32 asserts.AssertStatusReply(t, result, "string") 33 34 testDB.Remove(key) 35 result = testDB.Exec(nil, utils.ToCmdLine("type", key)) 36 asserts.AssertStatusReply(t, result, "none") 37 execRPush(testDB, utils.ToCmdLine(key, value)) 38 result = testDB.Exec(nil, utils.ToCmdLine("type", key)) 39 asserts.AssertStatusReply(t, result, "list") 40 41 testDB.Remove(key) 42 testDB.Exec(nil, utils.ToCmdLine("hset", key, key, value)) 43 result = testDB.Exec(nil, utils.ToCmdLine("type", key)) 44 asserts.AssertStatusReply(t, result, "hash") 45 46 testDB.Remove(key) 47 testDB.Exec(nil, utils.ToCmdLine("sadd", key, value)) 48 result = testDB.Exec(nil, utils.ToCmdLine("type", key)) 49 asserts.AssertStatusReply(t, result, "set") 50 51 testDB.Remove(key) 52 testDB.Exec(nil, utils.ToCmdLine("zadd", key, "1", value)) 53 result = testDB.Exec(nil, utils.ToCmdLine("type", key)) 54 asserts.AssertStatusReply(t, result, "zset") 55 } 56 57 func TestRename(t *testing.T) { 58 testDB.Flush() 59 key := utils.RandString(10) 60 value := utils.RandString(10) 61 newKey := key + utils.RandString(2) 62 testDB.Exec(nil, utils.ToCmdLine("set", key, value, "ex", "1000")) 63 result := testDB.Exec(nil, utils.ToCmdLine("rename", key, newKey)) 64 if _, ok := result.(*protocol.OkReply); !ok { 65 t.Error("expect ok") 66 return 67 } 68 result = testDB.Exec(nil, utils.ToCmdLine("exists", key)) 69 asserts.AssertIntReply(t, result, 0) 70 result = testDB.Exec(nil, utils.ToCmdLine("exists", newKey)) 71 asserts.AssertIntReply(t, result, 1) 72 // check ttl 73 result = testDB.Exec(nil, utils.ToCmdLine("ttl", newKey)) 74 intResult, ok := result.(*protocol.IntReply) 75 if !ok { 76 t.Error(fmt.Sprintf("expected int protocol, actually %s", result.ToBytes())) 77 return 78 } 79 if intResult.Code <= 0 { 80 t.Errorf("expected ttl more than 0, actual: %d", intResult.Code) 81 return 82 } 83 } 84 85 func TestRenameNx(t *testing.T) { 86 testDB.Flush() 87 key := utils.RandString(10) 88 value := utils.RandString(10) 89 newKey := key + utils.RandString(2) 90 testDB.Exec(nil, utils.ToCmdLine("set", key, value, "ex", "1000")) 91 result := testDB.Exec(nil, utils.ToCmdLine("RenameNx", key, newKey)) 92 asserts.AssertIntReply(t, result, 1) 93 result = testDB.Exec(nil, utils.ToCmdLine("exists", key)) 94 asserts.AssertIntReply(t, result, 0) 95 result = testDB.Exec(nil, utils.ToCmdLine("exists", newKey)) 96 asserts.AssertIntReply(t, result, 1) 97 result = testDB.Exec(nil, utils.ToCmdLine("ttl", newKey)) 98 intResult, ok := result.(*protocol.IntReply) 99 if !ok { 100 t.Error(fmt.Sprintf("expected int protocol, actually %s", result.ToBytes())) 101 return 102 } 103 if intResult.Code <= 0 { 104 t.Errorf("expected ttl more than 0, actual: %d", intResult.Code) 105 return 106 } 107 } 108 109 func TestTTL(t *testing.T) { 110 testDB.Flush() 111 key := utils.RandString(10) 112 value := utils.RandString(10) 113 testDB.Exec(nil, utils.ToCmdLine("set", key, value)) 114 115 result := testDB.Exec(nil, utils.ToCmdLine("expire", key, "1000")) 116 asserts.AssertIntReply(t, result, 1) 117 result = testDB.Exec(nil, utils.ToCmdLine("ttl", key)) 118 intResult, ok := result.(*protocol.IntReply) 119 if !ok { 120 t.Error(fmt.Sprintf("expected int protocol, actually %s", result.ToBytes())) 121 return 122 } 123 if intResult.Code <= 0 { 124 t.Errorf("expected ttl more than 0, actual: %d", intResult.Code) 125 return 126 } 127 128 result = testDB.Exec(nil, utils.ToCmdLine("persist", key)) 129 asserts.AssertIntReply(t, result, 1) 130 result = testDB.Exec(nil, utils.ToCmdLine("ttl", key)) 131 asserts.AssertIntReply(t, result, -1) 132 133 result = testDB.Exec(nil, utils.ToCmdLine("PExpire", key, "1000000")) 134 asserts.AssertIntReply(t, result, 1) 135 result = testDB.Exec(nil, utils.ToCmdLine("PTTL", key)) 136 intResult, ok = result.(*protocol.IntReply) 137 if !ok { 138 t.Error(fmt.Sprintf("expected int protocol, actually %s", result.ToBytes())) 139 return 140 } 141 if intResult.Code <= 0 { 142 t.Errorf("expected ttl more than 0, actual: %d", intResult.Code) 143 return 144 } 145 } 146 147 func TestExpire(t *testing.T) { 148 key := utils.RandString(10) 149 value := utils.RandString(10) 150 testDB.Exec(nil, utils.ToCmdLine("SET", key, value)) 151 testDB.Exec(nil, utils.ToCmdLine("PEXPIRE", key, "100")) 152 time.Sleep(2 * time.Second) 153 result := testDB.Exec(nil, utils.ToCmdLine("TTL", key)) 154 asserts.AssertIntReply(t, result, -2) 155 156 } 157 158 func TestExpireAt(t *testing.T) { 159 testDB.Flush() 160 key := utils.RandString(10) 161 value := utils.RandString(10) 162 testDB.Exec(nil, utils.ToCmdLine("set", key, value)) 163 164 expireAt := time.Now().Add(time.Minute).Unix() 165 result := testDB.Exec(nil, utils.ToCmdLine("ExpireAt", key, strconv.FormatInt(expireAt, 10))) 166 167 asserts.AssertIntReply(t, result, 1) 168 result = testDB.Exec(nil, utils.ToCmdLine("ttl", key)) 169 intResult, ok := result.(*protocol.IntReply) 170 if !ok { 171 t.Error(fmt.Sprintf("expected int protocol, actually %s", result.ToBytes())) 172 return 173 } 174 if intResult.Code <= 0 { 175 t.Errorf("expected ttl more than 0, actual: %d", intResult.Code) 176 return 177 } 178 179 expireAt = time.Now().Add(time.Minute).Unix() 180 result = testDB.Exec(nil, utils.ToCmdLine("PExpireAt", key, strconv.FormatInt(expireAt*1000, 10))) 181 asserts.AssertIntReply(t, result, 1) 182 result = testDB.Exec(nil, utils.ToCmdLine("ttl", key)) 183 intResult, ok = result.(*protocol.IntReply) 184 if !ok { 185 t.Error(fmt.Sprintf("expected int protocol, actually %s", result.ToBytes())) 186 return 187 } 188 if intResult.Code <= 0 { 189 t.Errorf("expected ttl more than 0, actual: %d", intResult.Code) 190 return 191 } 192 } 193 194 func TestExpiredTime(t *testing.T) { 195 //测试ExpireTime 196 testDB.Flush() 197 key := utils.RandString(10) 198 value := utils.RandString(10) 199 testDB.Exec(nil, utils.ToCmdLine("set", key, value)) 200 201 result := testDB.Exec(nil, utils.ToCmdLine("ttl", key)) 202 asserts.AssertIntReply(t, result, -1) 203 result = testDB.Exec(nil, utils.ToCmdLine("EXPIRETIME", key)) 204 asserts.AssertIntReply(t, result, -1) 205 result = testDB.Exec(nil, utils.ToCmdLine("PEXPIRETIME", key)) 206 asserts.AssertIntReply(t, result, -1) 207 208 testDB.Exec(nil, utils.ToCmdLine("EXPIRE", key, "2")) 209 //tt := time.Now() 210 result = testDB.Exec(nil, utils.ToCmdLine("ttl", key)) 211 intResult, ok := result.(*protocol.IntReply) 212 if !ok { 213 t.Error(fmt.Sprintf("expected int protocol, actually %s", result.ToBytes())) 214 return 215 } 216 if intResult.Code < 0 || intResult.Code > 2 { 217 t.Errorf("expected ttl more than 0, actual: %d", intResult.Code) 218 return 219 } 220 result = testDB.Exec(nil, utils.ToCmdLine("EXPIRETIME", key)) 221 asserts.AssertIntReply(t, result, int(time.Now().Add(2*time.Second).Unix())) 222 intResult, ok = result.(*protocol.IntReply) 223 if !ok { 224 t.Error(fmt.Sprintf("expected int protocol, actually %s", result.ToBytes())) 225 return 226 } 227 if intResult.Code <= 0 { 228 t.Errorf("expected ttl more than 0, actual: %d", intResult.Code) 229 return 230 } 231 232 result = testDB.Exec(nil, utils.ToCmdLine("PEXPIRETIME", key)) 233 asserts.AssertIntReply(t, result, int(time.Now().Add(2*time.Second).UnixMilli())) 234 intResult, ok = result.(*protocol.IntReply) 235 if !ok { 236 t.Error(fmt.Sprintf("expected int protocol, actually %s", result.ToBytes())) 237 return 238 } 239 if intResult.Code <= 0 { 240 t.Errorf("expected ttl more than 0, actual: %d", intResult.Code) 241 return 242 } 243 244 time.Sleep(3 * time.Second) 245 result = testDB.Exec(nil, utils.ToCmdLine("ttl", key)) 246 asserts.AssertIntReply(t, result, -2) 247 result = testDB.Exec(nil, utils.ToCmdLine("EXPIRETIME", key)) 248 asserts.AssertIntReply(t, result, -2) 249 intResult, ok = result.(*protocol.IntReply) 250 result = testDB.Exec(nil, utils.ToCmdLine("PEXPIRETIME", key)) 251 asserts.AssertIntReply(t, result, -2) 252 intResult, ok = result.(*protocol.IntReply) 253 254 } 255 256 func TestKeys(t *testing.T) { 257 testDB.Flush() 258 key := utils.RandString(10) 259 value := utils.RandString(10) 260 testDB.Exec(nil, utils.ToCmdLine("set", key, value)) 261 testDB.Exec(nil, utils.ToCmdLine("set", "a:"+key, value)) 262 testDB.Exec(nil, utils.ToCmdLine("set", "b:"+key, value)) 263 264 result := testDB.Exec(nil, utils.ToCmdLine("keys", "*")) 265 asserts.AssertMultiBulkReplySize(t, result, 3) 266 result = testDB.Exec(nil, utils.ToCmdLine("keys", "a:*")) 267 asserts.AssertMultiBulkReplySize(t, result, 1) 268 result = testDB.Exec(nil, utils.ToCmdLine("keys", "?:*")) 269 asserts.AssertMultiBulkReplySize(t, result, 2) 270 } 271 272 func TestCopy(t *testing.T) { 273 testDB.Flush() 274 testMDB := NewStandaloneServer() 275 srcKey := utils.RandString(10) 276 destKey := "from:" + srcKey 277 value := utils.RandString(10) 278 conn := new(connection.FakeConn) 279 280 testMDB.Exec(conn, utils.ToCmdLine("set", srcKey, value)) 281 282 // normal copy 283 result := testMDB.Exec(conn, utils.ToCmdLine("copy", srcKey, destKey)) 284 asserts.AssertIntReply(t, result, 1) 285 result = testMDB.Exec(conn, utils.ToCmdLine("get", destKey)) 286 asserts.AssertBulkReply(t, result, value) 287 288 // copy srcKey(DB 0) to destKey(DB 1) 289 testMDB.Exec(conn, utils.ToCmdLine("copy", srcKey, destKey, "db", "1")) 290 testMDB.Exec(conn, utils.ToCmdLine("select", "1")) 291 result = testMDB.Exec(conn, utils.ToCmdLine("get", destKey)) 292 asserts.AssertBulkReply(t, result, value) 293 294 // test destKey already exists 295 testMDB.Exec(conn, utils.ToCmdLine("select", "0")) 296 result = testMDB.Exec(conn, utils.ToCmdLine("copy", srcKey, destKey)) 297 asserts.AssertIntReply(t, result, 0) 298 299 // copy srcKey(DB 0) to destKey(DB 0) with "Replace" 300 value = "new:" + value 301 testMDB.Exec(conn, utils.ToCmdLine("set", srcKey, value)) // reset srcKey 302 result = testMDB.Exec(conn, utils.ToCmdLine("copy", srcKey, destKey, "replace")) 303 asserts.AssertIntReply(t, result, 1) 304 result = testMDB.Exec(conn, utils.ToCmdLine("get", destKey)) 305 asserts.AssertBulkReply(t, result, value) 306 307 // test copy expire time 308 testMDB.Exec(conn, utils.ToCmdLine("set", srcKey, value, "ex", "1000")) 309 result = testMDB.Exec(conn, utils.ToCmdLine("copy", srcKey, destKey, "replace")) 310 asserts.AssertIntReply(t, result, 1) 311 result = testMDB.Exec(conn, utils.ToCmdLine("ttl", srcKey)) 312 asserts.AssertIntReplyGreaterThan(t, result, 0) 313 result = testMDB.Exec(conn, utils.ToCmdLine("ttl", destKey)) 314 asserts.AssertIntReplyGreaterThan(t, result, 0) 315 }