test_helper.tcl 17 KB

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