scripting.tcl 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  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{t} b{t} c{t} d{t}
  31. } {a{t} b{t} c{t} d{t}}
  32. test {EVAL - is Lua able to call Redis API?} {
  33. r set mykey myval
  34. r eval {return redis.call('get',KEYS[1])} 1 mykey
  35. } {myval}
  36. test {EVALSHA - Can we call a SHA1 if already defined?} {
  37. r evalsha fd758d1589d044dd850a6f05d52f2eefd27f033f 1 mykey
  38. } {myval}
  39. test {EVALSHA - Can we call a SHA1 in uppercase?} {
  40. r evalsha FD758D1589D044DD850A6F05D52F2EEFD27F033F 1 mykey
  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 set x 0
  52. r eval {
  53. local foo = redis.pcall('incr',KEYS[1])
  54. return {type(foo),foo}
  55. } 1 x
  56. } {number 1}
  57. test {EVAL - Redis bulk -> Lua type conversion} {
  58. r set mykey myval
  59. r eval {
  60. local foo = redis.pcall('get',KEYS[1])
  61. return {type(foo),foo}
  62. } 1 mykey
  63. } {string myval}
  64. test {EVAL - Redis multi bulk -> Lua type conversion} {
  65. r del mylist
  66. r rpush mylist a
  67. r rpush mylist b
  68. r rpush mylist c
  69. r eval {
  70. local foo = redis.pcall('lrange',KEYS[1],0,-1)
  71. return {type(foo),foo[1],foo[2],foo[3],# foo}
  72. } 1 mylist
  73. } {table a b c 3}
  74. test {EVAL - Redis status reply -> Lua type conversion} {
  75. r eval {
  76. local foo = redis.pcall('set',KEYS[1],'myval')
  77. return {type(foo),foo['ok']}
  78. } 1 mykey
  79. } {table OK}
  80. test {EVAL - Redis error reply -> Lua type conversion} {
  81. r set mykey myval
  82. r eval {
  83. local foo = redis.pcall('incr',KEYS[1])
  84. return {type(foo),foo['err']}
  85. } 1 mykey
  86. } {table {ERR value is not an integer or out of range}}
  87. test {EVAL - Redis nil bulk reply -> Lua type conversion} {
  88. r del mykey
  89. r eval {
  90. local foo = redis.pcall('get',KEYS[1])
  91. return {type(foo),foo == false}
  92. } 1 mykey
  93. } {boolean 1}
  94. test {EVAL - Is the Lua client using the currently selected DB?} {
  95. r set mykey "this is DB 9"
  96. r select 10
  97. r set mykey "this is DB 10"
  98. r eval {return redis.pcall('get',KEYS[1])} 1 mykey
  99. } {this is DB 10} {singledb:skip}
  100. test {EVAL - SELECT inside Lua should not affect the caller} {
  101. # here we DB 10 is selected
  102. r set mykey "original value"
  103. r eval {return redis.pcall('select','9')} 0
  104. set res [r get mykey]
  105. r select 9
  106. set res
  107. } {original value} {singledb:skip}
  108. if 0 {
  109. test {EVAL - Script can't run more than configured time limit} {
  110. r config set lua-time-limit 1
  111. catch {
  112. r eval {
  113. local i = 0
  114. while true do i=i+1 end
  115. } 0
  116. } e
  117. set _ $e
  118. } {*execution time*}
  119. }
  120. test {EVAL - Scripts can't run blpop command} {
  121. set e {}
  122. catch {r eval {return redis.pcall('blpop','x',0)} 0} e
  123. set e
  124. } {*not allowed*}
  125. test {EVAL - Scripts can't run brpop command} {
  126. set e {}
  127. catch {r eval {return redis.pcall('brpop','empty_list',0)} 0} e
  128. set e
  129. } {*not allowed*}
  130. test {EVAL - Scripts can't run brpoplpush command} {
  131. set e {}
  132. catch {r eval {return redis.pcall('brpoplpush','empty_list1', 'empty_list2',0)} 0} e
  133. set e
  134. } {*not allowed*}
  135. test {EVAL - Scripts can't run blmove command} {
  136. set e {}
  137. catch {r eval {return redis.pcall('blmove','empty_list1', 'empty_list2', 'LEFT', 'LEFT', 0)} 0} e
  138. set e
  139. } {*not allowed*}
  140. test {EVAL - Scripts can't run bzpopmin command} {
  141. set e {}
  142. catch {r eval {return redis.pcall('bzpopmin','empty_zset', 0)} 0} e
  143. set e
  144. } {*not allowed*}
  145. test {EVAL - Scripts can't run bzpopmax command} {
  146. set e {}
  147. catch {r eval {return redis.pcall('bzpopmax','empty_zset', 0)} 0} e
  148. set e
  149. } {*not allowed*}
  150. test {EVAL - Scripts can't run XREAD and XREADGROUP with BLOCK option} {
  151. r del s
  152. r xgroup create s g $ MKSTREAM
  153. set res [r eval {return redis.pcall('xread','STREAMS','s','$')} 1 s]
  154. assert {$res eq {}}
  155. assert_error "*xread command is not allowed with BLOCK option from scripts" {r eval {return redis.pcall('xread','BLOCK',0,'STREAMS','s','$')} 1 s}
  156. set res [r eval {return redis.pcall('xreadgroup','group','g','c','STREAMS','s','>')} 1 s]
  157. assert {$res eq {}}
  158. assert_error "*xreadgroup command is not allowed with BLOCK option from scripts" {r eval {return redis.pcall('xreadgroup','group','g','c','BLOCK',0,'STREAMS','s','>')} 1 s}
  159. }
  160. test {EVAL - Scripts can't run certain commands} {
  161. set e {}
  162. r debug lua-always-replicate-commands 0
  163. catch {
  164. r eval "redis.pcall('randomkey'); return redis.pcall('set','x','ciao')" 0
  165. } e
  166. r debug lua-always-replicate-commands 1
  167. set e
  168. } {*not allowed after*} {needs:debug}
  169. test {EVAL - No arguments to redis.call/pcall is considered an error} {
  170. set e {}
  171. catch {r eval {return redis.call()} 0} e
  172. set e
  173. } {*one argument*}
  174. test {EVAL - redis.call variant raises a Lua error on Redis cmd error (1)} {
  175. set e {}
  176. catch {
  177. r eval "redis.call('nosuchcommand')" 0
  178. } e
  179. set e
  180. } {*Unknown Redis*}
  181. test {EVAL - redis.call variant raises a Lua error on Redis cmd error (1)} {
  182. set e {}
  183. catch {
  184. r eval "redis.call('get','a','b','c')" 0
  185. } e
  186. set e
  187. } {*number of args*}
  188. test {EVAL - redis.call variant raises a Lua error on Redis cmd error (1)} {
  189. set e {}
  190. r set foo bar
  191. catch {
  192. r eval {redis.call('lpush',KEYS[1],'val')} 1 foo
  193. } e
  194. set e
  195. } {*against a key*}
  196. test {EVAL - JSON numeric decoding} {
  197. # We must return the table as a string because otherwise
  198. # Redis converts floats to ints and we get 0 and 1023 instead
  199. # of 0.0003 and 1023.2 as the parsed output.
  200. r eval {return
  201. table.concat(
  202. cjson.decode(
  203. "[0.0, -5e3, -1, 0.3e-3, 1023.2, 0e10]"), " ")
  204. } 0
  205. } {0 -5000 -1 0.0003 1023.2 0}
  206. test {EVAL - JSON string decoding} {
  207. r eval {local decoded = cjson.decode('{"keya": "a", "keyb": "b"}')
  208. return {decoded.keya, decoded.keyb}
  209. } 0
  210. } {a b}
  211. test {EVAL - cmsgpack can pack double?} {
  212. r eval {local encoded = cmsgpack.pack(0.1)
  213. local h = ""
  214. for i = 1, #encoded do
  215. h = h .. string.format("%02x",string.byte(encoded,i))
  216. end
  217. return h
  218. } 0
  219. } {cb3fb999999999999a}
  220. test {EVAL - cmsgpack can pack negative int64?} {
  221. r eval {local encoded = cmsgpack.pack(-1099511627776)
  222. local h = ""
  223. for i = 1, #encoded do
  224. h = h .. string.format("%02x",string.byte(encoded,i))
  225. end
  226. return h
  227. } 0
  228. } {d3ffffff0000000000}
  229. test {EVAL - cmsgpack can pack and unpack circular references?} {
  230. r eval {local a = {x=nil,y=5}
  231. local b = {x=a}
  232. a['x'] = b
  233. local encoded = cmsgpack.pack(a)
  234. local h = ""
  235. -- cmsgpack encodes to a depth of 16, but can't encode
  236. -- references, so the encoded object has a deep copy recursive
  237. -- depth of 16.
  238. for i = 1, #encoded do
  239. h = h .. string.format("%02x",string.byte(encoded,i))
  240. end
  241. -- when unpacked, re.x.x != re because the unpack creates
  242. -- individual tables down to a depth of 16.
  243. -- (that's why the encoded output is so large)
  244. local re = cmsgpack.unpack(encoded)
  245. assert(re)
  246. assert(re.x)
  247. assert(re.x.x.y == re.y)
  248. assert(re.x.x.x.x.y == re.y)
  249. assert(re.x.x.x.x.x.x.y == re.y)
  250. assert(re.x.x.x.x.x.x.x.x.x.x.y == re.y)
  251. -- maximum working depth:
  252. assert(re.x.x.x.x.x.x.x.x.x.x.x.x.x.x.y == re.y)
  253. -- now the last x would be b above and has no y
  254. assert(re.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x)
  255. -- so, the final x.x is at the depth limit and was assigned nil
  256. assert(re.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x == nil)
  257. return {h, re.x.x.x.x.x.x.x.x.y == re.y, re.y == 5}
  258. } 0
  259. } {82a17905a17881a17882a17905a17881a17882a17905a17881a17882a17905a17881a17882a17905a17881a17882a17905a17881a17882a17905a17881a17882a17905a17881a178c0 1 1}
  260. test {EVAL - Numerical sanity check from bitop} {
  261. r eval {assert(0x7fffffff == 2147483647, "broken hex literals");
  262. assert(0xffffffff == -1 or 0xffffffff == 2^32-1,
  263. "broken hex literals");
  264. assert(tostring(-1) == "-1", "broken tostring()");
  265. assert(tostring(0xffffffff) == "-1" or
  266. tostring(0xffffffff) == "4294967295",
  267. "broken tostring()")
  268. } 0
  269. } {}
  270. test {EVAL - Verify minimal bitop functionality} {
  271. r eval {assert(bit.tobit(1) == 1);
  272. assert(bit.band(1) == 1);
  273. assert(bit.bxor(1,2) == 3);
  274. assert(bit.bor(1,2,4,8,16,32,64,128) == 255)
  275. } 0
  276. } {}
  277. test {EVAL - Able to parse trailing comments} {
  278. r eval {return 'hello' --trailing comment} 0
  279. } {hello}
  280. test {EVAL_RO - Successful case} {
  281. r set foo bar
  282. assert_equal bar [r eval_ro {return redis.call('get', KEYS[1]);} 1 foo]
  283. }
  284. test {EVAL_RO - Cannot run write commands} {
  285. r set foo bar
  286. catch {r eval_ro {redis.call('del', KEYS[1]);} 1 foo} e
  287. set e
  288. } {*Write commands are not allowed from read-only scripts*}
  289. test {SCRIPTING FLUSH - is able to clear the scripts cache?} {
  290. r set mykey myval
  291. set v [r evalsha fd758d1589d044dd850a6f05d52f2eefd27f033f 1 mykey]
  292. assert_equal $v myval
  293. set e ""
  294. r script flush
  295. catch {r evalsha fd758d1589d044dd850a6f05d52f2eefd27f033f 1 mykey} e
  296. set e
  297. } {NOSCRIPT*}
  298. test {SCRIPTING FLUSH ASYNC} {
  299. for {set j 0} {$j < 100} {incr j} {
  300. r script load "return $j"
  301. }
  302. assert { [string match "*number_of_cached_scripts:100*" [r info Memory]] }
  303. r script flush async
  304. assert { [string match "*number_of_cached_scripts:0*" [r info Memory]] }
  305. }
  306. test {SCRIPT EXISTS - can detect already defined scripts?} {
  307. r eval "return 1+1" 0
  308. r script exists a27e7e8a43702b7046d4f6a7ccf5b60cef6b9bd9 a27e7e8a43702b7046d4f6a7ccf5b60cef6b9bda
  309. } {1 0}
  310. test {SCRIPT LOAD - is able to register scripts in the scripting cache} {
  311. list \
  312. [r script load "return 'loaded'"] \
  313. [r evalsha b534286061d4b9e4026607613b95c06c06015ae8 0]
  314. } {b534286061d4b9e4026607613b95c06c06015ae8 loaded}
  315. test "In the context of Lua the output of random commands gets ordered" {
  316. r debug lua-always-replicate-commands 0
  317. r del myset
  318. 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
  319. set res [r eval {return redis.call('smembers',KEYS[1])} 1 myset]
  320. r debug lua-always-replicate-commands 1
  321. set res
  322. } {a aa aaa azz b c d e f g h i l m n o p q r s t u v z} {needs:debug}
  323. test "SORT is normally not alpha re-ordered for the scripting engine" {
  324. r del myset
  325. r sadd myset 1 2 3 4 10
  326. r eval {return redis.call('sort',KEYS[1],'desc')} 1 myset
  327. } {10 4 3 2 1} {cluster:skip}
  328. test "SORT BY <constant> output gets ordered for scripting" {
  329. r del myset
  330. 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
  331. r eval {return redis.call('sort',KEYS[1],'by','_')} 1 myset
  332. } {a aa aaa azz b c d e f g h i l m n o p q r s t u v z} {cluster:skip}
  333. test "SORT BY <constant> with GET gets ordered for scripting" {
  334. r del myset
  335. r sadd myset a b c
  336. r eval {return redis.call('sort',KEYS[1],'by','_','get','#','get','_:*')} 1 myset
  337. } {a {} b {} c {}} {cluster:skip}
  338. test "redis.sha1hex() implementation" {
  339. list [r eval {return redis.sha1hex('')} 0] \
  340. [r eval {return redis.sha1hex('Pizza & Mandolino')} 0]
  341. } {da39a3ee5e6b4b0d3255bfef95601890afd80709 74822d82031af7493c20eefa13bd07ec4fada82f}
  342. test {Globals protection reading an undeclared global variable} {
  343. catch {r eval {return a} 0} e
  344. set e
  345. } {*ERR*attempted to access * global*}
  346. test {Globals protection setting an undeclared global*} {
  347. catch {r eval {a=10} 0} e
  348. set e
  349. } {*ERR*attempted to create global*}
  350. test {Test an example script DECR_IF_GT} {
  351. set decr_if_gt {
  352. local current
  353. current = redis.call('get',KEYS[1])
  354. if not current then return nil end
  355. if current > ARGV[1] then
  356. return redis.call('decr',KEYS[1])
  357. else
  358. return redis.call('get',KEYS[1])
  359. end
  360. }
  361. r set foo 5
  362. set res {}
  363. lappend res [r eval $decr_if_gt 1 foo 2]
  364. lappend res [r eval $decr_if_gt 1 foo 2]
  365. lappend res [r eval $decr_if_gt 1 foo 2]
  366. lappend res [r eval $decr_if_gt 1 foo 2]
  367. lappend res [r eval $decr_if_gt 1 foo 2]
  368. set res
  369. } {4 3 2 2 2}
  370. test {Scripting engine resets PRNG at every script execution} {
  371. set rand1 [r eval {return tostring(math.random())} 0]
  372. set rand2 [r eval {return tostring(math.random())} 0]
  373. assert_equal $rand1 $rand2
  374. }
  375. test {Scripting engine PRNG can be seeded correctly} {
  376. set rand1 [r eval {
  377. math.randomseed(ARGV[1]); return tostring(math.random())
  378. } 0 10]
  379. set rand2 [r eval {
  380. math.randomseed(ARGV[1]); return tostring(math.random())
  381. } 0 10]
  382. set rand3 [r eval {
  383. math.randomseed(ARGV[1]); return tostring(math.random())
  384. } 0 20]
  385. assert_equal $rand1 $rand2
  386. assert {$rand2 ne $rand3}
  387. }
  388. test {EVAL does not leak in the Lua stack} {
  389. r set x 0
  390. # Use a non blocking client to speedup the loop.
  391. set rd [redis_deferring_client]
  392. for {set j 0} {$j < 10000} {incr j} {
  393. $rd eval {return redis.call("incr",KEYS[1])} 1 x
  394. }
  395. for {set j 0} {$j < 10000} {incr j} {
  396. $rd read
  397. }
  398. assert {[s used_memory_lua] < 1024*100}
  399. $rd close
  400. r get x
  401. } {10000}
  402. test {EVAL processes writes from AOF in read-only slaves} {
  403. r flushall
  404. r config set appendonly yes
  405. r config set aof-use-rdb-preamble no
  406. r eval {redis.call("set",KEYS[1],"100")} 1 foo
  407. r eval {redis.call("incr",KEYS[1])} 1 foo
  408. r eval {redis.call("incr",KEYS[1])} 1 foo
  409. wait_for_condition 50 100 {
  410. [s aof_rewrite_in_progress] == 0
  411. } else {
  412. fail "AOF rewrite can't complete after CONFIG SET appendonly yes."
  413. }
  414. r config set slave-read-only yes
  415. r slaveof 127.0.0.1 0
  416. r debug loadaof
  417. set res [r get foo]
  418. r slaveof no one
  419. r config set aof-use-rdb-preamble yes
  420. set res
  421. } {102} {external:skip}
  422. test {EVAL timeout from AOF} {
  423. # generate a long running script that is propagated to the AOF as script
  424. # make sure that the script times out during loading
  425. r config set appendonly no
  426. r config set aof-use-rdb-preamble no
  427. r config set lua-replicate-commands no
  428. r flushall
  429. r config set appendonly yes
  430. wait_for_condition 50 100 {
  431. [s aof_rewrite_in_progress] == 0
  432. } else {
  433. fail "AOF rewrite can't complete after CONFIG SET appendonly yes."
  434. }
  435. r config set lua-time-limit 1
  436. set rd [redis_deferring_client]
  437. set start [clock clicks -milliseconds]
  438. $rd eval {redis.call('set',KEYS[1],'y'); for i=1,1500000 do redis.call('ping') end return 'ok'} 1 x
  439. $rd flush
  440. after 100
  441. catch {r ping} err
  442. assert_match {BUSY*} $err
  443. $rd read
  444. set elapsed [expr [clock clicks -milliseconds]-$start]
  445. if {$::verbose} { puts "script took $elapsed milliseconds" }
  446. set start [clock clicks -milliseconds]
  447. $rd debug loadaof
  448. $rd flush
  449. after 100
  450. catch {r ping} err
  451. assert_match {LOADING*} $err
  452. $rd read
  453. set elapsed [expr [clock clicks -milliseconds]-$start]
  454. if {$::verbose} { puts "loading took $elapsed milliseconds" }
  455. $rd close
  456. r get x
  457. } {y} {external:skip}
  458. test {We can call scripts rewriting client->argv from Lua} {
  459. r del myset
  460. r sadd myset a b c
  461. r mset a{t} 1 b{t} 2 c{t} 3 d{t} 4
  462. assert {[r spop myset] ne {}}
  463. assert {[r spop myset 1] ne {}}
  464. assert {[r spop myset] ne {}}
  465. assert {[r mget a{t} b{t} c{t} d{t}] eq {1 2 3 4}}
  466. assert {[r spop myset] eq {}}
  467. }
  468. test {Call Redis command with many args from Lua (issue #1764)} {
  469. r eval {
  470. local i
  471. local x={}
  472. redis.call('del','mylist')
  473. for i=1,100 do
  474. table.insert(x,i)
  475. end
  476. redis.call('rpush','mylist',unpack(x))
  477. return redis.call('lrange','mylist',0,-1)
  478. } 1 mylist
  479. } {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}
  480. test {Number conversion precision test (issue #1118)} {
  481. r eval {
  482. local value = 9007199254740991
  483. redis.call("set","foo",value)
  484. return redis.call("get","foo")
  485. } 1 foo
  486. } {9007199254740991}
  487. test {String containing number precision test (regression of issue #1118)} {
  488. r eval {
  489. redis.call("set", "key", "12039611435714932082")
  490. return redis.call("get", "key")
  491. } 1 key
  492. } {12039611435714932082}
  493. test {Verify negative arg count is error instead of crash (issue #1842)} {
  494. catch { r eval { return "hello" } -12 } e
  495. set e
  496. } {ERR Number of keys can't be negative}
  497. test {Correct handling of reused argv (issue #1939)} {
  498. r eval {
  499. for i = 0, 10 do
  500. redis.call('SET', 'a{t}', '1')
  501. redis.call('MGET', 'a{t}', 'b{t}', 'c{t}')
  502. redis.call('EXPIRE', 'a{t}', 0)
  503. redis.call('GET', 'a{t}')
  504. redis.call('MGET', 'a{t}', 'b{t}', 'c{t}')
  505. end
  506. } 3 a{t} b{t} c{t}
  507. }
  508. test {Functions in the Redis namespace are able to report errors} {
  509. catch {
  510. r eval {
  511. redis.sha1hex()
  512. } 0
  513. } e
  514. set e
  515. } {*wrong number*}
  516. test {Script with RESP3 map} {
  517. set expected_dict [dict create field value]
  518. set expected_list [list field value]
  519. # Sanity test for RESP3 without scripts
  520. r HELLO 3
  521. r hset hash field value
  522. set res [r hgetall hash]
  523. assert_equal $res $expected_dict
  524. # Test RESP3 client with script in both RESP2 and RESP3 modes
  525. set res [r eval {redis.setresp(3); return redis.call('hgetall', KEYS[1])} 1 hash]
  526. assert_equal $res $expected_dict
  527. set res [r eval {redis.setresp(2); return redis.call('hgetall', KEYS[1])} 1 hash]
  528. assert_equal $res $expected_list
  529. # Test RESP2 client with script in both RESP2 and RESP3 modes
  530. r HELLO 2
  531. set res [r eval {redis.setresp(3); return redis.call('hgetall', KEYS[1])} 1 hash]
  532. assert_equal $res $expected_list
  533. set res [r eval {redis.setresp(2); return redis.call('hgetall', KEYS[1])} 1 hash]
  534. assert_equal $res $expected_list
  535. }
  536. test {Script return recursive object} {
  537. r readraw 1
  538. set res [r eval {local a = {}; local b = {a}; a[1] = b; return a} 0]
  539. # drain the response
  540. while {true} {
  541. if {$res == "-ERR reached lua stack limit"} {
  542. break
  543. }
  544. assert_equal $res "*1"
  545. set res [r read]
  546. }
  547. r readraw 0
  548. # make sure the connection is still valid
  549. assert_equal [r ping] {PONG}
  550. }
  551. }
  552. # Start a new server since the last test in this stanza will kill the
  553. # instance at all.
  554. start_server {tags {"scripting"}} {
  555. test {Timedout read-only scripts can be killed by SCRIPT KILL} {
  556. set rd [redis_deferring_client]
  557. r config set lua-time-limit 10
  558. $rd eval {while true do end} 0
  559. after 200
  560. catch {r ping} e
  561. assert_match {BUSY*} $e
  562. r script kill
  563. after 200 ; # Give some time to Lua to call the hook again...
  564. assert_equal [r ping] "PONG"
  565. }
  566. test {Timedout read-only scripts can be killed by SCRIPT KILL even when use pcall} {
  567. set rd [redis_deferring_client]
  568. r config set lua-time-limit 10
  569. $rd eval {local f = function() while 1 do redis.call('ping') end end while 1 do pcall(f) end} 0
  570. wait_for_condition 50 100 {
  571. [catch {r ping} e] == 1
  572. } else {
  573. fail "Can't wait for script to start running"
  574. }
  575. catch {r ping} e
  576. assert_match {BUSY*} $e
  577. r script kill
  578. wait_for_condition 50 100 {
  579. [catch {r ping} e] == 0
  580. } else {
  581. fail "Can't wait for script to be killed"
  582. }
  583. assert_equal [r ping] "PONG"
  584. catch {$rd read} res
  585. $rd close
  586. assert_match {*killed by user*} $res
  587. }
  588. test {Timedout script does not cause a false dead client} {
  589. set rd [redis_deferring_client]
  590. r config set lua-time-limit 10
  591. # senging (in a pipeline):
  592. # 1. eval "while 1 do redis.call('ping') end" 0
  593. # 2. ping
  594. set buf "*3\r\n\$4\r\neval\r\n\$33\r\nwhile 1 do redis.call('ping') end\r\n\$1\r\n0\r\n"
  595. append buf "*1\r\n\$4\r\nping\r\n"
  596. $rd write $buf
  597. $rd flush
  598. wait_for_condition 50 100 {
  599. [catch {r ping} e] == 1
  600. } else {
  601. fail "Can't wait for script to start running"
  602. }
  603. catch {r ping} e
  604. assert_match {BUSY*} $e
  605. r script kill
  606. wait_for_condition 50 100 {
  607. [catch {r ping} e] == 0
  608. } else {
  609. fail "Can't wait for script to be killed"
  610. }
  611. assert_equal [r ping] "PONG"
  612. catch {$rd read} res
  613. assert_match {*killed by user*} $res
  614. set res [$rd read]
  615. assert_match {*PONG*} $res
  616. $rd close
  617. }
  618. test {Timedout script link is still usable after Lua returns} {
  619. r config set lua-time-limit 10
  620. r eval {for i=1,100000 do redis.call('ping') end return 'ok'} 0
  621. r ping
  622. } {PONG}
  623. test {Timedout scripts that modified data can't be killed by SCRIPT KILL} {
  624. set rd [redis_deferring_client]
  625. r config set lua-time-limit 10
  626. $rd eval {redis.call('set',KEYS[1],'y'); while true do end} 1 x
  627. after 200
  628. catch {r ping} e
  629. assert_match {BUSY*} $e
  630. catch {r script kill} e
  631. assert_match {UNKILLABLE*} $e
  632. catch {r ping} e
  633. assert_match {BUSY*} $e
  634. } {} {external:skip}
  635. # Note: keep this test at the end of this server stanza because it
  636. # kills the server.
  637. test {SHUTDOWN NOSAVE can kill a timedout script anyway} {
  638. # The server should be still unresponding to normal commands.
  639. catch {r ping} e
  640. assert_match {BUSY*} $e
  641. catch {r shutdown nosave}
  642. # Make sure the server was killed
  643. catch {set rd [redis_deferring_client]} e
  644. assert_match {*connection refused*} $e
  645. } {} {external:skip}
  646. }
  647. foreach cmdrepl {0 1} {
  648. start_server {tags {"scripting repl needs:debug external:skip"}} {
  649. start_server {} {
  650. if {$cmdrepl == 1} {
  651. set rt "(commands replication)"
  652. } else {
  653. set rt "(scripts replication)"
  654. r debug lua-always-replicate-commands 1
  655. }
  656. test "Before the replica connects we issue two EVAL commands $rt" {
  657. # One with an error, but still executing a command.
  658. # SHA is: 67164fc43fa971f76fd1aaeeaf60c1c178d25876
  659. catch {
  660. r eval {redis.call('incr',KEYS[1]); redis.call('nonexisting')} 1 x
  661. }
  662. # One command is correct:
  663. # SHA is: 6f5ade10a69975e903c6d07b10ea44c6382381a5
  664. r eval {return redis.call('incr',KEYS[1])} 1 x
  665. } {2}
  666. test "Connect a replica to the master instance $rt" {
  667. r -1 slaveof [srv 0 host] [srv 0 port]
  668. wait_for_condition 50 100 {
  669. [s -1 role] eq {slave} &&
  670. [string match {*master_link_status:up*} [r -1 info replication]]
  671. } else {
  672. fail "Can't turn the instance into a replica"
  673. }
  674. }
  675. test "Now use EVALSHA against the master, with both SHAs $rt" {
  676. # The server should replicate successful and unsuccessful
  677. # commands as EVAL instead of EVALSHA.
  678. catch {
  679. r evalsha 67164fc43fa971f76fd1aaeeaf60c1c178d25876 1 x
  680. }
  681. r evalsha 6f5ade10a69975e903c6d07b10ea44c6382381a5 1 x
  682. } {4}
  683. test "If EVALSHA was replicated as EVAL, 'x' should be '4' $rt" {
  684. wait_for_condition 50 100 {
  685. [r -1 get x] eq {4}
  686. } else {
  687. fail "Expected 4 in x, but value is '[r -1 get x]'"
  688. }
  689. }
  690. test "Replication of script multiple pushes to list with BLPOP $rt" {
  691. set rd [redis_deferring_client]
  692. $rd brpop a 0
  693. r eval {
  694. redis.call("lpush",KEYS[1],"1");
  695. redis.call("lpush",KEYS[1],"2");
  696. } 1 a
  697. set res [$rd read]
  698. $rd close
  699. wait_for_condition 50 100 {
  700. [r -1 lrange a 0 -1] eq [r lrange a 0 -1]
  701. } else {
  702. fail "Expected list 'a' in replica and master to be the same, but they are respectively '[r -1 lrange a 0 -1]' and '[r lrange a 0 -1]'"
  703. }
  704. set res
  705. } {a 1}
  706. test "EVALSHA replication when first call is readonly $rt" {
  707. r del x
  708. r eval {if tonumber(ARGV[1]) > 0 then redis.call('incr', KEYS[1]) end} 1 x 0
  709. r evalsha 6e0e2745aa546d0b50b801a20983b70710aef3ce 1 x 0
  710. r evalsha 6e0e2745aa546d0b50b801a20983b70710aef3ce 1 x 1
  711. wait_for_condition 50 100 {
  712. [r -1 get x] eq {1}
  713. } else {
  714. fail "Expected 1 in x, but value is '[r -1 get x]'"
  715. }
  716. }
  717. test "Lua scripts using SELECT are replicated correctly $rt" {
  718. r eval {
  719. redis.call("set","foo1","bar1")
  720. redis.call("select","10")
  721. redis.call("incr","x")
  722. redis.call("select","11")
  723. redis.call("incr","z")
  724. } 0
  725. r eval {
  726. redis.call("set","foo1","bar1")
  727. redis.call("select","10")
  728. redis.call("incr","x")
  729. redis.call("select","11")
  730. redis.call("incr","z")
  731. } 0
  732. wait_for_condition 50 100 {
  733. [r -1 debug digest] eq [r debug digest]
  734. } else {
  735. fail "Master-Replica desync after Lua script using SELECT."
  736. }
  737. } {} {singledb:skip}
  738. }
  739. }
  740. }
  741. start_server {tags {"scripting repl external:skip"}} {
  742. start_server {overrides {appendonly yes aof-use-rdb-preamble no}} {
  743. test "Connect a replica to the master instance" {
  744. r -1 slaveof [srv 0 host] [srv 0 port]
  745. wait_for_condition 50 100 {
  746. [s -1 role] eq {slave} &&
  747. [string match {*master_link_status:up*} [r -1 info replication]]
  748. } else {
  749. fail "Can't turn the instance into a replica"
  750. }
  751. }
  752. test "Redis.replicate_commands() must be issued before any write" {
  753. r eval {
  754. redis.call('set','foo','bar');
  755. return redis.replicate_commands();
  756. } 0
  757. } {}
  758. test "Redis.replicate_commands() must be issued before any write (2)" {
  759. r eval {
  760. return redis.replicate_commands();
  761. } 0
  762. } {1}
  763. test "Redis.set_repl() must be issued after replicate_commands()" {
  764. r debug lua-always-replicate-commands 0
  765. catch {
  766. r eval {
  767. redis.set_repl(redis.REPL_ALL);
  768. } 0
  769. } e
  770. r debug lua-always-replicate-commands 1
  771. set e
  772. } {*only after turning on*}
  773. test "Redis.set_repl() don't accept invalid values" {
  774. catch {
  775. r eval {
  776. redis.replicate_commands();
  777. redis.set_repl(12345);
  778. } 0
  779. } e
  780. set e
  781. } {*Invalid*flags*}
  782. test "Test selective replication of certain Redis commands from Lua" {
  783. r del a b c d
  784. r eval {
  785. redis.replicate_commands();
  786. redis.call('set','a','1');
  787. redis.set_repl(redis.REPL_NONE);
  788. redis.call('set','b','2');
  789. redis.set_repl(redis.REPL_AOF);
  790. redis.call('set','c','3');
  791. redis.set_repl(redis.REPL_ALL);
  792. redis.call('set','d','4');
  793. } 0
  794. wait_for_condition 50 100 {
  795. [r -1 mget a b c d] eq {1 {} {} 4}
  796. } else {
  797. fail "Only a and c should be replicated to replica"
  798. }
  799. # Master should have everything right now
  800. assert {[r mget a b c d] eq {1 2 3 4}}
  801. # After an AOF reload only a, c and d should exist
  802. r debug loadaof
  803. assert {[r mget a b c d] eq {1 {} 3 4}}
  804. }
  805. test "PRNG is seeded randomly for command replication" {
  806. set a [
  807. r eval {
  808. redis.replicate_commands();
  809. return math.random()*100000;
  810. } 0
  811. ]
  812. set b [
  813. r eval {
  814. redis.replicate_commands();
  815. return math.random()*100000;
  816. } 0
  817. ]
  818. assert {$a ne $b}
  819. }
  820. test "Using side effects is not a problem with command replication" {
  821. r eval {
  822. redis.replicate_commands();
  823. redis.call('set','time',redis.call('time')[1])
  824. } 0
  825. assert {[r get time] ne {}}
  826. wait_for_condition 50 100 {
  827. [r get time] eq [r -1 get time]
  828. } else {
  829. fail "Time key does not match between master and replica"
  830. }
  831. }
  832. }
  833. }
  834. start_server {tags {"scripting external:skip"}} {
  835. r script debug sync
  836. r eval {return 'hello'} 0
  837. r eval {return 'hello'} 0
  838. }
  839. start_server {tags {"scripting needs:debug external:skip"}} {
  840. test {Test scripting debug protocol parsing} {
  841. r script debug sync
  842. r eval {return 'hello'} 0
  843. catch {r 'hello\0world'} e
  844. assert_match {*Unknown Redis Lua debugger command*} $e
  845. catch {r 'hello\0'} e
  846. assert_match {*Unknown Redis Lua debugger command*} $e
  847. catch {r '\0hello'} e
  848. assert_match {*Unknown Redis Lua debugger command*} $e
  849. catch {r '\0hello\0'} e
  850. assert_match {*Unknown Redis Lua debugger command*} $e
  851. }
  852. test {Test scripting debug lua stack overflow} {
  853. r script debug sync
  854. r eval {return 'hello'} 0
  855. set cmd "*101\r\n\$5\r\nredis\r\n"
  856. append cmd [string repeat "\$4\r\ntest\r\n" 100]
  857. r write $cmd
  858. r flush
  859. set ret [r read]
  860. assert_match {*Unknown Redis command called from Lua script*} $ret
  861. # make sure the server is still ok
  862. reconnect
  863. assert_equal [r ping] {PONG}
  864. }
  865. }
  866. start_server {tags {"scripting resp3 needs:debug"}} {
  867. r debug set-disable-deny-scripts 1
  868. for {set i 2} {$i <= 3} {incr i} {
  869. for {set client_proto 2} {$client_proto <= 3} {incr client_proto} {
  870. r hello $client_proto
  871. r readraw 1
  872. test {test resp3 big number protocol parsing} {
  873. set ret [r eval "redis.setresp($i);return redis.call('debug', 'protocol', 'bignum')" 0]
  874. if {$client_proto == 2 || $i == 2} {
  875. # if either Lua or the clien is RESP2 the reply will be RESP2
  876. assert_equal $ret {$37}
  877. assert_equal [r read] {1234567999999999999999999999999999999}
  878. } else {
  879. assert_equal $ret {(1234567999999999999999999999999999999}
  880. }
  881. }
  882. test {test resp3 map protocol parsing} {
  883. set ret [r eval "redis.setresp($i);return redis.call('debug', 'protocol', 'map')" 0]
  884. if {$client_proto == 2 || $i == 2} {
  885. # if either Lua or the clien is RESP2 the reply will be RESP2
  886. assert_equal $ret {*6}
  887. } else {
  888. assert_equal $ret {%3}
  889. }
  890. for {set j 0} {$j < 6} {incr j} {
  891. r read
  892. }
  893. }
  894. test {test resp3 set protocol parsing} {
  895. set ret [r eval "redis.setresp($i);return redis.call('debug', 'protocol', 'set')" 0]
  896. if {$client_proto == 2 || $i == 2} {
  897. # if either Lua or the clien is RESP2 the reply will be RESP2
  898. assert_equal $ret {*3}
  899. } else {
  900. assert_equal $ret {~3}
  901. }
  902. for {set j 0} {$j < 3} {incr j} {
  903. r read
  904. }
  905. }
  906. test {test resp3 double protocol parsing} {
  907. set ret [r eval "redis.setresp($i);return redis.call('debug', 'protocol', 'double')" 0]
  908. if {$client_proto == 2 || $i == 2} {
  909. # if either Lua or the clien is RESP2 the reply will be RESP2
  910. assert_equal $ret {$5}
  911. assert_equal [r read] {3.141}
  912. } else {
  913. assert_equal $ret {,3.141}
  914. }
  915. }
  916. test {test resp3 null protocol parsing} {
  917. set ret [r eval "redis.setresp($i);return redis.call('debug', 'protocol', 'null')" 0]
  918. if {$client_proto == 2} {
  919. # null is a special case in which a Lua client format does not effect the reply to the client
  920. assert_equal $ret {$-1}
  921. } else {
  922. assert_equal $ret {_}
  923. }
  924. } {}
  925. test {test resp3 verbatim protocol parsing} {
  926. set ret [r eval "redis.setresp($i);return redis.call('debug', 'protocol', 'verbatim')" 0]
  927. if {$client_proto == 2 || $i == 2} {
  928. # if either Lua or the clien is RESP2 the reply will be RESP2
  929. assert_equal $ret {$25}
  930. assert_equal [r read] {This is a verbatim}
  931. assert_equal [r read] {string}
  932. } else {
  933. assert_equal $ret {=29}
  934. assert_equal [r read] {txt:This is a verbatim}
  935. assert_equal [r read] {string}
  936. }
  937. }
  938. test {test resp3 true protocol parsing} {
  939. set ret [r eval "redis.setresp($i);return redis.call('debug', 'protocol', 'true')" 0]
  940. if {$client_proto == 2 || $i == 2} {
  941. # if either Lua or the clien is RESP2 the reply will be RESP2
  942. assert_equal $ret {:1}
  943. } else {
  944. assert_equal $ret {#t}
  945. }
  946. }
  947. test {test resp3 false protocol parsing} {
  948. set ret [r eval "redis.setresp($i);return redis.call('debug', 'protocol', 'false')" 0]
  949. if {$client_proto == 2 || $i == 2} {
  950. # if either Lua or the clien is RESP2 the reply will be RESP2
  951. assert_equal $ret {:0}
  952. } else {
  953. assert_equal $ret {#f}
  954. }
  955. }
  956. r readraw 0
  957. }
  958. }
  959. # attribute is not relevant to test with resp2
  960. test {test resp3 attribute protocol parsing} {
  961. # attributes are not (yet) expose to the script
  962. # So here we just check the parser handles them and they are ignored.
  963. r eval "redis.setresp(3);return redis.call('debug', 'protocol', 'attrib')" 0
  964. } {Some real reply following the attribute}
  965. r debug set-disable-deny-scripts 0
  966. }