test_helper.tcl 16 KB

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