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