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
48 set ::accurate 0; # If true runs fuzz tests with more iterations
50 # Set to 1 when we are running in client mode. The Redis test uses a
51 # server-client model to run tests simultaneously. The server instance
52 # runs the specified number of client instances that will actually run tests.
53 # The server is responsible of showing the result to the user, and exit with
54 # the appropriate exit code depending on the test outcome.
58 proc execute_tests name
{
59 set path
"tests/$name.tcl"
62 send_data_packet
$::test_server_fd done
"$name"
65 # Setup a list to hold a stack of server configs. When calls to start_server
66 # are nested, use "srv 0 pid" to get the pid of the inner server. To access
67 # outer servers, use "srv -1 pid" etcetera.
71 if {[string is integer
[lindex $args 0]]} {
72 set level
[lindex $args 0]
73 set property
[lindex $args 1]
75 set property
[lindex $args 0]
77 set srv
[lindex $::servers end
+$level]
78 dict get
$srv $property
81 # Provide easy access to the client for the inner server. It's possible to
82 # prepend the argument list with a negative level to access clients for
83 # servers running in outer blocks.
86 if {[string is integer
[lindex $args 0]]} {
87 set level
[lindex $args 0]
88 set args
[lrange $args 1 end
]
90 [srv
$level "client"] {*}$args
93 proc reconnect
{args
} {
94 set level
[lindex $args 0]
95 if {[string length
$level] == 0 ||
![string is integer
$level]} {
99 set srv
[lindex $::servers end
+$level]
100 set host
[dict get
$srv "host"]
101 set port
[dict get
$srv "port"]
102 set config
[dict get
$srv "config"]
103 set client
[redis
$host $port]
104 dict
set srv
"client" $client
106 # select the right db when we don't have to authenticate
107 if {![dict exists
$config "requirepass"]} {
111 # re-set $srv in the servers list
112 set ::servers [lreplace $::servers end
+$level 1 $srv]
115 proc redis_deferring_client
{args
} {
117 if {[llength $args] > 0 && [string is integer
[lindex $args 0]]} {
118 set level
[lindex $args 0]
119 set args
[lrange $args 1 end
]
122 # create client that defers reading reply
123 set client
[redis
[srv
$level "host"] [srv
$level "port"] 1]
125 # select the right db and read the response (OK)
131 # Provide easy access to INFO properties. Same semantic as "proc r".
134 if {[string is integer
[lindex $args 0]]} {
135 set level
[lindex $args 0]
136 set args
[lrange $args 1 end
]
138 status
[srv
$level "client"] [lindex $args 0]
142 puts -nonewline "Cleanup: warning may take some time... "
144 catch {exec rm
-rf {*}[glob tests
/tmp
/redis.conf.
*]}
145 catch {exec rm
-rf {*}[glob tests
/tmp
/server.
*]}
149 proc test_server_main
{} {
151 # Open a listening socket, trying different ports in order to find a
155 puts "Starting test server at port $port"
156 if {[catch {socket -server accept_test_clients
$port} e
]} {
157 if {[string match
{*address already in use
*} $e]} {
158 if {$port == 20000} {
159 puts "Can't find an available TCP port for test server."
165 puts "Fatal error starting test server: $e"
173 # Start the client instances
174 set ::clients_pids {}
175 for {set j
0} {$j < $::numclients} {incr j
} {
176 set p
[exec tclsh8.5
[info script
] {*}$::argv \
177 --client $port --port [expr {$::port+($j*10)}] &]
178 lappend ::clients_pids $p
181 # Setup global state for the test server
182 set ::idle_clients {}
183 set ::active_clients {}
184 array set ::clients_start_time {}
185 set ::clients_time_history {}
187 # Enter the event loop to handle clients I/O
188 after 100 test_server_cron
192 # This function gets called 10 times per second, for now does nothing but
193 # may be used in the future in order to detect test clients taking too much
194 # time to execute the task.
195 proc test_server_cron
{} {
198 proc accept_test_clients
{fd addr port
} {
199 fileevent $fd readable
[list read_from_test_client
$fd]
202 # This is the readable handler of our test server. Clients send us messages
203 # in the form of a status code such and additional data. Supported
206 # ready: the client is ready to execute the command. Only sent at client
207 # startup. The server will queue the client FD in the list of idle
209 # testing: just used to signal that a given test started.
210 # ok: a test was executed with success.
211 # err: a test was executed with an error.
212 # exception: there was a runtime exception while executing the test.
213 # done: all the specified test file was processed, this test client is
214 # ready to accept a new task.
215 proc read_from_test_client fd
{
217 set payload
[read $fd $bytes]
218 foreach {status data
} $payload break
219 if {$status eq
{ready
}} {
220 puts "\[$status\]: $data"
221 signal_idle_client
$fd
222 } elseif
{$status eq
{done
}} {
223 set elapsed
[expr {[clock seconds
]-$::clients_start_time($fd)}]
224 puts "\[[colorstr yellow $status]\]: $data ($elapsed seconds)"
225 puts "+++ [expr {[llength $::active_clients]-1}] units still in execution."
226 lappend ::clients_time_history $elapsed $data
227 signal_idle_client
$fd
228 } elseif
{$status eq
{ok
}} {
229 puts "\[[colorstr green $status]\]: $data"
230 } elseif
{$status eq
{err
}} {
231 puts "\[[colorstr red $status]\]: $data"
232 } elseif
{$status eq
{exception
}} {
233 puts "\[[colorstr red $status]\]: $data"
234 foreach p
$::clients_pids {
235 catch {exec kill
-9 $p}
238 } elseif
{$status eq
{testing
}} {
241 puts "\[$status\]: $data"
245 # A new client is idle. Remove it from the list of active clients and
246 # if there are still test units to run, launch them.
247 proc signal_idle_client fd
{
248 # Remove this fd from the list of active clients.
249 set ::active_clients \
250 [lsearch -all -inline -not -exact $::active_clients $fd]
251 # New unit to process?
252 if {$::next_test != [llength $::all_tests]} {
253 puts [colorstr bold-white
"Testing [lindex $::all_tests $::next_test]"]
254 set ::clients_start_time($fd) [clock seconds
]
255 send_data_packet
$fd run
[lindex $::all_tests $::next_test]
256 lappend ::active_clients $fd
259 lappend ::idle_clients $fd
260 if {[llength $::active_clients] == 0} {
266 # The the_end funciton gets called when all the test units were already
267 # executed, so the test finished.
269 # TODO: print the status, exit with the rigth exit code.
271 puts "Execution time of different units:"
272 foreach {time name
} $::clients_time_history {
273 puts " $time seconds - $name"
278 # The client is not even driven (the test server is instead) as we just need
279 # to read the command, execute, reply... all this in a loop.
280 proc test_client_main server_port
{
281 set ::test_server_fd [socket localhost
$server_port]
282 send_data_packet
$::test_server_fd ready
[pid]
284 set bytes
[gets $::test_server_fd]
285 set payload
[read $::test_server_fd $bytes]
286 foreach {cmd data
} $payload break
290 error "Unknown test client command: $cmd"
295 proc send_data_packet
{fd status data
} {
296 set payload
[list $status $data]
297 puts $fd [string length
$payload]
298 puts -nonewline $fd $payload
303 for {set j
0} {$j < [llength $argv]} {incr j
} {
304 set opt
[lindex $argv $j]
305 set arg
[lindex $argv [expr $j+1]]
306 if {$opt eq
{--tags}} {
308 if {[string index
$tag 0] eq
"-"} {
309 lappend ::denytags [string range
$tag 1 end
]
311 lappend ::allowtags $tag
315 } elseif
{$opt eq
{--valgrind}} {
317 } elseif
{$opt eq
{--file}} {
320 } elseif
{$opt eq
{--host}} {
324 } elseif
{$opt eq
{--port}} {
327 } elseif
{$opt eq
{--verbose}} {
329 } elseif
{$opt eq
{--accurate}} {
331 } elseif
{$opt eq
{--single}} {
334 } elseif
{$opt eq
{--list-tests
}} {
335 foreach t
$::all_tests {
339 } elseif
{$opt eq
{--client}} {
341 set ::test_server_port $arg
343 } elseif
{$opt eq
{--help}} {
344 puts "TODO print an help screen"
347 puts "Wrong argument: $opt"
353 if {[catch { test_client_main
$::test_server_port } err
]} {
354 set estr
"Executing test client: $err.\n$::errorInfo"
355 if {[catch {send_data_packet
$::test_server_fd exception
$estr}]} {
361 if {[catch { test_server_main
} err
]} {
362 if {[string length
$err] > 0} {
363 # only display error when not generated by the test suite
364 if {$err ne
"exception"} {