test_helper.tcl 15 KB

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