2
0

scripting.tcl 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. start_server {tags {"scripting"}} {
  2. test {EVAL - Does Lua interpreter replies to our requests?} {
  3. r eval {return 'hello'} 0
  4. } {hello}
  5. test {EVAL - Lua integer -> Redis protocol type conversion} {
  6. r eval {return 100.5} 0
  7. } {100}
  8. test {EVAL - Lua string -> Redis protocol type conversion} {
  9. r eval {return 'hello world'} 0
  10. } {hello world}
  11. test {EVAL - Lua true boolean -> Redis protocol type conversion} {
  12. r eval {return true} 0
  13. } {1}
  14. test {EVAL - Lua false boolean -> Redis protocol type conversion} {
  15. r eval {return false} 0
  16. } {}
  17. test {EVAL - Lua status code reply -> Redis protocol type conversion} {
  18. r eval {return {ok='fine'}} 0
  19. } {fine}
  20. test {EVAL - Lua error reply -> Redis protocol type conversion} {
  21. catch {
  22. r eval {return {err='this is an error'}} 0
  23. } e
  24. set _ $e
  25. } {this is an error}
  26. test {EVAL - Lua table -> Redis protocol type conversion} {
  27. r eval {return {1,2,3,'ciao',{1,2}}} 0
  28. } {1 2 3 ciao {1 2}}
  29. test {EVAL - Are the KEYS and ARGV arrays populated correctly?} {
  30. r eval {return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}} 2 a b c d
  31. } {a b c d}
  32. test {EVAL - is Lua able to call Redis API?} {
  33. r set mykey myval
  34. r eval {return redis.call('get','mykey')} 0
  35. } {myval}
  36. test {EVALSHA - Can we call a SHA1 if already defined?} {
  37. r evalsha 9bd632c7d33e571e9f24556ebed26c3479a87129 0
  38. } {myval}
  39. test {EVALSHA - Can we call a SHA1 in uppercase?} {
  40. r evalsha 9BD632C7D33E571E9F24556EBED26C3479A87129 0
  41. } {myval}
  42. test {EVALSHA - Do we get an error on invalid SHA1?} {
  43. catch {r evalsha NotValidShaSUM 0} e
  44. set _ $e
  45. } {NOSCRIPT*}
  46. test {EVALSHA - Do we get an error on non defined SHA1?} {
  47. catch {r evalsha ffd632c7d33e571e9f24556ebed26c3479a87130 0} e
  48. set _ $e
  49. } {NOSCRIPT*}
  50. test {EVAL - Redis integer -> Lua type conversion} {
  51. r eval {
  52. local foo = redis.pcall('incr','x')
  53. return {type(foo),foo}
  54. } 0
  55. } {number 1}
  56. test {EVAL - Redis bulk -> Lua type conversion} {
  57. r set mykey myval
  58. r eval {
  59. local foo = redis.pcall('get','mykey')
  60. return {type(foo),foo}
  61. } 0
  62. } {string myval}
  63. test {EVAL - Redis multi bulk -> Lua type conversion} {
  64. r del mylist
  65. r rpush mylist a
  66. r rpush mylist b
  67. r rpush mylist c
  68. r eval {
  69. local foo = redis.pcall('lrange','mylist',0,-1)
  70. return {type(foo),foo[1],foo[2],foo[3],# foo}
  71. } 0
  72. } {table a b c 3}
  73. test {EVAL - Redis status reply -> Lua type conversion} {
  74. r eval {
  75. local foo = redis.pcall('set','mykey','myval')
  76. return {type(foo),foo['ok']}
  77. } 0
  78. } {table OK}
  79. test {EVAL - Redis error reply -> Lua type conversion} {
  80. r set mykey myval
  81. r eval {
  82. local foo = redis.pcall('incr','mykey')
  83. return {type(foo),foo['err']}
  84. } 0
  85. } {table {ERR value is not an integer or out of range}}
  86. test {EVAL - Redis nil bulk reply -> Lua type conversion} {
  87. r del mykey
  88. r eval {
  89. local foo = redis.pcall('get','mykey')
  90. return {type(foo),foo == false}
  91. } 0
  92. } {boolean 1}
  93. test {EVAL - Is Lua affecting the currently selected DB?} {
  94. r set mykey "this is DB 9"
  95. r select 10
  96. r set mykey "this is DB 10"
  97. r eval {return redis.pcall('get','mykey')} 0
  98. } {this is DB 10}
  99. test {EVAL - Is Lua seleced DB retained?} {
  100. r eval {return redis.pcall('select','9')} 0
  101. r get mykey
  102. } {this is DB 9}
  103. if 0 {
  104. test {EVAL - Script can't run more than configured time limit} {
  105. r config set lua-time-limit 1
  106. catch {
  107. r eval {
  108. local i = 0
  109. while true do i=i+1 end
  110. } 0
  111. } e
  112. set _ $e
  113. } {*execution time*}
  114. }
  115. test {EVAL - Scripts can't run certain commands} {
  116. set e {}
  117. catch {r eval {return redis.pcall('spop','x')} 0} e
  118. set e
  119. } {*not allowed*}
  120. test {EVAL - Scripts can't run certain commands} {
  121. set e {}
  122. catch {
  123. r eval "redis.pcall('randomkey'); return redis.pcall('set','x','ciao')" 0
  124. } e
  125. set e
  126. } {*not allowed after*}
  127. test {EVAL - No arguments to redis.call/pcall is considered an error} {
  128. set e {}
  129. catch {r eval {return redis.call()} 0} e
  130. set e
  131. } {*one argument*}
  132. test {EVAL - redis.call variant raises a Lua error on Redis cmd error (1)} {
  133. set e {}
  134. catch {
  135. r eval "redis.call('nosuchcommand')" 0
  136. } e
  137. set e
  138. } {*Unknown Redis*}
  139. test {EVAL - redis.call variant raises a Lua error on Redis cmd error (1)} {
  140. set e {}
  141. catch {
  142. r eval "redis.call('get','a','b','c')" 0
  143. } e
  144. set e
  145. } {*number of args*}
  146. test {EVAL - redis.call variant raises a Lua error on Redis cmd error (1)} {
  147. set e {}
  148. r set foo bar
  149. catch {
  150. r eval "redis.call('lpush','foo','val')" 0
  151. } e
  152. set e
  153. } {*against a key*}
  154. test {SCRIPTING FLUSH - is able to clear the scripts cache?} {
  155. r set mykey myval
  156. set v [r evalsha 9bd632c7d33e571e9f24556ebed26c3479a87129 0]
  157. assert_equal $v myval
  158. set e ""
  159. r script flush
  160. catch {r evalsha 9bd632c7d33e571e9f24556ebed26c3479a87129 0} e
  161. set e
  162. } {NOSCRIPT*}
  163. test {SCRIPT EXISTS - can detect already defined scripts?} {
  164. r eval "return 1+1" 0
  165. r script exists a27e7e8a43702b7046d4f6a7ccf5b60cef6b9bd9 a27e7e8a43702b7046d4f6a7ccf5b60cef6b9bda
  166. } {1 0}
  167. test {SCRIPT LOAD - is able to register scripts in the scripting cache} {
  168. list \
  169. [r script load "return 'loaded'"] \
  170. [r evalsha b534286061d4b9e4026607613b95c06c06015ae8 0]
  171. } {b534286061d4b9e4026607613b95c06c06015ae8 loaded}
  172. test "In the context of Lua the output of random commands gets ordered" {
  173. r del myset
  174. 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
  175. r eval {return redis.call('smembers','myset')} 0
  176. } {a aa aaa azz b c d e f g h i l m n o p q r s t u v z}
  177. test "SORT is normally not alpha re-ordered for the scripting engine" {
  178. r del myset
  179. r sadd myset 1 2 3 4 10
  180. r eval {return redis.call('sort','myset','desc')} 0
  181. } {10 4 3 2 1}
  182. test "SORT BY <constant> output gets ordered for scripting" {
  183. r del myset
  184. 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
  185. r eval {return redis.call('sort','myset','by','_')} 0
  186. } {a aa aaa azz b c d e f g h i l m n o p q r s t u v z}
  187. test "SORT BY <constant> with GET gets ordered for scripting" {
  188. r del myset
  189. r sadd myset a b c
  190. r eval {return redis.call('sort','myset','by','_','get','#','get','_:*')} 0
  191. } {a {} b {} c {}}
  192. test "redis.sha1hex() implementation" {
  193. list [r eval {return redis.sha1hex('')} 0] \
  194. [r eval {return redis.sha1hex('Pizza & Mandolino')} 0]
  195. } {da39a3ee5e6b4b0d3255bfef95601890afd80709 74822d82031af7493c20eefa13bd07ec4fada82f}
  196. test {Globals protection reading an undeclared global variable} {
  197. catch {r eval {return a} 0} e
  198. set e
  199. } {*ERR*attempted to access unexisting global*}
  200. test {Globals protection setting an undeclared global*} {
  201. catch {r eval {a=10} 0} e
  202. set e
  203. } {*ERR*attempted to create global*}
  204. test {Test an example script DECR_IF_GT} {
  205. set decr_if_gt {
  206. local current
  207. current = redis.call('get',KEYS[1])
  208. if not current then return nil end
  209. if current > ARGV[1] then
  210. return redis.call('decr',KEYS[1])
  211. else
  212. return redis.call('get',KEYS[1])
  213. end
  214. }
  215. r set foo 5
  216. set res {}
  217. lappend res [r eval $decr_if_gt 1 foo 2]
  218. lappend res [r eval $decr_if_gt 1 foo 2]
  219. lappend res [r eval $decr_if_gt 1 foo 2]
  220. lappend res [r eval $decr_if_gt 1 foo 2]
  221. lappend res [r eval $decr_if_gt 1 foo 2]
  222. set res
  223. } {4 3 2 2 2}
  224. test {Scripting engine resets PRNG at every script execution} {
  225. set rand1 [r eval {return tostring(math.random())} 0]
  226. set rand2 [r eval {return tostring(math.random())} 0]
  227. assert_equal $rand1 $rand2
  228. }
  229. test {Scripting engine PRNG can be seeded correctly} {
  230. set rand1 [r eval {
  231. math.randomseed(ARGV[1]); return tostring(math.random())
  232. } 0 10]
  233. set rand2 [r eval {
  234. math.randomseed(ARGV[1]); return tostring(math.random())
  235. } 0 10]
  236. set rand3 [r eval {
  237. math.randomseed(ARGV[1]); return tostring(math.random())
  238. } 0 20]
  239. assert_equal $rand1 $rand2
  240. assert {$rand2 ne $rand3}
  241. }
  242. test {EVAL does not leak in the Lua stack} {
  243. r set x 0
  244. # Use a non blocking client to speedup the loop.
  245. set rd [redis_deferring_client]
  246. for {set j 0} {$j < 10000} {incr j} {
  247. $rd eval {return redis.call("incr",KEYS[1])} 1 x
  248. }
  249. for {set j 0} {$j < 10000} {incr j} {
  250. $rd read
  251. }
  252. assert {[s used_memory_lua] < 1024*100}
  253. $rd close
  254. r get x
  255. } {10000}
  256. test {EVAL processes writes from AOF in read-only slaves} {
  257. r flushall
  258. r config set appendonly yes
  259. r eval {redis.call("set","foo","100")} 0
  260. r eval {redis.call("incr","foo")} 0
  261. r eval {redis.call("incr","foo")} 0
  262. wait_for_condition 50 100 {
  263. [s aof_rewrite_in_progress] == 0
  264. } else {
  265. fail "AOF rewrite can't complete after CONFIG SET appendonly yes."
  266. }
  267. r config set slave-read-only yes
  268. r slaveof 127.0.0.1 0
  269. r debug loadaof
  270. set res [r get foo]
  271. r slaveof no one
  272. set res
  273. } {102}
  274. test {We can call scripts rewriting client->argv from Lua} {
  275. r del myset
  276. r sadd myset a b c
  277. r mset a 1 b 2 c 3 d 4
  278. assert {[r spop myset] ne {}}
  279. assert {[r spop myset] ne {}}
  280. assert {[r spop myset] ne {}}
  281. assert {[r mget a b c d] eq {1 2 3 4}}
  282. assert {[r spop myset] eq {}}
  283. }
  284. }
  285. # Start a new server since the last test in this stanza will kill the
  286. # instance at all.
  287. start_server {tags {"scripting"}} {
  288. test {Timedout read-only scripts can be killed by SCRIPT KILL} {
  289. set rd [redis_deferring_client]
  290. r config set lua-time-limit 10
  291. $rd eval {while true do end} 0
  292. after 200
  293. catch {r ping} e
  294. assert_match {BUSY*} $e
  295. r script kill
  296. after 200 ; # Give some time to Lua to call the hook again...
  297. assert_equal [r ping] "PONG"
  298. }
  299. test {Timedout script link is still usable after Lua returns} {
  300. r config set lua-time-limit 10
  301. r eval {for i=1,100000 do redis.call('ping') end return 'ok'} 0
  302. r ping
  303. } {PONG}
  304. test {Timedout scripts that modified data can't be killed by SCRIPT KILL} {
  305. set rd [redis_deferring_client]
  306. r config set lua-time-limit 10
  307. $rd eval {redis.call('set','x','y'); while true do end} 0
  308. after 200
  309. catch {r ping} e
  310. assert_match {BUSY*} $e
  311. catch {r script kill} e
  312. assert_match {UNKILLABLE*} $e
  313. catch {r ping} e
  314. assert_match {BUSY*} $e
  315. }
  316. # Note: keep this test at the end of this server stanza because it
  317. # kills the server.
  318. test {SHUTDOWN NOSAVE can kill a timedout script anyway} {
  319. # The server sould be still unresponding to normal commands.
  320. catch {r ping} e
  321. assert_match {BUSY*} $e
  322. catch {r shutdown nosave}
  323. # Make sure the server was killed
  324. catch {set rd [redis_deferring_client]} e
  325. assert_match {*connection refused*} $e
  326. }
  327. }
  328. start_server {tags {"scripting repl"}} {
  329. start_server {} {
  330. test {Before the slave connects we issue two EVAL commands} {
  331. # One with an error, but still executing a command.
  332. # SHA is: 6e8bd6bdccbe78899e3cc06b31b6dbf4324c2e56
  333. catch {
  334. r eval {redis.call('incr','x'); redis.call('nonexisting')} 0
  335. }
  336. # One command is correct:
  337. # SHA is: ae3477e27be955de7e1bc9adfdca626b478d3cb2
  338. r eval {return redis.call('incr','x')} 0
  339. } {2}
  340. test {Connect a slave to the main instance} {
  341. r -1 slaveof [srv 0 host] [srv 0 port]
  342. wait_for_condition 50 100 {
  343. [s -1 role] eq {slave} &&
  344. [string match {*master_link_status:up*} [r -1 info replication]]
  345. } else {
  346. fail "Can't turn the instance into a slave"
  347. }
  348. }
  349. test {Now use EVALSHA against the master, with both SHAs} {
  350. # The server should replicate successful and unsuccessful
  351. # commands as EVAL instead of EVALSHA.
  352. catch {
  353. r evalsha 6e8bd6bdccbe78899e3cc06b31b6dbf4324c2e56 0
  354. }
  355. r evalsha ae3477e27be955de7e1bc9adfdca626b478d3cb2 0
  356. } {4}
  357. test {If EVALSHA was replicated as EVAL, 'x' should be '4'} {
  358. wait_for_condition 50 100 {
  359. [r -1 get x] eq {4}
  360. } else {
  361. fail "Expected 4 in x, but value is '[r -1 get x]'"
  362. }
  363. }
  364. test {Replication of script multiple pushes to list with BLPOP} {
  365. set rd [redis_deferring_client]
  366. $rd brpop a 0
  367. r eval {
  368. redis.call("lpush","a","1");
  369. redis.call("lpush","a","2");
  370. } 0
  371. set res [$rd read]
  372. $rd close
  373. wait_for_condition 50 100 {
  374. [r -1 lrange a 0 -1] eq [r lrange a 0 -1]
  375. } else {
  376. 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]'"
  377. }
  378. set res
  379. } {a 1}
  380. test {EVALSHA replication when first call is readonly} {
  381. r del x
  382. r eval {if tonumber(KEYS[1]) > 0 then redis.call('incr', 'x') end} 1 0
  383. r evalsha 38fe3ddf5284a1d48f37f824b4c4e826879f3cb9 1 0
  384. r evalsha 38fe3ddf5284a1d48f37f824b4c4e826879f3cb9 1 1
  385. wait_for_condition 50 100 {
  386. [r -1 get x] eq {1}
  387. } else {
  388. fail "Expected 1 in x, but value is '[r -1 get x]'"
  389. }
  390. }
  391. }
  392. }