keyspace.tcl 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 a
  10. r set foo2 b
  11. r set foo3 c
  12. list [r del foo1 foo2 foo3 foo4] [r mget foo1 foo2 foo3]
  13. } {3 {{} {} {}}}
  14. test {KEYS with pattern} {
  15. foreach key {key_x key_y key_z foo_a foo_b foo_c} {
  16. r set $key hello
  17. }
  18. lsort [r keys foo*]
  19. } {foo_a foo_b foo_c}
  20. test {KEYS to get all keys} {
  21. lsort [r keys *]
  22. } {foo_a foo_b foo_c key_x key_y key_z}
  23. test {DBSIZE} {
  24. r dbsize
  25. } {6}
  26. test {DEL all keys} {
  27. foreach key [r keys *] {r del $key}
  28. r dbsize
  29. } {0}
  30. test "DEL against expired key" {
  31. r debug set-active-expire 0
  32. r setex keyExpire 1 valExpire
  33. after 1100
  34. assert_equal 0 [r del keyExpire]
  35. r debug set-active-expire 1
  36. }
  37. test {EXISTS} {
  38. set res {}
  39. r set newkey test
  40. append res [r exists newkey]
  41. r del newkey
  42. append res [r exists newkey]
  43. } {10}
  44. test {Zero length value in key. SET/GET/EXISTS} {
  45. r set emptykey {}
  46. set res [r get emptykey]
  47. append res [r exists emptykey]
  48. r del emptykey
  49. append res [r exists emptykey]
  50. } {10}
  51. test {Commands pipelining} {
  52. set fd [r channel]
  53. puts -nonewline $fd "SET k1 xyzk\r\nGET k1\r\nPING\r\n"
  54. flush $fd
  55. set res {}
  56. append res [string match OK* [r read]]
  57. append res [r read]
  58. append res [string match PONG* [r read]]
  59. format $res
  60. } {1xyzk1}
  61. test {Non existing command} {
  62. catch {r foobaredcommand} err
  63. string match ERR* $err
  64. } {1}
  65. test {RENAME basic usage} {
  66. r set mykey hello
  67. r rename mykey mykey1
  68. r rename mykey1 mykey2
  69. r get mykey2
  70. } {hello}
  71. test {RENAME source key should no longer exist} {
  72. r exists mykey
  73. } {0}
  74. test {RENAME against already existing key} {
  75. r set mykey a
  76. r set mykey2 b
  77. r rename mykey2 mykey
  78. set res [r get mykey]
  79. append res [r exists mykey2]
  80. } {b0}
  81. test {RENAMENX basic usage} {
  82. r del mykey
  83. r del mykey2
  84. r set mykey foobar
  85. r renamenx mykey mykey2
  86. set res [r get mykey2]
  87. append res [r exists mykey]
  88. } {foobar0}
  89. test {RENAMENX against already existing key} {
  90. r set mykey foo
  91. r set mykey2 bar
  92. r renamenx mykey mykey2
  93. } {0}
  94. test {RENAMENX against already existing key (2)} {
  95. set res [r get mykey]
  96. append res [r get mykey2]
  97. } {foobar}
  98. test {RENAME against non existing source key} {
  99. catch {r rename nokey foobar} err
  100. format $err
  101. } {ERR*}
  102. test {RENAME where source and dest key are the same (existing)} {
  103. r set mykey foo
  104. r rename mykey mykey
  105. } {OK}
  106. test {RENAMENX where source and dest key are the same (existing)} {
  107. r set mykey foo
  108. r renamenx mykey mykey
  109. } {0}
  110. test {RENAME where source and dest key are the same (non existing)} {
  111. r del mykey
  112. catch {r rename mykey mykey} err
  113. format $err
  114. } {ERR*}
  115. test {RENAME with volatile key, should move the TTL as well} {
  116. r del mykey mykey2
  117. r set mykey foo
  118. r expire mykey 100
  119. assert {[r ttl mykey] > 95 && [r ttl mykey] <= 100}
  120. r rename mykey mykey2
  121. assert {[r ttl mykey2] > 95 && [r ttl mykey2] <= 100}
  122. }
  123. test {RENAME with volatile key, should not inherit TTL of target key} {
  124. r del mykey mykey2
  125. r set mykey foo
  126. r set mykey2 bar
  127. r expire mykey2 100
  128. assert {[r ttl mykey] == -1 && [r ttl mykey2] > 0}
  129. r rename mykey mykey2
  130. r ttl mykey2
  131. } {-1}
  132. test {DEL all keys again (DB 0)} {
  133. foreach key [r keys *] {
  134. r del $key
  135. }
  136. r dbsize
  137. } {0}
  138. test {DEL all keys again (DB 1)} {
  139. r select 10
  140. foreach key [r keys *] {
  141. r del $key
  142. }
  143. set res [r dbsize]
  144. r select 9
  145. format $res
  146. } {0}
  147. test {MOVE basic usage} {
  148. r set mykey foobar
  149. r move mykey 10
  150. set res {}
  151. lappend res [r exists mykey]
  152. lappend res [r dbsize]
  153. r select 10
  154. lappend res [r get mykey]
  155. lappend res [r dbsize]
  156. r select 9
  157. format $res
  158. } [list 0 0 foobar 1]
  159. test {MOVE against key existing in the target DB} {
  160. r set mykey hello
  161. r move mykey 10
  162. } {0}
  163. test {MOVE against non-integer DB (#1428)} {
  164. r set mykey hello
  165. catch {r move mykey notanumber} e
  166. set e
  167. } {*ERR*index out of range}
  168. test {MOVE can move key expire metadata as well} {
  169. r select 10
  170. r flushdb
  171. r select 9
  172. r set mykey foo ex 100
  173. r move mykey 10
  174. assert {[r ttl mykey] == -2}
  175. r select 10
  176. assert {[r ttl mykey] > 0 && [r ttl mykey] <= 100}
  177. assert {[r get mykey] eq "foo"}
  178. r select 9
  179. }
  180. test {MOVE does not create an expire if it does not exist} {
  181. r select 10
  182. r flushdb
  183. r select 9
  184. r set mykey foo
  185. r move mykey 10
  186. assert {[r ttl mykey] == -2}
  187. r select 10
  188. assert {[r ttl mykey] == -1}
  189. assert {[r get mykey] eq "foo"}
  190. r select 9
  191. }
  192. test {SET/GET keys in different DBs} {
  193. r set a hello
  194. r set b world
  195. r select 10
  196. r set a foo
  197. r set b bared
  198. r select 9
  199. set res {}
  200. lappend res [r get a]
  201. lappend res [r get b]
  202. r select 10
  203. lappend res [r get a]
  204. lappend res [r get b]
  205. r select 9
  206. format $res
  207. } {hello world foo bared}
  208. test {RANDOMKEY} {
  209. r flushdb
  210. r set foo x
  211. r set bar y
  212. set foo_seen 0
  213. set bar_seen 0
  214. for {set i 0} {$i < 100} {incr i} {
  215. set rkey [r randomkey]
  216. if {$rkey eq {foo}} {
  217. set foo_seen 1
  218. }
  219. if {$rkey eq {bar}} {
  220. set bar_seen 1
  221. }
  222. }
  223. list $foo_seen $bar_seen
  224. } {1 1}
  225. test {RANDOMKEY against empty DB} {
  226. r flushdb
  227. r randomkey
  228. } {}
  229. test {RANDOMKEY regression 1} {
  230. r flushdb
  231. r set x 10
  232. r del x
  233. r randomkey
  234. } {}
  235. test {KEYS * two times with long key, Github issue #1208} {
  236. r flushdb
  237. r set dlskeriewrioeuwqoirueioqwrueoqwrueqw test
  238. r keys *
  239. r keys *
  240. } {dlskeriewrioeuwqoirueioqwrueoqwrueqw}
  241. }