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
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
28 integration
/replication
29 integration
/replication-2
30 integration
/replication-3
38 # Index to the next test to run in the ::all_tests list.
48 set ::external 0; # If "1" this means, we are running against external instance
49 set ::file ""; # If set, runs only the tests in this comma separated list
50 set ::curfile ""; # Hold the filename of the current suite
51 set ::accurate 0; # If true runs fuzz tests with more iterations
54 # Set to 1 when we are running in client mode. The Redis test uses a
55 # server-client model to run tests simultaneously. The server instance
56 # runs the specified number of client instances that will actually run tests.
57 # The server is responsible of showing the result to the user, and exit with
58 # the appropriate exit code depending on the test outcome.
62 proc execute_tests name
{
63 set path
"tests/$name.tcl"
66 send_data_packet
$::test_server_fd done
"$name"
69 # Setup a list to hold a stack of server configs. When calls to start_server
70 # are nested, use "srv 0 pid" to get the pid of the inner server. To access
71 # outer servers, use "srv -1 pid" etcetera.
75 if {[string is integer
[lindex $args 0]]} {
76 set level
[lindex $args 0]
77 set property
[lindex $args 1]
79 set property
[lindex $args 0]
81 set srv
[lindex $::servers end
+$level]
82 dict get
$srv $property
85 # Provide easy access to the client for the inner server. It's possible to
86 # prepend the argument list with a negative level to access clients for
87 # servers running in outer blocks.
90 if {[string is integer
[lindex $args 0]]} {
91 set level
[lindex $args 0]
92 set args
[lrange $args 1 end
]
94 [srv
$level "client"] {*}$args
97 proc reconnect
{args
} {
98 set level
[lindex $args 0]
99 if {[string length
$level] == 0 ||
![string is integer
$level]} {
103 set srv
[lindex $::servers end
+$level]
104 set host
[dict get
$srv "host"]
105 set port
[dict get
$srv "port"]
106 set config
[dict get
$srv "config"]
107 set client
[redis
$host $port]
108 dict
set srv
"client" $client
110 # select the right db when we don't have to authenticate
111 if {![dict exists
$config "requirepass"]} {
115 # re-set $srv in the servers list
116 set ::servers [lreplace $::servers end
+$level 1 $srv]
119 proc redis_deferring_client
{args
} {
121 if {[llength $args] > 0 && [string is integer
[lindex $args 0]]} {
122 set level
[lindex $args 0]
123 set args
[lrange $args 1 end
]
126 # create client that defers reading reply
127 set client
[redis
[srv
$level "host"] [srv
$level "port"] 1]
129 # select the right db and read the response (OK)
135 # Provide easy access to INFO properties. Same semantic as "proc r".
138 if {[string is integer
[lindex $args 0]]} {
139 set level
[lindex $args 0]
140 set args
[lrange $args 1 end
]
142 status
[srv
$level "client"] [lindex $args 0]
146 puts -nonewline "Cleanup: may take some time... "
148 catch {exec rm
-rf {*}[glob tests
/tmp
/redis.conf.
*]}
149 catch {exec rm
-rf {*}[glob tests
/tmp
/server.
*]}
153 proc test_server_main
{} {
155 # Open a listening socket, trying different ports in order to find a
159 puts "Starting test server at port $port"
160 if {[catch {socket -server accept_test_clients
$port} e
]} {
161 if {[string match
{*address already in use
*} $e]} {
162 if {$port == 20000} {
163 puts "Can't find an available TCP port for test server."
169 puts "Fatal error starting test server: $e"
177 # Start the client instances
178 set ::clients_pids {}
179 for {set j
0} {$j < $::numclients} {incr j
} {
180 set p
[exec tclsh8.5
[info script
] {*}$::argv \
181 --client $port --port [expr {$::port+($j*10)}] &]
182 lappend ::clients_pids $p
185 # Setup global state for the test server
186 set ::idle_clients {}
187 set ::active_clients {}
188 array set ::clients_start_time {}
189 set ::clients_time_history {}
190 set ::failed_tests {}
192 # Enter the event loop to handle clients I/O
193 after 100 test_server_cron
197 # This function gets called 10 times per second, for now does nothing but
198 # may be used in the future in order to detect test clients taking too much
199 # time to execute the task.
200 proc test_server_cron
{} {
203 proc accept_test_clients
{fd addr port
} {
204 fileevent $fd readable
[list read_from_test_client
$fd]
207 # This is the readable handler of our test server. Clients send us messages
208 # in the form of a status code such and additional data. Supported
211 # ready: the client is ready to execute the command. Only sent at client
212 # startup. The server will queue the client FD in the list of idle
214 # testing: just used to signal that a given test started.
215 # ok: a test was executed with success.
216 # err: a test was executed with an error.
217 # exception: there was a runtime exception while executing the test.
218 # done: all the specified test file was processed, this test client is
219 # ready to accept a new task.
220 proc read_from_test_client fd
{
222 set payload
[read $fd $bytes]
223 foreach {status data
} $payload break
224 if {$status eq
{ready
}} {
225 puts "\[$status\]: $data"
226 signal_idle_client
$fd
227 } elseif
{$status eq
{done
}} {
228 set elapsed
[expr {[clock seconds
]-$::clients_start_time($fd)}]
229 puts "\[[colorstr yellow $status]\]: $data ($elapsed seconds)"
230 puts "+++ [expr {[llength $::active_clients]-1}] units still in execution."
231 lappend ::clients_time_history $elapsed $data
232 signal_idle_client
$fd
233 } elseif
{$status eq
{ok
}} {
234 puts "\[[colorstr green $status]\]: $data"
235 } elseif
{$status eq
{err
}} {
236 set err
"\[[colorstr red $status]\]: $data"
238 lappend ::failed_tests $err
239 } elseif
{$status eq
{exception
}} {
240 puts "\[[colorstr red $status]\]: $data"
241 foreach p
$::clients_pids {
242 catch {exec kill
-9 $p}
245 } elseif
{$status eq
{testing
}} {
248 puts "\[$status\]: $data"
252 # A new client is idle. Remove it from the list of active clients and
253 # if there are still test units to run, launch them.
254 proc signal_idle_client fd
{
255 # Remove this fd from the list of active clients.
256 set ::active_clients \
257 [lsearch -all -inline -not -exact $::active_clients $fd]
258 # New unit to process?
259 if {$::next_test != [llength $::all_tests]} {
260 puts [colorstr bold-white
"Testing [lindex $::all_tests $::next_test]"]
261 set ::clients_start_time($fd) [clock seconds
]
262 send_data_packet
$fd run
[lindex $::all_tests $::next_test]
263 lappend ::active_clients $fd
266 lappend ::idle_clients $fd
267 if {[llength $::active_clients] == 0} {
273 # The the_end funciton gets called when all the test units were already
274 # executed, so the test finished.
276 # TODO: print the status, exit with the rigth exit code.
278 puts "Execution time of different units:"
279 foreach {time name
} $::clients_time_history {
280 puts " $time seconds - $name"
282 if {[llength $::failed_tests]} {
283 puts "\n[colorstr bold-red {!!! WARNING}] The following tests failed:\n"
284 foreach failed
$::failed_tests {
290 puts "\n[colorstr bold-white {\o/}] [colorstr bold-green {All tests passed without errors!}]\n"
296 # The client is not even driven (the test server is instead) as we just need
297 # to read the command, execute, reply... all this in a loop.
298 proc test_client_main server_port
{
299 set ::test_server_fd [socket localhost
$server_port]
300 send_data_packet
$::test_server_fd ready
[pid]
302 set bytes
[gets $::test_server_fd]
303 set payload
[read $::test_server_fd $bytes]
304 foreach {cmd data
} $payload break
308 error "Unknown test client command: $cmd"
313 proc send_data_packet
{fd status data
} {
314 set payload
[list $status $data]
315 puts $fd [string length
$payload]
316 puts -nonewline $fd $payload
320 proc print_help_screen
{} {
322 "--valgrind Run the test over valgrind."
323 "--accurate Run slow randomized tests for more iterations."
324 "--single <unit> Just execute the specified unit (see next option)."
325 "--list-tests List all the available test units."
326 "--force-failure Force the execution of a test that always fails."
327 "--help Print this help screen."
332 for {set j
0} {$j < [llength $argv]} {incr j
} {
333 set opt
[lindex $argv $j]
334 set arg
[lindex $argv [expr $j+1]]
335 if {$opt eq
{--tags}} {
337 if {[string index
$tag 0] eq
"-"} {
338 lappend ::denytags [string range
$tag 1 end
]
340 lappend ::allowtags $tag
344 } elseif
{$opt eq
{--valgrind}} {
346 } elseif
{$opt eq
{--host}} {
350 } elseif
{$opt eq
{--port}} {
353 } elseif
{$opt eq
{--accurate}} {
355 } elseif
{$opt eq
{--force-failure
}} {
356 set ::force_failure 1
357 } elseif
{$opt eq
{--single}} {
360 } elseif
{$opt eq
{--list-tests
}} {
361 foreach t
$::all_tests {
365 } elseif
{$opt eq
{--client}} {
367 set ::test_server_port $arg
369 } elseif
{$opt eq
{--help}} {
373 puts "Wrong argument: $opt"
379 if {[catch { test_client_main
$::test_server_port } err
]} {
380 set estr
"Executing test client: $err.\n$::errorInfo"
381 if {[catch {send_data_packet
$::test_server_fd exception
$estr}]} {
387 if {[catch { test_server_main
} err
]} {
388 if {[string length
$err] > 0} {
389 # only display error when not generated by the test suite
390 if {$err ne
"exception"} {