test_helper.tcl 17 KB

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