test_helper.tcl 16 KB

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