2
0

test_helper.tcl 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. # Redis test suite. Copyright (C) 2009 Salvatore Sanfilippo antirez@gmail.com
  2. # This softare is released under the BSD License. See the COPYING file for
  3. # more information.
  4. set tcl_precision 17
  5. source tests/support/redis.tcl
  6. source tests/support/server.tcl
  7. source tests/support/tmpfile.tcl
  8. source tests/support/test.tcl
  9. source tests/support/util.tcl
  10. set ::all_tests {
  11. unit/printver
  12. unit/auth
  13. unit/protocol
  14. unit/basic
  15. unit/type/list
  16. unit/type/list-2
  17. unit/type/list-3
  18. unit/type/set
  19. unit/type/zset
  20. unit/type/hash
  21. unit/sort
  22. unit/expire
  23. unit/other
  24. unit/cas
  25. unit/quit
  26. integration/replication
  27. integration/replication-2
  28. integration/replication-3
  29. integration/aof
  30. unit/pubsub
  31. unit/slowlog
  32. unit/maxmemory
  33. unit/introspection
  34. }
  35. # Index to the next test to run in the ::all_tests list.
  36. set ::next_test 0
  37. set ::host 127.0.0.1
  38. set ::port 21111
  39. set ::traceleaks 0
  40. set ::valgrind 0
  41. set ::verbose 0
  42. set ::quiet 0
  43. set ::denytags {}
  44. set ::allowtags {}
  45. set ::external 0; # If "1" this means, we are running against external instance
  46. set ::file ""; # If set, runs only the tests in this comma separated list
  47. set ::curfile ""; # Hold the filename of the current suite
  48. set ::accurate 0; # If true runs fuzz tests with more iterations
  49. set ::force_failure 0
  50. # Set to 1 when we are running in client mode. The Redis test uses a
  51. # server-client model to run tests simultaneously. The server instance
  52. # runs the specified number of client instances that will actually run tests.
  53. # The server is responsible of showing the result to the user, and exit with
  54. # the appropriate exit code depending on the test outcome.
  55. set ::client 0
  56. set ::numclients 16
  57. proc execute_tests name {
  58. set path "tests/$name.tcl"
  59. set ::curfile $path
  60. source $path
  61. send_data_packet $::test_server_fd done "$name"
  62. }
  63. # Setup a list to hold a stack of server configs. When calls to start_server
  64. # are nested, use "srv 0 pid" to get the pid of the inner server. To access
  65. # outer servers, use "srv -1 pid" etcetera.
  66. set ::servers {}
  67. proc srv {args} {
  68. set level 0
  69. if {[string is integer [lindex $args 0]]} {
  70. set level [lindex $args 0]
  71. set property [lindex $args 1]
  72. } else {
  73. set property [lindex $args 0]
  74. }
  75. set srv [lindex $::servers end+$level]
  76. dict get $srv $property
  77. }
  78. # Provide easy access to the client for the inner server. It's possible to
  79. # prepend the argument list with a negative level to access clients for
  80. # servers running in outer blocks.
  81. proc r {args} {
  82. set level 0
  83. if {[string is integer [lindex $args 0]]} {
  84. set level [lindex $args 0]
  85. set args [lrange $args 1 end]
  86. }
  87. [srv $level "client"] {*}$args
  88. }
  89. proc reconnect {args} {
  90. set level [lindex $args 0]
  91. if {[string length $level] == 0 || ![string is integer $level]} {
  92. set level 0
  93. }
  94. set srv [lindex $::servers end+$level]
  95. set host [dict get $srv "host"]
  96. set port [dict get $srv "port"]
  97. set config [dict get $srv "config"]
  98. set client [redis $host $port]
  99. dict set srv "client" $client
  100. # select the right db when we don't have to authenticate
  101. if {![dict exists $config "requirepass"]} {
  102. $client select 9
  103. }
  104. # re-set $srv in the servers list
  105. lset ::servers end+$level $srv
  106. }
  107. proc redis_deferring_client {args} {
  108. set level 0
  109. if {[llength $args] > 0 && [string is integer [lindex $args 0]]} {
  110. set level [lindex $args 0]
  111. set args [lrange $args 1 end]
  112. }
  113. # create client that defers reading reply
  114. set client [redis [srv $level "host"] [srv $level "port"] 1]
  115. # select the right db and read the response (OK)
  116. $client select 9
  117. $client read
  118. return $client
  119. }
  120. # Provide easy access to INFO properties. Same semantic as "proc r".
  121. proc s {args} {
  122. set level 0
  123. if {[string is integer [lindex $args 0]]} {
  124. set level [lindex $args 0]
  125. set args [lrange $args 1 end]
  126. }
  127. status [srv $level "client"] [lindex $args 0]
  128. }
  129. proc cleanup {} {
  130. if {!$::quiet} {puts -nonewline "Cleanup: may take some time... "}
  131. flush stdout
  132. catch {exec rm -rf {*}[glob tests/tmp/redis.conf.*]}
  133. catch {exec rm -rf {*}[glob tests/tmp/server.*]}
  134. if {!$::quiet} {puts "OK"}
  135. }
  136. proc find_available_port start {
  137. for {set j $start} {$j < $start+1024} {incr j} {
  138. if {[catch {
  139. set fd [socket 127.0.0.1 $j]
  140. }]} {
  141. return $j
  142. } else {
  143. close $fd
  144. }
  145. }
  146. if {$j == $start+1024} {
  147. error "Can't find a non busy port in the $start-[expr {$start+1023}] range."
  148. }
  149. }
  150. proc test_server_main {} {
  151. cleanup
  152. # Open a listening socket, trying different ports in order to find a
  153. # non busy one.
  154. set port [find_available_port 11111]
  155. if {!$::quiet} {
  156. puts "Starting test server at port $port"
  157. }
  158. socket -server accept_test_clients $port
  159. # Start the client instances
  160. set ::clients_pids {}
  161. set start_port [expr {$::port+100}]
  162. for {set j 0} {$j < $::numclients} {incr j} {
  163. set start_port [find_available_port $start_port]
  164. set p [exec tclsh8.5 [info script] {*}$::argv \
  165. --client $port --port $start_port &]
  166. lappend ::clients_pids $p
  167. incr start_port 10
  168. }
  169. # Setup global state for the test server
  170. set ::idle_clients {}
  171. set ::active_clients {}
  172. array set ::clients_start_time {}
  173. set ::clients_time_history {}
  174. set ::failed_tests {}
  175. # Enter the event loop to handle clients I/O
  176. after 100 test_server_cron
  177. vwait forever
  178. }
  179. # This function gets called 10 times per second, for now does nothing but
  180. # may be used in the future in order to detect test clients taking too much
  181. # time to execute the task.
  182. proc test_server_cron {} {
  183. }
  184. proc accept_test_clients {fd addr port} {
  185. fileevent $fd readable [list read_from_test_client $fd]
  186. }
  187. # This is the readable handler of our test server. Clients send us messages
  188. # in the form of a status code such and additional data. Supported
  189. # status types are:
  190. #
  191. # ready: the client is ready to execute the command. Only sent at client
  192. # startup. The server will queue the client FD in the list of idle
  193. # clients.
  194. # testing: just used to signal that a given test started.
  195. # ok: a test was executed with success.
  196. # err: a test was executed with an error.
  197. # exception: there was a runtime exception while executing the test.
  198. # done: all the specified test file was processed, this test client is
  199. # ready to accept a new task.
  200. proc read_from_test_client fd {
  201. set bytes [gets $fd]
  202. set payload [read $fd $bytes]
  203. foreach {status data} $payload break
  204. if {$status eq {ready}} {
  205. if {!$::quiet} {
  206. puts "\[$status\]: $data"
  207. }
  208. signal_idle_client $fd
  209. } elseif {$status eq {done}} {
  210. set elapsed [expr {[clock seconds]-$::clients_start_time($fd)}]
  211. set all_tests_count [llength $::all_tests]
  212. set running_tests_count [expr {[llength $::active_clients]-1}]
  213. set completed_tests_count [expr {$::next_test-$running_tests_count}]
  214. puts "\[$completed_tests_count/$all_tests_count [colorstr yellow $status]\]: $data ($elapsed seconds)"
  215. lappend ::clients_time_history $elapsed $data
  216. signal_idle_client $fd
  217. } elseif {$status eq {ok}} {
  218. if {!$::quiet} {
  219. puts "\[[colorstr green $status]\]: $data"
  220. }
  221. } elseif {$status eq {err}} {
  222. set err "\[[colorstr red $status]\]: $data"
  223. puts $err
  224. lappend ::failed_tests $err
  225. } elseif {$status eq {exception}} {
  226. puts "\[[colorstr red $status]\]: $data"
  227. foreach p $::clients_pids {
  228. catch {exec kill -9 $p}
  229. }
  230. exit 1
  231. } elseif {$status eq {testing}} {
  232. # No op
  233. } else {
  234. if {!$::quiet} {
  235. puts "\[$status\]: $data"
  236. }
  237. }
  238. }
  239. # A new client is idle. Remove it from the list of active clients and
  240. # if there are still test units to run, launch them.
  241. proc signal_idle_client fd {
  242. # Remove this fd from the list of active clients.
  243. set ::active_clients \
  244. [lsearch -all -inline -not -exact $::active_clients $fd]
  245. # New unit to process?
  246. if {$::next_test != [llength $::all_tests]} {
  247. if {!$::quiet} {
  248. puts [colorstr bold-white "Testing [lindex $::all_tests $::next_test]"]
  249. }
  250. set ::clients_start_time($fd) [clock seconds]
  251. send_data_packet $fd run [lindex $::all_tests $::next_test]
  252. lappend ::active_clients $fd
  253. incr ::next_test
  254. } else {
  255. lappend ::idle_clients $fd
  256. if {[llength $::active_clients] == 0} {
  257. the_end
  258. }
  259. }
  260. }
  261. # The the_end funciton gets called when all the test units were already
  262. # executed, so the test finished.
  263. proc the_end {} {
  264. # TODO: print the status, exit with the rigth exit code.
  265. puts "\n The End\n"
  266. puts "Execution time of different units:"
  267. foreach {time name} $::clients_time_history {
  268. puts " $time seconds - $name"
  269. }
  270. if {[llength $::failed_tests]} {
  271. puts "\n[colorstr bold-red {!!! WARNING}] The following tests failed:\n"
  272. foreach failed $::failed_tests {
  273. puts "*** $failed"
  274. }
  275. cleanup
  276. exit 1
  277. } else {
  278. puts "\n[colorstr bold-white {\o/}] [colorstr bold-green {All tests passed without errors!}]\n"
  279. cleanup
  280. exit 0
  281. }
  282. }
  283. # The client is not even driven (the test server is instead) as we just need
  284. # to read the command, execute, reply... all this in a loop.
  285. proc test_client_main server_port {
  286. set ::test_server_fd [socket localhost $server_port]
  287. send_data_packet $::test_server_fd ready [pid]
  288. while 1 {
  289. set bytes [gets $::test_server_fd]
  290. set payload [read $::test_server_fd $bytes]
  291. foreach {cmd data} $payload break
  292. if {$cmd eq {run}} {
  293. execute_tests $data
  294. } else {
  295. error "Unknown test client command: $cmd"
  296. }
  297. }
  298. }
  299. proc send_data_packet {fd status data} {
  300. set payload [list $status $data]
  301. puts $fd [string length $payload]
  302. puts -nonewline $fd $payload
  303. flush $fd
  304. }
  305. proc print_help_screen {} {
  306. puts [join {
  307. "--valgrind Run the test over valgrind."
  308. "--accurate Run slow randomized tests for more iterations."
  309. "--quiet Don't show individual tests."
  310. "--single <unit> Just execute the specified unit (see next option)."
  311. "--list-tests List all the available test units."
  312. "--force-failure Force the execution of a test that always fails."
  313. "--help Print this help screen."
  314. } "\n"]
  315. }
  316. # parse arguments
  317. for {set j 0} {$j < [llength $argv]} {incr j} {
  318. set opt [lindex $argv $j]
  319. set arg [lindex $argv [expr $j+1]]
  320. if {$opt eq {--tags}} {
  321. foreach tag $arg {
  322. if {[string index $tag 0] eq "-"} {
  323. lappend ::denytags [string range $tag 1 end]
  324. } else {
  325. lappend ::allowtags $tag
  326. }
  327. }
  328. incr j
  329. } elseif {$opt eq {--valgrind}} {
  330. set ::valgrind 1
  331. } elseif {$opt eq {--quiet}} {
  332. set ::quiet 1
  333. } elseif {$opt eq {--host}} {
  334. set ::external 1
  335. set ::host $arg
  336. incr j
  337. } elseif {$opt eq {--port}} {
  338. set ::port $arg
  339. incr j
  340. } elseif {$opt eq {--accurate}} {
  341. set ::accurate 1
  342. } elseif {$opt eq {--force-failure}} {
  343. set ::force_failure 1
  344. } elseif {$opt eq {--single}} {
  345. set ::all_tests $arg
  346. incr j
  347. } elseif {$opt eq {--list-tests}} {
  348. foreach t $::all_tests {
  349. puts $t
  350. }
  351. exit 0
  352. } elseif {$opt eq {--client}} {
  353. set ::client 1
  354. set ::test_server_port $arg
  355. incr j
  356. } elseif {$opt eq {--help}} {
  357. print_help_screen
  358. exit 0
  359. } else {
  360. puts "Wrong argument: $opt"
  361. exit 1
  362. }
  363. }
  364. # With the parallel test running multiple Redis instances at the same time
  365. # we need a fast enough computer, otherwise a lot of tests may generate
  366. # false positives.
  367. # If the computer is too slow we revert the sequetial test without any
  368. # parallelism, that is, clients == 1.
  369. proc is_a_slow_computer {} {
  370. set start [clock milliseconds]
  371. for {set j 0} {$j < 1000000} {incr j} {}
  372. set elapsed [expr [clock milliseconds]-$start]
  373. expr {$elapsed > 200}
  374. }
  375. if {$::client} {
  376. if {[catch { test_client_main $::test_server_port } err]} {
  377. set estr "Executing test client: $err.\n$::errorInfo"
  378. if {[catch {send_data_packet $::test_server_fd exception $estr}]} {
  379. puts $estr
  380. }
  381. exit 1
  382. }
  383. } else {
  384. if {[is_a_slow_computer]} {
  385. puts "** SLOW COMPUTER ** Using a single client to avoid false positives."
  386. set ::numclients 1
  387. }
  388. if {[catch { test_server_main } err]} {
  389. if {[string length $err] > 0} {
  390. # only display error when not generated by the test suite
  391. if {$err ne "exception"} {
  392. puts $::errorInfo
  393. }
  394. exit 1
  395. }
  396. }
  397. }