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
36 # Index to the next test to run in the ::all_tests list.
46 set ::external 0; # If "1" this means, we are running against external instance
47 set ::file ""; # If set, runs only the tests in this comma separated list
48 set ::curfile ""; # Hold the filename of the current suite
49 set ::accurate 0; # If true runs fuzz tests with more iterations
52 # Set to 1 when we are running in client mode. The Redis test uses a
53 # server-client model to run tests simultaneously. The server instance
54 # runs the specified number of client instances that will actually run tests.
55 # The server is responsible of showing the result to the user, and exit with
56 # the appropriate exit code depending on the test outcome.
60 proc execute_tests name
{
61 set path
"tests/$name.tcl"
64 send_data_packet
$::test_server_fd done
"$name"
67 # Setup a list to hold a stack of server configs. When calls to start_server
68 # are nested, use "srv 0 pid" to get the pid of the inner server. To access
69 # outer servers, use "srv -1 pid" etcetera.
73 if {[string is integer
[lindex $args 0]]} {
74 set level
[lindex $args 0]
75 set property
[lindex $args 1]
77 set property
[lindex $args 0]
79 set srv
[lindex $::servers end
+$level]
80 dict get
$srv $property
83 # Provide easy access to the client for the inner server. It's possible to
84 # prepend the argument list with a negative level to access clients for
85 # servers running in outer blocks.
88 if {[string is integer
[lindex $args 0]]} {
89 set level
[lindex $args 0]
90 set args
[lrange $args 1 end
]
92 [srv
$level "client"] {*}$args
95 proc reconnect
{args
} {
96 set level
[lindex $args 0]
97 if {[string length
$level] == 0 ||
![string is integer
$level]} {
101 set srv
[lindex $::servers end
+$level]
102 set host
[dict get
$srv "host"]
103 set port
[dict get
$srv "port"]
104 set config
[dict get
$srv "config"]
105 set client
[redis
$host $port]
106 dict
set srv
"client" $client
108 # select the right db when we don't have to authenticate
109 if {![dict exists
$config "requirepass"]} {
113 # re-set $srv in the servers list
114 set ::servers [lreplace $::servers end
+$level 1 $srv]
117 proc redis_deferring_client
{args
} {
119 if {[llength $args] > 0 && [string is integer
[lindex $args 0]]} {
120 set level
[lindex $args 0]
121 set args
[lrange $args 1 end
]
124 # create client that defers reading reply
125 set client
[redis
[srv
$level "host"] [srv
$level "port"] 1]
127 # select the right db and read the response (OK)
133 # Provide easy access to INFO properties. Same semantic as "proc r".
136 if {[string is integer
[lindex $args 0]]} {
137 set level
[lindex $args 0]
138 set args
[lrange $args 1 end
]
140 status
[srv
$level "client"] [lindex $args 0]
144 puts -nonewline "Cleanup: may take some time... "
146 catch {exec rm
-rf {*}[glob tests
/tmp
/redis.conf.
*]}
147 catch {exec rm
-rf {*}[glob tests
/tmp
/server.
*]}
151 proc test_server_main
{} {
153 # Open a listening socket, trying different ports in order to find a
157 puts "Starting test server at port $port"
158 if {[catch {socket -server accept_test_clients
$port} e
]} {
159 if {[string match
{*address already in use
*} $e]} {
160 if {$port == 20000} {
161 puts "Can't find an available TCP port for test server."
167 puts "Fatal error starting test server: $e"
175 # Start the client instances
176 set ::clients_pids {}
177 for {set j
0} {$j < $::numclients} {incr j
} {
178 set p
[exec tclsh8.5
[info script
] {*}$::argv \
179 --client $port --port [expr {$::port+($j*10)}] &]
180 lappend ::clients_pids $p
183 # Setup global state for the test server
184 set ::idle_clients {}
185 set ::active_clients {}
186 array set ::clients_start_time {}
187 set ::clients_time_history {}
188 set ::failed_tests {}
190 # Enter the event loop to handle clients I/O
191 after 100 test_server_cron
195 # This function gets called 10 times per second, for now does nothing but
196 # may be used in the future in order to detect test clients taking too much
197 # time to execute the task.
198 proc test_server_cron
{} {
201 proc accept_test_clients
{fd addr port
} {
202 fileevent $fd readable
[list read_from_test_client
$fd]
205 # This is the readable handler of our test server. Clients send us messages
206 # in the form of a status code such and additional data. Supported
209 # ready: the client is ready to execute the command. Only sent at client
210 # startup. The server will queue the client FD in the list of idle
212 # testing: just used to signal that a given test started.
213 # ok: a test was executed with success.
214 # err: a test was executed with an error.
215 # exception: there was a runtime exception while executing the test.
216 # done: all the specified test file was processed, this test client is
217 # ready to accept a new task.
218 proc read_from_test_client fd
{
220 set payload
[read $fd $bytes]
221 foreach {status data
} $payload break
222 if {$status eq
{ready
}} {
223 puts "\[$status\]: $data"
224 signal_idle_client
$fd
225 } elseif
{$status eq
{done
}} {
226 set elapsed
[expr {[clock seconds
]-$::clients_start_time($fd)}]
227 puts "\[[colorstr yellow $status]\]: $data ($elapsed seconds)"
228 puts "+++ [expr {[llength $::active_clients]-1}] units still in execution."
229 lappend ::clients_time_history $elapsed $data
230 signal_idle_client
$fd
231 } elseif
{$status eq
{ok
}} {
232 puts "\[[colorstr green $status]\]: $data"
233 } elseif
{$status eq
{err
}} {
234 set err
"\[[colorstr red $status]\]: $data"
236 lappend ::failed_tests $err
237 } elseif
{$status eq
{exception
}} {
238 puts "\[[colorstr red $status]\]: $data"
239 foreach p
$::clients_pids {
240 catch {exec kill
-9 $p}
243 } elseif
{$status eq
{testing
}} {
246 puts "\[$status\]: $data"
250 # A new client is idle. Remove it from the list of active clients and
251 # if there are still test units to run, launch them.
252 proc signal_idle_client fd
{
253 # Remove this fd from the list of active clients.
254 set ::active_clients \
255 [lsearch -all -inline -not -exact $::active_clients $fd]
256 # New unit to process?
257 if {$::next_test != [llength $::all_tests]} {
258 puts [colorstr bold-white
"Testing [lindex $::all_tests $::next_test]"]
259 set ::clients_start_time($fd) [clock seconds
]
260 send_data_packet
$fd run
[lindex $::all_tests $::next_test]
261 lappend ::active_clients $fd
264 lappend ::idle_clients $fd
265 if {[llength $::active_clients] == 0} {
271 # The the_end funciton gets called when all the test units were already
272 # executed, so the test finished.
274 # TODO: print the status, exit with the rigth exit code.
276 puts "Execution time of different units:"
277 foreach {time name
} $::clients_time_history {
278 puts " $time seconds - $name"
280 if {[llength $::failed_tests]} {
281 puts "\n[colorstr bold-red {!!! WARNING}] The following tests failed:\n"
282 foreach failed
$::failed_tests {
288 puts "\n[colorstr bold-white {\o/}] [colorstr bold-green {All tests passed without errors!}]\n"
294 # The client is not even driven (the test server is instead) as we just need
295 # to read the command, execute, reply... all this in a loop.
296 proc test_client_main server_port
{
297 set ::test_server_fd [socket localhost
$server_port]
298 send_data_packet
$::test_server_fd ready
[pid]
300 set bytes
[gets $::test_server_fd]
301 set payload
[read $::test_server_fd $bytes]
302 foreach {cmd data
} $payload break
306 error "Unknown test client command: $cmd"
311 proc send_data_packet
{fd status data
} {
312 set payload
[list $status $data]
313 puts $fd [string length
$payload]
314 puts -nonewline $fd $payload
318 proc print_help_screen
{} {
320 "--valgrind Run the test over valgrind."
321 "--accurate Run slow randomized tests for more iterations."
322 "--single <unit> Just execute the specified unit (see next option)."
323 "--list-tests List all the available test units."
324 "--force-failure Force the execution of a test that always fails."
325 "--help Print this help screen."
330 for {set j
0} {$j < [llength $argv]} {incr j
} {
331 set opt
[lindex $argv $j]
332 set arg
[lindex $argv [expr $j+1]]
333 if {$opt eq
{--tags}} {
335 if {[string index
$tag 0] eq
"-"} {
336 lappend ::denytags [string range
$tag 1 end
]
338 lappend ::allowtags $tag
342 } elseif
{$opt eq
{--valgrind}} {
344 } elseif
{$opt eq
{--host}} {
348 } elseif
{$opt eq
{--port}} {
351 } elseif
{$opt eq
{--accurate}} {
353 } elseif
{$opt eq
{--force-failure
}} {
354 set ::force_failure 1
355 } elseif
{$opt eq
{--single}} {
358 } elseif
{$opt eq
{--list-tests
}} {
359 foreach t
$::all_tests {
363 } elseif
{$opt eq
{--client}} {
365 set ::test_server_port $arg
367 } elseif
{$opt eq
{--help}} {
371 puts "Wrong argument: $opt"
377 if {[catch { test_client_main
$::test_server_port } err
]} {
378 set estr
"Executing test client: $err.\n$::errorInfo"
379 if {[catch {send_data_packet
$::test_server_fd exception
$estr}]} {
385 if {[catch { test_server_main
} err
]} {
386 if {[string length
$err] > 0} {
387 # only display error when not generated by the test suite
388 if {$err ne
"exception"} {