2
0

test_helper.tcl 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  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/type/stream
  27. unit/type/stream-cgroups
  28. unit/sort
  29. unit/expire
  30. unit/other
  31. unit/multi
  32. unit/quit
  33. unit/aofrw
  34. unit/acl
  35. unit/latency-monitor
  36. integration/block-repl
  37. integration/replication
  38. integration/replication-2
  39. integration/replication-3
  40. integration/replication-4
  41. integration/replication-psync
  42. integration/aof
  43. integration/rdb
  44. integration/convert-zipmap-hash-on-load
  45. integration/logging
  46. integration/psync2
  47. integration/psync2-reg
  48. integration/psync2-pingoff
  49. integration/redis-cli
  50. unit/pubsub
  51. unit/slowlog
  52. unit/scripting
  53. unit/maxmemory
  54. unit/introspection
  55. unit/introspection-2
  56. unit/limits
  57. unit/obuf-limits
  58. unit/bitops
  59. unit/bitfield
  60. unit/geo
  61. unit/memefficiency
  62. unit/hyperloglog
  63. unit/lazyfree
  64. unit/wait
  65. unit/pendingquerybuf
  66. unit/tls
  67. unit/tracking
  68. }
  69. # Index to the next test to run in the ::all_tests list.
  70. set ::next_test 0
  71. set ::host 127.0.0.1
  72. set ::port 6379; # port for external server
  73. set ::baseport 21111; # initial port for spawned redis servers
  74. set ::portcount 8000; # we don't wanna use more than 10000 to avoid collision with cluster bus ports
  75. set ::traceleaks 0
  76. set ::valgrind 0
  77. set ::tls 0
  78. set ::stack_logging 0
  79. set ::verbose 0
  80. set ::quiet 0
  81. set ::denytags {}
  82. set ::skiptests {}
  83. set ::allowtags {}
  84. set ::only_tests {}
  85. set ::single_tests {}
  86. set ::run_solo_tests {}
  87. set ::skip_till ""
  88. set ::external 0; # If "1" this means, we are running against external instance
  89. set ::file ""; # If set, runs only the tests in this comma separated list
  90. set ::curfile ""; # Hold the filename of the current suite
  91. set ::accurate 0; # If true runs fuzz tests with more iterations
  92. set ::force_failure 0
  93. set ::timeout 1200; # 20 minutes without progresses will quit the test.
  94. set ::last_progress [clock seconds]
  95. set ::active_servers {} ; # Pids of active Redis instances.
  96. set ::dont_clean 0
  97. set ::wait_server 0
  98. set ::stop_on_failure 0
  99. set ::loop 0
  100. set ::tlsdir "tests/tls"
  101. # Set to 1 when we are running in client mode. The Redis test uses a
  102. # server-client model to run tests simultaneously. The server instance
  103. # runs the specified number of client instances that will actually run tests.
  104. # The server is responsible of showing the result to the user, and exit with
  105. # the appropriate exit code depending on the test outcome.
  106. set ::client 0
  107. set ::numclients 16
  108. # This function is called by one of the test clients when it receives
  109. # a "run" command from the server, with a filename as data.
  110. # It will run the specified test source file and signal it to the
  111. # test server when finished.
  112. proc execute_test_file name {
  113. set path "tests/$name.tcl"
  114. set ::curfile $path
  115. source $path
  116. send_data_packet $::test_server_fd done "$name"
  117. }
  118. # This function is called by one of the test clients when it receives
  119. # a "run_code" command from the server, with a verbatim test source code
  120. # as argument, and an associated name.
  121. # It will run the specified code and signal it to the test server when
  122. # finished.
  123. proc execute_test_code {name code} {
  124. eval $code
  125. send_data_packet $::test_server_fd done "$name"
  126. }
  127. # Setup a list to hold a stack of server configs. When calls to start_server
  128. # are nested, use "srv 0 pid" to get the pid of the inner server. To access
  129. # outer servers, use "srv -1 pid" etcetera.
  130. set ::servers {}
  131. proc srv {args} {
  132. set level 0
  133. if {[string is integer [lindex $args 0]]} {
  134. set level [lindex $args 0]
  135. set property [lindex $args 1]
  136. } else {
  137. set property [lindex $args 0]
  138. }
  139. set srv [lindex $::servers end+$level]
  140. dict get $srv $property
  141. }
  142. # Provide easy access to the client for the inner server. It's possible to
  143. # prepend the argument list with a negative level to access clients for
  144. # servers running in outer blocks.
  145. proc r {args} {
  146. set level 0
  147. if {[string is integer [lindex $args 0]]} {
  148. set level [lindex $args 0]
  149. set args [lrange $args 1 end]
  150. }
  151. [srv $level "client"] {*}$args
  152. }
  153. proc reconnect {args} {
  154. set level [lindex $args 0]
  155. if {[string length $level] == 0 || ![string is integer $level]} {
  156. set level 0
  157. }
  158. set srv [lindex $::servers end+$level]
  159. set host [dict get $srv "host"]
  160. set port [dict get $srv "port"]
  161. set config [dict get $srv "config"]
  162. set client [redis $host $port 0 $::tls]
  163. dict set srv "client" $client
  164. # select the right db when we don't have to authenticate
  165. if {![dict exists $config "requirepass"]} {
  166. $client select 9
  167. }
  168. # re-set $srv in the servers list
  169. lset ::servers end+$level $srv
  170. }
  171. proc redis_deferring_client {args} {
  172. set level 0
  173. if {[llength $args] > 0 && [string is integer [lindex $args 0]]} {
  174. set level [lindex $args 0]
  175. set args [lrange $args 1 end]
  176. }
  177. # create client that defers reading reply
  178. set client [redis [srv $level "host"] [srv $level "port"] 1 $::tls]
  179. # select the right db and read the response (OK)
  180. $client select 9
  181. $client read
  182. return $client
  183. }
  184. proc redis_client {args} {
  185. set level 0
  186. if {[llength $args] > 0 && [string is integer [lindex $args 0]]} {
  187. set level [lindex $args 0]
  188. set args [lrange $args 1 end]
  189. }
  190. # create client that defers reading reply
  191. set client [redis [srv $level "host"] [srv $level "port"] 0 $::tls]
  192. # select the right db and read the response (OK)
  193. $client select 9
  194. return $client
  195. }
  196. # Provide easy access to INFO properties. Same semantic as "proc r".
  197. proc s {args} {
  198. set level 0
  199. if {[string is integer [lindex $args 0]]} {
  200. set level [lindex $args 0]
  201. set args [lrange $args 1 end]
  202. }
  203. status [srv $level "client"] [lindex $args 0]
  204. }
  205. # Test wrapped into run_solo are sent back from the client to the
  206. # test server, so that the test server will send them again to
  207. # clients once the clients are idle.
  208. proc run_solo {name code} {
  209. if {$::numclients == 1 || $::loop || $::external} {
  210. # run_solo is not supported in these scenarios, just run the code.
  211. eval $code
  212. return
  213. }
  214. send_data_packet $::test_server_fd run_solo [list $name $code]
  215. }
  216. proc cleanup {} {
  217. if {!$::quiet} {puts -nonewline "Cleanup: may take some time... "}
  218. flush stdout
  219. catch {exec rm -rf {*}[glob tests/tmp/redis.conf.*]}
  220. catch {exec rm -rf {*}[glob tests/tmp/server.*]}
  221. if {!$::quiet} {puts "OK"}
  222. }
  223. proc test_server_main {} {
  224. cleanup
  225. set tclsh [info nameofexecutable]
  226. # Open a listening socket, trying different ports in order to find a
  227. # non busy one.
  228. set clientport [find_available_port 11111 32]
  229. if {!$::quiet} {
  230. puts "Starting test server at port $clientport"
  231. }
  232. socket -server accept_test_clients -myaddr 127.0.0.1 $clientport
  233. # Start the client instances
  234. set ::clients_pids {}
  235. if {$::external} {
  236. set p [exec $tclsh [info script] {*}$::argv \
  237. --client $clientport &]
  238. lappend ::clients_pids $p
  239. } else {
  240. set start_port $::baseport
  241. set port_count [expr {$::portcount / $::numclients}]
  242. for {set j 0} {$j < $::numclients} {incr j} {
  243. set p [exec $tclsh [info script] {*}$::argv \
  244. --client $clientport --baseport $start_port --portcount $port_count &]
  245. lappend ::clients_pids $p
  246. incr start_port $port_count
  247. }
  248. }
  249. # Setup global state for the test server
  250. set ::idle_clients {}
  251. set ::active_clients {}
  252. array set ::active_clients_task {}
  253. array set ::clients_start_time {}
  254. set ::clients_time_history {}
  255. set ::failed_tests {}
  256. # Enter the event loop to handle clients I/O
  257. after 100 test_server_cron
  258. vwait forever
  259. }
  260. # This function gets called 10 times per second.
  261. proc test_server_cron {} {
  262. set elapsed [expr {[clock seconds]-$::last_progress}]
  263. if {$elapsed > $::timeout} {
  264. set err "\[[colorstr red TIMEOUT]\]: clients state report follows."
  265. puts $err
  266. lappend ::failed_tests $err
  267. show_clients_state
  268. kill_clients
  269. force_kill_all_servers
  270. the_end
  271. }
  272. after 100 test_server_cron
  273. }
  274. proc accept_test_clients {fd addr port} {
  275. fconfigure $fd -encoding binary
  276. fileevent $fd readable [list read_from_test_client $fd]
  277. }
  278. # This is the readable handler of our test server. Clients send us messages
  279. # in the form of a status code such and additional data. Supported
  280. # status types are:
  281. #
  282. # ready: the client is ready to execute the command. Only sent at client
  283. # startup. The server will queue the client FD in the list of idle
  284. # clients.
  285. # testing: just used to signal that a given test started.
  286. # ok: a test was executed with success.
  287. # err: a test was executed with an error.
  288. # skip: a test was skipped by skipfile or individual test options.
  289. # ignore: a test was skipped by a group tag.
  290. # exception: there was a runtime exception while executing the test.
  291. # done: all the specified test file was processed, this test client is
  292. # ready to accept a new task.
  293. proc read_from_test_client fd {
  294. set bytes [gets $fd]
  295. set payload [read $fd $bytes]
  296. foreach {status data} $payload break
  297. set ::last_progress [clock seconds]
  298. if {$status eq {ready}} {
  299. if {!$::quiet} {
  300. puts "\[$status\]: $data"
  301. }
  302. signal_idle_client $fd
  303. } elseif {$status eq {done}} {
  304. set elapsed [expr {[clock seconds]-$::clients_start_time($fd)}]
  305. set all_tests_count [llength $::all_tests]
  306. set running_tests_count [expr {[llength $::active_clients]-1}]
  307. set completed_tests_count [expr {$::next_test-$running_tests_count}]
  308. puts "\[$completed_tests_count/$all_tests_count [colorstr yellow $status]\]: $data ($elapsed seconds)"
  309. lappend ::clients_time_history $elapsed $data
  310. signal_idle_client $fd
  311. set ::active_clients_task($fd) "(DONE) $data"
  312. } elseif {$status eq {ok}} {
  313. if {!$::quiet} {
  314. puts "\[[colorstr green $status]\]: $data"
  315. }
  316. set ::active_clients_task($fd) "(OK) $data"
  317. } elseif {$status eq {skip}} {
  318. if {!$::quiet} {
  319. puts "\[[colorstr yellow $status]\]: $data"
  320. }
  321. } elseif {$status eq {ignore}} {
  322. if {!$::quiet} {
  323. puts "\[[colorstr cyan $status]\]: $data"
  324. }
  325. } elseif {$status eq {err}} {
  326. set err "\[[colorstr red $status]\]: $data"
  327. puts $err
  328. lappend ::failed_tests $err
  329. set ::active_clients_task($fd) "(ERR) $data"
  330. if {$::stop_on_failure} {
  331. puts -nonewline "(Test stopped, press enter to resume the tests)"
  332. flush stdout
  333. gets stdin
  334. }
  335. } elseif {$status eq {exception}} {
  336. puts "\[[colorstr red $status]\]: $data"
  337. kill_clients
  338. force_kill_all_servers
  339. exit 1
  340. } elseif {$status eq {testing}} {
  341. set ::active_clients_task($fd) "(IN PROGRESS) $data"
  342. } elseif {$status eq {server-spawning}} {
  343. set ::active_clients_task($fd) "(SPAWNING SERVER) $data"
  344. } elseif {$status eq {server-spawned}} {
  345. lappend ::active_servers $data
  346. set ::active_clients_task($fd) "(SPAWNED SERVER) pid:$data"
  347. } elseif {$status eq {server-killing}} {
  348. set ::active_clients_task($fd) "(KILLING SERVER) pid:$data"
  349. } elseif {$status eq {server-killed}} {
  350. set ::active_servers [lsearch -all -inline -not -exact $::active_servers $data]
  351. set ::active_clients_task($fd) "(KILLED SERVER) pid:$data"
  352. } elseif {$status eq {run_solo}} {
  353. lappend ::run_solo_tests $data
  354. } else {
  355. if {!$::quiet} {
  356. puts "\[$status\]: $data"
  357. }
  358. }
  359. }
  360. proc show_clients_state {} {
  361. # The following loop is only useful for debugging tests that may
  362. # enter an infinite loop.
  363. foreach x $::active_clients {
  364. if {[info exist ::active_clients_task($x)]} {
  365. puts "$x => $::active_clients_task($x)"
  366. } else {
  367. puts "$x => ???"
  368. }
  369. }
  370. }
  371. proc kill_clients {} {
  372. foreach p $::clients_pids {
  373. catch {exec kill $p}
  374. }
  375. }
  376. proc force_kill_all_servers {} {
  377. foreach p $::active_servers {
  378. puts "Killing still running Redis server $p"
  379. catch {exec kill -9 $p}
  380. }
  381. }
  382. proc lpop {listVar {count 1}} {
  383. upvar 1 $listVar l
  384. set ele [lindex $l 0]
  385. set l [lrange $l 1 end]
  386. set ele
  387. }
  388. # A new client is idle. Remove it from the list of active clients and
  389. # if there are still test units to run, launch them.
  390. proc signal_idle_client fd {
  391. # Remove this fd from the list of active clients.
  392. set ::active_clients \
  393. [lsearch -all -inline -not -exact $::active_clients $fd]
  394. # New unit to process?
  395. if {$::next_test != [llength $::all_tests]} {
  396. if {!$::quiet} {
  397. puts [colorstr bold-white "Testing [lindex $::all_tests $::next_test]"]
  398. set ::active_clients_task($fd) "ASSIGNED: $fd ([lindex $::all_tests $::next_test])"
  399. }
  400. set ::clients_start_time($fd) [clock seconds]
  401. send_data_packet $fd run [lindex $::all_tests $::next_test]
  402. lappend ::active_clients $fd
  403. incr ::next_test
  404. if {$::loop && $::next_test == [llength $::all_tests]} {
  405. set ::next_test 0
  406. }
  407. } elseif {[llength $::run_solo_tests] != 0 && [llength $::active_clients] == 0} {
  408. if {!$::quiet} {
  409. puts [colorstr bold-white "Testing solo test"]
  410. set ::active_clients_task($fd) "ASSIGNED: $fd solo test"
  411. }
  412. set ::clients_start_time($fd) [clock seconds]
  413. send_data_packet $fd run_code [lpop ::run_solo_tests]
  414. lappend ::active_clients $fd
  415. } else {
  416. lappend ::idle_clients $fd
  417. set ::active_clients_task($fd) "SLEEPING, no more units to assign"
  418. if {[llength $::active_clients] == 0} {
  419. the_end
  420. }
  421. }
  422. }
  423. # The the_end function gets called when all the test units were already
  424. # executed, so the test finished.
  425. proc the_end {} {
  426. # TODO: print the status, exit with the rigth exit code.
  427. puts "\n The End\n"
  428. puts "Execution time of different units:"
  429. foreach {time name} $::clients_time_history {
  430. puts " $time seconds - $name"
  431. }
  432. if {[llength $::failed_tests]} {
  433. puts "\n[colorstr bold-red {!!! WARNING}] The following tests failed:\n"
  434. foreach failed $::failed_tests {
  435. puts "*** $failed"
  436. }
  437. if {!$::dont_clean} cleanup
  438. exit 1
  439. } else {
  440. puts "\n[colorstr bold-white {\o/}] [colorstr bold-green {All tests passed without errors!}]\n"
  441. if {!$::dont_clean} cleanup
  442. exit 0
  443. }
  444. }
  445. # The client is not even driven (the test server is instead) as we just need
  446. # to read the command, execute, reply... all this in a loop.
  447. proc test_client_main server_port {
  448. set ::test_server_fd [socket localhost $server_port]
  449. fconfigure $::test_server_fd -encoding binary
  450. send_data_packet $::test_server_fd ready [pid]
  451. while 1 {
  452. set bytes [gets $::test_server_fd]
  453. set payload [read $::test_server_fd $bytes]
  454. foreach {cmd data} $payload break
  455. if {$cmd eq {run}} {
  456. execute_test_file $data
  457. } elseif {$cmd eq {run_code}} {
  458. foreach {name code} $data break
  459. execute_test_code $name $code
  460. } else {
  461. error "Unknown test client command: $cmd"
  462. }
  463. }
  464. }
  465. proc send_data_packet {fd status data} {
  466. set payload [list $status $data]
  467. puts $fd [string length $payload]
  468. puts -nonewline $fd $payload
  469. flush $fd
  470. }
  471. proc print_help_screen {} {
  472. puts [join {
  473. "--valgrind Run the test over valgrind."
  474. "--stack-logging Enable OSX leaks/malloc stack logging."
  475. "--accurate Run slow randomized tests for more iterations."
  476. "--quiet Don't show individual tests."
  477. "--single <unit> Just execute the specified unit (see next option). this option can be repeated."
  478. "--list-tests List all the available test units."
  479. "--only <test> Just execute the specified test by test name. this option can be repeated."
  480. "--skip-till <unit> Skip all units until (and including) the specified one."
  481. "--clients <num> Number of test clients (default 16)."
  482. "--timeout <sec> Test timeout in seconds (default 10 min)."
  483. "--force-failure Force the execution of a test that always fails."
  484. "--config <k> <v> Extra config file argument."
  485. "--skipfile <file> Name of a file containing test names that should be skipped (one per line)."
  486. "--dont-clean Don't delete redis log files after the run."
  487. "--stop Blocks once the first test fails."
  488. "--loop Execute the specified set of tests forever."
  489. "--wait-server Wait after server is started (so that you can attach a debugger)."
  490. "--tls Run tests in TLS mode."
  491. "--host <addr> Run tests against an external host."
  492. "--port <port> TCP port to use against external host."
  493. "--baseport <port> Initial port number for spawned redis servers."
  494. "--portcount <num> Port range for spawned redis servers."
  495. "--help Print this help screen."
  496. } "\n"]
  497. }
  498. # parse arguments
  499. for {set j 0} {$j < [llength $argv]} {incr j} {
  500. set opt [lindex $argv $j]
  501. set arg [lindex $argv [expr $j+1]]
  502. if {$opt eq {--tags}} {
  503. foreach tag $arg {
  504. if {[string index $tag 0] eq "-"} {
  505. lappend ::denytags [string range $tag 1 end]
  506. } else {
  507. lappend ::allowtags $tag
  508. }
  509. }
  510. incr j
  511. } elseif {$opt eq {--config}} {
  512. set arg2 [lindex $argv [expr $j+2]]
  513. lappend ::global_overrides $arg
  514. lappend ::global_overrides $arg2
  515. incr j 2
  516. } elseif {$opt eq {--skipfile}} {
  517. incr j
  518. set fp [open $arg r]
  519. set file_data [read $fp]
  520. close $fp
  521. set ::skiptests [split $file_data "\n"]
  522. } elseif {$opt eq {--valgrind}} {
  523. set ::valgrind 1
  524. } elseif {$opt eq {--stack-logging}} {
  525. if {[string match {*Darwin*} [exec uname -a]]} {
  526. set ::stack_logging 1
  527. }
  528. } elseif {$opt eq {--quiet}} {
  529. set ::quiet 1
  530. } elseif {$opt eq {--tls}} {
  531. package require tls 1.6
  532. set ::tls 1
  533. ::tls::init \
  534. -cafile "$::tlsdir/ca.crt" \
  535. -certfile "$::tlsdir/redis.crt" \
  536. -keyfile "$::tlsdir/redis.key"
  537. } elseif {$opt eq {--host}} {
  538. set ::external 1
  539. set ::host $arg
  540. incr j
  541. } elseif {$opt eq {--port}} {
  542. set ::port $arg
  543. incr j
  544. } elseif {$opt eq {--baseport}} {
  545. set ::baseport $arg
  546. incr j
  547. } elseif {$opt eq {--portcount}} {
  548. set ::portcount $arg
  549. incr j
  550. } elseif {$opt eq {--accurate}} {
  551. set ::accurate 1
  552. } elseif {$opt eq {--force-failure}} {
  553. set ::force_failure 1
  554. } elseif {$opt eq {--single}} {
  555. lappend ::single_tests $arg
  556. incr j
  557. } elseif {$opt eq {--only}} {
  558. lappend ::only_tests $arg
  559. incr j
  560. } elseif {$opt eq {--skip-till}} {
  561. set ::skip_till $arg
  562. incr j
  563. } elseif {$opt eq {--list-tests}} {
  564. foreach t $::all_tests {
  565. puts $t
  566. }
  567. exit 0
  568. } elseif {$opt eq {--verbose}} {
  569. set ::verbose 1
  570. } elseif {$opt eq {--client}} {
  571. set ::client 1
  572. set ::test_server_port $arg
  573. incr j
  574. } elseif {$opt eq {--clients}} {
  575. set ::numclients $arg
  576. incr j
  577. } elseif {$opt eq {--dont-clean}} {
  578. set ::dont_clean 1
  579. } elseif {$opt eq {--wait-server}} {
  580. set ::wait_server 1
  581. } elseif {$opt eq {--stop}} {
  582. set ::stop_on_failure 1
  583. } elseif {$opt eq {--loop}} {
  584. set ::loop 1
  585. } elseif {$opt eq {--timeout}} {
  586. set ::timeout $arg
  587. incr j
  588. } elseif {$opt eq {--help}} {
  589. print_help_screen
  590. exit 0
  591. } else {
  592. puts "Wrong argument: $opt"
  593. exit 1
  594. }
  595. }
  596. # If --skil-till option was given, we populate the list of single tests
  597. # to run with everything *after* the specified unit.
  598. if {$::skip_till != ""} {
  599. set skipping 1
  600. foreach t $::all_tests {
  601. if {$skipping == 0} {
  602. lappend ::single_tests $t
  603. }
  604. if {$t == $::skip_till} {
  605. set skipping 0
  606. }
  607. }
  608. if {$skipping} {
  609. puts "test $::skip_till not found"
  610. exit 0
  611. }
  612. }
  613. # Override the list of tests with the specific tests we want to run
  614. # in case there was some filter, that is --single or --skip-till options.
  615. if {[llength $::single_tests] > 0} {
  616. set ::all_tests $::single_tests
  617. }
  618. proc attach_to_replication_stream {} {
  619. if {$::tls} {
  620. set s [::tls::socket [srv 0 "host"] [srv 0 "port"]]
  621. } else {
  622. set s [socket [srv 0 "host"] [srv 0 "port"]]
  623. }
  624. fconfigure $s -translation binary
  625. puts -nonewline $s "SYNC\r\n"
  626. flush $s
  627. # Get the count
  628. while 1 {
  629. set count [gets $s]
  630. set prefix [string range $count 0 0]
  631. if {$prefix ne {}} break; # Newlines are allowed as PINGs.
  632. }
  633. if {$prefix ne {$}} {
  634. error "attach_to_replication_stream error. Received '$count' as count."
  635. }
  636. set count [string range $count 1 end]
  637. # Consume the bulk payload
  638. while {$count} {
  639. set buf [read $s $count]
  640. set count [expr {$count-[string length $buf]}]
  641. }
  642. return $s
  643. }
  644. proc read_from_replication_stream {s} {
  645. fconfigure $s -blocking 0
  646. set attempt 0
  647. while {[gets $s count] == -1} {
  648. if {[incr attempt] == 10} return ""
  649. after 100
  650. }
  651. fconfigure $s -blocking 1
  652. set count [string range $count 1 end]
  653. # Return a list of arguments for the command.
  654. set res {}
  655. for {set j 0} {$j < $count} {incr j} {
  656. read $s 1
  657. set arg [::redis::redis_bulk_read $s]
  658. if {$j == 0} {set arg [string tolower $arg]}
  659. lappend res $arg
  660. }
  661. return $res
  662. }
  663. proc assert_replication_stream {s patterns} {
  664. for {set j 0} {$j < [llength $patterns]} {incr j} {
  665. assert_match [lindex $patterns $j] [read_from_replication_stream $s]
  666. }
  667. }
  668. proc close_replication_stream {s} {
  669. close $s
  670. }
  671. # With the parallel test running multiple Redis instances at the same time
  672. # we need a fast enough computer, otherwise a lot of tests may generate
  673. # false positives.
  674. # If the computer is too slow we revert the sequential test without any
  675. # parallelism, that is, clients == 1.
  676. proc is_a_slow_computer {} {
  677. set start [clock milliseconds]
  678. for {set j 0} {$j < 1000000} {incr j} {}
  679. set elapsed [expr [clock milliseconds]-$start]
  680. expr {$elapsed > 200}
  681. }
  682. if {$::client} {
  683. if {[catch { test_client_main $::test_server_port } err]} {
  684. set estr "Executing test client: $err.\n$::errorInfo"
  685. if {[catch {send_data_packet $::test_server_fd exception $estr}]} {
  686. puts $estr
  687. }
  688. exit 1
  689. }
  690. } else {
  691. if {[is_a_slow_computer]} {
  692. puts "** SLOW COMPUTER ** Using a single client to avoid false positives."
  693. set ::numclients 1
  694. }
  695. if {[catch { test_server_main } err]} {
  696. if {[string length $err] > 0} {
  697. # only display error when not generated by the test suite
  698. if {$err ne "exception"} {
  699. puts $::errorInfo
  700. }
  701. exit 1
  702. }
  703. }
  704. }