]> git.saurik.com Git - redis.git/blame - tests/test_helper.tcl
client buffer handling refactoring and optimization
[redis.git] / tests / test_helper.tcl
CommitLineData
98578b57
PN
1# Redis test suite. Copyright (C) 2009 Salvatore Sanfilippo antirez@gmail.com
2# This softare is released under the BSD License. See the COPYING file for
3# more information.
4
5set tcl_precision 17
ab72b483 6source tests/support/redis.tcl
7source tests/support/server.tcl
8source tests/support/tmpfile.tcl
9source tests/support/test.tcl
10source tests/support/util.tcl
98578b57 11
13566085 12set ::all_tests {
13 unit/printver
14 unit/auth
15 unit/protocol
16 unit/basic
17 unit/type/list
be9250c8 18 unit/type/list-2
6209797d 19 unit/type/list-3
13566085 20 unit/type/set
21 unit/type/zset
22 unit/type/hash
23 unit/sort
24 unit/expire
25 unit/other
26 unit/cas
27 unit/quit
570bbcf8 28 unit/aofrw
13566085 29 integration/replication
569f84aa 30 integration/replication-2
31 integration/replication-3
13566085 32 integration/aof
33 unit/pubsub
34 unit/slowlog
0681c5ad 35 unit/scripting
243b783f 36 unit/maxmemory
f4bddefe 37 unit/introspection
13566085 38}
39# Index to the next test to run in the ::all_tests list.
40set ::next_test 0
41
98578b57 42set ::host 127.0.0.1
24bfb570 43set ::port 21111
e59a64b8 44set ::traceleaks 0
c4669d25 45set ::valgrind 0
322ea972 46set ::verbose 0
38b957d8 47set ::quiet 0
6e0e5bed
PN
48set ::denytags {}
49set ::allowtags {}
7d04fc75 50set ::external 0; # If "1" this means, we are running against external instance
9f1ae9ab 51set ::file ""; # If set, runs only the tests in this comma separated list
6f8a32d5 52set ::curfile ""; # Hold the filename of the current suite
524d515f 53set ::accurate 0; # If true runs fuzz tests with more iterations
04e2410d 54set ::force_failure 0
13566085 55
56# Set to 1 when we are running in client mode. The Redis test uses a
57# server-client model to run tests simultaneously. The server instance
58# runs the specified number of client instances that will actually run tests.
59# The server is responsible of showing the result to the user, and exit with
60# the appropriate exit code depending on the test outcome.
61set ::client 0
62set ::numclients 16
98578b57
PN
63
64proc execute_tests name {
6f8a32d5
PN
65 set path "tests/$name.tcl"
66 set ::curfile $path
67 source $path
36e790a0 68 send_data_packet $::test_server_fd done "$name"
98578b57
PN
69}
70
1c4114be
PN
71# Setup a list to hold a stack of server configs. When calls to start_server
72# are nested, use "srv 0 pid" to get the pid of the inner server. To access
73# outer servers, use "srv -1 pid" etcetera.
74set ::servers {}
f2dd4769
PN
75proc srv {args} {
76 set level 0
77 if {[string is integer [lindex $args 0]]} {
78 set level [lindex $args 0]
79 set property [lindex $args 1]
80 } else {
81 set property [lindex $args 0]
82 }
1c4114be
PN
83 set srv [lindex $::servers end+$level]
84 dict get $srv $property
85}
86
87# Provide easy access to the client for the inner server. It's possible to
88# prepend the argument list with a negative level to access clients for
89# servers running in outer blocks.
98578b57 90proc r {args} {
1c4114be
PN
91 set level 0
92 if {[string is integer [lindex $args 0]]} {
93 set level [lindex $args 0]
94 set args [lrange $args 1 end]
95 }
96 [srv $level "client"] {*}$args
97}
98
941c9fa2
PN
99proc reconnect {args} {
100 set level [lindex $args 0]
101 if {[string length $level] == 0 || ![string is integer $level]} {
102 set level 0
103 }
104
105 set srv [lindex $::servers end+$level]
106 set host [dict get $srv "host"]
107 set port [dict get $srv "port"]
108 set config [dict get $srv "config"]
109 set client [redis $host $port]
110 dict set srv "client" $client
111
112 # select the right db when we don't have to authenticate
113 if {![dict exists $config "requirepass"]} {
114 $client select 9
115 }
116
117 # re-set $srv in the servers list
414c3dea 118 lset ::servers end+$level $srv
941c9fa2
PN
119}
120
5eedc9c6
PN
121proc redis_deferring_client {args} {
122 set level 0
123 if {[llength $args] > 0 && [string is integer [lindex $args 0]]} {
124 set level [lindex $args 0]
125 set args [lrange $args 1 end]
126 }
127
128 # create client that defers reading reply
129 set client [redis [srv $level "host"] [srv $level "port"] 1]
130
131 # select the right db and read the response (OK)
132 $client select 9
133 $client read
134 return $client
135}
136
1c4114be
PN
137# Provide easy access to INFO properties. Same semantic as "proc r".
138proc s {args} {
139 set level 0
140 if {[string is integer [lindex $args 0]]} {
141 set level [lindex $args 0]
142 set args [lrange $args 1 end]
143 }
144 status [srv $level "client"] [lindex $args 0]
98578b57
PN
145}
146
f166bb1d 147proc cleanup {} {
38b957d8 148 if {!$::quiet} {puts -nonewline "Cleanup: may take some time... "}
13566085 149 flush stdout
c4669d25 150 catch {exec rm -rf {*}[glob tests/tmp/redis.conf.*]}
151 catch {exec rm -rf {*}[glob tests/tmp/server.*]}
38b957d8 152 if {!$::quiet} {puts "OK"}
f166bb1d
PN
153}
154
24bfb570 155proc find_available_port start {
156 for {set j $start} {$j < $start+1024} {incr j} {
157 if {[catch {
dfcf5a0a 158 set fd [socket 127.0.0.1 $j]
24bfb570 159 }]} {
dfcf5a0a 160 return $j
24bfb570 161 } else {
162 close $fd
163 }
164 }
165 if {$j == $start+1024} {
166 error "Can't find a non busy port in the $start-[expr {$start+1023}] range."
167 }
168}
169
13566085 170proc test_server_main {} {
9f1ae9ab 171 cleanup
13566085 172 # Open a listening socket, trying different ports in order to find a
173 # non busy one.
24bfb570 174 set port [find_available_port 11111]
38b957d8 175 if {!$::quiet} {
176 puts "Starting test server at port $port"
177 }
24bfb570 178 socket -server accept_test_clients $port
3fe40d6e 179
13566085 180 # Start the client instances
569f84aa 181 set ::clients_pids {}
24bfb570 182 set start_port [expr {$::port+100}]
13566085 183 for {set j 0} {$j < $::numclients} {incr j} {
24bfb570 184 set start_port [find_available_port $start_port]
569f84aa 185 set p [exec tclsh8.5 [info script] {*}$::argv \
24bfb570 186 --client $port --port $start_port &]
569f84aa 187 lappend ::clients_pids $p
24bfb570 188 incr start_port 10
13566085 189 }
9f1ae9ab 190
13566085 191 # Setup global state for the test server
192 set ::idle_clients {}
193 set ::active_clients {}
36e790a0 194 array set ::clients_start_time {}
195 set ::clients_time_history {}
04e2410d 196 set ::failed_tests {}
13566085 197
198 # Enter the event loop to handle clients I/O
199 after 100 test_server_cron
200 vwait forever
201}
202
203# This function gets called 10 times per second, for now does nothing but
204# may be used in the future in order to detect test clients taking too much
205# time to execute the task.
206proc test_server_cron {} {
207}
208
209proc accept_test_clients {fd addr port} {
210 fileevent $fd readable [list read_from_test_client $fd]
211}
212
213# This is the readable handler of our test server. Clients send us messages
214# in the form of a status code such and additional data. Supported
215# status types are:
216#
217# ready: the client is ready to execute the command. Only sent at client
218# startup. The server will queue the client FD in the list of idle
219# clients.
220# testing: just used to signal that a given test started.
221# ok: a test was executed with success.
222# err: a test was executed with an error.
223# exception: there was a runtime exception while executing the test.
224# done: all the specified test file was processed, this test client is
225# ready to accept a new task.
226proc read_from_test_client fd {
227 set bytes [gets $fd]
228 set payload [read $fd $bytes]
229 foreach {status data} $payload break
13566085 230 if {$status eq {ready}} {
38b957d8 231 if {!$::quiet} {
232 puts "\[$status\]: $data"
233 }
13566085 234 signal_idle_client $fd
235 } elseif {$status eq {done}} {
36e790a0 236 set elapsed [expr {[clock seconds]-$::clients_start_time($fd)}]
38b957d8 237 set all_tests_count [llength $::all_tests]
238 set running_tests_count [expr {[llength $::active_clients]-1}]
239 set completed_tests_count [expr {$::next_test-$running_tests_count}]
240 puts "\[$completed_tests_count/$all_tests_count [colorstr yellow $status]\]: $data ($elapsed seconds)"
36e790a0 241 lappend ::clients_time_history $elapsed $data
13566085 242 signal_idle_client $fd
3744824c 243 } elseif {$status eq {ok}} {
38b957d8 244 if {!$::quiet} {
245 puts "\[[colorstr green $status]\]: $data"
246 }
3744824c 247 } elseif {$status eq {err}} {
04e2410d 248 set err "\[[colorstr red $status]\]: $data"
249 puts $err
250 lappend ::failed_tests $err
569f84aa 251 } elseif {$status eq {exception}} {
252 puts "\[[colorstr red $status]\]: $data"
253 foreach p $::clients_pids {
254 catch {exec kill -9 $p}
9f1ae9ab 255 }
569f84aa 256 exit 1
daab1599 257 } elseif {$status eq {testing}} {
258 # No op
9f1ae9ab 259 } else {
38b957d8 260 if {!$::quiet} {
261 puts "\[$status\]: $data"
262 }
9f1ae9ab 263 }
13566085 264}
e39c8b50 265
13566085 266# A new client is idle. Remove it from the list of active clients and
267# if there are still test units to run, launch them.
268proc signal_idle_client fd {
269 # Remove this fd from the list of active clients.
270 set ::active_clients \
271 [lsearch -all -inline -not -exact $::active_clients $fd]
272 # New unit to process?
273 if {$::next_test != [llength $::all_tests]} {
38b957d8 274 if {!$::quiet} {
275 puts [colorstr bold-white "Testing [lindex $::all_tests $::next_test]"]
276 }
36e790a0 277 set ::clients_start_time($fd) [clock seconds]
13566085 278 send_data_packet $fd run [lindex $::all_tests $::next_test]
279 lappend ::active_clients $fd
280 incr ::next_test
9f1ae9ab 281 } else {
13566085 282 lappend ::idle_clients $fd
283 if {[llength $::active_clients] == 0} {
284 the_end
6f8a32d5 285 }
9f1ae9ab 286 }
13566085 287}
6f8a32d5 288
13566085 289# The the_end funciton gets called when all the test units were already
290# executed, so the test finished.
291proc the_end {} {
292 # TODO: print the status, exit with the rigth exit code.
04e2410d 293 puts "\n The End\n"
36e790a0 294 puts "Execution time of different units:"
295 foreach {time name} $::clients_time_history {
296 puts " $time seconds - $name"
297 }
04e2410d 298 if {[llength $::failed_tests]} {
121ffc85 299 puts "\n[colorstr bold-red {!!! WARNING}] The following tests failed:\n"
04e2410d 300 foreach failed $::failed_tests {
301 puts "*** $failed"
302 }
c7c16a32 303 cleanup
e39c8b50 304 exit 1
04e2410d 305 } else {
306 puts "\n[colorstr bold-white {\o/}] [colorstr bold-green {All tests passed without errors!}]\n"
c7c16a32 307 cleanup
04e2410d 308 exit 0
98578b57 309 }
98578b57
PN
310}
311
13566085 312# The client is not even driven (the test server is instead) as we just need
313# to read the command, execute, reply... all this in a loop.
314proc test_client_main server_port {
315 set ::test_server_fd [socket localhost $server_port]
316 send_data_packet $::test_server_fd ready [pid]
317 while 1 {
318 set bytes [gets $::test_server_fd]
319 set payload [read $::test_server_fd $bytes]
320 foreach {cmd data} $payload break
321 if {$cmd eq {run}} {
322 execute_tests $data
323 } else {
324 error "Unknown test client command: $cmd"
6f8a32d5 325 }
98578b57 326 }
13566085 327}
cabe03eb 328
13566085 329proc send_data_packet {fd status data} {
330 set payload [list $status $data]
331 puts $fd [string length $payload]
332 puts -nonewline $fd $payload
333 flush $fd
98578b57
PN
334}
335
e4715f00 336proc print_help_screen {} {
337 puts [join {
338 "--valgrind Run the test over valgrind."
339 "--accurate Run slow randomized tests for more iterations."
38b957d8 340 "--quiet Don't show individual tests."
e4715f00 341 "--single <unit> Just execute the specified unit (see next option)."
342 "--list-tests List all the available test units."
343 "--force-failure Force the execution of a test that always fails."
344 "--help Print this help screen."
345 } "\n"]
346}
347
73bd6c58
PN
348# parse arguments
349for {set j 0} {$j < [llength $argv]} {incr j} {
350 set opt [lindex $argv $j]
351 set arg [lindex $argv [expr $j+1]]
352 if {$opt eq {--tags}} {
353 foreach tag $arg {
354 if {[string index $tag 0] eq "-"} {
355 lappend ::denytags [string range $tag 1 end]
356 } else {
357 lappend ::allowtags $tag
358 }
359 }
360 incr j
4b918769 361 } elseif {$opt eq {--valgrind}} {
362 set ::valgrind 1
38b957d8 363 } elseif {$opt eq {--quiet}} {
364 set ::quiet 1
7d04fc75 365 } elseif {$opt eq {--host}} {
366 set ::external 1
367 set ::host $arg
368 incr j
369 } elseif {$opt eq {--port}} {
370 set ::port $arg
371 incr j
524d515f 372 } elseif {$opt eq {--accurate}} {
373 set ::accurate 1
04e2410d 374 } elseif {$opt eq {--force-failure}} {
375 set ::force_failure 1
524d515f 376 } elseif {$opt eq {--single}} {
377 set ::all_tests $arg
378 incr j
379 } elseif {$opt eq {--list-tests}} {
380 foreach t $::all_tests {
381 puts $t
382 }
383 exit 0
13566085 384 } elseif {$opt eq {--client}} {
385 set ::client 1
386 set ::test_server_port $arg
387 incr j
524d515f 388 } elseif {$opt eq {--help}} {
e4715f00 389 print_help_screen
524d515f 390 exit 0
73bd6c58
PN
391 } else {
392 puts "Wrong argument: $opt"
393 exit 1
394 }
395}
396
13566085 397if {$::client} {
398 if {[catch { test_client_main $::test_server_port } err]} {
399 set estr "Executing test client: $err.\n$::errorInfo"
400 if {[catch {send_data_packet $::test_server_fd exception $estr}]} {
401 puts $estr
436f18b6
PN
402 }
403 exit 1
404 }
13566085 405} else {
406 if {[catch { test_server_main } err]} {
407 if {[string length $err] > 0} {
408 # only display error when not generated by the test suite
409 if {$err ne "exception"} {
410 puts $::errorInfo
411 }
412 exit 1
413 }
414 }
436f18b6 415}