1
0

scripting.tcl 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  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. }
  537. # Start a new server since the last test in this stanza will kill the
  538. # instance at all.
  539. start_server {tags {"scripting"}} {
  540. test {Timedout read-only scripts can be killed by SCRIPT KILL} {
  541. set rd [redis_deferring_client]
  542. r config set lua-time-limit 10
  543. $rd eval {while true do end} 0
  544. after 200
  545. catch {r ping} e
  546. assert_match {BUSY*} $e
  547. r script kill
  548. after 200 ; # Give some time to Lua to call the hook again...
  549. assert_equal [r ping] "PONG"
  550. }
  551. test {Timedout read-only scripts can be killed by SCRIPT KILL even when use pcall} {
  552. set rd [redis_deferring_client]
  553. r config set lua-time-limit 10
  554. $rd eval {local f = function() while 1 do redis.call('ping') end end while 1 do pcall(f) end} 0
  555. wait_for_condition 50 100 {
  556. [catch {r ping} e] == 1
  557. } else {
  558. fail "Can't wait for script to start running"
  559. }
  560. catch {r ping} e
  561. assert_match {BUSY*} $e
  562. r script kill
  563. wait_for_condition 50 100 {
  564. [catch {r ping} e] == 0
  565. } else {
  566. fail "Can't wait for script to be killed"
  567. }
  568. assert_equal [r ping] "PONG"
  569. catch {$rd read} res
  570. $rd close
  571. assert_match {*killed by user*} $res
  572. }
  573. test {Timedout script does not cause a false dead client} {
  574. set rd [redis_deferring_client]
  575. r config set lua-time-limit 10
  576. # senging (in a pipeline):
  577. # 1. eval "while 1 do redis.call('ping') end" 0
  578. # 2. ping
  579. 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"
  580. append buf "*1\r\n\$4\r\nping\r\n"
  581. $rd write $buf
  582. $rd flush
  583. wait_for_condition 50 100 {
  584. [catch {r ping} e] == 1
  585. } else {
  586. fail "Can't wait for script to start running"
  587. }
  588. catch {r ping} e
  589. assert_match {BUSY*} $e
  590. r script kill
  591. wait_for_condition 50 100 {
  592. [catch {r ping} e] == 0
  593. } else {
  594. fail "Can't wait for script to be killed"
  595. }
  596. assert_equal [r ping] "PONG"
  597. catch {$rd read} res
  598. assert_match {*killed by user*} $res
  599. set res [$rd read]
  600. assert_match {*PONG*} $res
  601. $rd close
  602. }
  603. test {Timedout script link is still usable after Lua returns} {
  604. r config set lua-time-limit 10
  605. r eval {for i=1,100000 do redis.call('ping') end return 'ok'} 0
  606. r ping
  607. } {PONG}
  608. test {Timedout scripts that modified data can't be killed by SCRIPT KILL} {
  609. set rd [redis_deferring_client]
  610. r config set lua-time-limit 10
  611. $rd eval {redis.call('set',KEYS[1],'y'); while true do end} 1 x
  612. after 200
  613. catch {r ping} e
  614. assert_match {BUSY*} $e
  615. catch {r script kill} e
  616. assert_match {UNKILLABLE*} $e
  617. catch {r ping} e
  618. assert_match {BUSY*} $e
  619. } {} {external:skip}
  620. # Note: keep this test at the end of this server stanza because it
  621. # kills the server.
  622. test {SHUTDOWN NOSAVE can kill a timedout script anyway} {
  623. # The server should be still unresponding to normal commands.
  624. catch {r ping} e
  625. assert_match {BUSY*} $e
  626. catch {r shutdown nosave}
  627. # Make sure the server was killed
  628. catch {set rd [redis_deferring_client]} e
  629. assert_match {*connection refused*} $e
  630. } {} {external:skip}
  631. }
  632. foreach cmdrepl {0 1} {
  633. start_server {tags {"scripting repl needs:debug external:skip"}} {
  634. start_server {} {
  635. if {$cmdrepl == 1} {
  636. set rt "(commands replication)"
  637. } else {
  638. set rt "(scripts replication)"
  639. r debug lua-always-replicate-commands 1
  640. }
  641. test "Before the replica connects we issue two EVAL commands $rt" {
  642. # One with an error, but still executing a command.
  643. # SHA is: 67164fc43fa971f76fd1aaeeaf60c1c178d25876
  644. catch {
  645. r eval {redis.call('incr',KEYS[1]); redis.call('nonexisting')} 1 x
  646. }
  647. # One command is correct:
  648. # SHA is: 6f5ade10a69975e903c6d07b10ea44c6382381a5
  649. r eval {return redis.call('incr',KEYS[1])} 1 x
  650. } {2}
  651. test "Connect a replica to the master instance $rt" {
  652. r -1 slaveof [srv 0 host] [srv 0 port]
  653. wait_for_condition 50 100 {
  654. [s -1 role] eq {slave} &&
  655. [string match {*master_link_status:up*} [r -1 info replication]]
  656. } else {
  657. fail "Can't turn the instance into a replica"
  658. }
  659. }
  660. test "Now use EVALSHA against the master, with both SHAs $rt" {
  661. # The server should replicate successful and unsuccessful
  662. # commands as EVAL instead of EVALSHA.
  663. catch {
  664. r evalsha 67164fc43fa971f76fd1aaeeaf60c1c178d25876 1 x
  665. }
  666. r evalsha 6f5ade10a69975e903c6d07b10ea44c6382381a5 1 x
  667. } {4}
  668. test "If EVALSHA was replicated as EVAL, 'x' should be '4' $rt" {
  669. wait_for_condition 50 100 {
  670. [r -1 get x] eq {4}
  671. } else {
  672. fail "Expected 4 in x, but value is '[r -1 get x]'"
  673. }
  674. }
  675. test "Replication of script multiple pushes to list with BLPOP $rt" {
  676. set rd [redis_deferring_client]
  677. $rd brpop a 0
  678. r eval {
  679. redis.call("lpush",KEYS[1],"1");
  680. redis.call("lpush",KEYS[1],"2");
  681. } 1 a
  682. set res [$rd read]
  683. $rd close
  684. wait_for_condition 50 100 {
  685. [r -1 lrange a 0 -1] eq [r lrange a 0 -1]
  686. } else {
  687. 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]'"
  688. }
  689. set res
  690. } {a 1}
  691. test "EVALSHA replication when first call is readonly $rt" {
  692. r del x
  693. r eval {if tonumber(ARGV[1]) > 0 then redis.call('incr', KEYS[1]) end} 1 x 0
  694. r evalsha 6e0e2745aa546d0b50b801a20983b70710aef3ce 1 x 0
  695. r evalsha 6e0e2745aa546d0b50b801a20983b70710aef3ce 1 x 1
  696. wait_for_condition 50 100 {
  697. [r -1 get x] eq {1}
  698. } else {
  699. fail "Expected 1 in x, but value is '[r -1 get x]'"
  700. }
  701. }
  702. test "Lua scripts using SELECT are replicated correctly $rt" {
  703. r eval {
  704. redis.call("set","foo1","bar1")
  705. redis.call("select","10")
  706. redis.call("incr","x")
  707. redis.call("select","11")
  708. redis.call("incr","z")
  709. } 0
  710. r eval {
  711. redis.call("set","foo1","bar1")
  712. redis.call("select","10")
  713. redis.call("incr","x")
  714. redis.call("select","11")
  715. redis.call("incr","z")
  716. } 0
  717. wait_for_condition 50 100 {
  718. [r -1 debug digest] eq [r debug digest]
  719. } else {
  720. fail "Master-Replica desync after Lua script using SELECT."
  721. }
  722. } {} {singledb:skip}
  723. }
  724. }
  725. }
  726. start_server {tags {"scripting repl external:skip"}} {
  727. start_server {overrides {appendonly yes aof-use-rdb-preamble no}} {
  728. test "Connect a replica to the master instance" {
  729. r -1 slaveof [srv 0 host] [srv 0 port]
  730. wait_for_condition 50 100 {
  731. [s -1 role] eq {slave} &&
  732. [string match {*master_link_status:up*} [r -1 info replication]]
  733. } else {
  734. fail "Can't turn the instance into a replica"
  735. }
  736. }
  737. test "Redis.replicate_commands() must be issued before any write" {
  738. r eval {
  739. redis.call('set','foo','bar');
  740. return redis.replicate_commands();
  741. } 0
  742. } {}
  743. test "Redis.replicate_commands() must be issued before any write (2)" {
  744. r eval {
  745. return redis.replicate_commands();
  746. } 0
  747. } {1}
  748. test "Redis.set_repl() must be issued after replicate_commands()" {
  749. r debug lua-always-replicate-commands 0
  750. catch {
  751. r eval {
  752. redis.set_repl(redis.REPL_ALL);
  753. } 0
  754. } e
  755. r debug lua-always-replicate-commands 1
  756. set e
  757. } {*only after turning on*}
  758. test "Redis.set_repl() don't accept invalid values" {
  759. catch {
  760. r eval {
  761. redis.replicate_commands();
  762. redis.set_repl(12345);
  763. } 0
  764. } e
  765. set e
  766. } {*Invalid*flags*}
  767. test "Test selective replication of certain Redis commands from Lua" {
  768. r del a b c d
  769. r eval {
  770. redis.replicate_commands();
  771. redis.call('set','a','1');
  772. redis.set_repl(redis.REPL_NONE);
  773. redis.call('set','b','2');
  774. redis.set_repl(redis.REPL_AOF);
  775. redis.call('set','c','3');
  776. redis.set_repl(redis.REPL_ALL);
  777. redis.call('set','d','4');
  778. } 0
  779. wait_for_condition 50 100 {
  780. [r -1 mget a b c d] eq {1 {} {} 4}
  781. } else {
  782. fail "Only a and c should be replicated to replica"
  783. }
  784. # Master should have everything right now
  785. assert {[r mget a b c d] eq {1 2 3 4}}
  786. # After an AOF reload only a, c and d should exist
  787. r debug loadaof
  788. assert {[r mget a b c d] eq {1 {} 3 4}}
  789. }
  790. test "PRNG is seeded randomly for command replication" {
  791. set a [
  792. r eval {
  793. redis.replicate_commands();
  794. return math.random()*100000;
  795. } 0
  796. ]
  797. set b [
  798. r eval {
  799. redis.replicate_commands();
  800. return math.random()*100000;
  801. } 0
  802. ]
  803. assert {$a ne $b}
  804. }
  805. test "Using side effects is not a problem with command replication" {
  806. r eval {
  807. redis.replicate_commands();
  808. redis.call('set','time',redis.call('time')[1])
  809. } 0
  810. assert {[r get time] ne {}}
  811. wait_for_condition 50 100 {
  812. [r get time] eq [r -1 get time]
  813. } else {
  814. fail "Time key does not match between master and replica"
  815. }
  816. }
  817. }
  818. }
  819. start_server {tags {"scripting external:skip"}} {
  820. r script debug sync
  821. r eval {return 'hello'} 0
  822. r eval {return 'hello'} 0
  823. }
  824. start_server {tags {"scripting resp3 needs:debug"}} {
  825. r debug set-disable-deny-scripts 1
  826. for {set i 2} {$i <= 3} {incr i} {
  827. for {set client_proto 2} {$client_proto <= 3} {incr client_proto} {
  828. r hello $client_proto
  829. r readraw 1
  830. test {test resp3 big number protocol parsing} {
  831. set ret [r eval "redis.setresp($i);return redis.call('debug', 'protocol', 'bignum')" 0]
  832. if {$client_proto == 2 || $i == 2} {
  833. # if either Lua or the clien is RESP2 the reply will be RESP2
  834. assert_equal $ret {$37}
  835. assert_equal [r read] {1234567999999999999999999999999999999}
  836. } else {
  837. assert_equal $ret {(1234567999999999999999999999999999999}
  838. }
  839. }
  840. test {test resp3 map protocol parsing} {
  841. set ret [r eval "redis.setresp($i);return redis.call('debug', 'protocol', 'map')" 0]
  842. if {$client_proto == 2 || $i == 2} {
  843. # if either Lua or the clien is RESP2 the reply will be RESP2
  844. assert_equal $ret {*6}
  845. } else {
  846. assert_equal $ret {%3}
  847. }
  848. for {set j 0} {$j < 6} {incr j} {
  849. r read
  850. }
  851. }
  852. test {test resp3 set protocol parsing} {
  853. set ret [r eval "redis.setresp($i);return redis.call('debug', 'protocol', 'set')" 0]
  854. if {$client_proto == 2 || $i == 2} {
  855. # if either Lua or the clien is RESP2 the reply will be RESP2
  856. assert_equal $ret {*3}
  857. } else {
  858. assert_equal $ret {~3}
  859. }
  860. for {set j 0} {$j < 3} {incr j} {
  861. r read
  862. }
  863. }
  864. test {test resp3 double protocol parsing} {
  865. set ret [r eval "redis.setresp($i);return redis.call('debug', 'protocol', 'double')" 0]
  866. if {$client_proto == 2 || $i == 2} {
  867. # if either Lua or the clien is RESP2 the reply will be RESP2
  868. assert_equal $ret {$18}
  869. assert_equal [r read] {3.1415926535900001}
  870. } else {
  871. assert_equal $ret {,3.1415926535900001}
  872. }
  873. }
  874. test {test resp3 null protocol parsing} {
  875. set ret [r eval "redis.setresp($i);return redis.call('debug', 'protocol', 'null')" 0]
  876. if {$client_proto == 2} {
  877. # null is a special case in which a Lua client format does not effect the reply to the client
  878. assert_equal $ret {$-1}
  879. } else {
  880. assert_equal $ret {_}
  881. }
  882. } {}
  883. test {test resp3 verbatim protocol parsing} {
  884. set ret [r eval "redis.setresp($i);return redis.call('debug', 'protocol', 'verbatim')" 0]
  885. if {$client_proto == 2 || $i == 2} {
  886. # if either Lua or the clien is RESP2 the reply will be RESP2
  887. assert_equal $ret {$25}
  888. assert_equal [r read] {This is a verbatim}
  889. assert_equal [r read] {string}
  890. } else {
  891. assert_equal $ret {=29}
  892. assert_equal [r read] {txt:This is a verbatim}
  893. assert_equal [r read] {string}
  894. }
  895. }
  896. test {test resp3 true protocol parsing} {
  897. set ret [r eval "redis.setresp($i);return redis.call('debug', 'protocol', 'true')" 0]
  898. if {$client_proto == 2 || $i == 2} {
  899. # if either Lua or the clien is RESP2 the reply will be RESP2
  900. assert_equal $ret {:1}
  901. } else {
  902. assert_equal $ret {#t}
  903. }
  904. }
  905. test {test resp3 false protocol parsing} {
  906. set ret [r eval "redis.setresp($i);return redis.call('debug', 'protocol', 'false')" 0]
  907. if {$client_proto == 2 || $i == 2} {
  908. # if either Lua or the clien is RESP2 the reply will be RESP2
  909. assert_equal $ret {:0}
  910. } else {
  911. assert_equal $ret {#f}
  912. }
  913. }
  914. r readraw 0
  915. }
  916. }
  917. # attribute is not relevant to test with resp2
  918. test {test resp3 attribute protocol parsing} {
  919. # attributes are not (yet) expose to the script
  920. # So here we just check the parser handles them and they are ignored.
  921. r eval "redis.setresp(3);return redis.call('debug', 'protocol', 'attrib')" 0
  922. } {Some real reply following the attribute}
  923. r debug set-disable-deny-scripts 0
  924. }