test_helper.tcl 23 KB

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