keyspace.tcl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. start_server {tags {"keyspace"}} {
  2. test {DEL against a single item} {
  3. r set x foo
  4. assert {[r get x] eq "foo"}
  5. r del x
  6. r get x
  7. } {}
  8. test {Vararg DEL} {
  9. r set foo1{t} a
  10. r set foo2{t} b
  11. r set foo3{t} c
  12. list [r del foo1{t} foo2{t} foo3{t} foo4{t}] [r mget foo1{t} foo2{t} foo3{t}]
  13. } {3 {{} {} {}}}
  14. test {Untagged multi-key commands} {
  15. r mset foo1 a foo2 b foo3 c
  16. assert_equal {a b c {}} [r mget foo1 foo2 foo3 foo4]
  17. r del foo1 foo2 foo3 foo4
  18. } {3} {cluster:skip}
  19. test {KEYS with pattern} {
  20. foreach key {key_x key_y key_z foo_a foo_b foo_c} {
  21. r set $key hello
  22. }
  23. lsort [r keys foo*]
  24. } {foo_a foo_b foo_c}
  25. test {KEYS to get all keys} {
  26. lsort [r keys *]
  27. } {foo_a foo_b foo_c key_x key_y key_z}
  28. test {DBSIZE} {
  29. r dbsize
  30. } {6}
  31. test {DEL all keys} {
  32. foreach key [r keys *] {r del $key}
  33. r dbsize
  34. } {0}
  35. test "DEL against expired key" {
  36. r debug set-active-expire 0
  37. r setex keyExpire 1 valExpire
  38. after 1100
  39. assert_equal 0 [r del keyExpire]
  40. r debug set-active-expire 1
  41. } {OK} {needs:debug}
  42. test {EXISTS} {
  43. set res {}
  44. r set newkey test
  45. append res [r exists newkey]
  46. r del newkey
  47. append res [r exists newkey]
  48. } {10}
  49. test {Zero length value in key. SET/GET/EXISTS} {
  50. r set emptykey {}
  51. set res [r get emptykey]
  52. append res [r exists emptykey]
  53. r del emptykey
  54. append res [r exists emptykey]
  55. } {10}
  56. test {Commands pipelining} {
  57. set fd [r channel]
  58. puts -nonewline $fd "SET k1 xyzk\r\nGET k1\r\nPING\r\n"
  59. flush $fd
  60. set res {}
  61. append res [string match OK* [r read]]
  62. append res [r read]
  63. append res [string match PONG* [r read]]
  64. format $res
  65. } {1xyzk1}
  66. test {Non existing command} {
  67. catch {r foobaredcommand} err
  68. string match ERR* $err
  69. } {1}
  70. test {RENAME basic usage} {
  71. r set mykey{t} hello
  72. r rename mykey{t} mykey1{t}
  73. r rename mykey1{t} mykey2{t}
  74. r get mykey2{t}
  75. } {hello}
  76. test {RENAME source key should no longer exist} {
  77. r exists mykey
  78. } {0}
  79. test {RENAME against already existing key} {
  80. r set mykey{t} a
  81. r set mykey2{t} b
  82. r rename mykey2{t} mykey{t}
  83. set res [r get mykey{t}]
  84. append res [r exists mykey2{t}]
  85. } {b0}
  86. test {RENAMENX basic usage} {
  87. r del mykey{t}
  88. r del mykey2{t}
  89. r set mykey{t} foobar
  90. r renamenx mykey{t} mykey2{t}
  91. set res [r get mykey2{t}]
  92. append res [r exists mykey{t}]
  93. } {foobar0}
  94. test {RENAMENX against already existing key} {
  95. r set mykey{t} foo
  96. r set mykey2{t} bar
  97. r renamenx mykey{t} mykey2{t}
  98. } {0}
  99. test {RENAMENX against already existing key (2)} {
  100. set res [r get mykey{t}]
  101. append res [r get mykey2{t}]
  102. } {foobar}
  103. test {RENAME against non existing source key} {
  104. catch {r rename nokey{t} foobar{t}} err
  105. format $err
  106. } {ERR*}
  107. test {RENAME where source and dest key are the same (existing)} {
  108. r set mykey foo
  109. r rename mykey mykey
  110. } {OK}
  111. test {RENAMENX where source and dest key are the same (existing)} {
  112. r set mykey foo
  113. r renamenx mykey mykey
  114. } {0}
  115. test {RENAME where source and dest key are the same (non existing)} {
  116. r del mykey
  117. catch {r rename mykey mykey} err
  118. format $err
  119. } {ERR*}
  120. test {RENAME with volatile key, should move the TTL as well} {
  121. r del mykey{t} mykey2{t}
  122. r set mykey{t} foo
  123. r expire mykey{t} 100
  124. assert {[r ttl mykey{t}] > 95 && [r ttl mykey{t}] <= 100}
  125. r rename mykey{t} mykey2{t}
  126. assert {[r ttl mykey2{t}] > 95 && [r ttl mykey2{t}] <= 100}
  127. }
  128. test {RENAME with volatile key, should not inherit TTL of target key} {
  129. r del mykey{t} mykey2{t}
  130. r set mykey{t} foo
  131. r set mykey2{t} bar
  132. r expire mykey2{t} 100
  133. assert {[r ttl mykey{t}] == -1 && [r ttl mykey2{t}] > 0}
  134. r rename mykey{t} mykey2{t}
  135. r ttl mykey2{t}
  136. } {-1}
  137. test {DEL all keys again (DB 0)} {
  138. foreach key [r keys *] {
  139. r del $key
  140. }
  141. r dbsize
  142. } {0}
  143. test {DEL all keys again (DB 1)} {
  144. r select 10
  145. foreach key [r keys *] {
  146. r del $key
  147. }
  148. set res [r dbsize]
  149. r select 9
  150. format $res
  151. } {0} {singledb:skip}
  152. test {COPY basic usage for string} {
  153. r set mykey{t} foobar
  154. set res {}
  155. r copy mykey{t} mynewkey{t}
  156. lappend res [r get mynewkey{t}]
  157. lappend res [r dbsize]
  158. if {$::singledb} {
  159. assert_equal [list foobar 2] [format $res]
  160. } else {
  161. r copy mykey{t} mynewkey{t} DB 10
  162. r select 10
  163. lappend res [r get mynewkey{t}]
  164. lappend res [r dbsize]
  165. r select 9
  166. assert_equal [list foobar 2 foobar 1] [format $res]
  167. }
  168. }
  169. test {COPY for string does not replace an existing key without REPLACE option} {
  170. r set mykey2{t} hello
  171. catch {r copy mykey2{t} mynewkey{t} DB 10} e
  172. set e
  173. } {0} {singledb:skip}
  174. test {COPY for string can replace an existing key with REPLACE option} {
  175. r copy mykey2{t} mynewkey{t} DB 10 REPLACE
  176. r select 10
  177. r get mynewkey{t}
  178. } {hello} {singledb:skip}
  179. test {COPY for string ensures that copied data is independent of copying data} {
  180. r flushdb
  181. r select 9
  182. r set mykey{t} foobar
  183. set res {}
  184. r copy mykey{t} mynewkey{t} DB 10
  185. r select 10
  186. lappend res [r get mynewkey{t}]
  187. r set mynewkey{t} hoge
  188. lappend res [r get mynewkey{t}]
  189. r select 9
  190. lappend res [r get mykey{t}]
  191. r select 10
  192. r flushdb
  193. r select 9
  194. format $res
  195. } [list foobar hoge foobar] {singledb:skip}
  196. test {COPY for string does not copy data to no-integer DB} {
  197. r set mykey{t} foobar
  198. catch {r copy mykey{t} mynewkey{t} DB notanumber} e
  199. set e
  200. } {ERR value is not an integer or out of range}
  201. test {COPY can copy key expire metadata as well} {
  202. r set mykey{t} foobar ex 100
  203. r copy mykey{t} mynewkey{t} REPLACE
  204. assert {[r ttl mynewkey{t}] > 0 && [r ttl mynewkey{t}] <= 100}
  205. assert {[r get mynewkey{t}] eq "foobar"}
  206. }
  207. test {COPY does not create an expire if it does not exist} {
  208. r set mykey{t} foobar
  209. assert {[r ttl mykey{t}] == -1}
  210. r copy mykey{t} mynewkey{t} REPLACE
  211. assert {[r ttl mynewkey{t}] == -1}
  212. assert {[r get mynewkey{t}] eq "foobar"}
  213. }
  214. test {COPY basic usage for list} {
  215. r del mylist{t} mynewlist{t}
  216. r lpush mylist{t} a b c d
  217. r copy mylist{t} mynewlist{t}
  218. set digest [debug_digest_value mylist{t}]
  219. assert_equal $digest [debug_digest_value mynewlist{t}]
  220. assert_equal 1 [r object refcount mylist{t}]
  221. assert_equal 1 [r object refcount mynewlist{t}]
  222. r del mylist{t}
  223. assert_equal $digest [debug_digest_value mynewlist{t}]
  224. }
  225. test {COPY basic usage for intset set} {
  226. r del set1{t} newset1{t}
  227. r sadd set1{t} 1 2 3
  228. assert_encoding intset set1{t}
  229. r copy set1{t} newset1{t}
  230. set digest [debug_digest_value set1{t}]
  231. assert_equal $digest [debug_digest_value newset1{t}]
  232. assert_equal 1 [r object refcount set1{t}]
  233. assert_equal 1 [r object refcount newset1{t}]
  234. r del set1{t}
  235. assert_equal $digest [debug_digest_value newset1{t}]
  236. }
  237. test {COPY basic usage for hashtable set} {
  238. r del set2{t} newset2{t}
  239. r sadd set2{t} 1 2 3 a
  240. assert_encoding hashtable set2{t}
  241. r copy set2{t} newset2{t}
  242. set digest [debug_digest_value set2{t}]
  243. assert_equal $digest [debug_digest_value newset2{t}]
  244. assert_equal 1 [r object refcount set2{t}]
  245. assert_equal 1 [r object refcount newset2{t}]
  246. r del set2{t}
  247. assert_equal $digest [debug_digest_value newset2{t}]
  248. }
  249. test {COPY basic usage for listpack hash} {
  250. r del hash1{t} newhash1{t}
  251. r hset hash1{t} tmp 17179869184
  252. assert_encoding listpack hash1{t}
  253. r copy hash1{t} newhash1{t}
  254. set digest [debug_digest_value hash1{t}]
  255. assert_equal $digest [debug_digest_value newhash1{t}]
  256. assert_equal 1 [r object refcount hash1{t}]
  257. assert_equal 1 [r object refcount newhash1{t}]
  258. r del hash1{t}
  259. assert_equal $digest [debug_digest_value newhash1{t}]
  260. }
  261. test {MOVE basic usage} {
  262. r flushdb
  263. r set mykey foobar
  264. r move mykey 10
  265. set res {}
  266. lappend res [r exists mykey]
  267. lappend res [r dbsize]
  268. r select 10
  269. lappend res [r get mykey]
  270. lappend res [r dbsize]
  271. r select 9
  272. format $res
  273. } [list 0 0 foobar 1] {singledb:skip}
  274. test {MOVE against key existing in the target DB} {
  275. r set mykey hello
  276. r move mykey 10
  277. } {0} {singledb:skip}
  278. test {MOVE against non-integer DB (#1428)} {
  279. r set mykey hello
  280. catch {r move mykey notanumber} e
  281. set e
  282. } {ERR value is not an integer or out of range} {singledb:skip}
  283. test {MOVE can move key expire metadata as well} {
  284. r select 10
  285. r flushdb
  286. r select 9
  287. r set mykey foo ex 100
  288. r move mykey 10
  289. assert {[r ttl mykey] == -2}
  290. r select 10
  291. assert {[r ttl mykey] > 0 && [r ttl mykey] <= 100}
  292. assert {[r get mykey] eq "foo"}
  293. r select 9
  294. } {OK} {singledb:skip}
  295. test {MOVE does not create an expire if it does not exist} {
  296. r select 10
  297. r flushdb
  298. r select 9
  299. r set mykey foo
  300. r move mykey 10
  301. assert {[r ttl mykey] == -2}
  302. r select 10
  303. assert {[r ttl mykey] == -1}
  304. assert {[r get mykey] eq "foo"}
  305. r select 9
  306. } {OK} {singledb:skip}
  307. test {SET/GET keys in different DBs} {
  308. r set a hello
  309. r set b world
  310. r select 10
  311. r set a foo
  312. r set b bared
  313. r select 9
  314. set res {}
  315. lappend res [r get a]
  316. lappend res [r get b]
  317. r select 10
  318. lappend res [r get a]
  319. lappend res [r get b]
  320. r select 9
  321. format $res
  322. } {hello world foo bared} {singledb:skip}
  323. test {RANDOMKEY} {
  324. r flushdb
  325. r set foo x
  326. r set bar y
  327. set foo_seen 0
  328. set bar_seen 0
  329. for {set i 0} {$i < 100} {incr i} {
  330. set rkey [r randomkey]
  331. if {$rkey eq {foo}} {
  332. set foo_seen 1
  333. }
  334. if {$rkey eq {bar}} {
  335. set bar_seen 1
  336. }
  337. }
  338. list $foo_seen $bar_seen
  339. } {1 1}
  340. test {RANDOMKEY against empty DB} {
  341. r flushdb
  342. r randomkey
  343. } {}
  344. test {RANDOMKEY regression 1} {
  345. r flushdb
  346. r set x 10
  347. r del x
  348. r randomkey
  349. } {}
  350. test {KEYS * two times with long key, Github issue #1208} {
  351. r flushdb
  352. r set dlskeriewrioeuwqoirueioqwrueoqwrueqw test
  353. r keys *
  354. r keys *
  355. } {dlskeriewrioeuwqoirueioqwrueoqwrueqw}
  356. }