4 "list-max-ziplist-value" 16
5 "list-max-ziplist-entries" 256
8 source "tests/unit/type/list-common.tcl"
10 test
{LPUSH
, RPUSH
, LLENGTH
, LINDEX
- ziplist
} {
11 # first lpush then rpush
12 assert_equal
1 [r lpush myziplist1 a
]
13 assert_equal
2 [r rpush myziplist1 b
]
14 assert_equal
3 [r rpush myziplist1 c
]
15 assert_equal
3 [r llen myziplist1
]
16 assert_equal a
[r
lindex myziplist1
0]
17 assert_equal b
[r
lindex myziplist1
1]
18 assert_equal c
[r
lindex myziplist1
2]
19 assert_encoding ziplist myziplist1
21 # first rpush then lpush
22 assert_equal
1 [r rpush myziplist2 a
]
23 assert_equal
2 [r lpush myziplist2 b
]
24 assert_equal
3 [r lpush myziplist2 c
]
25 assert_equal
3 [r llen myziplist2
]
26 assert_equal c
[r
lindex myziplist2
0]
27 assert_equal b
[r
lindex myziplist2
1]
28 assert_equal a
[r
lindex myziplist2
2]
29 assert_encoding ziplist myziplist2
32 test
{LPUSH
, RPUSH
, LLENGTH
, LINDEX
- regular
list} {
33 # first lpush then rpush
34 assert_equal
1 [r lpush mylist1
$largevalue(linkedlist
)]
35 assert_encoding linkedlist mylist1
36 assert_equal
2 [r rpush mylist1 b
]
37 assert_equal
3 [r rpush mylist1 c
]
38 assert_equal
3 [r llen mylist1
]
39 assert_equal
$largevalue(linkedlist
) [r
lindex mylist1
0]
40 assert_equal b
[r
lindex mylist1
1]
41 assert_equal c
[r
lindex mylist1
2]
43 # first rpush then lpush
44 assert_equal
1 [r rpush mylist2
$largevalue(linkedlist
)]
45 assert_encoding linkedlist mylist2
46 assert_equal
2 [r lpush mylist2 b
]
47 assert_equal
3 [r lpush mylist2 c
]
48 assert_equal
3 [r llen mylist2
]
49 assert_equal c
[r
lindex mylist2
0]
50 assert_equal b
[r
lindex mylist2
1]
51 assert_equal
$largevalue(linkedlist
) [r
lindex mylist2
2]
54 test
{Variadic RPUSH
/LPUSH
} {
56 assert_equal
4 [r lpush mylist a b c d
]
57 assert_equal
8 [r rpush mylist
0 1 2 3]
58 assert_equal
{d c b a
0 1 2 3} [r
lrange mylist
0 -1]
61 test
{DEL a
list - ziplist
} {
62 assert_equal
1 [r del myziplist2
]
63 assert_equal
0 [r exists myziplist2
]
64 assert_equal
0 [r llen myziplist2
]
67 test
{DEL a
list - regular
list} {
68 assert_equal
1 [r del mylist2
]
69 assert_equal
0 [r exists mylist2
]
70 assert_equal
0 [r llen mylist2
]
73 proc create_ziplist
{key entries
} {
75 foreach entry $entries { r rpush
$key $entry }
76 assert_encoding ziplist
$key
79 proc create_linkedlist
{key entries
} {
81 foreach entry $entries { r rpush
$key $entry }
82 assert_encoding linkedlist
$key
85 foreach {type large
} [array get largevalue
] {
86 test
"BLPOP, BRPOP: single existing list - $type" {
87 set rd
[redis_deferring_client
]
88 create_
$type blist
"a b $large c d"
91 assert_equal
{blist a
} [$rd read]
93 assert_equal
{blist d
} [$rd read]
96 assert_equal
{blist b
} [$rd read]
98 assert_equal
{blist c
} [$rd read]
101 test
"BLPOP, BRPOP: multiple existing lists - $type" {
102 set rd
[redis_deferring_client
]
103 create_
$type blist1
"a $large c"
104 create_
$type blist2
"d $large f"
106 $rd blpop blist1 blist2
1
107 assert_equal
{blist1 a
} [$rd read]
108 $rd brpop blist1 blist2
1
109 assert_equal
{blist1 c
} [$rd read]
110 assert_equal
1 [r llen blist1
]
111 assert_equal
3 [r llen blist2
]
113 $rd blpop blist2 blist1
1
114 assert_equal
{blist2 d
} [$rd read]
115 $rd brpop blist2 blist1
1
116 assert_equal
{blist2 f
} [$rd read]
117 assert_equal
1 [r llen blist1
]
118 assert_equal
1 [r llen blist2
]
121 test
"BLPOP, BRPOP: second list has an entry - $type" {
122 set rd
[redis_deferring_client
]
124 create_
$type blist2
"d $large f"
126 $rd blpop blist1 blist2
1
127 assert_equal
{blist2 d
} [$rd read]
128 $rd brpop blist1 blist2
1
129 assert_equal
{blist2 f
} [$rd read]
130 assert_equal
0 [r llen blist1
]
131 assert_equal
1 [r llen blist2
]
134 test
"BRPOPLPUSH - $type" {
137 set rd
[redis_deferring_client
]
138 create_
$type blist
"a b $large c d"
140 $rd brpoplpush blist target
1
141 assert_equal d
[$rd read]
143 assert_equal d
[r rpop target
]
144 assert_equal
"a b $large c" [r
lrange blist
0 -1]
148 test
"BLPOP with variadic LPUSH" {
149 set rd
[redis_deferring_client
]
152 assert_equal
2 [r lpush blist foo bar
]
153 assert_equal
{blist foo
} [$rd read]
154 assert_equal bar
[lindex [r
lrange blist
0 -1] 0]
157 test
"BRPOPLPUSH with zero timeout should block indefinitely" {
158 set rd
[redis_deferring_client
]
160 $rd brpoplpush blist target
0
163 assert_equal foo
[$rd read]
164 assert_equal
{foo
} [r
lrange target
0 -1]
167 test
"BRPOPLPUSH with a client BLPOPing the target list" {
168 set rd
[redis_deferring_client
]
169 set rd2
[redis_deferring_client
]
172 $rd brpoplpush blist target
0
175 assert_equal foo
[$rd read]
176 assert_equal
{target foo
} [$rd2 read]
177 assert_equal
0 [r exists target
]
180 test
"BRPOPLPUSH with wrong source type" {
181 set rd
[redis_deferring_client
]
184 $rd brpoplpush blist target
1
185 assert_error
"ERR*wrong kind*" {$rd read}
188 test
"BRPOPLPUSH with wrong destination type" {
189 set rd
[redis_deferring_client
]
193 $rd brpoplpush blist target
1
194 assert_error
"ERR*wrong kind*" {$rd read}
196 set rd
[redis_deferring_client
]
199 $rd brpoplpush blist target
0
202 assert_error
"ERR*wrong kind*" {$rd read}
203 assert_equal
{foo
} [r
lrange blist
0 -1]
206 test
"BRPOPLPUSH with multiple blocked clients" {
207 set rd1
[redis_deferring_client
]
208 set rd2
[redis_deferring_client
]
209 r del blist target1 target2
211 $rd1 brpoplpush blist target1
0
212 $rd2 brpoplpush blist target2
0
215 assert_error
"ERR*wrong kind*" {$rd1 read}
216 assert_equal
{foo
} [$rd2 read]
217 assert_equal
{foo
} [r
lrange target2
0 -1]
220 test
"Linked BRPOPLPUSH" {
221 set rd1
[redis_deferring_client
]
222 set rd2
[redis_deferring_client
]
224 r del list1 list2 list3
226 $rd1 brpoplpush list1 list2
0
227 $rd2 brpoplpush list2 list3
0
231 assert_equal
{} [r
lrange list1
0 -1]
232 assert_equal
{} [r
lrange list2
0 -1]
233 assert_equal
{foo
} [r
lrange list3
0 -1]
236 test
"Circular BRPOPLPUSH" {
237 set rd1
[redis_deferring_client
]
238 set rd2
[redis_deferring_client
]
242 $rd1 brpoplpush list1 list2
0
243 $rd2 brpoplpush list2 list1
0
247 assert_equal
{foo
} [r
lrange list1
0 -1]
248 assert_equal
{} [r
lrange list2
0 -1]
251 test
"Self-referential BRPOPLPUSH" {
252 set rd
[redis_deferring_client
]
256 $rd brpoplpush blist blist
0
260 assert_equal
{foo
} [r
lrange blist
0 -1]
263 test
"BRPOPLPUSH inside a transaction" {
269 r brpoplpush xlist target
0
270 r brpoplpush xlist target
0
271 r brpoplpush xlist target
0
275 } {foo bar
{} {} {bar foo
}}
277 test
{BRPOPLPUSH timeout
} {
278 set rd
[redis_deferring_client
]
280 $rd brpoplpush foo_list bar_list
1
285 foreach {pop
} {BLPOP BRPOP
} {
286 test
"$pop: with single empty list argument" {
287 set rd
[redis_deferring_client
]
291 assert_equal
{blist1 foo
} [$rd read]
292 assert_equal
0 [r exists blist1
]
295 test
"$pop: with negative timeout" {
296 set rd
[redis_deferring_client
]
298 assert_error
"ERR*is negative*" {$rd read}
301 test
"$pop: with non-integer timeout" {
302 set rd
[redis_deferring_client
]
304 assert_error
"ERR*not an integer*" {$rd read}
307 test
"$pop: with zero timeout should block indefinitely" {
308 # To test this, use a timeout of 0 and wait a second.
309 # The blocking pop should still be waiting for a push.
310 set rd
[redis_deferring_client
]
314 assert_equal
{blist1 foo
} [$rd read]
317 test
"$pop: second argument is not a list" {
318 set rd
[redis_deferring_client
]
321 $rd $pop blist1 blist2
1
322 assert_error
"ERR*wrong kind*" {$rd read}
325 test
"$pop: timeout" {
326 set rd
[redis_deferring_client
]
328 $rd $pop blist1 blist2
1
329 assert_equal
{} [$rd read]
332 test
"$pop: arguments are empty" {
333 set rd
[redis_deferring_client
]
336 $rd $pop blist1 blist2
1
338 assert_equal
{blist1 foo
} [$rd read]
339 assert_equal
0 [r exists blist1
]
340 assert_equal
0 [r exists blist2
]
342 $rd $pop blist1 blist2
1
344 assert_equal
{blist2 foo
} [$rd read]
345 assert_equal
0 [r exists blist1
]
346 assert_equal
0 [r exists blist2
]
350 test
{BLPOP inside a transaction
} {
359 } {{xlist bar
} {xlist foo
} {}}
361 test
{LPUSHX
, RPUSHX
- generic
} {
363 assert_equal
0 [r lpushx xlist a
]
364 assert_equal
0 [r llen xlist
]
365 assert_equal
0 [r rpushx xlist a
]
366 assert_equal
0 [r llen xlist
]
369 foreach {type large
} [array get largevalue
] {
370 test
"LPUSHX, RPUSHX - $type" {
371 create_
$type xlist
"$large c"
372 assert_equal
3 [r rpushx xlist d
]
373 assert_equal
4 [r lpushx xlist a
]
374 assert_equal
"a $large c d" [r
lrange xlist
0 -1]
377 test
"LINSERT - $type" {
378 create_
$type xlist
"a $large c d"
379 assert_equal
5 [r
linsert xlist before c zz
]
380 assert_equal
"a $large zz c d" [r
lrange xlist
0 10]
381 assert_equal
6 [r
linsert xlist
after c yy
]
382 assert_equal
"a $large zz c yy d" [r
lrange xlist
0 10]
383 assert_equal
7 [r
linsert xlist
after d dd
]
384 assert_equal
-1 [r
linsert xlist
after bad ddd
]
385 assert_equal
"a $large zz c yy d dd" [r
lrange xlist
0 10]
386 assert_equal
8 [r
linsert xlist before a aa
]
387 assert_equal
-1 [r
linsert xlist before bad aaa
]
388 assert_equal
"aa a $large zz c yy d dd" [r
lrange xlist
0 10]
390 # check inserting integer encoded value
391 assert_equal
9 [r
linsert xlist before aa
42]
392 assert_equal
42 [r
lrange xlist
0 0]
396 test
{LPUSHX
, RPUSHX convert from ziplist to
list} {
397 set large
$largevalue(linkedlist
)
399 # convert when a large value is pushed
400 create_ziplist xlist a
401 assert_equal
2 [r rpushx xlist
$large]
402 assert_encoding linkedlist xlist
403 create_ziplist xlist a
404 assert_equal
2 [r lpushx xlist
$large]
405 assert_encoding linkedlist xlist
407 # convert when the length threshold is exceeded
408 create_ziplist xlist
[lrepeat
256 a
]
409 assert_equal
257 [r rpushx xlist b
]
410 assert_encoding linkedlist xlist
411 create_ziplist xlist
[lrepeat
256 a
]
412 assert_equal
257 [r lpushx xlist b
]
413 assert_encoding linkedlist xlist
416 test
{LINSERT convert from ziplist to
list} {
417 set large
$largevalue(linkedlist
)
419 # convert when a large value is inserted
420 create_ziplist xlist a
421 assert_equal
2 [r
linsert xlist before a
$large]
422 assert_encoding linkedlist xlist
423 create_ziplist xlist a
424 assert_equal
2 [r
linsert xlist
after a
$large]
425 assert_encoding linkedlist xlist
427 # convert when the length threshold is exceeded
428 create_ziplist xlist
[lrepeat
256 a
]
429 assert_equal
257 [r
linsert xlist before a a
]
430 assert_encoding linkedlist xlist
431 create_ziplist xlist
[lrepeat
256 a
]
432 assert_equal
257 [r
linsert xlist
after a a
]
433 assert_encoding linkedlist xlist
435 # don't convert when the value could not be inserted
436 create_ziplist xlist
[lrepeat
256 a
]
437 assert_equal
-1 [r
linsert xlist before foo a
]
438 assert_encoding ziplist xlist
439 create_ziplist xlist
[lrepeat
256 a
]
440 assert_equal
-1 [r
linsert xlist
after foo a
]
441 assert_encoding ziplist xlist
444 foreach {type num
} {ziplist
250 linkedlist
500} {
445 proc check_numbered_list_consistency
{key
} {
446 set len
[r llen
$key]
447 for {set i
0} {$i < $len} {incr i
} {
448 assert_equal
$i [r
lindex $key $i]
449 assert_equal
[expr $len-1-$i] [r
lindex $key [expr (-$i)-1]]
453 proc check_random_access_consistency
{key
} {
454 set len
[r llen
$key]
455 for {set i
0} {$i < $len} {incr i
} {
456 set rint
[expr int
(rand
()*$len)]
457 assert_equal
$rint [r
lindex $key $rint]
458 assert_equal
[expr $len-1-$rint] [r
lindex $key [expr (-$rint)-1]]
462 test
"LINDEX consistency test - $type" {
464 for {set i
0} {$i < $num} {incr i
} {
467 assert_encoding
$type mylist
468 check_numbered_list_consistency mylist
471 test
"LINDEX random access - $type" {
472 assert_encoding
$type mylist
473 check_random_access_consistency mylist
476 test
"Check if list is still ok after a DEBUG RELOAD - $type" {
478 assert_encoding
$type mylist
479 check_numbered_list_consistency mylist
480 check_random_access_consistency mylist
484 test
{LLEN against non-list value
error} {
487 assert_error ERR
* {r llen mylist
}
490 test
{LLEN against non existing key
} {
491 assert_equal
0 [r llen not-a-key
]
494 test
{LINDEX against non-list value
error} {
495 assert_error ERR
* {r
lindex mylist
0}
498 test
{LINDEX against non existing key
} {
499 assert_equal
"" [r
lindex not-a-key
10]
502 test
{LPUSH against non-list value
error} {
503 assert_error ERR
* {r lpush mylist
0}
506 test
{RPUSH against non-list value
error} {
507 assert_error ERR
* {r rpush mylist
0}
510 foreach {type large
} [array get largevalue
] {
511 test
"RPOPLPUSH base case - $type" {
512 r del mylist1 mylist2
513 create_
$type mylist1
"a $large c d"
514 assert_equal d
[r rpoplpush mylist1 mylist2
]
515 assert_equal c
[r rpoplpush mylist1 mylist2
]
516 assert_equal
"a $large" [r
lrange mylist1
0 -1]
517 assert_equal
"c d" [r
lrange mylist2
0 -1]
518 assert_encoding ziplist mylist2
521 test
"RPOPLPUSH with the same list as src and dst - $type" {
522 create_
$type mylist
"a $large c"
523 assert_equal
"a $large c" [r
lrange mylist
0 -1]
524 assert_equal c
[r rpoplpush mylist mylist
]
525 assert_equal
"c a $large" [r
lrange mylist
0 -1]
528 foreach {othertype otherlarge
} [array get largevalue
] {
529 test
"RPOPLPUSH with $type source and existing target $othertype" {
530 create_
$type srclist
"a b c $large"
531 create_
$othertype dstlist
"$otherlarge"
532 assert_equal
$large [r rpoplpush srclist dstlist
]
533 assert_equal c
[r rpoplpush srclist dstlist
]
534 assert_equal
"a b" [r
lrange srclist
0 -1]
535 assert_equal
"c $large $otherlarge" [r
lrange dstlist
0 -1]
537 # When we rpoplpush'ed a large value, dstlist should be
538 # converted to the same encoding as srclist.
539 if {$type eq
"linkedlist"} {
540 assert_encoding linkedlist dstlist
546 test
{RPOPLPUSH against non existing key
} {
547 r del srclist dstlist
548 assert_equal
{} [r rpoplpush srclist dstlist
]
549 assert_equal
0 [r exists srclist
]
550 assert_equal
0 [r exists dstlist
]
553 test
{RPOPLPUSH against non
list src key
} {
554 r del srclist dstlist
556 assert_error ERR
* {r rpoplpush srclist dstlist
}
557 assert_type
string srclist
558 assert_equal
0 [r exists newlist
]
561 test
{RPOPLPUSH against non
list dst key
} {
562 create_ziplist srclist
{a b c d
}
564 assert_error ERR
* {r rpoplpush srclist dstlist
}
565 assert_type
string dstlist
566 assert_equal
{a b c d
} [r
lrange srclist
0 -1]
569 test
{RPOPLPUSH against non existing src key
} {
570 r del srclist dstlist
571 assert_equal
{} [r rpoplpush srclist dstlist
]
574 foreach {type large
} [array get largevalue
] {
575 test
"Basic LPOP/RPOP - $type" {
576 create_
$type mylist
"$large 1 2"
577 assert_equal
$large [r lpop mylist
]
578 assert_equal
2 [r rpop mylist
]
579 assert_equal
1 [r lpop mylist
]
580 assert_equal
0 [r llen mylist
]
583 assert_equal
{} [r lpop mylist
]
584 assert_equal
{} [r rpop mylist
]
588 test
{LPOP
/RPOP against non
list value
} {
590 assert_error ERR
*kind
* {r lpop notalist
}
591 assert_error ERR
*kind
* {r rpop notalist
}
594 foreach {type num
} {ziplist
250 linkedlist
500} {
595 test
"Mass RPOP/LPOP - $type" {
598 for {set i
0} {$i < $num} {incr i
} {
602 assert_encoding
$type mylist
604 for {set i
0} {$i < [expr $num/2]} {incr i
} {
605 incr sum2
[r lpop mylist
]
606 incr sum2
[r rpop mylist
]
608 assert_equal
$sum1 $sum2
612 foreach {type large
} [array get largevalue
] {
613 test
"LRANGE basics - $type" {
614 create_
$type mylist
"$large 1 2 3 4 5 6 7 8 9"
615 assert_equal
{1 2 3 4 5 6 7 8} [r
lrange mylist
1 -2]
616 assert_equal
{7 8 9} [r
lrange mylist
-3 -1]
617 assert_equal
{4} [r
lrange mylist
4 4]
620 test
"LRANGE inverted indexes - $type" {
621 create_
$type mylist
"$large 1 2 3 4 5 6 7 8 9"
622 assert_equal
{} [r
lrange mylist
6 2]
625 test
"LRANGE out of range indexes including the full list - $type" {
626 create_
$type mylist
"$large 1 2 3"
627 assert_equal
"$large 1 2 3" [r
lrange mylist
-1000 1000]
630 test
"LRANGE out of range negative end index - $type" {
631 create_
$type mylist
"$large 1 2 3"
632 assert_equal
$large [r
lrange mylist
0 -4]
633 assert_equal
{} [r
lrange mylist
0 -5]
637 test
{LRANGE against non existing key
} {
638 assert_equal
{} [r
lrange nosuchkey
0 1]
641 foreach {type large
} [array get largevalue
] {
642 proc trim_list
{type min max
} {
645 create_
$type mylist
"1 2 3 4 $large"
646 r ltrim mylist
$min $max
650 test
"LTRIM basics - $type" {
651 assert_equal
"1" [trim_list
$type 0 0]
652 assert_equal
"1 2" [trim_list
$type 0 1]
653 assert_equal
"1 2 3" [trim_list
$type 0 2]
654 assert_equal
"2 3" [trim_list
$type 1 2]
655 assert_equal
"2 3 4 $large" [trim_list
$type 1 -1]
656 assert_equal
"2 3 4" [trim_list
$type 1 -2]
657 assert_equal
"4 $large" [trim_list
$type -2 -1]
658 assert_equal
"$large" [trim_list
$type -1 -1]
659 assert_equal
"1 2 3 4 $large" [trim_list
$type -5 -1]
660 assert_equal
"1 2 3 4 $large" [trim_list
$type -10 10]
661 assert_equal
"1 2 3 4 $large" [trim_list
$type 0 5]
662 assert_equal
"1 2 3 4 $large" [trim_list
$type 0 10]
665 test
"LTRIM out of range negative end index - $type" {
666 assert_equal
{1} [trim_list
$type 0 -5]
667 assert_equal
{} [trim_list
$type 0 -6]
672 foreach {type large
} [array get largevalue
] {
673 test
"LSET - $type" {
674 create_
$type mylist
"99 98 $large 96 95"
677 assert_equal
"99 foo $large 96 bar" [r
lrange mylist
0 -1]
680 test
"LSET out of range index - $type" {
681 assert_error ERR
*range
* {r
lset mylist
10 foo
}
685 test
{LSET against non existing key
} {
686 assert_error ERR
*key
* {r
lset nosuchkey
10 foo
}
689 test
{LSET against non
list value
} {
691 assert_error ERR
*value
* {r
lset nolist
0 foo
}
694 foreach {type e
} [array get largevalue
] {
695 test
"LREM remove all the occurrences - $type" {
696 create_
$type mylist
"$e foo bar foobar foobared zap bar test foo"
697 assert_equal
2 [r lrem mylist
0 bar
]
698 assert_equal
"$e foo foobar foobared zap test foo" [r
lrange mylist
0 -1]
701 test
"LREM remove the first occurrence - $type" {
702 assert_equal
1 [r lrem mylist
1 foo
]
703 assert_equal
"$e foobar foobared zap test foo" [r
lrange mylist
0 -1]
706 test
"LREM remove non existing element - $type" {
707 assert_equal
0 [r lrem mylist
1 nosuchelement
]
708 assert_equal
"$e foobar foobared zap test foo" [r
lrange mylist
0 -1]
711 test
"LREM starting from tail with negative count - $type" {
712 create_
$type mylist
"$e foo bar foobar foobared zap bar test foo foo"
713 assert_equal
1 [r lrem mylist
-1 bar
]
714 assert_equal
"$e foo bar foobar foobared zap test foo foo" [r
lrange mylist
0 -1]
717 test
"LREM starting from tail with negative count (2) - $type" {
718 assert_equal
2 [r lrem mylist
-2 foo
]
719 assert_equal
"$e foo bar foobar foobared zap test" [r
lrange mylist
0 -1]
722 test
"LREM deleting objects that may be int encoded - $type" {
723 create_
$type myotherlist
"$e 1 2 3"
724 assert_equal
1 [r lrem myotherlist
1 2]
725 assert_equal
3 [r llen myotherlist
]