test_helper.tcl 23 KB

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