zset.tcl 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214
  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/ZCOUNT 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 "ZRANGEBYSLEX with LIMIT" {
  358. create_default_lex_zset
  359. assert_equal {alpha bar} [r zrangebylex zset - \[cool LIMIT 0 2]
  360. assert_equal {bar cool} [r zrangebylex zset - \[cool LIMIT 1 2]
  361. assert_equal {} [r zrangebylex zset \[bar \[down LIMIT 0 0]
  362. assert_equal {} [r zrangebylex zset \[bar \[down LIMIT 2 0]
  363. assert_equal {bar} [r zrangebylex zset \[bar \[down LIMIT 0 1]
  364. assert_equal {cool} [r zrangebylex zset \[bar \[down LIMIT 1 1]
  365. assert_equal {bar cool down} [r zrangebylex zset \[bar \[down LIMIT 0 100]
  366. assert_equal {omega hill great foo elephant} [r zrevrangebylex zset + \[d LIMIT 0 5]
  367. assert_equal {omega hill great foo} [r zrevrangebylex zset + \[d LIMIT 0 4]
  368. }
  369. test "ZRANGEBYLEX with invalid lex range specifiers" {
  370. assert_error "*not*string*" {r zrangebylex fooz foo bar}
  371. assert_error "*not*string*" {r zrangebylex fooz \[foo bar}
  372. assert_error "*not*string*" {r zrangebylex fooz foo \[bar}
  373. assert_error "*not*string*" {r zrangebylex fooz +x \[bar}
  374. assert_error "*not*string*" {r zrangebylex fooz -x \[bar}
  375. }
  376. test "ZREMRANGEBYSCORE basics" {
  377. proc remrangebyscore {min max} {
  378. create_zset zset {1 a 2 b 3 c 4 d 5 e}
  379. assert_equal 1 [r exists zset]
  380. r zremrangebyscore zset $min $max
  381. }
  382. # inner range
  383. assert_equal 3 [remrangebyscore 2 4]
  384. assert_equal {a e} [r zrange zset 0 -1]
  385. # start underflow
  386. assert_equal 1 [remrangebyscore -10 1]
  387. assert_equal {b c d e} [r zrange zset 0 -1]
  388. # end overflow
  389. assert_equal 1 [remrangebyscore 5 10]
  390. assert_equal {a b c d} [r zrange zset 0 -1]
  391. # switch min and max
  392. assert_equal 0 [remrangebyscore 4 2]
  393. assert_equal {a b c d e} [r zrange zset 0 -1]
  394. # -inf to mid
  395. assert_equal 3 [remrangebyscore -inf 3]
  396. assert_equal {d e} [r zrange zset 0 -1]
  397. # mid to +inf
  398. assert_equal 3 [remrangebyscore 3 +inf]
  399. assert_equal {a b} [r zrange zset 0 -1]
  400. # -inf to +inf
  401. assert_equal 5 [remrangebyscore -inf +inf]
  402. assert_equal {} [r zrange zset 0 -1]
  403. # exclusive min
  404. assert_equal 4 [remrangebyscore (1 5]
  405. assert_equal {a} [r zrange zset 0 -1]
  406. assert_equal 3 [remrangebyscore (2 5]
  407. assert_equal {a b} [r zrange zset 0 -1]
  408. # exclusive max
  409. assert_equal 4 [remrangebyscore 1 (5]
  410. assert_equal {e} [r zrange zset 0 -1]
  411. assert_equal 3 [remrangebyscore 1 (4]
  412. assert_equal {d e} [r zrange zset 0 -1]
  413. # exclusive min and max
  414. assert_equal 3 [remrangebyscore (1 (5]
  415. assert_equal {a e} [r zrange zset 0 -1]
  416. # destroy when empty
  417. assert_equal 5 [remrangebyscore 1 5]
  418. assert_equal 0 [r exists zset]
  419. }
  420. test "ZREMRANGEBYSCORE with non-value min or max" {
  421. assert_error "*not*float*" {r zremrangebyscore fooz str 1}
  422. assert_error "*not*float*" {r zremrangebyscore fooz 1 str}
  423. assert_error "*not*float*" {r zremrangebyscore fooz 1 NaN}
  424. }
  425. test "ZREMRANGEBYRANK basics" {
  426. proc remrangebyrank {min max} {
  427. create_zset zset {1 a 2 b 3 c 4 d 5 e}
  428. assert_equal 1 [r exists zset]
  429. r zremrangebyrank zset $min $max
  430. }
  431. # inner range
  432. assert_equal 3 [remrangebyrank 1 3]
  433. assert_equal {a e} [r zrange zset 0 -1]
  434. # start underflow
  435. assert_equal 1 [remrangebyrank -10 0]
  436. assert_equal {b c d e} [r zrange zset 0 -1]
  437. # start overflow
  438. assert_equal 0 [remrangebyrank 10 -1]
  439. assert_equal {a b c d e} [r zrange zset 0 -1]
  440. # end underflow
  441. assert_equal 0 [remrangebyrank 0 -10]
  442. assert_equal {a b c d e} [r zrange zset 0 -1]
  443. # end overflow
  444. assert_equal 5 [remrangebyrank 0 10]
  445. assert_equal {} [r zrange zset 0 -1]
  446. # destroy when empty
  447. assert_equal 5 [remrangebyrank 0 4]
  448. assert_equal 0 [r exists zset]
  449. }
  450. test "ZUNIONSTORE against non-existing key doesn't set destination - $encoding" {
  451. r del zseta
  452. assert_equal 0 [r zunionstore dst_key 1 zseta]
  453. assert_equal 0 [r exists dst_key]
  454. }
  455. test "ZUNIONSTORE with empty set - $encoding" {
  456. r del zseta zsetb
  457. r zadd zseta 1 a
  458. r zadd zseta 2 b
  459. r zunionstore zsetc 2 zseta zsetb
  460. r zrange zsetc 0 -1 withscores
  461. } {a 1 b 2}
  462. test "ZUNIONSTORE basics - $encoding" {
  463. r del zseta zsetb zsetc
  464. r zadd zseta 1 a
  465. r zadd zseta 2 b
  466. r zadd zseta 3 c
  467. r zadd zsetb 1 b
  468. r zadd zsetb 2 c
  469. r zadd zsetb 3 d
  470. assert_equal 4 [r zunionstore zsetc 2 zseta zsetb]
  471. assert_equal {a 1 b 3 d 3 c 5} [r zrange zsetc 0 -1 withscores]
  472. }
  473. test "ZUNIONSTORE with weights - $encoding" {
  474. assert_equal 4 [r zunionstore zsetc 2 zseta zsetb weights 2 3]
  475. assert_equal {a 2 b 7 d 9 c 12} [r zrange zsetc 0 -1 withscores]
  476. }
  477. test "ZUNIONSTORE with a regular set and weights - $encoding" {
  478. r del seta
  479. r sadd seta a
  480. r sadd seta b
  481. r sadd seta c
  482. assert_equal 4 [r zunionstore zsetc 2 seta zsetb weights 2 3]
  483. assert_equal {a 2 b 5 c 8 d 9} [r zrange zsetc 0 -1 withscores]
  484. }
  485. test "ZUNIONSTORE with AGGREGATE MIN - $encoding" {
  486. assert_equal 4 [r zunionstore zsetc 2 zseta zsetb aggregate min]
  487. assert_equal {a 1 b 1 c 2 d 3} [r zrange zsetc 0 -1 withscores]
  488. }
  489. test "ZUNIONSTORE with AGGREGATE MAX - $encoding" {
  490. assert_equal 4 [r zunionstore zsetc 2 zseta zsetb aggregate max]
  491. assert_equal {a 1 b 2 c 3 d 3} [r zrange zsetc 0 -1 withscores]
  492. }
  493. test "ZINTERSTORE basics - $encoding" {
  494. assert_equal 2 [r zinterstore zsetc 2 zseta zsetb]
  495. assert_equal {b 3 c 5} [r zrange zsetc 0 -1 withscores]
  496. }
  497. test "ZINTERSTORE with weights - $encoding" {
  498. assert_equal 2 [r zinterstore zsetc 2 zseta zsetb weights 2 3]
  499. assert_equal {b 7 c 12} [r zrange zsetc 0 -1 withscores]
  500. }
  501. test "ZINTERSTORE with a regular set and weights - $encoding" {
  502. r del seta
  503. r sadd seta a
  504. r sadd seta b
  505. r sadd seta c
  506. assert_equal 2 [r zinterstore zsetc 2 seta zsetb weights 2 3]
  507. assert_equal {b 5 c 8} [r zrange zsetc 0 -1 withscores]
  508. }
  509. test "ZINTERSTORE with AGGREGATE MIN - $encoding" {
  510. assert_equal 2 [r zinterstore zsetc 2 zseta zsetb aggregate min]
  511. assert_equal {b 1 c 2} [r zrange zsetc 0 -1 withscores]
  512. }
  513. test "ZINTERSTORE with AGGREGATE MAX - $encoding" {
  514. assert_equal 2 [r zinterstore zsetc 2 zseta zsetb aggregate max]
  515. assert_equal {b 2 c 3} [r zrange zsetc 0 -1 withscores]
  516. }
  517. foreach cmd {ZUNIONSTORE ZINTERSTORE} {
  518. test "$cmd with +inf/-inf scores - $encoding" {
  519. r del zsetinf1 zsetinf2
  520. r zadd zsetinf1 +inf key
  521. r zadd zsetinf2 +inf key
  522. r $cmd zsetinf3 2 zsetinf1 zsetinf2
  523. assert_equal inf [r zscore zsetinf3 key]
  524. r zadd zsetinf1 -inf key
  525. r zadd zsetinf2 +inf key
  526. r $cmd zsetinf3 2 zsetinf1 zsetinf2
  527. assert_equal 0 [r zscore zsetinf3 key]
  528. r zadd zsetinf1 +inf key
  529. r zadd zsetinf2 -inf key
  530. r $cmd zsetinf3 2 zsetinf1 zsetinf2
  531. assert_equal 0 [r zscore zsetinf3 key]
  532. r zadd zsetinf1 -inf key
  533. r zadd zsetinf2 -inf key
  534. r $cmd zsetinf3 2 zsetinf1 zsetinf2
  535. assert_equal -inf [r zscore zsetinf3 key]
  536. }
  537. test "$cmd with NaN weights $encoding" {
  538. r del zsetinf1 zsetinf2
  539. r zadd zsetinf1 1.0 key
  540. r zadd zsetinf2 1.0 key
  541. assert_error "*weight*not*float*" {
  542. r $cmd zsetinf3 2 zsetinf1 zsetinf2 weights nan nan
  543. }
  544. }
  545. }
  546. test "Basic ZPOP with a single key - $encoding" {
  547. r del zset
  548. assert_equal {} [r zpopmin zset]
  549. create_zset zset {-1 a 1 b 2 c 3 d 4 e}
  550. assert_equal {a -1} [r zpopmin zset]
  551. assert_equal {b 1} [r zpopmin zset]
  552. assert_equal {e 4} [r zpopmax zset]
  553. assert_equal {d 3} [r zpopmax zset]
  554. assert_equal {c 2} [r zpopmin zset]
  555. assert_equal 0 [r exists zset]
  556. r set foo bar
  557. assert_error "*WRONGTYPE*" {r zpopmin foo}
  558. }
  559. test "ZPOP with count - $encoding" {
  560. r del z1 z2 z3 foo
  561. r set foo bar
  562. assert_equal {} [r zpopmin z1 2]
  563. assert_error "*WRONGTYPE*" {r zpopmin foo 2}
  564. create_zset z1 {0 a 1 b 2 c 3 d}
  565. assert_equal {a 0 b 1} [r zpopmin z1 2]
  566. assert_equal {d 3 c 2} [r zpopmax z1 2]
  567. }
  568. test "BZPOP with a single existing sorted set - $encoding" {
  569. set rd [redis_deferring_client]
  570. create_zset zset {0 a 1 b 2 c}
  571. $rd bzpopmin zset 5
  572. assert_equal {zset a 0} [$rd read]
  573. $rd bzpopmin zset 5
  574. assert_equal {zset b 1} [$rd read]
  575. $rd bzpopmax zset 5
  576. assert_equal {zset c 2} [$rd read]
  577. assert_equal 0 [r exists zset]
  578. }
  579. test "BZPOP with multiple existing sorted sets - $encoding" {
  580. set rd [redis_deferring_client]
  581. create_zset z1 {0 a 1 b 2 c}
  582. create_zset z2 {3 d 4 e 5 f}
  583. $rd bzpopmin z1 z2 5
  584. assert_equal {z1 a 0} [$rd read]
  585. $rd bzpopmax z1 z2 5
  586. assert_equal {z1 c 2} [$rd read]
  587. assert_equal 1 [r zcard z1]
  588. assert_equal 3 [r zcard z2]
  589. $rd bzpopmax z2 z1 5
  590. assert_equal {z2 f 5} [$rd read]
  591. $rd bzpopmin z2 z1 5
  592. assert_equal {z2 d 3} [$rd read]
  593. assert_equal 1 [r zcard z1]
  594. assert_equal 1 [r zcard z2]
  595. }
  596. test "BZPOP second sorted set has members - $encoding" {
  597. set rd [redis_deferring_client]
  598. r del z1
  599. create_zset z2 {3 d 4 e 5 f}
  600. $rd bzpopmax z1 z2 5
  601. assert_equal {z2 f 5} [$rd read]
  602. $rd bzpopmin z2 z1 5
  603. assert_equal {z2 d 3} [$rd read]
  604. assert_equal 0 [r zcard z1]
  605. assert_equal 1 [r zcard z2]
  606. }
  607. }
  608. basics ziplist
  609. basics skiplist
  610. test {ZINTERSTORE regression with two sets, intset+hashtable} {
  611. r del seta setb setc
  612. r sadd set1 a
  613. r sadd set2 10
  614. r zinterstore set3 2 set1 set2
  615. } {0}
  616. test {ZUNIONSTORE regression, should not create NaN in scores} {
  617. r zadd z -inf neginf
  618. r zunionstore out 1 z weights 0
  619. r zrange out 0 -1 withscores
  620. } {neginf 0}
  621. test {ZINTERSTORE #516 regression, mixed sets and ziplist zsets} {
  622. r sadd one 100 101 102 103
  623. r sadd two 100 200 201 202
  624. r zadd three 1 500 1 501 1 502 1 503 1 100
  625. r zinterstore to_here 3 one two three WEIGHTS 0 0 1
  626. r zrange to_here 0 -1
  627. } {100}
  628. test {ZUNIONSTORE result is sorted} {
  629. # Create two sets with common and not common elements, perform
  630. # the UNION, check that elements are still sorted.
  631. r del one two dest
  632. set cmd1 [list r zadd one]
  633. set cmd2 [list r zadd two]
  634. for {set j 0} {$j < 1000} {incr j} {
  635. lappend cmd1 [expr rand()] [randomInt 1000]
  636. lappend cmd2 [expr rand()] [randomInt 1000]
  637. }
  638. {*}$cmd1
  639. {*}$cmd2
  640. assert {[r zcard one] > 100}
  641. assert {[r zcard two] > 100}
  642. r zunionstore dest 2 one two
  643. set oldscore 0
  644. foreach {ele score} [r zrange dest 0 -1 withscores] {
  645. assert {$score >= $oldscore}
  646. set oldscore $score
  647. }
  648. }
  649. test "ZSET commands don't accept the empty strings as valid score" {
  650. assert_error "*not*float*" {r zadd myzset "" abc}
  651. }
  652. proc stressers {encoding} {
  653. if {$encoding == "ziplist"} {
  654. # Little extra to allow proper fuzzing in the sorting stresser
  655. r config set zset-max-ziplist-entries 256
  656. r config set zset-max-ziplist-value 64
  657. set elements 128
  658. } elseif {$encoding == "skiplist"} {
  659. r config set zset-max-ziplist-entries 0
  660. r config set zset-max-ziplist-value 0
  661. if {$::accurate} {set elements 1000} else {set elements 100}
  662. } else {
  663. puts "Unknown sorted set encoding"
  664. exit
  665. }
  666. test "ZSCORE - $encoding" {
  667. r del zscoretest
  668. set aux {}
  669. for {set i 0} {$i < $elements} {incr i} {
  670. set score [expr rand()]
  671. lappend aux $score
  672. r zadd zscoretest $score $i
  673. }
  674. assert_encoding $encoding zscoretest
  675. for {set i 0} {$i < $elements} {incr i} {
  676. assert_equal [lindex $aux $i] [r zscore zscoretest $i]
  677. }
  678. }
  679. test "ZSCORE after a DEBUG RELOAD - $encoding" {
  680. r del zscoretest
  681. set aux {}
  682. for {set i 0} {$i < $elements} {incr i} {
  683. set score [expr rand()]
  684. lappend aux $score
  685. r zadd zscoretest $score $i
  686. }
  687. r debug reload
  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 "ZSET sorting stresser - $encoding" {
  694. set delta 0
  695. for {set test 0} {$test < 2} {incr test} {
  696. unset -nocomplain auxarray
  697. array set auxarray {}
  698. set auxlist {}
  699. r del myzset
  700. for {set i 0} {$i < $elements} {incr i} {
  701. if {$test == 0} {
  702. set score [expr rand()]
  703. } else {
  704. set score [expr int(rand()*10)]
  705. }
  706. set auxarray($i) $score
  707. r zadd myzset $score $i
  708. # Random update
  709. if {[expr rand()] < .2} {
  710. set j [expr int(rand()*1000)]
  711. if {$test == 0} {
  712. set score [expr rand()]
  713. } else {
  714. set score [expr int(rand()*10)]
  715. }
  716. set auxarray($j) $score
  717. r zadd myzset $score $j
  718. }
  719. }
  720. foreach {item score} [array get auxarray] {
  721. lappend auxlist [list $score $item]
  722. }
  723. set sorted [lsort -command zlistAlikeSort $auxlist]
  724. set auxlist {}
  725. foreach x $sorted {
  726. lappend auxlist [lindex $x 1]
  727. }
  728. assert_encoding $encoding myzset
  729. set fromredis [r zrange myzset 0 -1]
  730. set delta 0
  731. for {set i 0} {$i < [llength $fromredis]} {incr i} {
  732. if {[lindex $fromredis $i] != [lindex $auxlist $i]} {
  733. incr delta
  734. }
  735. }
  736. }
  737. assert_equal 0 $delta
  738. }
  739. test "ZRANGEBYSCORE fuzzy test, 100 ranges in $elements element sorted set - $encoding" {
  740. set err {}
  741. r del zset
  742. for {set i 0} {$i < $elements} {incr i} {
  743. r zadd zset [expr rand()] $i
  744. }
  745. assert_encoding $encoding zset
  746. for {set i 0} {$i < 100} {incr i} {
  747. set min [expr rand()]
  748. set max [expr rand()]
  749. if {$min > $max} {
  750. set aux $min
  751. set min $max
  752. set max $aux
  753. }
  754. set low [r zrangebyscore zset -inf $min]
  755. set ok [r zrangebyscore zset $min $max]
  756. set high [r zrangebyscore zset $max +inf]
  757. set lowx [r zrangebyscore zset -inf ($min]
  758. set okx [r zrangebyscore zset ($min ($max]
  759. set highx [r zrangebyscore zset ($max +inf]
  760. if {[r zcount zset -inf $min] != [llength $low]} {
  761. append err "Error, len does not match zcount\n"
  762. }
  763. if {[r zcount zset $min $max] != [llength $ok]} {
  764. append err "Error, len does not match zcount\n"
  765. }
  766. if {[r zcount zset $max +inf] != [llength $high]} {
  767. append err "Error, len does not match zcount\n"
  768. }
  769. if {[r zcount zset -inf ($min] != [llength $lowx]} {
  770. append err "Error, len does not match zcount\n"
  771. }
  772. if {[r zcount zset ($min ($max] != [llength $okx]} {
  773. append err "Error, len does not match zcount\n"
  774. }
  775. if {[r zcount zset ($max +inf] != [llength $highx]} {
  776. append err "Error, len does not match zcount\n"
  777. }
  778. foreach x $low {
  779. set score [r zscore zset $x]
  780. if {$score > $min} {
  781. append err "Error, score for $x is $score > $min\n"
  782. }
  783. }
  784. foreach x $lowx {
  785. set score [r zscore zset $x]
  786. if {$score >= $min} {
  787. append err "Error, score for $x is $score >= $min\n"
  788. }
  789. }
  790. foreach x $ok {
  791. set score [r zscore zset $x]
  792. if {$score < $min || $score > $max} {
  793. append err "Error, score for $x is $score outside $min-$max range\n"
  794. }
  795. }
  796. foreach x $okx {
  797. set score [r zscore zset $x]
  798. if {$score <= $min || $score >= $max} {
  799. append err "Error, score for $x is $score outside $min-$max open range\n"
  800. }
  801. }
  802. foreach x $high {
  803. set score [r zscore zset $x]
  804. if {$score < $max} {
  805. append err "Error, score for $x is $score < $max\n"
  806. }
  807. }
  808. foreach x $highx {
  809. set score [r zscore zset $x]
  810. if {$score <= $max} {
  811. append err "Error, score for $x is $score <= $max\n"
  812. }
  813. }
  814. }
  815. assert_equal {} $err
  816. }
  817. test "ZRANGEBYLEX fuzzy test, 100 ranges in $elements element sorted set - $encoding" {
  818. set lexset {}
  819. r del zset
  820. for {set j 0} {$j < $elements} {incr j} {
  821. set e [randstring 0 30 alpha]
  822. lappend lexset $e
  823. r zadd zset 0 $e
  824. }
  825. set lexset [lsort -unique $lexset]
  826. for {set j 0} {$j < 100} {incr j} {
  827. set min [randstring 0 30 alpha]
  828. set max [randstring 0 30 alpha]
  829. set mininc [randomInt 2]
  830. set maxinc [randomInt 2]
  831. if {$mininc} {set cmin "\[$min"} else {set cmin "($min"}
  832. if {$maxinc} {set cmax "\[$max"} else {set cmax "($max"}
  833. set rev [randomInt 2]
  834. if {$rev} {
  835. set cmd zrevrangebylex
  836. } else {
  837. set cmd zrangebylex
  838. }
  839. # Make sure data is the same in both sides
  840. assert {[r zrange zset 0 -1] eq $lexset}
  841. # Get the Redis output
  842. set output [r $cmd zset $cmin $cmax]
  843. if {$rev} {
  844. set outlen [r zlexcount zset $cmax $cmin]
  845. } else {
  846. set outlen [r zlexcount zset $cmin $cmax]
  847. }
  848. # Compute the same output via Tcl
  849. set o {}
  850. set copy $lexset
  851. if {(!$rev && [string compare $min $max] > 0) ||
  852. ($rev && [string compare $max $min] > 0)} {
  853. # Empty output when ranges are inverted.
  854. } else {
  855. if {$rev} {
  856. # Invert the Tcl array using Redis itself.
  857. set copy [r zrevrange zset 0 -1]
  858. # Invert min / max as well
  859. lassign [list $min $max $mininc $maxinc] \
  860. max min maxinc mininc
  861. }
  862. foreach e $copy {
  863. set mincmp [string compare $e $min]
  864. set maxcmp [string compare $e $max]
  865. if {
  866. ($mininc && $mincmp >= 0 || !$mininc && $mincmp > 0)
  867. &&
  868. ($maxinc && $maxcmp <= 0 || !$maxinc && $maxcmp < 0)
  869. } {
  870. lappend o $e
  871. }
  872. }
  873. }
  874. assert {$o eq $output}
  875. assert {$outlen eq [llength $output]}
  876. }
  877. }
  878. test "ZREMRANGEBYLEX fuzzy test, 100 ranges in $elements element sorted set - $encoding" {
  879. set lexset {}
  880. r del zset zsetcopy
  881. for {set j 0} {$j < $elements} {incr j} {
  882. set e [randstring 0 30 alpha]
  883. lappend lexset $e
  884. r zadd zset 0 $e
  885. }
  886. set lexset [lsort -unique $lexset]
  887. for {set j 0} {$j < 100} {incr j} {
  888. # Copy...
  889. r zunionstore zsetcopy 1 zset
  890. set lexsetcopy $lexset
  891. set min [randstring 0 30 alpha]
  892. set max [randstring 0 30 alpha]
  893. set mininc [randomInt 2]
  894. set maxinc [randomInt 2]
  895. if {$mininc} {set cmin "\[$min"} else {set cmin "($min"}
  896. if {$maxinc} {set cmax "\[$max"} else {set cmax "($max"}
  897. # Make sure data is the same in both sides
  898. assert {[r zrange zset 0 -1] eq $lexset}
  899. # Get the range we are going to remove
  900. set torem [r zrangebylex zset $cmin $cmax]
  901. set toremlen [r zlexcount zset $cmin $cmax]
  902. r zremrangebylex zsetcopy $cmin $cmax
  903. set output [r zrange zsetcopy 0 -1]
  904. # Remove the range with Tcl from the original list
  905. if {$toremlen} {
  906. set first [lsearch -exact $lexsetcopy [lindex $torem 0]]
  907. set last [expr {$first+$toremlen-1}]
  908. set lexsetcopy [lreplace $lexsetcopy $first $last]
  909. }
  910. assert {$lexsetcopy eq $output}
  911. }
  912. }
  913. test "ZSETs skiplist implementation backlink consistency test - $encoding" {
  914. set diff 0
  915. for {set j 0} {$j < $elements} {incr j} {
  916. r zadd myzset [expr rand()] "Element-$j"
  917. r zrem myzset "Element-[expr int(rand()*$elements)]"
  918. }
  919. assert_encoding $encoding myzset
  920. set l1 [r zrange myzset 0 -1]
  921. set l2 [r zrevrange myzset 0 -1]
  922. for {set j 0} {$j < [llength $l1]} {incr j} {
  923. if {[lindex $l1 $j] ne [lindex $l2 end-$j]} {
  924. incr diff
  925. }
  926. }
  927. assert_equal 0 $diff
  928. }
  929. test "ZSETs ZRANK augmented skip list stress testing - $encoding" {
  930. set err {}
  931. r del myzset
  932. for {set k 0} {$k < 2000} {incr k} {
  933. set i [expr {$k % $elements}]
  934. if {[expr rand()] < .2} {
  935. r zrem myzset $i
  936. } else {
  937. set score [expr rand()]
  938. r zadd myzset $score $i
  939. assert_encoding $encoding myzset
  940. }
  941. set card [r zcard myzset]
  942. if {$card > 0} {
  943. set index [randomInt $card]
  944. set ele [lindex [r zrange myzset $index $index] 0]
  945. set rank [r zrank myzset $ele]
  946. if {$rank != $index} {
  947. set err "$ele RANK is wrong! ($rank != $index)"
  948. break
  949. }
  950. }
  951. }
  952. assert_equal {} $err
  953. }
  954. test "BZPOPMIN, ZADD + DEL should not awake blocked client" {
  955. set rd [redis_deferring_client]
  956. r del zset
  957. $rd bzpopmin zset 0
  958. r multi
  959. r zadd zset 0 foo
  960. r del zset
  961. r exec
  962. r del zset
  963. r zadd zset 1 bar
  964. $rd read
  965. } {zset bar 1}
  966. test "BZPOPMIN, ZADD + DEL + SET should not awake blocked client" {
  967. set rd [redis_deferring_client]
  968. r del list
  969. r del zset
  970. $rd bzpopmin zset 0
  971. r multi
  972. r zadd zset 0 foo
  973. r del zset
  974. r set zset foo
  975. r exec
  976. r del zset
  977. r zadd zset 1 bar
  978. $rd read
  979. } {zset bar 1}
  980. test "BZPOPMIN with same key multiple times should work" {
  981. set rd [redis_deferring_client]
  982. r del z1 z2
  983. # Data arriving after the BZPOPMIN.
  984. $rd bzpopmin z1 z2 z2 z1 0
  985. r zadd z1 0 a
  986. assert_equal [$rd read] {z1 a 0}
  987. $rd bzpopmin z1 z2 z2 z1 0
  988. r zadd z2 1 b
  989. assert_equal [$rd read] {z2 b 1}
  990. # Data already there.
  991. r zadd z1 0 a
  992. r zadd z2 1 b
  993. $rd bzpopmin z1 z2 z2 z1 0
  994. assert_equal [$rd read] {z1 a 0}
  995. $rd bzpopmin z1 z2 z2 z1 0
  996. assert_equal [$rd read] {z2 b 1}
  997. }
  998. test "MULTI/EXEC is isolated from the point of view of BZPOPMIN" {
  999. set rd [redis_deferring_client]
  1000. r del zset
  1001. $rd bzpopmin zset 0
  1002. r multi
  1003. r zadd zset 0 a
  1004. r zadd zset 1 b
  1005. r zadd zset 2 c
  1006. r exec
  1007. $rd read
  1008. } {zset a 0}
  1009. test "BZPOPMIN with variadic ZADD" {
  1010. set rd [redis_deferring_client]
  1011. r del zset
  1012. if {$::valgrind} {after 100}
  1013. $rd bzpopmin zset 0
  1014. if {$::valgrind} {after 100}
  1015. assert_equal 2 [r zadd zset -1 foo 1 bar]
  1016. if {$::valgrind} {after 100}
  1017. assert_equal {zset foo -1} [$rd read]
  1018. assert_equal {bar} [r zrange zset 0 -1]
  1019. }
  1020. test "BZPOPMIN with zero timeout should block indefinitely" {
  1021. set rd [redis_deferring_client]
  1022. r del zset
  1023. $rd bzpopmin zset 0
  1024. after 1000
  1025. r zadd zset 0 foo
  1026. assert_equal {zset foo 0} [$rd read]
  1027. }
  1028. }
  1029. tags {"slow"} {
  1030. stressers ziplist
  1031. stressers skiplist
  1032. }
  1033. test {ZSET skiplist order consistency when elements are moved} {
  1034. set original_max [lindex [r config get zset-max-ziplist-entries] 1]
  1035. r config set zset-max-ziplist-entries 0
  1036. for {set times 0} {$times < 10} {incr times} {
  1037. r del zset
  1038. for {set j 0} {$j < 1000} {incr j} {
  1039. r zadd zset [randomInt 50] ele-[randomInt 10]
  1040. }
  1041. # Make sure that element ordering is correct
  1042. set prev_element {}
  1043. set prev_score -1
  1044. foreach {element score} [r zrange zset 0 -1 WITHSCORES] {
  1045. # Assert that elements are in increasing ordering
  1046. assert {
  1047. $prev_score < $score ||
  1048. ($prev_score == $score &&
  1049. [string compare $prev_element $element] == -1)
  1050. }
  1051. set prev_element $element
  1052. set prev_score $score
  1053. }
  1054. }
  1055. r config set zset-max-ziplist-entries $original_max
  1056. }
  1057. }