test_helper.tcl 17 KB

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