xref: /redis-3.2.3/tests/unit/scripting.tcl (revision 38cf2bd2)
1start_server {tags {"scripting"}} {
2    test {EVAL - Does Lua interpreter replies to our requests?} {
3        r eval {return 'hello'} 0
4    } {hello}
5
6    test {EVAL - Lua integer -> Redis protocol type conversion} {
7        r eval {return 100.5} 0
8    } {100}
9
10    test {EVAL - Lua string -> Redis protocol type conversion} {
11        r eval {return 'hello world'} 0
12    } {hello world}
13
14    test {EVAL - Lua true boolean -> Redis protocol type conversion} {
15        r eval {return true} 0
16    } {1}
17
18    test {EVAL - Lua false boolean -> Redis protocol type conversion} {
19        r eval {return false} 0
20    } {}
21
22    test {EVAL - Lua status code reply -> Redis protocol type conversion} {
23        r eval {return {ok='fine'}} 0
24    } {fine}
25
26    test {EVAL - Lua error reply -> Redis protocol type conversion} {
27        catch {
28            r eval {return {err='this is an error'}} 0
29        } e
30        set _ $e
31    } {this is an error}
32
33    test {EVAL - Lua table -> Redis protocol type conversion} {
34        r eval {return {1,2,3,'ciao',{1,2}}} 0
35    } {1 2 3 ciao {1 2}}
36
37    test {EVAL - Are the KEYS and ARGV arrays populated correctly?} {
38        r eval {return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}} 2 a b c d
39    } {a b c d}
40
41    test {EVAL - is Lua able to call Redis API?} {
42        r set mykey myval
43        r eval {return redis.call('get',KEYS[1])} 1 mykey
44    } {myval}
45
46    test {EVALSHA - Can we call a SHA1 if already defined?} {
47        r evalsha fd758d1589d044dd850a6f05d52f2eefd27f033f 1 mykey
48    } {myval}
49
50    test {EVALSHA - Can we call a SHA1 in uppercase?} {
51        r evalsha FD758D1589D044DD850A6F05D52F2EEFD27F033F 1 mykey
52    } {myval}
53
54    test {EVALSHA - Do we get an error on invalid SHA1?} {
55        catch {r evalsha NotValidShaSUM 0} e
56        set _ $e
57    } {NOSCRIPT*}
58
59    test {EVALSHA - Do we get an error on non defined SHA1?} {
60        catch {r evalsha ffd632c7d33e571e9f24556ebed26c3479a87130 0} e
61        set _ $e
62    } {NOSCRIPT*}
63
64    test {EVAL - Redis integer -> Lua type conversion} {
65        r eval {
66            local foo = redis.pcall('incr','x')
67            return {type(foo),foo}
68        } 0
69    } {number 1}
70
71    test {EVAL - Redis bulk -> Lua type conversion} {
72        r set mykey myval
73        r eval {
74            local foo = redis.pcall('get','mykey')
75            return {type(foo),foo}
76        } 0
77    } {string myval}
78
79    test {EVAL - Redis multi bulk -> Lua type conversion} {
80        r del mylist
81        r rpush mylist a
82        r rpush mylist b
83        r rpush mylist c
84        r eval {
85            local foo = redis.pcall('lrange','mylist',0,-1)
86            return {type(foo),foo[1],foo[2],foo[3],# foo}
87        } 0
88    } {table a b c 3}
89
90    test {EVAL - Redis status reply -> Lua type conversion} {
91        r eval {
92            local foo = redis.pcall('set','mykey','myval')
93            return {type(foo),foo['ok']}
94        } 0
95    } {table OK}
96
97    test {EVAL - Redis error reply -> Lua type conversion} {
98        r set mykey myval
99        r eval {
100            local foo = redis.pcall('incr','mykey')
101            return {type(foo),foo['err']}
102        } 0
103    } {table {ERR value is not an integer or out of range}}
104
105    test {EVAL - Redis nil bulk reply -> Lua type conversion} {
106        r del mykey
107        r eval {
108            local foo = redis.pcall('get','mykey')
109            return {type(foo),foo == false}
110        } 0
111    } {boolean 1}
112
113    test {EVAL - Is the Lua client using the currently selected DB?} {
114        r set mykey "this is DB 9"
115        r select 10
116        r set mykey "this is DB 10"
117        r eval {return redis.pcall('get','mykey')} 0
118    } {this is DB 10}
119
120    test {EVAL - SELECT inside Lua should not affect the caller} {
121        # here we DB 10 is selected
122        r set mykey "original value"
123        r eval {return redis.pcall('select','9')} 0
124        set res [r get mykey]
125        r select 9
126        set res
127    } {original value}
128
129    if 0 {
130        test {EVAL - Script can't run more than configured time limit} {
131            r config set lua-time-limit 1
132            catch {
133                r eval {
134                    local i = 0
135                    while true do i=i+1 end
136                } 0
137            } e
138            set _ $e
139        } {*execution time*}
140    }
141
142    test {EVAL - Scripts can't run certain commands} {
143        set e {}
144        catch {r eval {return redis.pcall('spop','x')} 0} e
145        set e
146    } {*not allowed*}
147
148    test {EVAL - Scripts can't run certain commands} {
149        set e {}
150        catch {
151            r eval "redis.pcall('randomkey'); return redis.pcall('set','x','ciao')" 0
152        } e
153        set e
154    } {*not allowed after*}
155
156    test {EVAL - No arguments to redis.call/pcall is considered an error} {
157        set e {}
158        catch {r eval {return redis.call()} 0} e
159        set e
160    } {*one argument*}
161
162    test {EVAL - redis.call variant raises a Lua error on Redis cmd error (1)} {
163        set e {}
164        catch {
165            r eval "redis.call('nosuchcommand')" 0
166        } e
167        set e
168    } {*Unknown Redis*}
169
170    test {EVAL - redis.call variant raises a Lua error on Redis cmd error (1)} {
171        set e {}
172        catch {
173            r eval "redis.call('get','a','b','c')" 0
174        } e
175        set e
176    } {*number of args*}
177
178    test {EVAL - redis.call variant raises a Lua error on Redis cmd error (1)} {
179        set e {}
180        r set foo bar
181        catch {
182            r eval {redis.call('lpush',KEYS[1],'val')} 1 foo
183        } e
184        set e
185    } {*against a key*}
186
187    test {EVAL - JSON numeric decoding} {
188        # We must return the table as a string because otherwise
189        # Redis converts floats to ints and we get 0 and 1023 instead
190        # of 0.0003 and 1023.2 as the parsed output.
191        r eval {return
192                 table.concat(
193                   cjson.decode(
194                    "[0.0, -5e3, -1, 0.3e-3, 1023.2, 0e10]"), " ")
195        } 0
196    } {0 -5000 -1 0.0003 1023.2 0}
197
198    test {EVAL - JSON string decoding} {
199        r eval {local decoded = cjson.decode('{"keya": "a", "keyb": "b"}')
200                return {decoded.keya, decoded.keyb}
201        } 0
202    } {a b}
203
204    test {EVAL - cmsgpack can pack double?} {
205        r eval {local encoded = cmsgpack.pack(0.1)
206                local h = ""
207                for i = 1, #encoded do
208                    h = h .. string.format("%02x",string.byte(encoded,i))
209                end
210                return h
211        } 0
212    } {cb3fb999999999999a}
213
214    test {EVAL - cmsgpack can pack negative int64?} {
215        r eval {local encoded = cmsgpack.pack(-1099511627776)
216                local h = ""
217                for i = 1, #encoded do
218                    h = h .. string.format("%02x",string.byte(encoded,i))
219                end
220                return h
221        } 0
222    } {d3ffffff0000000000}
223
224    test {EVAL - cmsgpack can pack and unpack circular references?} {
225        r eval {local a = {x=nil,y=5}
226                local b = {x=a}
227                a['x'] = b
228                local encoded = cmsgpack.pack(a)
229                local h = ""
230                -- cmsgpack encodes to a depth of 16, but can't encode
231                -- references, so the encoded object has a deep copy recusive
232                -- depth of 16.
233                for i = 1, #encoded do
234                    h = h .. string.format("%02x",string.byte(encoded,i))
235                end
236                -- when unpacked, re.x.x != re because the unpack creates
237                -- individual tables down to a depth of 16.
238                -- (that's why the encoded output is so large)
239                local re = cmsgpack.unpack(encoded)
240                assert(re)
241                assert(re.x)
242                assert(re.x.x.y == re.y)
243                assert(re.x.x.x.x.y == re.y)
244                assert(re.x.x.x.x.x.x.y == re.y)
245                assert(re.x.x.x.x.x.x.x.x.x.x.y == re.y)
246                -- maximum working depth:
247                assert(re.x.x.x.x.x.x.x.x.x.x.x.x.x.x.y == re.y)
248                -- now the last x would be b above and has no y
249                assert(re.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x)
250                -- so, the final x.x is at the depth limit and was assigned nil
251                assert(re.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x == nil)
252                return {h, re.x.x.x.x.x.x.x.x.y == re.y, re.y == 5}
253        } 0
254    } {82a17905a17881a17882a17905a17881a17882a17905a17881a17882a17905a17881a17882a17905a17881a17882a17905a17881a17882a17905a17881a17882a17905a17881a178c0 1 1}
255
256    test {EVAL - Numerical sanity check from bitop} {
257        r eval {assert(0x7fffffff == 2147483647, "broken hex literals");
258                assert(0xffffffff == -1 or 0xffffffff == 2^32-1,
259                    "broken hex literals");
260                assert(tostring(-1) == "-1", "broken tostring()");
261                assert(tostring(0xffffffff) == "-1" or
262                    tostring(0xffffffff) == "4294967295",
263                    "broken tostring()")
264        } 0
265    } {}
266
267    test {EVAL - Verify minimal bitop functionality} {
268        r eval {assert(bit.tobit(1) == 1);
269                assert(bit.band(1) == 1);
270                assert(bit.bxor(1,2) == 3);
271                assert(bit.bor(1,2,4,8,16,32,64,128) == 255)
272        } 0
273    } {}
274
275    test {EVAL - Able to parse trailing comments} {
276        r eval {return 'hello' --trailing comment} 0
277    } {hello}
278
279    test {SCRIPTING FLUSH - is able to clear the scripts cache?} {
280        r set mykey myval
281        set v [r evalsha fd758d1589d044dd850a6f05d52f2eefd27f033f 1 mykey]
282        assert_equal $v myval
283        set e ""
284        r script flush
285        catch {r evalsha fd758d1589d044dd850a6f05d52f2eefd27f033f 1 mykey} e
286        set e
287    } {NOSCRIPT*}
288
289    test {SCRIPT EXISTS - can detect already defined scripts?} {
290        r eval "return 1+1" 0
291        r script exists a27e7e8a43702b7046d4f6a7ccf5b60cef6b9bd9 a27e7e8a43702b7046d4f6a7ccf5b60cef6b9bda
292    } {1 0}
293
294    test {SCRIPT LOAD - is able to register scripts in the scripting cache} {
295        list \
296            [r script load "return 'loaded'"] \
297            [r evalsha b534286061d4b9e4026607613b95c06c06015ae8 0]
298    } {b534286061d4b9e4026607613b95c06c06015ae8 loaded}
299
300    test "In the context of Lua the output of random commands gets ordered" {
301        r del myset
302        r sadd myset a b c d e f g h i l m n o p q r s t u v z aa aaa azz
303        r eval {return redis.call('smembers',KEYS[1])} 1 myset
304    } {a aa aaa azz b c d e f g h i l m n o p q r s t u v z}
305
306    test "SORT is normally not alpha re-ordered for the scripting engine" {
307        r del myset
308        r sadd myset 1 2 3 4 10
309        r eval {return redis.call('sort',KEYS[1],'desc')} 1 myset
310    } {10 4 3 2 1}
311
312    test "SORT BY <constant> output gets ordered for scripting" {
313        r del myset
314        r sadd myset a b c d e f g h i l m n o p q r s t u v z aa aaa azz
315        r eval {return redis.call('sort',KEYS[1],'by','_')} 1 myset
316    } {a aa aaa azz b c d e f g h i l m n o p q r s t u v z}
317
318    test "SORT BY <constant> with GET gets ordered for scripting" {
319        r del myset
320        r sadd myset a b c
321        r eval {return redis.call('sort',KEYS[1],'by','_','get','#','get','_:*')} 1 myset
322    } {a {} b {} c {}}
323
324    test "redis.sha1hex() implementation" {
325        list [r eval {return redis.sha1hex('')} 0] \
326             [r eval {return redis.sha1hex('Pizza & Mandolino')} 0]
327    } {da39a3ee5e6b4b0d3255bfef95601890afd80709 74822d82031af7493c20eefa13bd07ec4fada82f}
328
329    test {Globals protection reading an undeclared global variable} {
330        catch {r eval {return a} 0} e
331        set e
332    } {*ERR*attempted to access unexisting global*}
333
334    test {Globals protection setting an undeclared global*} {
335        catch {r eval {a=10} 0} e
336        set e
337    } {*ERR*attempted to create global*}
338
339    test {Test an example script DECR_IF_GT} {
340        set decr_if_gt {
341            local current
342
343            current = redis.call('get',KEYS[1])
344            if not current then return nil end
345            if current > ARGV[1] then
346                return redis.call('decr',KEYS[1])
347            else
348                return redis.call('get',KEYS[1])
349            end
350        }
351        r set foo 5
352        set res {}
353        lappend res [r eval $decr_if_gt 1 foo 2]
354        lappend res [r eval $decr_if_gt 1 foo 2]
355        lappend res [r eval $decr_if_gt 1 foo 2]
356        lappend res [r eval $decr_if_gt 1 foo 2]
357        lappend res [r eval $decr_if_gt 1 foo 2]
358        set res
359    } {4 3 2 2 2}
360
361    test {Scripting engine resets PRNG at every script execution} {
362        set rand1 [r eval {return tostring(math.random())} 0]
363        set rand2 [r eval {return tostring(math.random())} 0]
364        assert_equal $rand1 $rand2
365    }
366
367    test {Scripting engine PRNG can be seeded correctly} {
368        set rand1 [r eval {
369            math.randomseed(ARGV[1]); return tostring(math.random())
370        } 0 10]
371        set rand2 [r eval {
372            math.randomseed(ARGV[1]); return tostring(math.random())
373        } 0 10]
374        set rand3 [r eval {
375            math.randomseed(ARGV[1]); return tostring(math.random())
376        } 0 20]
377        assert_equal $rand1 $rand2
378        assert {$rand2 ne $rand3}
379    }
380
381    test {EVAL does not leak in the Lua stack} {
382        r set x 0
383        # Use a non blocking client to speedup the loop.
384        set rd [redis_deferring_client]
385        for {set j 0} {$j < 10000} {incr j} {
386            $rd eval {return redis.call("incr",KEYS[1])} 1 x
387        }
388        for {set j 0} {$j < 10000} {incr j} {
389            $rd read
390        }
391        assert {[s used_memory_lua] < 1024*100}
392        $rd close
393        r get x
394    } {10000}
395
396    test {EVAL processes writes from AOF in read-only slaves} {
397        r flushall
398        r config set appendonly yes
399        r eval {redis.call("set",KEYS[1],"100")} 1 foo
400        r eval {redis.call("incr",KEYS[1])} 1 foo
401        r eval {redis.call("incr",KEYS[1])} 1 foo
402        wait_for_condition 50 100 {
403            [s aof_rewrite_in_progress] == 0
404        } else {
405            fail "AOF rewrite can't complete after CONFIG SET appendonly yes."
406        }
407        r config set slave-read-only yes
408        r slaveof 127.0.0.1 0
409        r debug loadaof
410        set res [r get foo]
411        r slaveof no one
412        set res
413    } {102}
414
415    test {We can call scripts rewriting client->argv from Lua} {
416        r del myset
417        r sadd myset a b c
418        r mset a 1 b 2 c 3 d 4
419        assert {[r spop myset] ne {}}
420        assert {[r spop myset 1] ne {}}
421        assert {[r spop myset] ne {}}
422        assert {[r mget a b c d] eq {1 2 3 4}}
423        assert {[r spop myset] eq {}}
424    }
425
426    test {Call Redis command with many args from Lua (issue #1764)} {
427        r eval {
428            local i
429            local x={}
430            redis.call('del','mylist')
431            for i=1,100 do
432                table.insert(x,i)
433            end
434            redis.call('rpush','mylist',unpack(x))
435            return redis.call('lrange','mylist',0,-1)
436        } 0
437    } {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100}
438
439    test {Number conversion precision test (issue #1118)} {
440        r eval {
441              local value = 9007199254740991
442              redis.call("set","foo",value)
443              return redis.call("get","foo")
444        } 0
445    } {9007199254740991}
446
447    test {String containing number precision test (regression of issue #1118)} {
448        r eval {
449            redis.call("set", "key", "12039611435714932082")
450            return redis.call("get", "key")
451        } 0
452    } {12039611435714932082}
453
454    test {Verify negative arg count is error instead of crash (issue #1842)} {
455        catch { r eval { return "hello" } -12 } e
456        set e
457    } {ERR Number of keys can't be negative}
458
459    test {Correct handling of reused argv (issue #1939)} {
460        r eval {
461              for i = 0, 10 do
462                  redis.call('SET', 'a', '1')
463                  redis.call('MGET', 'a', 'b', 'c')
464                  redis.call('EXPIRE', 'a', 0)
465                  redis.call('GET', 'a')
466                  redis.call('MGET', 'a', 'b', 'c')
467              end
468        } 0
469    }
470
471    test {Functions in the Redis namespace are able to report errors} {
472        catch {
473            r eval {
474                  redis.sha1hex()
475            } 0
476        } e
477        set e
478    } {*wrong number*}
479}
480
481# Start a new server since the last test in this stanza will kill the
482# instance at all.
483start_server {tags {"scripting"}} {
484    test {Timedout read-only scripts can be killed by SCRIPT KILL} {
485        set rd [redis_deferring_client]
486        r config set lua-time-limit 10
487        $rd eval {while true do end} 0
488        after 200
489        catch {r ping} e
490        assert_match {BUSY*} $e
491        r script kill
492        after 200 ; # Give some time to Lua to call the hook again...
493        assert_equal [r ping] "PONG"
494    }
495
496    test {Timedout script link is still usable after Lua returns} {
497        r config set lua-time-limit 10
498        r eval {for i=1,100000 do redis.call('ping') end return 'ok'} 0
499        r ping
500    } {PONG}
501
502    test {Timedout scripts that modified data can't be killed by SCRIPT KILL} {
503        set rd [redis_deferring_client]
504        r config set lua-time-limit 10
505        $rd eval {redis.call('set',KEYS[1],'y'); while true do end} 1 x
506        after 200
507        catch {r ping} e
508        assert_match {BUSY*} $e
509        catch {r script kill} e
510        assert_match {UNKILLABLE*} $e
511        catch {r ping} e
512        assert_match {BUSY*} $e
513    }
514
515    # Note: keep this test at the end of this server stanza because it
516    # kills the server.
517    test {SHUTDOWN NOSAVE can kill a timedout script anyway} {
518        # The server sould be still unresponding to normal commands.
519        catch {r ping} e
520        assert_match {BUSY*} $e
521        catch {r shutdown nosave}
522        # Make sure the server was killed
523        catch {set rd [redis_deferring_client]} e
524        assert_match {*connection refused*} $e
525    }
526}
527
528foreach cmdrepl {0 1} {
529    start_server {tags {"scripting repl"}} {
530        start_server {} {
531            if {$cmdrepl == 1} {
532                set rt "(commmands replication)"
533            } else {
534                set rt "(scripts replication)"
535                r debug lua-always-replicate-commands 1
536            }
537
538            test "Before the slave connects we issue two EVAL commands $rt" {
539                # One with an error, but still executing a command.
540                # SHA is: 67164fc43fa971f76fd1aaeeaf60c1c178d25876
541                catch {
542                    r eval {redis.call('incr',KEYS[1]); redis.call('nonexisting')} 1 x
543                }
544                # One command is correct:
545                # SHA is: 6f5ade10a69975e903c6d07b10ea44c6382381a5
546                r eval {return redis.call('incr',KEYS[1])} 1 x
547            } {2}
548
549            test "Connect a slave to the master instance $rt" {
550                r -1 slaveof [srv 0 host] [srv 0 port]
551                wait_for_condition 50 100 {
552                    [s -1 role] eq {slave} &&
553                    [string match {*master_link_status:up*} [r -1 info replication]]
554                } else {
555                    fail "Can't turn the instance into a slave"
556                }
557            }
558
559            test "Now use EVALSHA against the master, with both SHAs $rt" {
560                # The server should replicate successful and unsuccessful
561                # commands as EVAL instead of EVALSHA.
562                catch {
563                    r evalsha 67164fc43fa971f76fd1aaeeaf60c1c178d25876 1 x
564                }
565                r evalsha 6f5ade10a69975e903c6d07b10ea44c6382381a5 1 x
566            } {4}
567
568            test "If EVALSHA was replicated as EVAL, 'x' should be '4' $rt" {
569                wait_for_condition 50 100 {
570                    [r -1 get x] eq {4}
571                } else {
572                    fail "Expected 4 in x, but value is '[r -1 get x]'"
573                }
574            }
575
576            test "Replication of script multiple pushes to list with BLPOP $rt" {
577                set rd [redis_deferring_client]
578                $rd brpop a 0
579                r eval {
580                    redis.call("lpush",KEYS[1],"1");
581                    redis.call("lpush",KEYS[1],"2");
582                } 1 a
583                set res [$rd read]
584                $rd close
585                wait_for_condition 50 100 {
586                    [r -1 lrange a 0 -1] eq [r lrange a 0 -1]
587                } else {
588                    fail "Expected list 'a' in slave and master to be the same, but they are respectively '[r -1 lrange a 0 -1]' and '[r lrange a 0 -1]'"
589                }
590                set res
591            } {a 1}
592
593            test "EVALSHA replication when first call is readonly $rt" {
594                r del x
595                r eval {if tonumber(ARGV[1]) > 0 then redis.call('incr', KEYS[1]) end} 1 x 0
596                r evalsha 6e0e2745aa546d0b50b801a20983b70710aef3ce 1 x 0
597                r evalsha 6e0e2745aa546d0b50b801a20983b70710aef3ce 1 x 1
598                wait_for_condition 50 100 {
599                    [r -1 get x] eq {1}
600                } else {
601                    fail "Expected 1 in x, but value is '[r -1 get x]'"
602                }
603            }
604
605            test "Lua scripts using SELECT are replicated correctly $rt" {
606                r eval {
607                    redis.call("set","foo1","bar1")
608                    redis.call("select","10")
609                    redis.call("incr","x")
610                    redis.call("select","11")
611                    redis.call("incr","z")
612                } 0
613                r eval {
614                    redis.call("set","foo1","bar1")
615                    redis.call("select","10")
616                    redis.call("incr","x")
617                    redis.call("select","11")
618                    redis.call("incr","z")
619                } 0
620                wait_for_condition 50 100 {
621                    [r -1 debug digest] eq [r debug digest]
622                } else {
623                    fail "Master-Slave desync after Lua script using SELECT."
624                }
625            }
626        }
627    }
628}
629
630start_server {tags {"scripting repl"}} {
631    start_server {overrides {appendonly yes}} {
632        test "Connect a slave to the master instance" {
633            r -1 slaveof [srv 0 host] [srv 0 port]
634            wait_for_condition 50 100 {
635                [s -1 role] eq {slave} &&
636                [string match {*master_link_status:up*} [r -1 info replication]]
637            } else {
638                fail "Can't turn the instance into a slave"
639            }
640        }
641
642        test "Redis.replicate_commands() must be issued before any write" {
643            r eval {
644                redis.call('set','foo','bar');
645                return redis.replicate_commands();
646            } 0
647        } {}
648
649        test "Redis.replicate_commands() must be issued before any write (2)" {
650            r eval {
651                return redis.replicate_commands();
652            } 0
653        } {1}
654
655        test "Redis.set_repl() must be issued after replicate_commands()" {
656            catch {
657                r eval {
658                    redis.set_repl(redis.REPL_ALL);
659                } 0
660            } e
661            set e
662        } {*only after turning on*}
663
664        test "Redis.set_repl() don't accept invalid values" {
665            catch {
666                r eval {
667                    redis.replicate_commands();
668                    redis.set_repl(12345);
669                } 0
670            } e
671            set e
672        } {*Invalid*flags*}
673
674        test "Test selective replication of certain Redis commands from Lua" {
675            r del a b c d
676            r eval {
677                redis.replicate_commands();
678                redis.call('set','a','1');
679                redis.set_repl(redis.REPL_NONE);
680                redis.call('set','b','2');
681                redis.set_repl(redis.REPL_AOF);
682                redis.call('set','c','3');
683                redis.set_repl(redis.REPL_ALL);
684                redis.call('set','d','4');
685            } 0
686
687            wait_for_condition 50 100 {
688                [r -1 mget a b c d] eq {1 {} {} 4}
689            } else {
690                fail "Only a and c should be replicated to slave"
691            }
692
693            # Master should have everything right now
694            assert {[r mget a b c d] eq {1 2 3 4}}
695
696            # After an AOF reload only a, c and d should exist
697            r debug loadaof
698
699            assert {[r mget a b c d] eq {1 {} 3 4}}
700        }
701
702        test "PRNG is seeded randomly for command replication" {
703            set a [
704                r eval {
705                    redis.replicate_commands();
706                    return math.random()*100000;
707                } 0
708            ]
709            set b [
710                r eval {
711                    redis.replicate_commands();
712                    return math.random()*100000;
713                } 0
714            ]
715            assert {$a ne $b}
716        }
717
718        test "Using side effects is not a problem with command replication" {
719            r eval {
720                redis.replicate_commands();
721                redis.call('set','time',redis.call('time')[1])
722            } 0
723
724            assert {[r get time] ne {}}
725
726            wait_for_condition 50 100 {
727                [r get time] eq [r -1 get time]
728            } else {
729                fail "Time key does not match between master and slave"
730            }
731        }
732    }
733}
734
735