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
26 integration
/replication
31 # Index to the next test to run in the ::all_tests list.
41 set ::external 0; # If "1" this means, we are running against external instance
42 set ::file ""; # If set, runs only the tests in this comma separated list
43 set ::curfile ""; # Hold the filename of the current suite
45 # Set to 1 when we are running in client mode. The Redis test uses a
46 # server-client model to run tests simultaneously. The server instance
47 # runs the specified number of client instances that will actually run tests.
48 # The server is responsible of showing the result to the user, and exit with
49 # the appropriate exit code depending on the test outcome.
53 proc execute_tests name
{
54 set path
"tests/$name.tcl"
57 send_data_packet
$::test_server_fd done
"$name finished"
60 # Setup a list to hold a stack of server configs. When calls to start_server
61 # are nested, use "srv 0 pid" to get the pid of the inner server. To access
62 # outer servers, use "srv -1 pid" etcetera.
66 if {[string is integer
[lindex $args 0]]} {
67 set level
[lindex $args 0]
68 set property
[lindex $args 1]
70 set property
[lindex $args 0]
72 set srv
[lindex $::servers end
+$level]
73 dict get
$srv $property
76 # Provide easy access to the client for the inner server. It's possible to
77 # prepend the argument list with a negative level to access clients for
78 # servers running in outer blocks.
81 if {[string is integer
[lindex $args 0]]} {
82 set level
[lindex $args 0]
83 set args
[lrange $args 1 end
]
85 [srv
$level "client"] {*}$args
88 proc reconnect
{args
} {
89 set level
[lindex $args 0]
90 if {[string length
$level] == 0 ||
![string is integer
$level]} {
94 set srv
[lindex $::servers end
+$level]
95 set host
[dict get
$srv "host"]
96 set port
[dict get
$srv "port"]
97 set config
[dict get
$srv "config"]
98 set client
[redis
$host $port]
99 dict
set srv
"client" $client
101 # select the right db when we don't have to authenticate
102 if {![dict exists
$config "requirepass"]} {
106 # re-set $srv in the servers list
107 set ::servers [lreplace $::servers end
+$level 1 $srv]
110 proc redis_deferring_client
{args
} {
112 if {[llength $args] > 0 && [string is integer
[lindex $args 0]]} {
113 set level
[lindex $args 0]
114 set args
[lrange $args 1 end
]
117 # create client that defers reading reply
118 set client
[redis
[srv
$level "host"] [srv
$level "port"] 1]
120 # select the right db and read the response (OK)
126 # Provide easy access to INFO properties. Same semantic as "proc r".
129 if {[string is integer
[lindex $args 0]]} {
130 set level
[lindex $args 0]
131 set args
[lrange $args 1 end
]
133 status
[srv
$level "client"] [lindex $args 0]
137 puts -nonewline "Cleanup: warning may take some time... "
139 catch {exec rm
-rf {*}[glob tests
/tmp
/redis.conf.
*]}
140 catch {exec rm
-rf {*}[glob tests
/tmp
/server.
*]}
144 proc test_server_main
{} {
146 # Open a listening socket, trying different ports in order to find a
150 puts "Starting test server at port $port"
151 if {[catch {socket -server accept_test_clients
$port} e
]} {
152 if {[string match
{*address already in use
*} $e]} {
153 if {$port == 20000} {
154 puts "Can't find an available TCP port for test server."
160 puts "Fatal error starting test server: $e"
168 # Start the client instances
169 for {set j
0} {$j < $::numclients} {incr j
} {
170 exec tclsh8.5
[info script
] {*}$::argv \
171 --client $port --port [expr {$::port+($j*10)}] &
174 # Setup global state for the test server
175 set ::idle_clients {}
176 set ::active_clients {}
178 # Enter the event loop to handle clients I/O
179 after 100 test_server_cron
183 # This function gets called 10 times per second, for now does nothing but
184 # may be used in the future in order to detect test clients taking too much
185 # time to execute the task.
186 proc test_server_cron
{} {
189 proc accept_test_clients
{fd addr port
} {
190 fileevent $fd readable
[list read_from_test_client
$fd]
193 # This is the readable handler of our test server. Clients send us messages
194 # in the form of a status code such and additional data. Supported
197 # ready: the client is ready to execute the command. Only sent at client
198 # startup. The server will queue the client FD in the list of idle
200 # testing: just used to signal that a given test started.
201 # ok: a test was executed with success.
202 # err: a test was executed with an error.
203 # exception: there was a runtime exception while executing the test.
204 # done: all the specified test file was processed, this test client is
205 # ready to accept a new task.
206 proc read_from_test_client fd
{
208 set payload
[read $fd $bytes]
209 foreach {status data
} $payload break
210 puts "($fd) \[$status\]: $data"
211 if {$status eq
{ready
}} {
212 signal_idle_client
$fd
213 } elseif
{$status eq
{done
}} {
214 signal_idle_client
$fd
215 puts "+++ [llength $::active_clients] units still in execution."
219 # A new client is idle. Remove it from the list of active clients and
220 # if there are still test units to run, launch them.
221 proc signal_idle_client fd
{
222 # Remove this fd from the list of active clients.
223 set ::active_clients \
224 [lsearch -all -inline -not -exact $::active_clients $fd]
225 # New unit to process?
226 if {$::next_test != [llength $::all_tests]} {
227 puts "Spawing new test process for: [lindex $::all_tests $::next_test]"
228 send_data_packet
$fd run
[lindex $::all_tests $::next_test]
229 lappend ::active_clients $fd
232 lappend ::idle_clients $fd
233 if {[llength $::active_clients] == 0} {
239 # The the_end funciton gets called when all the test units were already
240 # executed, so the test finished.
242 # TODO: print the status, exit with the rigth exit code.
247 # The client is not even driven (the test server is instead) as we just need
248 # to read the command, execute, reply... all this in a loop.
249 proc test_client_main server_port
{
250 set ::test_server_fd [socket localhost
$server_port]
251 send_data_packet
$::test_server_fd ready
[pid]
253 set bytes
[gets $::test_server_fd]
254 set payload
[read $::test_server_fd $bytes]
255 foreach {cmd data
} $payload break
259 error "Unknown test client command: $cmd"
264 proc send_data_packet
{fd status data
} {
265 set payload
[list $status $data]
266 puts $fd [string length
$payload]
267 puts -nonewline $fd $payload
272 for {set j
0} {$j < [llength $argv]} {incr j
} {
273 set opt
[lindex $argv $j]
274 set arg
[lindex $argv [expr $j+1]]
275 if {$opt eq
{--tags}} {
277 if {[string index
$tag 0] eq
"-"} {
278 lappend ::denytags [string range
$tag 1 end
]
280 lappend ::allowtags $tag
284 } elseif
{$opt eq
{--valgrind}} {
286 } elseif
{$opt eq
{--file}} {
289 } elseif
{$opt eq
{--host}} {
293 } elseif
{$opt eq
{--port}} {
296 } elseif
{$opt eq
{--verbose}} {
298 } elseif
{$opt eq
{--client}} {
300 set ::test_server_port $arg
303 puts "Wrong argument: $opt"
309 if {[catch { test_client_main
$::test_server_port } err
]} {
310 set estr
"Executing test client: $err.\n$::errorInfo"
311 if {[catch {send_data_packet
$::test_server_fd exception
$estr}]} {
317 if {[catch { test_server_main
} err
]} {
318 if {[string length
$err] > 0} {
319 # only display error when not generated by the test suite
320 if {$err ne
"exception"} {