zset.tcl 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230
  1. start_server {tags {"zset"}} {
  2. proc create_zset {key items} {
  3. r del $key
  4. foreach {score entry} $items {
  5. r zadd $key $score $entry
  6. }
  7. }
  8. proc basics {encoding} {
  9. if {$encoding == "ziplist"} {
  10. r config set zset-max-ziplist-entries 128
  11. r config set zset-max-ziplist-value 64
  12. } elseif {$encoding == "skiplist"} {
  13. r config set zset-max-ziplist-entries 0
  14. r config set zset-max-ziplist-value 0
  15. } else {
  16. puts "Unknown sorted set encoding"
  17. exit
  18. }
  19. test "Check encoding - $encoding" {
  20. r del ztmp
  21. r zadd ztmp 10 x
  22. assert_encoding $encoding ztmp
  23. }
  24. test "ZSET basic ZADD and score update - $encoding" {
  25. r del ztmp
  26. r zadd ztmp 10 x
  27. r zadd ztmp 20 y
  28. r zadd ztmp 30 z
  29. assert_equal {x y z} [r zrange ztmp 0 -1]
  30. r zadd ztmp 1 y
  31. assert_equal {y x z} [r zrange ztmp 0 -1]
  32. }
  33. test "ZSET element can't be set to NaN with ZADD - $encoding" {
  34. assert_error "*not*float*" {r zadd myzset nan abc}
  35. }
  36. test "ZSET element can't be set to NaN with ZINCRBY" {
  37. assert_error "*not*float*" {r zadd myzset nan abc}
  38. }
  39. test "ZADD with options syntax error with incomplete pair" {
  40. r del ztmp
  41. catch {r zadd ztmp xx 10 x 20} err
  42. set err
  43. } {ERR*}
  44. test "ZADD XX option without key - $encoding" {
  45. r del ztmp
  46. assert {[r zadd ztmp xx 10 x] == 0}
  47. assert {[r type ztmp] eq {none}}
  48. }
  49. test "ZADD XX existing key - $encoding" {
  50. r del ztmp
  51. r zadd ztmp 10 x
  52. assert {[r zadd ztmp xx 20 y] == 0}
  53. assert {[r zcard ztmp] == 1}
  54. }
  55. test "ZADD XX returns the number of elements actually added" {
  56. r del ztmp
  57. r zadd ztmp 10 x
  58. set retval [r zadd ztmp 10 x 20 y 30 z]
  59. assert {$retval == 2}
  60. }
  61. test "ZADD XX updates existing elements score" {
  62. r del ztmp
  63. r zadd ztmp 10 x 20 y 30 z
  64. r zadd ztmp xx 5 foo 11 x 21 y 40 zap
  65. assert {[r zcard ztmp] == 3}
  66. assert {[r zscore ztmp x] == 11}
  67. assert {[r zscore ztmp y] == 21}
  68. }
  69. test "ZADD XX and NX are not compatible" {
  70. r del ztmp
  71. catch {r zadd ztmp xx nx 10 x} err
  72. set err
  73. } {ERR*}
  74. test "ZADD NX with non existing key" {
  75. r del ztmp
  76. r zadd ztmp nx 10 x 20 y 30 z
  77. assert {[r zcard ztmp] == 3}
  78. }
  79. test "ZADD NX only add new elements without updating old ones" {
  80. r del ztmp
  81. r zadd ztmp 10 x 20 y 30 z
  82. assert {[r zadd ztmp nx 11 x 21 y 100 a 200 b] == 2}
  83. assert {[r zscore ztmp x] == 10}
  84. assert {[r zscore ztmp y] == 20}
  85. assert {[r zscore ztmp a] == 100}
  86. assert {[r zscore ztmp b] == 200}
  87. }
  88. test "ZADD INCR works like ZINCRBY" {
  89. r del ztmp
  90. r zadd ztmp 10 x 20 y 30 z
  91. r zadd ztmp INCR 15 x
  92. assert {[r zscore ztmp x] == 25}
  93. }
  94. test "ZADD INCR works with a single score-elemenet pair" {
  95. r del ztmp
  96. r zadd ztmp 10 x 20 y 30 z
  97. catch {r zadd ztmp INCR 15 x 10 y} err
  98. set err
  99. } {ERR*}
  100. test "ZADD CH option changes return value to all changed elements" {
  101. r del ztmp
  102. r zadd ztmp 10 x 20 y 30 z
  103. assert {[r zadd ztmp 11 x 21 y 30 z] == 0}
  104. assert {[r zadd ztmp ch 12 x 22 y 30 z] == 2}
  105. }
  106. test "ZINCRBY calls leading to NaN result in error" {
  107. r zincrby myzset +inf abc
  108. assert_error "*NaN*" {r zincrby myzset -inf abc}
  109. }
  110. test {ZADD - Variadic version base case} {
  111. r del myzset
  112. list [r zadd myzset 10 a 20 b 30 c] [r zrange myzset 0 -1 withscores]
  113. } {3 {a 10 b 20 c 30}}
  114. test {ZADD - Return value is the number of actually added items} {
  115. list [r zadd myzset 5 x 20 b 30 c] [r zrange myzset 0 -1 withscores]
  116. } {1 {x 5 a 10 b 20 c 30}}
  117. test {ZADD - Variadic version does not add nothing on single parsing err} {
  118. r del myzset
  119. catch {r zadd myzset 10 a 20 b 30.badscore c} e
  120. assert_match {*ERR*not*float*} $e
  121. r exists myzset
  122. } {0}
  123. test {ZADD - Variadic version will raise error on missing arg} {
  124. r del myzset
  125. catch {r zadd myzset 10 a 20 b 30 c 40} e
  126. assert_match {*ERR*syntax*} $e
  127. }
  128. test {ZINCRBY does not work variadic even if shares ZADD implementation} {
  129. r del myzset
  130. catch {r zincrby myzset 10 a 20 b 30 c} e
  131. assert_match {*ERR*wrong*number*arg*} $e
  132. }
  133. test "ZCARD basics - $encoding" {
  134. r del ztmp
  135. r zadd ztmp 10 a 20 b 30 c
  136. assert_equal 3 [r zcard ztmp]
  137. assert_equal 0 [r zcard zdoesntexist]
  138. }
  139. test "ZREM removes key after last element is removed" {
  140. r del ztmp
  141. r zadd ztmp 10 x
  142. r zadd ztmp 20 y
  143. assert_equal 1 [r exists ztmp]
  144. assert_equal 0 [r zrem ztmp z]
  145. assert_equal 1 [r zrem ztmp y]
  146. assert_equal 1 [r zrem ztmp x]
  147. assert_equal 0 [r exists ztmp]
  148. }
  149. test "ZREM variadic version" {
  150. r del ztmp
  151. r zadd ztmp 10 a 20 b 30 c
  152. assert_equal 2 [r zrem ztmp x y a b k]
  153. assert_equal 0 [r zrem ztmp foo bar]
  154. assert_equal 1 [r zrem ztmp c]
  155. r exists ztmp
  156. } {0}
  157. test "ZREM variadic version -- remove elements after key deletion" {
  158. r del ztmp
  159. r zadd ztmp 10 a 20 b 30 c
  160. r zrem ztmp a b c d e f g
  161. } {3}
  162. test "ZRANGE basics - $encoding" {
  163. r del ztmp
  164. r zadd ztmp 1 a
  165. r zadd ztmp 2 b
  166. r zadd ztmp 3 c
  167. r zadd ztmp 4 d
  168. assert_equal {a b c d} [r zrange ztmp 0 -1]
  169. assert_equal {a b c} [r zrange ztmp 0 -2]
  170. assert_equal {b c d} [r zrange ztmp 1 -1]
  171. assert_equal {b c} [r zrange ztmp 1 -2]
  172. assert_equal {c d} [r zrange ztmp -2 -1]
  173. assert_equal {c} [r zrange ztmp -2 -2]
  174. # out of range start index
  175. assert_equal {a b c} [r zrange ztmp -5 2]
  176. assert_equal {a b} [r zrange ztmp -5 1]
  177. assert_equal {} [r zrange ztmp 5 -1]
  178. assert_equal {} [r zrange ztmp 5 -2]
  179. # out of range end index
  180. assert_equal {a b c d} [r zrange ztmp 0 5]
  181. assert_equal {b c d} [r zrange ztmp 1 5]
  182. assert_equal {} [r zrange ztmp 0 -5]
  183. assert_equal {} [r zrange ztmp 1 -5]
  184. # withscores
  185. assert_equal {a 1 b 2 c 3 d 4} [r zrange ztmp 0 -1 withscores]
  186. }
  187. test "ZREVRANGE basics - $encoding" {
  188. r del ztmp
  189. r zadd ztmp 1 a
  190. r zadd ztmp 2 b
  191. r zadd ztmp 3 c
  192. r zadd ztmp 4 d
  193. assert_equal {d c b a} [r zrevrange ztmp 0 -1]
  194. assert_equal {d c b} [r zrevrange ztmp 0 -2]
  195. assert_equal {c b a} [r zrevrange ztmp 1 -1]
  196. assert_equal {c b} [r zrevrange ztmp 1 -2]
  197. assert_equal {b a} [r zrevrange ztmp -2 -1]
  198. assert_equal {b} [r zrevrange ztmp -2 -2]
  199. # out of range start index
  200. assert_equal {d c b} [r zrevrange ztmp -5 2]
  201. assert_equal {d c} [r zrevrange ztmp -5 1]
  202. assert_equal {} [r zrevrange ztmp 5 -1]
  203. assert_equal {} [r zrevrange ztmp 5 -2]
  204. # out of range end index
  205. assert_equal {d c b a} [r zrevrange ztmp 0 5]
  206. assert_equal {c b a} [r zrevrange ztmp 1 5]
  207. assert_equal {} [r zrevrange ztmp 0 -5]
  208. assert_equal {} [r zrevrange ztmp 1 -5]
  209. # withscores
  210. assert_equal {d 4 c 3 b 2 a 1} [r zrevrange ztmp 0 -1 withscores]
  211. }
  212. test "ZRANK/ZREVRANK basics - $encoding" {
  213. r del zranktmp
  214. r zadd zranktmp 10 x
  215. r zadd zranktmp 20 y
  216. r zadd zranktmp 30 z
  217. assert_equal 0 [r zrank zranktmp x]
  218. assert_equal 1 [r zrank zranktmp y]
  219. assert_equal 2 [r zrank zranktmp z]
  220. assert_equal "" [r zrank zranktmp foo]
  221. assert_equal 2 [r zrevrank zranktmp x]
  222. assert_equal 1 [r zrevrank zranktmp y]
  223. assert_equal 0 [r zrevrank zranktmp z]
  224. assert_equal "" [r zrevrank zranktmp foo]
  225. }
  226. test "ZRANK - after deletion - $encoding" {
  227. r zrem zranktmp y
  228. assert_equal 0 [r zrank zranktmp x]
  229. assert_equal 1 [r zrank zranktmp z]
  230. }
  231. test "ZINCRBY - can create a new sorted set - $encoding" {
  232. r del zset
  233. r zincrby zset 1 foo
  234. assert_equal {foo} [r zrange zset 0 -1]
  235. assert_equal 1 [r zscore zset foo]
  236. }
  237. test "ZINCRBY - increment and decrement - $encoding" {
  238. r zincrby zset 2 foo
  239. r zincrby zset 1 bar
  240. assert_equal {bar foo} [r zrange zset 0 -1]
  241. r zincrby zset 10 bar
  242. r zincrby zset -5 foo
  243. r zincrby zset -5 bar
  244. assert_equal {foo bar} [r zrange zset 0 -1]
  245. assert_equal -2 [r zscore zset foo]
  246. assert_equal 6 [r zscore zset bar]
  247. }
  248. test "ZINCRBY return value" {
  249. r del ztmp
  250. set retval [r zincrby ztmp 1.0 x]
  251. assert {$retval == 1.0}
  252. }
  253. proc create_default_zset {} {
  254. create_zset zset {-inf a 1 b 2 c 3 d 4 e 5 f +inf g}
  255. }
  256. test "ZRANGEBYSCORE/ZREVRANGEBYSCORE/ZCOUNT basics" {
  257. create_default_zset
  258. # inclusive range
  259. assert_equal {a b c} [r zrangebyscore zset -inf 2]
  260. assert_equal {b c d} [r zrangebyscore zset 0 3]
  261. assert_equal {d e f} [r zrangebyscore zset 3 6]
  262. assert_equal {e f g} [r zrangebyscore zset 4 +inf]
  263. assert_equal {c b a} [r zrevrangebyscore zset 2 -inf]
  264. assert_equal {d c b} [r zrevrangebyscore zset 3 0]
  265. assert_equal {f e d} [r zrevrangebyscore zset 6 3]
  266. assert_equal {g f e} [r zrevrangebyscore zset +inf 4]
  267. assert_equal 3 [r zcount zset 0 3]
  268. # exclusive range
  269. assert_equal {b} [r zrangebyscore zset (-inf (2]
  270. assert_equal {b c} [r zrangebyscore zset (0 (3]
  271. assert_equal {e f} [r zrangebyscore zset (3 (6]
  272. assert_equal {f} [r zrangebyscore zset (4 (+inf]
  273. assert_equal {b} [r zrevrangebyscore zset (2 (-inf]
  274. assert_equal {c b} [r zrevrangebyscore zset (3 (0]
  275. assert_equal {f e} [r zrevrangebyscore zset (6 (3]
  276. assert_equal {f} [r zrevrangebyscore zset (+inf (4]
  277. assert_equal 2 [r zcount zset (0 (3]
  278. # test empty ranges
  279. r zrem zset a
  280. r zrem zset g
  281. # inclusive
  282. assert_equal {} [r zrangebyscore zset 4 2]
  283. assert_equal {} [r zrangebyscore zset 6 +inf]
  284. assert_equal {} [r zrangebyscore zset -inf -6]
  285. assert_equal {} [r zrevrangebyscore zset +inf 6]
  286. assert_equal {} [r zrevrangebyscore zset -6 -inf]
  287. # exclusive
  288. assert_equal {} [r zrangebyscore zset (4 (2]
  289. assert_equal {} [r zrangebyscore zset 2 (2]
  290. assert_equal {} [r zrangebyscore zset (2 2]
  291. assert_equal {} [r zrangebyscore zset (6 (+inf]
  292. assert_equal {} [r zrangebyscore zset (-inf (-6]
  293. assert_equal {} [r zrevrangebyscore zset (+inf (6]
  294. assert_equal {} [r zrevrangebyscore zset (-6 (-inf]
  295. # empty inner range
  296. assert_equal {} [r zrangebyscore zset 2.4 2.6]
  297. assert_equal {} [r zrangebyscore zset (2.4 2.6]
  298. assert_equal {} [r zrangebyscore zset 2.4 (2.6]
  299. assert_equal {} [r zrangebyscore zset (2.4 (2.6]
  300. }
  301. test "ZRANGEBYSCORE with WITHSCORES" {
  302. create_default_zset
  303. assert_equal {b 1 c 2 d 3} [r zrangebyscore zset 0 3 withscores]
  304. assert_equal {d 3 c 2 b 1} [r zrevrangebyscore zset 3 0 withscores]
  305. }
  306. test "ZRANGEBYSCORE with LIMIT" {
  307. create_default_zset
  308. assert_equal {b c} [r zrangebyscore zset 0 10 LIMIT 0 2]
  309. assert_equal {d e f} [r zrangebyscore zset 0 10 LIMIT 2 3]
  310. assert_equal {d e f} [r zrangebyscore zset 0 10 LIMIT 2 10]
  311. assert_equal {} [r zrangebyscore zset 0 10 LIMIT 20 10]
  312. assert_equal {f e} [r zrevrangebyscore zset 10 0 LIMIT 0 2]
  313. assert_equal {d c b} [r zrevrangebyscore zset 10 0 LIMIT 2 3]
  314. assert_equal {d c b} [r zrevrangebyscore zset 10 0 LIMIT 2 10]
  315. assert_equal {} [r zrevrangebyscore zset 10 0 LIMIT 20 10]
  316. }
  317. test "ZRANGEBYSCORE with LIMIT and WITHSCORES" {
  318. create_default_zset
  319. assert_equal {e 4 f 5} [r zrangebyscore zset 2 5 LIMIT 2 3 WITHSCORES]
  320. assert_equal {d 3 c 2} [r zrevrangebyscore zset 5 2 LIMIT 2 3 WITHSCORES]
  321. }
  322. test "ZRANGEBYSCORE with non-value min or max" {
  323. assert_error "*not*float*" {r zrangebyscore fooz str 1}
  324. assert_error "*not*float*" {r zrangebyscore fooz 1 str}
  325. assert_error "*not*float*" {r zrangebyscore fooz 1 NaN}
  326. }
  327. proc create_default_lex_zset {} {
  328. create_zset zset {0 alpha 0 bar 0 cool 0 down
  329. 0 elephant 0 foo 0 great 0 hill
  330. 0 omega}
  331. }
  332. test "ZRANGEBYLEX/ZREVRANGEBYLEX/ZLEXCOUNT basics" {
  333. create_default_lex_zset
  334. # inclusive range
  335. assert_equal {alpha bar cool} [r zrangebylex zset - \[cool]
  336. assert_equal {bar cool down} [r zrangebylex zset \[bar \[down]
  337. assert_equal {great hill omega} [r zrangebylex zset \[g +]
  338. assert_equal {cool bar alpha} [r zrevrangebylex zset \[cool -]
  339. assert_equal {down cool bar} [r zrevrangebylex zset \[down \[bar]
  340. assert_equal {omega hill great foo elephant down} [r zrevrangebylex zset + \[d]
  341. assert_equal 3 [r zlexcount zset \[ele \[h]
  342. # exclusive range
  343. assert_equal {alpha bar} [r zrangebylex zset - (cool]
  344. assert_equal {cool} [r zrangebylex zset (bar (down]
  345. assert_equal {hill omega} [r zrangebylex zset (great +]
  346. assert_equal {bar alpha} [r zrevrangebylex zset (cool -]
  347. assert_equal {cool} [r zrevrangebylex zset (down (bar]
  348. assert_equal {omega hill} [r zrevrangebylex zset + (great]
  349. assert_equal 2 [r zlexcount zset (ele (great]
  350. # inclusive and exclusive
  351. assert_equal {} [r zrangebylex zset (az (b]
  352. assert_equal {} [r zrangebylex zset (z +]
  353. assert_equal {} [r zrangebylex zset - \[aaaa]
  354. assert_equal {} [r zrevrangebylex zset \[elez \[elex]
  355. assert_equal {} [r zrevrangebylex zset (hill (omega]
  356. }
  357. test "ZLEXCOUNT advanced" {
  358. create_default_lex_zset
  359. assert_equal 9 [r zlexcount zset - +]
  360. assert_equal 0 [r zlexcount zset + -]
  361. assert_equal 0 [r zlexcount zset + \[c]
  362. assert_equal 0 [r zlexcount zset \[c -]
  363. assert_equal 8 [r zlexcount zset \[bar +]
  364. assert_equal 5 [r zlexcount zset \[bar \[foo]
  365. assert_equal 4 [r zlexcount zset \[bar (foo]
  366. assert_equal 4 [r zlexcount zset (bar \[foo]
  367. assert_equal 3 [r zlexcount zset (bar (foo]
  368. assert_equal 5 [r zlexcount zset - (foo]
  369. assert_equal 1 [r zlexcount zset (maxstring +]
  370. }
  371. test "ZRANGEBYSLEX with LIMIT" {
  372. create_default_lex_zset
  373. assert_equal {alpha bar} [r zrangebylex zset - \[cool LIMIT 0 2]
  374. assert_equal {bar cool} [r zrangebylex zset - \[cool LIMIT 1 2]
  375. assert_equal {} [r zrangebylex zset \[bar \[down LIMIT 0 0]
  376. assert_equal {} [r zrangebylex zset \[bar \[down LIMIT 2 0]
  377. assert_equal {bar} [r zrangebylex zset \[bar \[down LIMIT 0 1]
  378. assert_equal {cool} [r zrangebylex zset \[bar \[down LIMIT 1 1]
  379. assert_equal {bar cool down} [r zrangebylex zset \[bar \[down LIMIT 0 100]
  380. assert_equal {omega hill great foo elephant} [r zrevrangebylex zset + \[d LIMIT 0 5]
  381. assert_equal {omega hill great foo} [r zrevrangebylex zset + \[d LIMIT 0 4]
  382. }
  383. test "ZRANGEBYLEX with invalid lex range specifiers" {
  384. assert_error "*not*string*" {r zrangebylex fooz foo bar}
  385. assert_error "*not*string*" {r zrangebylex fooz \[foo bar}
  386. assert_error "*not*string*" {r zrangebylex fooz foo \[bar}
  387. assert_error "*not*string*" {r zrangebylex fooz +x \[bar}
  388. assert_error "*not*string*" {r zrangebylex fooz -x \[bar}
  389. }
  390. test "ZREMRANGEBYSCORE basics" {
  391. proc remrangebyscore {min max} {
  392. create_zset zset {1 a 2 b 3 c 4 d 5 e}
  393. assert_equal 1 [r exists zset]
  394. r zremrangebyscore zset $min $max
  395. }
  396. # inner range
  397. assert_equal 3 [remrangebyscore 2 4]
  398. assert_equal {a e} [r zrange zset 0 -1]
  399. # start underflow
  400. assert_equal 1 [remrangebyscore -10 1]
  401. assert_equal {b c d e} [r zrange zset 0 -1]
  402. # end overflow
  403. assert_equal 1 [remrangebyscore 5 10]
  404. assert_equal {a b c d} [r zrange zset 0 -1]
  405. # switch min and max
  406. assert_equal 0 [remrangebyscore 4 2]
  407. assert_equal {a b c d e} [r zrange zset 0 -1]
  408. # -inf to mid
  409. assert_equal 3 [remrangebyscore -inf 3]
  410. assert_equal {d e} [r zrange zset 0 -1]
  411. # mid to +inf
  412. assert_equal 3 [remrangebyscore 3 +inf]
  413. assert_equal {a b} [r zrange zset 0 -1]
  414. # -inf to +inf
  415. assert_equal 5 [remrangebyscore -inf +inf]
  416. assert_equal {} [r zrange zset 0 -1]
  417. # exclusive min
  418. assert_equal 4 [remrangebyscore (1 5]
  419. assert_equal {a} [r zrange zset 0 -1]
  420. assert_equal 3 [remrangebyscore (2 5]
  421. assert_equal {a b} [r zrange zset 0 -1]
  422. # exclusive max
  423. assert_equal 4 [remrangebyscore 1 (5]
  424. assert_equal {e} [r zrange zset 0 -1]
  425. assert_equal 3 [remrangebyscore 1 (4]
  426. assert_equal {d e} [r zrange zset 0 -1]
  427. # exclusive min and max
  428. assert_equal 3 [remrangebyscore (1 (5]
  429. assert_equal {a e} [r zrange zset 0 -1]
  430. # destroy when empty
  431. assert_equal 5 [remrangebyscore 1 5]
  432. assert_equal 0 [r exists zset]
  433. }
  434. test "ZREMRANGEBYSCORE with non-value min or max" {
  435. assert_error "*not*float*" {r zremrangebyscore fooz str 1}
  436. assert_error "*not*float*" {r zremrangebyscore fooz 1 str}
  437. assert_error "*not*float*" {r zremrangebyscore fooz 1 NaN}
  438. }
  439. test "ZREMRANGEBYRANK basics" {
  440. proc remrangebyrank {min max} {
  441. create_zset zset {1 a 2 b 3 c 4 d 5 e}
  442. assert_equal 1 [r exists zset]
  443. r zremrangebyrank zset $min $max
  444. }
  445. # inner range
  446. assert_equal 3 [remrangebyrank 1 3]
  447. assert_equal {a e} [r zrange zset 0 -1]
  448. # start underflow
  449. assert_equal 1 [remrangebyrank -10 0]
  450. assert_equal {b c d e} [r zrange zset 0 -1]
  451. # start overflow
  452. assert_equal 0 [remrangebyrank 10 -1]
  453. assert_equal {a b c d e} [r zrange zset 0 -1]
  454. # end underflow
  455. assert_equal 0 [remrangebyrank 0 -10]
  456. assert_equal {a b c d e} [r zrange zset 0 -1]
  457. # end overflow
  458. assert_equal 5 [remrangebyrank 0 10]
  459. assert_equal {} [r zrange zset 0 -1]
  460. # destroy when empty
  461. assert_equal 5 [remrangebyrank 0 4]
  462. assert_equal 0 [r exists zset]
  463. }
  464. test "ZUNIONSTORE against non-existing key doesn't set destination - $encoding" {
  465. r del zseta
  466. assert_equal 0 [r zunionstore dst_key 1 zseta]
  467. assert_equal 0 [r exists dst_key]
  468. }
  469. test "ZUNIONSTORE with empty set - $encoding" {
  470. r del zseta zsetb
  471. r zadd zseta 1 a
  472. r zadd zseta 2 b
  473. r zunionstore zsetc 2 zseta zsetb
  474. r zrange zsetc 0 -1 withscores
  475. } {a 1 b 2}
  476. test "ZUNIONSTORE basics - $encoding" {
  477. r del zseta zsetb zsetc
  478. r zadd zseta 1 a
  479. r zadd zseta 2 b
  480. r zadd zseta 3 c
  481. r zadd zsetb 1 b
  482. r zadd zsetb 2 c
  483. r zadd zsetb 3 d
  484. assert_equal 4 [r zunionstore zsetc 2 zseta zsetb]
  485. assert_equal {a 1 b 3 d 3 c 5} [r zrange zsetc 0 -1 withscores]
  486. }
  487. test "ZUNIONSTORE with weights - $encoding" {
  488. assert_equal 4 [r zunionstore zsetc 2 zseta zsetb weights 2 3]
  489. assert_equal {a 2 b 7 d 9 c 12} [r zrange zsetc 0 -1 withscores]
  490. }
  491. test "ZUNIONSTORE with a regular set and weights - $encoding" {
  492. r del seta
  493. r sadd seta a
  494. r sadd seta b
  495. r sadd seta c
  496. assert_equal 4 [r zunionstore zsetc 2 seta zsetb weights 2 3]
  497. assert_equal {a 2 b 5 c 8 d 9} [r zrange zsetc 0 -1 withscores]
  498. }
  499. test "ZUNIONSTORE with AGGREGATE MIN - $encoding" {
  500. assert_equal 4 [r zunionstore zsetc 2 zseta zsetb aggregate min]
  501. assert_equal {a 1 b 1 c 2 d 3} [r zrange zsetc 0 -1 withscores]
  502. }
  503. test "ZUNIONSTORE with AGGREGATE MAX - $encoding" {
  504. assert_equal 4 [r zunionstore zsetc 2 zseta zsetb aggregate max]
  505. assert_equal {a 1 b 2 c 3 d 3} [r zrange zsetc 0 -1 withscores]
  506. }
  507. test "ZINTERSTORE basics - $encoding" {
  508. assert_equal 2 [r zinterstore zsetc 2 zseta zsetb]
  509. assert_equal {b 3 c 5} [r zrange zsetc 0 -1 withscores]
  510. }
  511. test "ZINTERSTORE with weights - $encoding" {
  512. assert_equal 2 [r zinterstore zsetc 2 zseta zsetb weights 2 3]
  513. assert_equal {b 7 c 12} [r zrange zsetc 0 -1 withscores]
  514. }
  515. test "ZINTERSTORE with a regular set and weights - $encoding" {
  516. r del seta
  517. r sadd seta a
  518. r sadd seta b
  519. r sadd seta c
  520. assert_equal 2 [r zinterstore zsetc 2 seta zsetb weights 2 3]
  521. assert_equal {b 5 c 8} [r zrange zsetc 0 -1 withscores]
  522. }
  523. test "ZINTERSTORE with AGGREGATE MIN - $encoding" {
  524. assert_equal 2 [r zinterstore zsetc 2 zseta zsetb aggregate min]
  525. assert_equal {b 1 c 2} [r zrange zsetc 0 -1 withscores]
  526. }
  527. test "ZINTERSTORE with AGGREGATE MAX - $encoding" {
  528. assert_equal 2 [r zinterstore zsetc 2 zseta zsetb aggregate max]
  529. assert_equal {b 2 c 3} [r zrange zsetc 0 -1 withscores]
  530. }
  531. foreach cmd {ZUNIONSTORE ZINTERSTORE} {
  532. test "$cmd with +inf/-inf scores - $encoding" {
  533. r del zsetinf1 zsetinf2
  534. r zadd zsetinf1 +inf key
  535. r zadd zsetinf2 +inf key
  536. r $cmd zsetinf3 2 zsetinf1 zsetinf2
  537. assert_equal inf [r zscore zsetinf3 key]
  538. r zadd zsetinf1 -inf key
  539. r zadd zsetinf2 +inf key
  540. r $cmd zsetinf3 2 zsetinf1 zsetinf2
  541. assert_equal 0 [r zscore zsetinf3 key]
  542. r zadd zsetinf1 +inf key
  543. r zadd zsetinf2 -inf key
  544. r $cmd zsetinf3 2 zsetinf1 zsetinf2
  545. assert_equal 0 [r zscore zsetinf3 key]
  546. r zadd zsetinf1 -inf key
  547. r zadd zsetinf2 -inf key
  548. r $cmd zsetinf3 2 zsetinf1 zsetinf2
  549. assert_equal -inf [r zscore zsetinf3 key]
  550. }
  551. test "$cmd with NaN weights $encoding" {
  552. r del zsetinf1 zsetinf2
  553. r zadd zsetinf1 1.0 key
  554. r zadd zsetinf2 1.0 key
  555. assert_error "*weight*not*float*" {
  556. r $cmd zsetinf3 2 zsetinf1 zsetinf2 weights nan nan
  557. }
  558. }
  559. }
  560. test "Basic ZPOP with a single key - $encoding" {
  561. r del zset
  562. assert_equal {} [r zpopmin zset]
  563. create_zset zset {-1 a 1 b 2 c 3 d 4 e}
  564. assert_equal {a -1} [r zpopmin zset]
  565. assert_equal {b 1} [r zpopmin zset]
  566. assert_equal {e 4} [r zpopmax zset]
  567. assert_equal {d 3} [r zpopmax zset]
  568. assert_equal {c 2} [r zpopmin zset]
  569. assert_equal 0 [r exists zset]
  570. r set foo bar
  571. assert_error "*WRONGTYPE*" {r zpopmin foo}
  572. }
  573. test "ZPOP with count - $encoding" {
  574. r del z1 z2 z3 foo
  575. r set foo bar
  576. assert_equal {} [r zpopmin z1 2]
  577. assert_error "*WRONGTYPE*" {r zpopmin foo 2}
  578. create_zset z1 {0 a 1 b 2 c 3 d}
  579. assert_equal {a 0 b 1} [r zpopmin z1 2]
  580. assert_equal {d 3 c 2} [r zpopmax z1 2]
  581. }
  582. test "BZPOP with a single existing sorted set - $encoding" {
  583. set rd [redis_deferring_client]
  584. create_zset zset {0 a 1 b 2 c}
  585. $rd bzpopmin zset 5
  586. assert_equal {zset a 0} [$rd read]
  587. $rd bzpopmin zset 5
  588. assert_equal {zset b 1} [$rd read]
  589. $rd bzpopmax zset 5
  590. assert_equal {zset c 2} [$rd read]
  591. assert_equal 0 [r exists zset]
  592. }
  593. test "BZPOP with multiple existing sorted sets - $encoding" {
  594. set rd [redis_deferring_client]
  595. create_zset z1 {0 a 1 b 2 c}
  596. create_zset z2 {3 d 4 e 5 f}
  597. $rd bzpopmin z1 z2 5
  598. assert_equal {z1 a 0} [$rd read]
  599. $rd bzpopmax z1 z2 5
  600. assert_equal {z1 c 2} [$rd read]
  601. assert_equal 1 [r zcard z1]
  602. assert_equal 3 [r zcard z2]
  603. $rd bzpopmax z2 z1 5
  604. assert_equal {z2 f 5} [$rd read]
  605. $rd bzpopmin z2 z1 5
  606. assert_equal {z2 d 3} [$rd read]
  607. assert_equal 1 [r zcard z1]
  608. assert_equal 1 [r zcard z2]
  609. }
  610. test "BZPOP second sorted set has members - $encoding" {
  611. set rd [redis_deferring_client]
  612. r del z1
  613. create_zset z2 {3 d 4 e 5 f}
  614. $rd bzpopmax z1 z2 5
  615. assert_equal {z2 f 5} [$rd read]
  616. $rd bzpopmin z2 z1 5
  617. assert_equal {z2 d 3} [$rd read]
  618. assert_equal 0 [r zcard z1]
  619. assert_equal 1 [r zcard z2]
  620. }
  621. }
  622. basics ziplist
  623. basics skiplist
  624. test {ZINTERSTORE regression with two sets, intset+hashtable} {
  625. r del seta setb setc
  626. r sadd set1 a
  627. r sadd set2 10
  628. r zinterstore set3 2 set1 set2
  629. } {0}
  630. test {ZUNIONSTORE regression, should not create NaN in scores} {
  631. r zadd z -inf neginf
  632. r zunionstore out 1 z weights 0
  633. r zrange out 0 -1 withscores
  634. } {neginf 0}
  635. test {ZINTERSTORE #516 regression, mixed sets and ziplist zsets} {
  636. r sadd one 100 101 102 103
  637. r sadd two 100 200 201 202
  638. r zadd three 1 500 1 501 1 502 1 503 1 100
  639. r zinterstore to_here 3 one two three WEIGHTS 0 0 1
  640. r zrange to_here 0 -1
  641. } {100}
  642. test {ZUNIONSTORE result is sorted} {
  643. # Create two sets with common and not common elements, perform
  644. # the UNION, check that elements are still sorted.
  645. r del one two dest
  646. set cmd1 [list r zadd one]
  647. set cmd2 [list r zadd two]
  648. for {set j 0} {$j < 1000} {incr j} {
  649. lappend cmd1 [expr rand()] [randomInt 1000]
  650. lappend cmd2 [expr rand()] [randomInt 1000]
  651. }
  652. {*}$cmd1
  653. {*}$cmd2
  654. assert {[r zcard one] > 100}
  655. assert {[r zcard two] > 100}
  656. r zunionstore dest 2 one two
  657. set oldscore 0
  658. foreach {ele score} [r zrange dest 0 -1 withscores] {
  659. assert {$score >= $oldscore}
  660. set oldscore $score
  661. }
  662. }
  663. test "ZSET commands don't accept the empty strings as valid score" {
  664. assert_error "*not*float*" {r zadd myzset "" abc}
  665. }
  666. proc stressers {encoding} {
  667. if {$encoding == "ziplist"} {
  668. # Little extra to allow proper fuzzing in the sorting stresser
  669. r config set zset-max-ziplist-entries 256
  670. r config set zset-max-ziplist-value 64
  671. set elements 128
  672. } elseif {$encoding == "skiplist"} {
  673. r config set zset-max-ziplist-entries 0
  674. r config set zset-max-ziplist-value 0
  675. if {$::accurate} {set elements 1000} else {set elements 100}
  676. } else {
  677. puts "Unknown sorted set encoding"
  678. exit
  679. }
  680. test "ZSCORE - $encoding" {
  681. r del zscoretest
  682. set aux {}
  683. for {set i 0} {$i < $elements} {incr i} {
  684. set score [expr rand()]
  685. lappend aux $score
  686. r zadd zscoretest $score $i
  687. }
  688. assert_encoding $encoding zscoretest
  689. for {set i 0} {$i < $elements} {incr i} {
  690. assert_equal [lindex $aux $i] [r zscore zscoretest $i]
  691. }
  692. }
  693. test "ZSCORE after a DEBUG RELOAD - $encoding" {
  694. r del zscoretest
  695. set aux {}
  696. for {set i 0} {$i < $elements} {incr i} {
  697. set score [expr rand()]
  698. lappend aux $score
  699. r zadd zscoretest $score $i
  700. }
  701. r debug reload
  702. assert_encoding $encoding zscoretest
  703. for {set i 0} {$i < $elements} {incr i} {
  704. assert_equal [lindex $aux $i] [r zscore zscoretest $i]
  705. }
  706. }
  707. test "ZSET sorting stresser - $encoding" {
  708. set delta 0
  709. for {set test 0} {$test < 2} {incr test} {
  710. unset -nocomplain auxarray
  711. array set auxarray {}
  712. set auxlist {}
  713. r del myzset
  714. for {set i 0} {$i < $elements} {incr i} {
  715. if {$test == 0} {
  716. set score [expr rand()]
  717. } else {
  718. set score [expr int(rand()*10)]
  719. }
  720. set auxarray($i) $score
  721. r zadd myzset $score $i
  722. # Random update
  723. if {[expr rand()] < .2} {
  724. set j [expr int(rand()*1000)]
  725. if {$test == 0} {
  726. set score [expr rand()]
  727. } else {
  728. set score [expr int(rand()*10)]
  729. }
  730. set auxarray($j) $score
  731. r zadd myzset $score $j
  732. }
  733. }
  734. foreach {item score} [array get auxarray] {
  735. lappend auxlist [list $score $item]
  736. }
  737. set sorted [lsort -command zlistAlikeSort $auxlist]
  738. set auxlist {}
  739. foreach x $sorted {
  740. lappend auxlist [lindex $x 1]
  741. }
  742. assert_encoding $encoding myzset
  743. set fromredis [r zrange myzset 0 -1]
  744. set delta 0
  745. for {set i 0} {$i < [llength $fromredis]} {incr i} {
  746. if {[lindex $fromredis $i] != [lindex $auxlist $i]} {
  747. incr delta
  748. }
  749. }
  750. }
  751. assert_equal 0 $delta
  752. }
  753. test "ZRANGEBYSCORE fuzzy test, 100 ranges in $elements element sorted set - $encoding" {
  754. set err {}
  755. r del zset
  756. for {set i 0} {$i < $elements} {incr i} {
  757. r zadd zset [expr rand()] $i
  758. }
  759. assert_encoding $encoding zset
  760. for {set i 0} {$i < 100} {incr i} {
  761. set min [expr rand()]
  762. set max [expr rand()]
  763. if {$min > $max} {
  764. set aux $min
  765. set min $max
  766. set max $aux
  767. }
  768. set low [r zrangebyscore zset -inf $min]
  769. set ok [r zrangebyscore zset $min $max]
  770. set high [r zrangebyscore zset $max +inf]
  771. set lowx [r zrangebyscore zset -inf ($min]
  772. set okx [r zrangebyscore zset ($min ($max]
  773. set highx [r zrangebyscore zset ($max +inf]
  774. if {[r zcount zset -inf $min] != [llength $low]} {
  775. append err "Error, len does not match zcount\n"
  776. }
  777. if {[r zcount zset $min $max] != [llength $ok]} {
  778. append err "Error, len does not match zcount\n"
  779. }
  780. if {[r zcount zset $max +inf] != [llength $high]} {
  781. append err "Error, len does not match zcount\n"
  782. }
  783. if {[r zcount zset -inf ($min] != [llength $lowx]} {
  784. append err "Error, len does not match zcount\n"
  785. }
  786. if {[r zcount zset ($min ($max] != [llength $okx]} {
  787. append err "Error, len does not match zcount\n"
  788. }
  789. if {[r zcount zset ($max +inf] != [llength $highx]} {
  790. append err "Error, len does not match zcount\n"
  791. }
  792. foreach x $low {
  793. set score [r zscore zset $x]
  794. if {$score > $min} {
  795. append err "Error, score for $x is $score > $min\n"
  796. }
  797. }
  798. foreach x $lowx {
  799. set score [r zscore zset $x]
  800. if {$score >= $min} {
  801. append err "Error, score for $x is $score >= $min\n"
  802. }
  803. }
  804. foreach x $ok {
  805. set score [r zscore zset $x]
  806. if {$score < $min || $score > $max} {
  807. append err "Error, score for $x is $score outside $min-$max range\n"
  808. }
  809. }
  810. foreach x $okx {
  811. set score [r zscore zset $x]
  812. if {$score <= $min || $score >= $max} {
  813. append err "Error, score for $x is $score outside $min-$max open range\n"
  814. }
  815. }
  816. foreach x $high {
  817. set score [r zscore zset $x]
  818. if {$score < $max} {
  819. append err "Error, score for $x is $score < $max\n"
  820. }
  821. }
  822. foreach x $highx {
  823. set score [r zscore zset $x]
  824. if {$score <= $max} {
  825. append err "Error, score for $x is $score <= $max\n"
  826. }
  827. }
  828. }
  829. assert_equal {} $err
  830. }
  831. test "ZRANGEBYLEX fuzzy test, 100 ranges in $elements element sorted set - $encoding" {
  832. set lexset {}
  833. r del zset
  834. for {set j 0} {$j < $elements} {incr j} {
  835. set e [randstring 0 30 alpha]
  836. lappend lexset $e
  837. r zadd zset 0 $e
  838. }
  839. set lexset [lsort -unique $lexset]
  840. for {set j 0} {$j < 100} {incr j} {
  841. set min [randstring 0 30 alpha]
  842. set max [randstring 0 30 alpha]
  843. set mininc [randomInt 2]
  844. set maxinc [randomInt 2]
  845. if {$mininc} {set cmin "\[$min"} else {set cmin "($min"}
  846. if {$maxinc} {set cmax "\[$max"} else {set cmax "($max"}
  847. set rev [randomInt 2]
  848. if {$rev} {
  849. set cmd zrevrangebylex
  850. } else {
  851. set cmd zrangebylex
  852. }
  853. # Make sure data is the same in both sides
  854. assert {[r zrange zset 0 -1] eq $lexset}
  855. # Get the Redis output
  856. set output [r $cmd zset $cmin $cmax]
  857. if {$rev} {
  858. set outlen [r zlexcount zset $cmax $cmin]
  859. } else {
  860. set outlen [r zlexcount zset $cmin $cmax]
  861. }
  862. # Compute the same output via Tcl
  863. set o {}
  864. set copy $lexset
  865. if {(!$rev && [string compare $min $max] > 0) ||
  866. ($rev && [string compare $max $min] > 0)} {
  867. # Empty output when ranges are inverted.
  868. } else {
  869. if {$rev} {
  870. # Invert the Tcl array using Redis itself.
  871. set copy [r zrevrange zset 0 -1]
  872. # Invert min / max as well
  873. lassign [list $min $max $mininc $maxinc] \
  874. max min maxinc mininc
  875. }
  876. foreach e $copy {
  877. set mincmp [string compare $e $min]
  878. set maxcmp [string compare $e $max]
  879. if {
  880. ($mininc && $mincmp >= 0 || !$mininc && $mincmp > 0)
  881. &&
  882. ($maxinc && $maxcmp <= 0 || !$maxinc && $maxcmp < 0)
  883. } {
  884. lappend o $e
  885. }
  886. }
  887. }
  888. assert {$o eq $output}
  889. assert {$outlen eq [llength $output]}
  890. }
  891. }
  892. test "ZREMRANGEBYLEX fuzzy test, 100 ranges in $elements element sorted set - $encoding" {
  893. set lexset {}
  894. r del zset zsetcopy
  895. for {set j 0} {$j < $elements} {incr j} {
  896. set e [randstring 0 30 alpha]
  897. lappend lexset $e
  898. r zadd zset 0 $e
  899. }
  900. set lexset [lsort -unique $lexset]
  901. for {set j 0} {$j < 100} {incr j} {
  902. # Copy...
  903. r zunionstore zsetcopy 1 zset
  904. set lexsetcopy $lexset
  905. set min [randstring 0 30 alpha]
  906. set max [randstring 0 30 alpha]
  907. set mininc [randomInt 2]
  908. set maxinc [randomInt 2]
  909. if {$mininc} {set cmin "\[$min"} else {set cmin "($min"}
  910. if {$maxinc} {set cmax "\[$max"} else {set cmax "($max"}
  911. # Make sure data is the same in both sides
  912. assert {[r zrange zset 0 -1] eq $lexset}
  913. # Get the range we are going to remove
  914. set torem [r zrangebylex zset $cmin $cmax]
  915. set toremlen [r zlexcount zset $cmin $cmax]
  916. r zremrangebylex zsetcopy $cmin $cmax
  917. set output [r zrange zsetcopy 0 -1]
  918. # Remove the range with Tcl from the original list
  919. if {$toremlen} {
  920. set first [lsearch -exact $lexsetcopy [lindex $torem 0]]
  921. set last [expr {$first+$toremlen-1}]
  922. set lexsetcopy [lreplace $lexsetcopy $first $last]
  923. }
  924. assert {$lexsetcopy eq $output}
  925. }
  926. }
  927. test "ZSETs skiplist implementation backlink consistency test - $encoding" {
  928. set diff 0
  929. for {set j 0} {$j < $elements} {incr j} {
  930. r zadd myzset [expr rand()] "Element-$j"
  931. r zrem myzset "Element-[expr int(rand()*$elements)]"
  932. }
  933. assert_encoding $encoding myzset
  934. set l1 [r zrange myzset 0 -1]
  935. set l2 [r zrevrange myzset 0 -1]
  936. for {set j 0} {$j < [llength $l1]} {incr j} {
  937. if {[lindex $l1 $j] ne [lindex $l2 end-$j]} {
  938. incr diff
  939. }
  940. }
  941. assert_equal 0 $diff
  942. }
  943. test "ZSETs ZRANK augmented skip list stress testing - $encoding" {
  944. set err {}
  945. r del myzset
  946. for {set k 0} {$k < 2000} {incr k} {
  947. set i [expr {$k % $elements}]
  948. if {[expr rand()] < .2} {
  949. r zrem myzset $i
  950. } else {
  951. set score [expr rand()]
  952. r zadd myzset $score $i
  953. assert_encoding $encoding myzset
  954. }
  955. set card [r zcard myzset]
  956. if {$card > 0} {
  957. set index [randomInt $card]
  958. set ele [lindex [r zrange myzset $index $index] 0]
  959. set rank [r zrank myzset $ele]
  960. if {$rank != $index} {
  961. set err "$ele RANK is wrong! ($rank != $index)"
  962. break
  963. }
  964. }
  965. }
  966. assert_equal {} $err
  967. }
  968. test "BZPOPMIN, ZADD + DEL should not awake blocked client" {
  969. set rd [redis_deferring_client]
  970. r del zset
  971. $rd bzpopmin zset 0
  972. r multi
  973. r zadd zset 0 foo
  974. r del zset
  975. r exec
  976. r del zset
  977. r zadd zset 1 bar
  978. $rd read
  979. } {zset bar 1}
  980. test "BZPOPMIN, ZADD + DEL + SET should not awake blocked client" {
  981. set rd [redis_deferring_client]
  982. r del list
  983. r del zset
  984. $rd bzpopmin zset 0
  985. r multi
  986. r zadd zset 0 foo
  987. r del zset
  988. r set zset foo
  989. r exec
  990. r del zset
  991. r zadd zset 1 bar
  992. $rd read
  993. } {zset bar 1}
  994. test "BZPOPMIN with same key multiple times should work" {
  995. set rd [redis_deferring_client]
  996. r del z1 z2
  997. # Data arriving after the BZPOPMIN.
  998. $rd bzpopmin z1 z2 z2 z1 0
  999. r zadd z1 0 a
  1000. assert_equal [$rd read] {z1 a 0}
  1001. $rd bzpopmin z1 z2 z2 z1 0
  1002. r zadd z2 1 b
  1003. assert_equal [$rd read] {z2 b 1}
  1004. # Data already there.
  1005. r zadd z1 0 a
  1006. r zadd z2 1 b
  1007. $rd bzpopmin z1 z2 z2 z1 0
  1008. assert_equal [$rd read] {z1 a 0}
  1009. $rd bzpopmin z1 z2 z2 z1 0
  1010. assert_equal [$rd read] {z2 b 1}
  1011. }
  1012. test "MULTI/EXEC is isolated from the point of view of BZPOPMIN" {
  1013. set rd [redis_deferring_client]
  1014. r del zset
  1015. $rd bzpopmin zset 0
  1016. r multi
  1017. r zadd zset 0 a
  1018. r zadd zset 1 b
  1019. r zadd zset 2 c
  1020. r exec
  1021. $rd read
  1022. } {zset a 0}
  1023. test "BZPOPMIN with variadic ZADD" {
  1024. set rd [redis_deferring_client]
  1025. r del zset
  1026. if {$::valgrind} {after 100}
  1027. $rd bzpopmin zset 0
  1028. if {$::valgrind} {after 100}
  1029. assert_equal 2 [r zadd zset -1 foo 1 bar]
  1030. if {$::valgrind} {after 100}
  1031. assert_equal {zset foo -1} [$rd read]
  1032. assert_equal {bar} [r zrange zset 0 -1]
  1033. }
  1034. test "BZPOPMIN with zero timeout should block indefinitely" {
  1035. set rd [redis_deferring_client]
  1036. r del zset
  1037. $rd bzpopmin zset 0
  1038. after 1000
  1039. r zadd zset 0 foo
  1040. assert_equal {zset foo 0} [$rd read]
  1041. }
  1042. }
  1043. tags {"slow"} {
  1044. stressers ziplist
  1045. stressers skiplist
  1046. }
  1047. test {ZSET skiplist order consistency when elements are moved} {
  1048. set original_max [lindex [r config get zset-max-ziplist-entries] 1]
  1049. r config set zset-max-ziplist-entries 0
  1050. for {set times 0} {$times < 10} {incr times} {
  1051. r del zset
  1052. for {set j 0} {$j < 1000} {incr j} {
  1053. r zadd zset [randomInt 50] ele-[randomInt 10]
  1054. }
  1055. # Make sure that element ordering is correct
  1056. set prev_element {}
  1057. set prev_score -1
  1058. foreach {element score} [r zrange zset 0 -1 WITHSCORES] {
  1059. # Assert that elements are in increasing ordering
  1060. assert {
  1061. $prev_score < $score ||
  1062. ($prev_score == $score &&
  1063. [string compare $prev_element $element] == -1)
  1064. }
  1065. set prev_element $element
  1066. set prev_score $score
  1067. }
  1068. }
  1069. r config set zset-max-ziplist-entries $original_max
  1070. }
  1071. }