scripting.tcl 26 KB

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