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
35 # Index to the next test to run in the ::all_tests list.
45 set ::external 0; # If "1" this means, we are running against external instance
46 set ::file ""; # If set, runs only the tests in this comma separated list
47 set ::curfile ""; # Hold the filename of the current suite
49 # Set to 1 when we are running in client mode. The Redis test uses a
50 # server-client model to run tests simultaneously. The server instance
51 # runs the specified number of client instances that will actually run tests.
52 # The server is responsible of showing the result to the user, and exit with
53 # the appropriate exit code depending on the test outcome.
57 proc execute_tests name
{
58 set path
"tests/$name.tcl"
61 send_data_packet
$::test_server_fd done
"$name"
64 # Setup a list to hold a stack of server configs. When calls to start_server
65 # are nested, use "srv 0 pid" to get the pid of the inner server. To access
66 # outer servers, use "srv -1 pid" etcetera.
70 if {[string is integer
[lindex $args 0]]} {
71 set level
[lindex $args 0]
72 set property
[lindex $args 1]
74 set property
[lindex $args 0]
76 set srv
[lindex $::servers end
+$level]
77 dict get
$srv $property
80 # Provide easy access to the client for the inner server. It's possible to
81 # prepend the argument list with a negative level to access clients for
82 # servers running in outer blocks.
85 if {[string is integer
[lindex $args 0]]} {
86 set level
[lindex $args 0]
87 set args
[lrange $args 1 end
]
89 [srv
$level "client"] {*}$args
92 proc reconnect
{args
} {
93 set level
[lindex $args 0]
94 if {[string length
$level] == 0 ||
![string is integer
$level]} {
98 set srv
[lindex $::servers end
+$level]
99 set host
[dict get
$srv "host"]
100 set port
[dict get
$srv "port"]
101 set config
[dict get
$srv "config"]
102 set client
[redis
$host $port]
103 dict
set srv
"client" $client
105 # select the right db when we don't have to authenticate
106 if {![dict exists
$config "requirepass"]} {
110 # re-set $srv in the servers list
111 set ::servers [lreplace $::servers end
+$level 1 $srv]
114 proc redis_deferring_client
{args
} {
116 if {[llength $args] > 0 && [string is integer
[lindex $args 0]]} {
117 set level
[lindex $args 0]
118 set args
[lrange $args 1 end
]
121 # create client that defers reading reply
122 set client
[redis
[srv
$level "host"] [srv
$level "port"] 1]
124 # select the right db and read the response (OK)
130 # Provide easy access to INFO properties. Same semantic as "proc r".
133 if {[string is integer
[lindex $args 0]]} {
134 set level
[lindex $args 0]
135 set args
[lrange $args 1 end
]
137 status
[srv
$level "client"] [lindex $args 0]
141 puts -nonewline "Cleanup: warning may take some time... "
143 catch {exec rm
-rf {*}[glob tests
/tmp
/redis.conf.
*]}
144 catch {exec rm
-rf {*}[glob tests
/tmp
/server.
*]}
148 proc test_server_main
{} {
150 # Open a listening socket, trying different ports in order to find a
154 puts "Starting test server at port $port"
155 if {[catch {socket -server accept_test_clients
$port} e
]} {
156 if {[string match
{*address already in use
*} $e]} {
157 if {$port == 20000} {
158 puts "Can't find an available TCP port for test server."
164 puts "Fatal error starting test server: $e"
172 # Start the client instances
173 set ::clients_pids {}
174 for {set j
0} {$j < $::numclients} {incr j
} {
175 set p
[exec tclsh8.5
[info script
] {*}$::argv \
176 --client $port --port [expr {$::port+($j*10)}] &]
177 lappend ::clients_pids $p
180 # Setup global state for the test server
181 set ::idle_clients {}
182 set ::active_clients {}
183 array set ::clients_start_time {}
184 set ::clients_time_history {}
186 # Enter the event loop to handle clients I/O
187 after 100 test_server_cron
191 # This function gets called 10 times per second, for now does nothing but
192 # may be used in the future in order to detect test clients taking too much
193 # time to execute the task.
194 proc test_server_cron
{} {
197 proc accept_test_clients
{fd addr port
} {
198 fileevent $fd readable
[list read_from_test_client
$fd]
201 # This is the readable handler of our test server. Clients send us messages
202 # in the form of a status code such and additional data. Supported
205 # ready: the client is ready to execute the command. Only sent at client
206 # startup. The server will queue the client FD in the list of idle
208 # testing: just used to signal that a given test started.
209 # ok: a test was executed with success.
210 # err: a test was executed with an error.
211 # exception: there was a runtime exception while executing the test.
212 # done: all the specified test file was processed, this test client is
213 # ready to accept a new task.
214 proc read_from_test_client fd
{
216 set payload
[read $fd $bytes]
217 foreach {status data
} $payload break
218 if {$status eq
{ready
}} {
219 puts "\[$status\]: $data"
220 signal_idle_client
$fd
221 } elseif
{$status eq
{done
}} {
222 set elapsed
[expr {[clock seconds
]-$::clients_start_time($fd)}]
223 puts "\[[colorstr yellow $status]\]: $data ($elapsed seconds)"
224 puts "+++ [expr {[llength $::active_clients]-1}] units still in execution."
225 lappend ::clients_time_history $elapsed $data
226 signal_idle_client
$fd
227 } elseif
{$status eq
{ok
}} {
228 puts "\[[colorstr green $status]\]: $data"
229 } elseif
{$status eq
{err
}} {
230 puts "\[[colorstr red $status]\]: $data"
231 } elseif
{$status eq
{exception
}} {
232 puts "\[[colorstr red $status]\]: $data"
233 foreach p
$::clients_pids {
234 catch {exec kill
-9 $p}
237 } elseif
{$status eq
{testing
}} {
240 puts "\[$status\]: $data"
244 # A new client is idle. Remove it from the list of active clients and
245 # if there are still test units to run, launch them.
246 proc signal_idle_client fd
{
247 # Remove this fd from the list of active clients.
248 set ::active_clients \
249 [lsearch -all -inline -not -exact $::active_clients $fd]
250 # New unit to process?
251 if {$::next_test != [llength $::all_tests]} {
252 puts [colorstr bold-white
"Testing [lindex $::all_tests $::next_test]"]
253 set ::clients_start_time($fd) [clock seconds
]
254 send_data_packet
$fd run
[lindex $::all_tests $::next_test]
255 lappend ::active_clients $fd
258 lappend ::idle_clients $fd
259 if {[llength $::active_clients] == 0} {
265 # The the_end funciton gets called when all the test units were already
266 # executed, so the test finished.
268 # TODO: print the status, exit with the rigth exit code.
270 puts "Execution time of different units:"
271 foreach {time name
} $::clients_time_history {
272 puts " $time seconds - $name"
277 # The client is not even driven (the test server is instead) as we just need
278 # to read the command, execute, reply... all this in a loop.
279 proc test_client_main server_port
{
280 set ::test_server_fd [socket localhost
$server_port]
281 send_data_packet
$::test_server_fd ready
[pid]
283 set bytes
[gets $::test_server_fd]
284 set payload
[read $::test_server_fd $bytes]
285 foreach {cmd data
} $payload break
289 error "Unknown test client command: $cmd"
294 proc send_data_packet
{fd status data
} {
295 set payload
[list $status $data]
296 puts $fd [string length
$payload]
297 puts -nonewline $fd $payload
302 for {set j
0} {$j < [llength $argv]} {incr j
} {
303 set opt
[lindex $argv $j]
304 set arg
[lindex $argv [expr $j+1]]
305 if {$opt eq
{--tags}} {
307 if {[string index
$tag 0] eq
"-"} {
308 lappend ::denytags [string range
$tag 1 end
]
310 lappend ::allowtags $tag
314 } elseif
{$opt eq
{--valgrind}} {
316 } elseif
{$opt eq
{--file}} {
319 } elseif
{$opt eq
{--host}} {
323 } elseif
{$opt eq
{--port}} {
326 } elseif
{$opt eq
{--verbose}} {
328 } elseif
{$opt eq
{--client}} {
330 set ::test_server_port $arg
333 puts "Wrong argument: $opt"
339 if {[catch { test_client_main
$::test_server_port } err
]} {
340 set estr
"Executing test client: $err.\n$::errorInfo"
341 if {[catch {send_data_packet
$::test_server_fd exception
$estr}]} {
347 if {[catch { test_server_main
} err
]} {
348 if {[string length
$err] > 0} {
349 # only display error when not generated by the test suite
350 if {$err ne
"exception"} {