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 {SCRIPTING FLUSH - is able to clear the scripts cache?} { 188 r set mykey myval 189 set v [r evalsha fd758d1589d044dd850a6f05d52f2eefd27f033f 1 mykey] 190 assert_equal $v myval 191 set e "" 192 r script flush 193 catch {r evalsha fd758d1589d044dd850a6f05d52f2eefd27f033f 1 mykey} e 194 set e 195 } {NOSCRIPT*} 196 197 test {SCRIPT EXISTS - can detect already defined scripts?} { 198 r eval "return 1+1" 0 199 r script exists a27e7e8a43702b7046d4f6a7ccf5b60cef6b9bd9 a27e7e8a43702b7046d4f6a7ccf5b60cef6b9bda 200 } {1 0} 201 202 test {SCRIPT LOAD - is able to register scripts in the scripting cache} { 203 list \ 204 [r script load "return 'loaded'"] \ 205 [r evalsha b534286061d4b9e4026607613b95c06c06015ae8 0] 206 } {b534286061d4b9e4026607613b95c06c06015ae8 loaded} 207 208 test "In the context of Lua the output of random commands gets ordered" { 209 r del myset 210 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 211 r eval {return redis.call('smembers',KEYS[1])} 1 myset 212 } {a aa aaa azz b c d e f g h i l m n o p q r s t u v z} 213 214 test "SORT is normally not alpha re-ordered for the scripting engine" { 215 r del myset 216 r sadd myset 1 2 3 4 10 217 r eval {return redis.call('sort',KEYS[1],'desc')} 1 myset 218 } {10 4 3 2 1} 219 220 test "SORT BY <constant> output gets ordered for scripting" { 221 r del myset 222 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 223 r eval {return redis.call('sort',KEYS[1],'by','_')} 1 myset 224 } {a aa aaa azz b c d e f g h i l m n o p q r s t u v z} 225 226 test "SORT BY <constant> with GET gets ordered for scripting" { 227 r del myset 228 r sadd myset a b c 229 r eval {return redis.call('sort',KEYS[1],'by','_','get','#','get','_:*')} 1 myset 230 } {a {} b {} c {}} 231 232 test "redis.sha1hex() implementation" { 233 list [r eval {return redis.sha1hex('')} 0] \ 234 [r eval {return redis.sha1hex('Pizza & Mandolino')} 0] 235 } {da39a3ee5e6b4b0d3255bfef95601890afd80709 74822d82031af7493c20eefa13bd07ec4fada82f} 236 237 test {Globals protection reading an undeclared global variable} { 238 catch {r eval {return a} 0} e 239 set e 240 } {*ERR*attempted to access unexisting global*} 241 242 test {Globals protection setting an undeclared global*} { 243 catch {r eval {a=10} 0} e 244 set e 245 } {*ERR*attempted to create global*} 246 247 test {Test an example script DECR_IF_GT} { 248 set decr_if_gt { 249 local current 250 251 current = redis.call('get',KEYS[1]) 252 if not current then return nil end 253 if current > ARGV[1] then 254 return redis.call('decr',KEYS[1]) 255 else 256 return redis.call('get',KEYS[1]) 257 end 258 } 259 r set foo 5 260 set res {} 261 lappend res [r eval $decr_if_gt 1 foo 2] 262 lappend res [r eval $decr_if_gt 1 foo 2] 263 lappend res [r eval $decr_if_gt 1 foo 2] 264 lappend res [r eval $decr_if_gt 1 foo 2] 265 lappend res [r eval $decr_if_gt 1 foo 2] 266 set res 267 } {4 3 2 2 2} 268 269 test {Scripting engine resets PRNG at every script execution} { 270 set rand1 [r eval {return tostring(math.random())} 0] 271 set rand2 [r eval {return tostring(math.random())} 0] 272 assert_equal $rand1 $rand2 273 } 274 275 test {Scripting engine PRNG can be seeded correctly} { 276 set rand1 [r eval { 277 math.randomseed(ARGV[1]); return tostring(math.random()) 278 } 0 10] 279 set rand2 [r eval { 280 math.randomseed(ARGV[1]); return tostring(math.random()) 281 } 0 10] 282 set rand3 [r eval { 283 math.randomseed(ARGV[1]); return tostring(math.random()) 284 } 0 20] 285 assert_equal $rand1 $rand2 286 assert {$rand2 ne $rand3} 287 } 288 289 test {EVAL does not leak in the Lua stack} { 290 r set x 0 291 # Use a non blocking client to speedup the loop. 292 set rd [redis_deferring_client] 293 for {set j 0} {$j < 10000} {incr j} { 294 $rd eval {return redis.call("incr",KEYS[1])} 1 x 295 } 296 for {set j 0} {$j < 10000} {incr j} { 297 $rd read 298 } 299 assert {[s used_memory_lua] < 1024*100} 300 $rd close 301 r get x 302 } {10000} 303 304 test {EVAL processes writes from AOF in read-only slaves} { 305 r flushall 306 r config set appendonly yes 307 r eval {redis.call("set",KEYS[1],"100")} 1 foo 308 r eval {redis.call("incr",KEYS[1])} 1 foo 309 r eval {redis.call("incr",KEYS[1])} 1 foo 310 wait_for_condition 50 100 { 311 [s aof_rewrite_in_progress] == 0 312 } else { 313 fail "AOF rewrite can't complete after CONFIG SET appendonly yes." 314 } 315 r config set slave-read-only yes 316 r slaveof 127.0.0.1 0 317 r debug loadaof 318 set res [r get foo] 319 r slaveof no one 320 set res 321 } {102} 322 323 test {We can call scripts rewriting client->argv from Lua} { 324 r del myset 325 r sadd myset a b c 326 r mset a 1 b 2 c 3 d 4 327 assert {[r spop myset] ne {}} 328 assert {[r spop myset] ne {}} 329 assert {[r spop myset] ne {}} 330 assert {[r mget a b c d] eq {1 2 3 4}} 331 assert {[r spop myset] eq {}} 332 } 333 334 test {Call Redis command with many args from Lua (issue #1764)} { 335 r eval { 336 local i 337 local x={} 338 redis.call('del','mylist') 339 for i=1,100 do 340 table.insert(x,i) 341 end 342 redis.call('rpush','mylist',unpack(x)) 343 return redis.call('lrange','mylist',0,-1) 344 } 0 345 } {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} 346 347 test {Number conversion precision test (issue #1118)} { 348 r eval { 349 local value = 9007199254740991 350 redis.call("set","foo",value) 351 return redis.call("get","foo") 352 } 0 353 } {9007199254740991} 354 355 test {String containing number precision test (regression of issue #1118)} { 356 r eval { 357 redis.call("set", "key", "12039611435714932082") 358 return redis.call("get", "key") 359 } 0 360 } {12039611435714932082} 361} 362 363# Start a new server since the last test in this stanza will kill the 364# instance at all. 365start_server {tags {"scripting"}} { 366 test {Timedout read-only scripts can be killed by SCRIPT KILL} { 367 set rd [redis_deferring_client] 368 r config set lua-time-limit 10 369 $rd eval {while true do end} 0 370 after 200 371 catch {r ping} e 372 assert_match {BUSY*} $e 373 r script kill 374 after 200 ; # Give some time to Lua to call the hook again... 375 assert_equal [r ping] "PONG" 376 } 377 378 test {Timedout script link is still usable after Lua returns} { 379 r config set lua-time-limit 10 380 r eval {for i=1,100000 do redis.call('ping') end return 'ok'} 0 381 r ping 382 } {PONG} 383 384 test {Timedout scripts that modified data can't be killed by SCRIPT KILL} { 385 set rd [redis_deferring_client] 386 r config set lua-time-limit 10 387 $rd eval {redis.call('set',KEYS[1],'y'); while true do end} 1 x 388 after 200 389 catch {r ping} e 390 assert_match {BUSY*} $e 391 catch {r script kill} e 392 assert_match {UNKILLABLE*} $e 393 catch {r ping} e 394 assert_match {BUSY*} $e 395 } 396 397 # Note: keep this test at the end of this server stanza because it 398 # kills the server. 399 test {SHUTDOWN NOSAVE can kill a timedout script anyway} { 400 # The server sould be still unresponding to normal commands. 401 catch {r ping} e 402 assert_match {BUSY*} $e 403 catch {r shutdown nosave} 404 # Make sure the server was killed 405 catch {set rd [redis_deferring_client]} e 406 assert_match {*connection refused*} $e 407 } 408} 409 410start_server {tags {"scripting repl"}} { 411 start_server {} { 412 test {Before the slave connects we issue two EVAL commands} { 413 # One with an error, but still executing a command. 414 # SHA is: 67164fc43fa971f76fd1aaeeaf60c1c178d25876 415 catch { 416 r eval {redis.call('incr',KEYS[1]); redis.call('nonexisting')} 1 x 417 } 418 # One command is correct: 419 # SHA is: 6f5ade10a69975e903c6d07b10ea44c6382381a5 420 r eval {return redis.call('incr',KEYS[1])} 1 x 421 } {2} 422 423 test {Connect a slave to the main instance} { 424 r -1 slaveof [srv 0 host] [srv 0 port] 425 wait_for_condition 50 100 { 426 [s -1 role] eq {slave} && 427 [string match {*master_link_status:up*} [r -1 info replication]] 428 } else { 429 fail "Can't turn the instance into a slave" 430 } 431 } 432 433 test {Now use EVALSHA against the master, with both SHAs} { 434 # The server should replicate successful and unsuccessful 435 # commands as EVAL instead of EVALSHA. 436 catch { 437 r evalsha 67164fc43fa971f76fd1aaeeaf60c1c178d25876 1 x 438 } 439 r evalsha 6f5ade10a69975e903c6d07b10ea44c6382381a5 1 x 440 } {4} 441 442 test {If EVALSHA was replicated as EVAL, 'x' should be '4'} { 443 wait_for_condition 50 100 { 444 [r -1 get x] eq {4} 445 } else { 446 fail "Expected 4 in x, but value is '[r -1 get x]'" 447 } 448 } 449 450 test {Replication of script multiple pushes to list with BLPOP} { 451 set rd [redis_deferring_client] 452 $rd brpop a 0 453 r eval { 454 redis.call("lpush",KEYS[1],"1"); 455 redis.call("lpush",KEYS[1],"2"); 456 } 1 a 457 set res [$rd read] 458 $rd close 459 wait_for_condition 50 100 { 460 [r -1 lrange a 0 -1] eq [r lrange a 0 -1] 461 } else { 462 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]'" 463 } 464 set res 465 } {a 1} 466 467 test {EVALSHA replication when first call is readonly} { 468 r del x 469 r eval {if tonumber(ARGV[1]) > 0 then redis.call('incr', KEYS[1]) end} 1 x 0 470 r evalsha 6e0e2745aa546d0b50b801a20983b70710aef3ce 1 x 0 471 r evalsha 6e0e2745aa546d0b50b801a20983b70710aef3ce 1 x 1 472 wait_for_condition 50 100 { 473 [r -1 get x] eq {1} 474 } else { 475 fail "Expected 1 in x, but value is '[r -1 get x]'" 476 } 477 } 478 479 test {Lua scripts using SELECT are replicated correctly} { 480 r eval { 481 redis.call("set","foo1","bar1") 482 redis.call("select","10") 483 redis.call("incr","x") 484 redis.call("select","11") 485 redis.call("incr","z") 486 } 0 487 r eval { 488 redis.call("set","foo1","bar1") 489 redis.call("select","10") 490 redis.call("incr","x") 491 redis.call("select","11") 492 redis.call("incr","z") 493 } 0 494 wait_for_condition 50 100 { 495 [r -1 debug digest] eq [r debug digest] 496 } else { 497 fail "Master-Slave desync after Lua script using SELECT." 498 } 499 } 500 } 501} 502