]> git.saurik.com Git - redis.git/blame - tests/test_helper.tcl
list test split into smaller parts
[redis.git] / tests / test_helper.tcl
CommitLineData
98578b57
PN
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
3# more information.
4
5set tcl_precision 17
ab72b483 6source tests/support/redis.tcl
7source tests/support/server.tcl
8source tests/support/tmpfile.tcl
9source tests/support/test.tcl
10source tests/support/util.tcl
98578b57 11
13566085 12set ::all_tests {
13 unit/printver
14 unit/auth
15 unit/protocol
16 unit/basic
17 unit/type/list
18 unit/type/set
19 unit/type/zset
20 unit/type/hash
21 unit/sort
22 unit/expire
23 unit/other
24 unit/cas
25 unit/quit
26 integration/replication
569f84aa 27 integration/replication-2
28 integration/replication-3
13566085 29 integration/aof
30 unit/pubsub
31 unit/slowlog
32}
33# Index to the next test to run in the ::all_tests list.
34set ::next_test 0
35
98578b57 36set ::host 127.0.0.1
47bebf15 37set ::port 16379
e59a64b8 38set ::traceleaks 0
c4669d25 39set ::valgrind 0
322ea972 40set ::verbose 0
6e0e5bed
PN
41set ::denytags {}
42set ::allowtags {}
7d04fc75 43set ::external 0; # If "1" this means, we are running against external instance
9f1ae9ab 44set ::file ""; # If set, runs only the tests in this comma separated list
6f8a32d5 45set ::curfile ""; # Hold the filename of the current suite
13566085 46
47# Set to 1 when we are running in client mode. The Redis test uses a
48# server-client model to run tests simultaneously. The server instance
49# runs the specified number of client instances that will actually run tests.
50# The server is responsible of showing the result to the user, and exit with
51# the appropriate exit code depending on the test outcome.
52set ::client 0
53set ::numclients 16
98578b57
PN
54
55proc execute_tests name {
6f8a32d5
PN
56 set path "tests/$name.tcl"
57 set ::curfile $path
58 source $path
36e790a0 59 send_data_packet $::test_server_fd done "$name"
98578b57
PN
60}
61
1c4114be
PN
62# Setup a list to hold a stack of server configs. When calls to start_server
63# are nested, use "srv 0 pid" to get the pid of the inner server. To access
64# outer servers, use "srv -1 pid" etcetera.
65set ::servers {}
f2dd4769
PN
66proc srv {args} {
67 set level 0
68 if {[string is integer [lindex $args 0]]} {
69 set level [lindex $args 0]
70 set property [lindex $args 1]
71 } else {
72 set property [lindex $args 0]
73 }
1c4114be
PN
74 set srv [lindex $::servers end+$level]
75 dict get $srv $property
76}
77
78# Provide easy access to the client for the inner server. It's possible to
79# prepend the argument list with a negative level to access clients for
80# servers running in outer blocks.
98578b57 81proc r {args} {
1c4114be
PN
82 set level 0
83 if {[string is integer [lindex $args 0]]} {
84 set level [lindex $args 0]
85 set args [lrange $args 1 end]
86 }
87 [srv $level "client"] {*}$args
88}
89
941c9fa2
PN
90proc reconnect {args} {
91 set level [lindex $args 0]
92 if {[string length $level] == 0 || ![string is integer $level]} {
93 set level 0
94 }
95
96 set srv [lindex $::servers end+$level]
97 set host [dict get $srv "host"]
98 set port [dict get $srv "port"]
99 set config [dict get $srv "config"]
100 set client [redis $host $port]
101 dict set srv "client" $client
102
103 # select the right db when we don't have to authenticate
104 if {![dict exists $config "requirepass"]} {
105 $client select 9
106 }
107
108 # re-set $srv in the servers list
109 set ::servers [lreplace $::servers end+$level 1 $srv]
110}
111
5eedc9c6
PN
112proc redis_deferring_client {args} {
113 set level 0
114 if {[llength $args] > 0 && [string is integer [lindex $args 0]]} {
115 set level [lindex $args 0]
116 set args [lrange $args 1 end]
117 }
118
119 # create client that defers reading reply
120 set client [redis [srv $level "host"] [srv $level "port"] 1]
121
122 # select the right db and read the response (OK)
123 $client select 9
124 $client read
125 return $client
126}
127
1c4114be
PN
128# Provide easy access to INFO properties. Same semantic as "proc r".
129proc s {args} {
130 set level 0
131 if {[string is integer [lindex $args 0]]} {
132 set level [lindex $args 0]
133 set args [lrange $args 1 end]
134 }
135 status [srv $level "client"] [lindex $args 0]
98578b57
PN
136}
137
f166bb1d 138proc cleanup {} {
13566085 139 puts -nonewline "Cleanup: warning may take some time... "
140 flush stdout
c4669d25 141 catch {exec rm -rf {*}[glob tests/tmp/redis.conf.*]}
142 catch {exec rm -rf {*}[glob tests/tmp/server.*]}
13566085 143 puts "OK"
f166bb1d
PN
144}
145
13566085 146proc test_server_main {} {
147 cleanup
148 # Open a listening socket, trying different ports in order to find a
149 # non busy one.
150 set port 11111
151 while 1 {
152 puts "Starting test server at port $port"
153 if {[catch {socket -server accept_test_clients $port} e]} {
154 if {[string match {*address already in use*} $e]} {
155 if {$port == 20000} {
156 puts "Can't find an available TCP port for test server."
157 exit 1
158 } else {
159 incr port
160 }
161 } else {
162 puts "Fatal error starting test server: $e"
163 exit 1
164 }
165 } else {
166 break
167 }
3fe40d6e 168 }
169
13566085 170 # Start the client instances
569f84aa 171 set ::clients_pids {}
13566085 172 for {set j 0} {$j < $::numclients} {incr j} {
569f84aa 173 set p [exec tclsh8.5 [info script] {*}$::argv \
174 --client $port --port [expr {$::port+($j*10)}] &]
175 lappend ::clients_pids $p
13566085 176 }
9f1ae9ab 177
13566085 178 # Setup global state for the test server
179 set ::idle_clients {}
180 set ::active_clients {}
36e790a0 181 array set ::clients_start_time {}
182 set ::clients_time_history {}
13566085 183
184 # Enter the event loop to handle clients I/O
185 after 100 test_server_cron
186 vwait forever
187}
188
189# This function gets called 10 times per second, for now does nothing but
190# may be used in the future in order to detect test clients taking too much
191# time to execute the task.
192proc test_server_cron {} {
193}
194
195proc accept_test_clients {fd addr port} {
196 fileevent $fd readable [list read_from_test_client $fd]
197}
198
199# This is the readable handler of our test server. Clients send us messages
200# in the form of a status code such and additional data. Supported
201# status types are:
202#
203# ready: the client is ready to execute the command. Only sent at client
204# startup. The server will queue the client FD in the list of idle
205# clients.
206# testing: just used to signal that a given test started.
207# ok: a test was executed with success.
208# err: a test was executed with an error.
209# exception: there was a runtime exception while executing the test.
210# done: all the specified test file was processed, this test client is
211# ready to accept a new task.
212proc read_from_test_client fd {
213 set bytes [gets $fd]
214 set payload [read $fd $bytes]
215 foreach {status data} $payload break
13566085 216 if {$status eq {ready}} {
82e5dd35 217 puts "\[$status\]: $data"
13566085 218 signal_idle_client $fd
219 } elseif {$status eq {done}} {
36e790a0 220 set elapsed [expr {[clock seconds]-$::clients_start_time($fd)}]
82e5dd35 221 puts "\[[colorstr yellow $status]\]: $data ($elapsed seconds)"
569f84aa 222 puts "+++ [expr {[llength $::active_clients]-1}] units still in execution."
36e790a0 223 lappend ::clients_time_history $elapsed $data
13566085 224 signal_idle_client $fd
3744824c 225 } elseif {$status eq {ok}} {
82e5dd35 226 puts "\[[colorstr green $status]\]: $data"
3744824c 227 } elseif {$status eq {err}} {
82e5dd35 228 puts "\[[colorstr red $status]\]: $data"
569f84aa 229 } elseif {$status eq {exception}} {
230 puts "\[[colorstr red $status]\]: $data"
231 foreach p $::clients_pids {
232 catch {exec kill -9 $p}
233 }
234 exit 1
daab1599 235 } elseif {$status eq {testing}} {
236 # No op
3744824c 237 } else {
82e5dd35 238 puts "\[$status\]: $data"
13566085 239 }
240}
241
242# A new client is idle. Remove it from the list of active clients and
243# if there are still test units to run, launch them.
244proc signal_idle_client fd {
245 # Remove this fd from the list of active clients.
246 set ::active_clients \
247 [lsearch -all -inline -not -exact $::active_clients $fd]
248 # New unit to process?
249 if {$::next_test != [llength $::all_tests]} {
82e5dd35 250 puts [colorstr bold-white "Testing [lindex $::all_tests $::next_test]"]
36e790a0 251 set ::clients_start_time($fd) [clock seconds]
13566085 252 send_data_packet $fd run [lindex $::all_tests $::next_test]
253 lappend ::active_clients $fd
254 incr ::next_test
9f1ae9ab 255 } else {
13566085 256 lappend ::idle_clients $fd
257 if {[llength $::active_clients] == 0} {
258 the_end
259 }
9f1ae9ab 260 }
13566085 261}
e39c8b50 262
13566085 263# The the_end funciton gets called when all the test units were already
264# executed, so the test finished.
265proc the_end {} {
266 # TODO: print the status, exit with the rigth exit code.
36e790a0 267 puts "The End\n"
268 puts "Execution time of different units:"
269 foreach {time name} $::clients_time_history {
270 puts " $time seconds - $name"
271 }
13566085 272 exit 1
273}
6f8a32d5 274
13566085 275# The client is not even driven (the test server is instead) as we just need
276# to read the command, execute, reply... all this in a loop.
277proc test_client_main server_port {
278 set ::test_server_fd [socket localhost $server_port]
279 send_data_packet $::test_server_fd ready [pid]
280 while 1 {
281 set bytes [gets $::test_server_fd]
282 set payload [read $::test_server_fd $bytes]
283 foreach {cmd data} $payload break
284 if {$cmd eq {run}} {
285 execute_tests $data
286 } else {
287 error "Unknown test client command: $cmd"
6f8a32d5 288 }
98578b57 289 }
13566085 290}
cabe03eb 291
13566085 292proc send_data_packet {fd status data} {
293 set payload [list $status $data]
294 puts $fd [string length $payload]
295 puts -nonewline $fd $payload
296 flush $fd
98578b57
PN
297}
298
73bd6c58
PN
299# parse arguments
300for {set j 0} {$j < [llength $argv]} {incr j} {
301 set opt [lindex $argv $j]
302 set arg [lindex $argv [expr $j+1]]
303 if {$opt eq {--tags}} {
304 foreach tag $arg {
305 if {[string index $tag 0] eq "-"} {
306 lappend ::denytags [string range $tag 1 end]
307 } else {
308 lappend ::allowtags $tag
309 }
310 }
311 incr j
4b918769 312 } elseif {$opt eq {--valgrind}} {
313 set ::valgrind 1
9f1ae9ab
PN
314 } elseif {$opt eq {--file}} {
315 set ::file $arg
316 incr j
7d04fc75 317 } elseif {$opt eq {--host}} {
318 set ::external 1
319 set ::host $arg
320 incr j
321 } elseif {$opt eq {--port}} {
322 set ::port $arg
323 incr j
6f8a32d5
PN
324 } elseif {$opt eq {--verbose}} {
325 set ::verbose 1
13566085 326 } elseif {$opt eq {--client}} {
327 set ::client 1
328 set ::test_server_port $arg
329 incr j
73bd6c58
PN
330 } else {
331 puts "Wrong argument: $opt"
332 exit 1
333 }
334}
335
13566085 336if {$::client} {
337 if {[catch { test_client_main $::test_server_port } err]} {
338 set estr "Executing test client: $err.\n$::errorInfo"
339 if {[catch {send_data_packet $::test_server_fd exception $estr}]} {
340 puts $estr
436f18b6
PN
341 }
342 exit 1
343 }
13566085 344} else {
345 if {[catch { test_server_main } err]} {
346 if {[string length $err] > 0} {
347 # only display error when not generated by the test suite
348 if {$err ne "exception"} {
349 puts $::errorInfo
350 }
351 exit 1
352 }
353 }
436f18b6 354}