other.tcl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. start_server {tags {"other"}} {
  2. if {$::force_failure} {
  3. # This is used just for test suite development purposes.
  4. test {Failing test} {
  5. format err
  6. } {ok}
  7. }
  8. test {SAVE - make sure there are all the types as values} {
  9. # Wait for a background saving in progress to terminate
  10. waitForBgsave r
  11. r lpush mysavelist hello
  12. r lpush mysavelist world
  13. r set myemptykey {}
  14. r set mynormalkey {blablablba}
  15. r zadd mytestzset 10 a
  16. r zadd mytestzset 20 b
  17. r zadd mytestzset 30 c
  18. r save
  19. } {OK} {needs:save}
  20. tags {slow} {
  21. if {$::accurate} {set iterations 10000} else {set iterations 1000}
  22. foreach fuzztype {binary alpha compr} {
  23. test "FUZZ stresser with data model $fuzztype" {
  24. set err 0
  25. for {set i 0} {$i < $iterations} {incr i} {
  26. set fuzz [randstring 0 512 $fuzztype]
  27. r set foo $fuzz
  28. set got [r get foo]
  29. if {$got ne $fuzz} {
  30. set err [list $fuzz $got]
  31. break
  32. }
  33. }
  34. set _ $err
  35. } {0}
  36. }
  37. }
  38. test {BGSAVE} {
  39. r flushdb
  40. waitForBgsave r
  41. r save
  42. r set x 10
  43. r bgsave
  44. waitForBgsave r
  45. r debug reload
  46. r get x
  47. } {10} {needs:save}
  48. test {SELECT an out of range DB} {
  49. catch {r select 1000000} err
  50. set _ $err
  51. } {*index is out of range*} {cluster:skip}
  52. tags {consistency} {
  53. proc check_consistency {dumpname code} {
  54. set dump [csvdump r]
  55. set sha1 [r debug digest]
  56. uplevel 1 $code
  57. set sha1_after [r debug digest]
  58. if {$sha1 eq $sha1_after} {
  59. return 1
  60. }
  61. # Failed
  62. set newdump [csvdump r]
  63. puts "Consistency test failed!"
  64. puts "You can inspect the two dumps in /tmp/${dumpname}*.txt"
  65. set fd [open /tmp/${dumpname}1.txt w]
  66. puts $fd $dump
  67. close $fd
  68. set fd [open /tmp/${dumpname}2.txt w]
  69. puts $fd $newdump
  70. close $fd
  71. return 0
  72. }
  73. if {$::accurate} {set numops 10000} else {set numops 1000}
  74. test {Check consistency of different data types after a reload} {
  75. r flushdb
  76. createComplexDataset r $numops usetag
  77. if {$::ignoredigest} {
  78. set _ 1
  79. } else {
  80. check_consistency {repldump} {
  81. r debug reload
  82. }
  83. }
  84. } {1}
  85. test {Same dataset digest if saving/reloading as AOF?} {
  86. if {$::ignoredigest} {
  87. set _ 1
  88. } else {
  89. check_consistency {aofdump} {
  90. r config set aof-use-rdb-preamble no
  91. r bgrewriteaof
  92. waitForBgrewriteaof r
  93. r debug loadaof
  94. }
  95. }
  96. } {1} {needs:debug}
  97. }
  98. test {EXPIRES after a reload (snapshot + append only file rewrite)} {
  99. r flushdb
  100. r set x 10
  101. r expire x 1000
  102. r save
  103. r debug reload
  104. set ttl [r ttl x]
  105. set e1 [expr {$ttl > 900 && $ttl <= 1000}]
  106. r bgrewriteaof
  107. waitForBgrewriteaof r
  108. r debug loadaof
  109. set ttl [r ttl x]
  110. set e2 [expr {$ttl > 900 && $ttl <= 1000}]
  111. list $e1 $e2
  112. } {1 1} {needs:debug needs:save}
  113. test {EXPIRES after AOF reload (without rewrite)} {
  114. r flushdb
  115. r config set appendonly yes
  116. r config set aof-use-rdb-preamble no
  117. r set x somevalue
  118. r expire x 1000
  119. r setex y 2000 somevalue
  120. r set z somevalue
  121. r expireat z [expr {[clock seconds]+3000}]
  122. # Milliseconds variants
  123. r set px somevalue
  124. r pexpire px 1000000
  125. r psetex py 2000000 somevalue
  126. r set pz somevalue
  127. r pexpireat pz [expr {([clock seconds]+3000)*1000}]
  128. # Reload and check
  129. waitForBgrewriteaof r
  130. # We need to wait two seconds to avoid false positives here, otherwise
  131. # the DEBUG LOADAOF command may read a partial file.
  132. # Another solution would be to set the fsync policy to no, since this
  133. # prevents write() to be delayed by the completion of fsync().
  134. after 2000
  135. r debug loadaof
  136. set ttl [r ttl x]
  137. assert {$ttl > 900 && $ttl <= 1000}
  138. set ttl [r ttl y]
  139. assert {$ttl > 1900 && $ttl <= 2000}
  140. set ttl [r ttl z]
  141. assert {$ttl > 2900 && $ttl <= 3000}
  142. set ttl [r ttl px]
  143. assert {$ttl > 900 && $ttl <= 1000}
  144. set ttl [r ttl py]
  145. assert {$ttl > 1900 && $ttl <= 2000}
  146. set ttl [r ttl pz]
  147. assert {$ttl > 2900 && $ttl <= 3000}
  148. r config set appendonly no
  149. } {OK} {needs:debug}
  150. tags {protocol} {
  151. test {PIPELINING stresser (also a regression for the old epoll bug)} {
  152. if {$::tls} {
  153. set fd2 [::tls::socket [srv host] [srv port]]
  154. } else {
  155. set fd2 [socket [srv host] [srv port]]
  156. }
  157. fconfigure $fd2 -encoding binary -translation binary
  158. puts -nonewline $fd2 "SELECT 9\r\n"
  159. flush $fd2
  160. gets $fd2
  161. for {set i 0} {$i < 100000} {incr i} {
  162. set q {}
  163. set val "0000${i}0000"
  164. append q "SET key:$i $val\r\n"
  165. puts -nonewline $fd2 $q
  166. set q {}
  167. append q "GET key:$i\r\n"
  168. puts -nonewline $fd2 $q
  169. }
  170. flush $fd2
  171. for {set i 0} {$i < 100000} {incr i} {
  172. gets $fd2 line
  173. gets $fd2 count
  174. set count [string range $count 1 end]
  175. set val [read $fd2 $count]
  176. read $fd2 2
  177. }
  178. close $fd2
  179. set _ 1
  180. } {1}
  181. }
  182. test {APPEND basics} {
  183. r del foo
  184. list [r append foo bar] [r get foo] \
  185. [r append foo 100] [r get foo]
  186. } {3 bar 6 bar100}
  187. test {APPEND basics, integer encoded values} {
  188. set res {}
  189. r del foo
  190. r append foo 1
  191. r append foo 2
  192. lappend res [r get foo]
  193. r set foo 1
  194. r append foo 2
  195. lappend res [r get foo]
  196. } {12 12}
  197. test {APPEND fuzzing} {
  198. set err {}
  199. foreach type {binary alpha compr} {
  200. set buf {}
  201. r del x
  202. for {set i 0} {$i < 1000} {incr i} {
  203. set bin [randstring 0 10 $type]
  204. append buf $bin
  205. r append x $bin
  206. }
  207. if {$buf != [r get x]} {
  208. set err "Expected '$buf' found '[r get x]'"
  209. break
  210. }
  211. }
  212. set _ $err
  213. } {}
  214. # Leave the user with a clean DB before to exit
  215. test {FLUSHDB} {
  216. set aux {}
  217. if {$::singledb} {
  218. r flushdb
  219. lappend aux 0 [r dbsize]
  220. } else {
  221. r select 9
  222. r flushdb
  223. lappend aux [r dbsize]
  224. r select 10
  225. r flushdb
  226. lappend aux [r dbsize]
  227. }
  228. } {0 0}
  229. test {Perform a final SAVE to leave a clean DB on disk} {
  230. waitForBgsave r
  231. r save
  232. } {OK} {needs:save}
  233. test {RESET clears client state} {
  234. r client setname test-client
  235. r client tracking on
  236. assert_equal [r reset] "RESET"
  237. set client [r client list]
  238. assert_match {*name= *} $client
  239. assert_match {*flags=N *} $client
  240. } {} {needs:reset}
  241. test {RESET clears MONITOR state} {
  242. set rd [redis_deferring_client]
  243. $rd monitor
  244. assert_equal [$rd read] "OK"
  245. $rd reset
  246. assert_equal [$rd read] "RESET"
  247. assert_no_match {*flags=O*} [r client list]
  248. } {} {needs:reset}
  249. test {RESET clears and discards MULTI state} {
  250. r multi
  251. r set key-a a
  252. r reset
  253. catch {r exec} err
  254. assert_match {*EXEC without MULTI*} $err
  255. } {} {needs:reset}
  256. test {RESET clears Pub/Sub state} {
  257. r subscribe channel-1
  258. r reset
  259. # confirm we're not subscribed by executing another command
  260. r set key val
  261. } {OK} {needs:reset}
  262. test {RESET clears authenticated state} {
  263. r acl setuser user1 on >secret +@all
  264. r auth user1 secret
  265. assert_equal [r acl whoami] user1
  266. r reset
  267. assert_equal [r acl whoami] default
  268. } {} {needs:reset}
  269. }
  270. start_server {tags {"other external:skip"}} {
  271. test {Don't rehash if redis has child proecess} {
  272. r config set save ""
  273. r config set rdb-key-save-delay 1000000
  274. populate 4096 "" 1
  275. r bgsave
  276. wait_for_condition 10 100 {
  277. [s rdb_bgsave_in_progress] eq 1
  278. } else {
  279. fail "bgsave did not start in time"
  280. }
  281. r mset k1 v1 k2 v2
  282. # Hash table should not rehash
  283. assert_no_match "*table size: 8192*" [r debug HTSTATS 9]
  284. exec kill -9 [get_child_pid 0]
  285. after 200
  286. # Hash table should rehash since there is no child process,
  287. # size is power of two and over 4098, so it is 8192
  288. r set k3 v3
  289. assert_match "*table size: 8192*" [r debug HTSTATS 9]
  290. } {} {needs:local-process}
  291. }
  292. proc read_proc_title {pid} {
  293. set fd [open "/proc/$pid/cmdline" "r"]
  294. set cmdline [read $fd 1024]
  295. close $fd
  296. return $cmdline
  297. }
  298. start_server {tags {"other external:skip"}} {
  299. test {Process title set as expected} {
  300. # Test only on Linux where it's easy to get cmdline without relying on tools.
  301. # Skip valgrind as it messes up the arguments.
  302. set os [exec uname]
  303. if {$os == "Linux" && !$::valgrind} {
  304. # Set a custom template
  305. r config set "proc-title-template" "TEST {title} {listen-addr} {port} {tls-port} {unixsocket} {config-file}"
  306. set cmdline [read_proc_title [srv 0 pid]]
  307. assert_equal "TEST" [lindex $cmdline 0]
  308. assert_match "*/redis-server" [lindex $cmdline 1]
  309. if {$::tls} {
  310. set expect_port 0
  311. set expect_tls_port [srv 0 port]
  312. } else {
  313. set expect_port [srv 0 port]
  314. set expect_tls_port 0
  315. }
  316. set port [srv 0 port]
  317. assert_equal "$::host:$port" [lindex $cmdline 2]
  318. assert_equal $expect_port [lindex $cmdline 3]
  319. assert_equal $expect_tls_port [lindex $cmdline 4]
  320. assert_match "*/tests/tmp/server.*/socket" [lindex $cmdline 5]
  321. assert_match "*/tests/tmp/redis.conf.*" [lindex $cmdline 6]
  322. # Try setting a bad template
  323. catch {r config set "proc-title-template" "{invalid-var}"} err
  324. assert_match {*template format is invalid*} $err
  325. }
  326. }
  327. }