]> git.saurik.com Git - redis.git/blob - tests/test_helper.tcl
8629fa57ca9c14ffa4940b35ea904c0eeb389df3
[redis.git] / tests / test_helper.tcl
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
5 set tcl_precision 17
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
11
12 set ::all_tests {
13 unit/printver
14 unit/auth
15 unit/protocol
16 unit/basic
17 unit/type/list
18 unit/type/list-2
19 unit/type/list-3
20 unit/type/set
21 unit/type/zset
22 unit/type/hash
23 unit/sort
24 unit/expire
25 unit/other
26 unit/cas
27 unit/quit
28 integration/replication
29 integration/replication-2
30 integration/replication-3
31 integration/aof
32 unit/pubsub
33 unit/slowlog
34 }
35 # Index to the next test to run in the ::all_tests list.
36 set ::next_test 0
37
38 set ::host 127.0.0.1
39 set ::port 16379
40 set ::traceleaks 0
41 set ::valgrind 0
42 set ::verbose 0
43 set ::denytags {}
44 set ::allowtags {}
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
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.
54 set ::client 0
55 set ::numclients 16
56
57 proc execute_tests name {
58 set path "tests/$name.tcl"
59 set ::curfile $path
60 source $path
61 send_data_packet $::test_server_fd done "$name"
62 }
63
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.
67 set ::servers {}
68 proc srv {args} {
69 set level 0
70 if {[string is integer [lindex $args 0]]} {
71 set level [lindex $args 0]
72 set property [lindex $args 1]
73 } else {
74 set property [lindex $args 0]
75 }
76 set srv [lindex $::servers end+$level]
77 dict get $srv $property
78 }
79
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.
83 proc r {args} {
84 set level 0
85 if {[string is integer [lindex $args 0]]} {
86 set level [lindex $args 0]
87 set args [lrange $args 1 end]
88 }
89 [srv $level "client"] {*}$args
90 }
91
92 proc reconnect {args} {
93 set level [lindex $args 0]
94 if {[string length $level] == 0 || ![string is integer $level]} {
95 set level 0
96 }
97
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
104
105 # select the right db when we don't have to authenticate
106 if {![dict exists $config "requirepass"]} {
107 $client select 9
108 }
109
110 # re-set $srv in the servers list
111 set ::servers [lreplace $::servers end+$level 1 $srv]
112 }
113
114 proc redis_deferring_client {args} {
115 set level 0
116 if {[llength $args] > 0 && [string is integer [lindex $args 0]]} {
117 set level [lindex $args 0]
118 set args [lrange $args 1 end]
119 }
120
121 # create client that defers reading reply
122 set client [redis [srv $level "host"] [srv $level "port"] 1]
123
124 # select the right db and read the response (OK)
125 $client select 9
126 $client read
127 return $client
128 }
129
130 # Provide easy access to INFO properties. Same semantic as "proc r".
131 proc s {args} {
132 set level 0
133 if {[string is integer [lindex $args 0]]} {
134 set level [lindex $args 0]
135 set args [lrange $args 1 end]
136 }
137 status [srv $level "client"] [lindex $args 0]
138 }
139
140 proc cleanup {} {
141 puts -nonewline "Cleanup: warning may take some time... "
142 flush stdout
143 catch {exec rm -rf {*}[glob tests/tmp/redis.conf.*]}
144 catch {exec rm -rf {*}[glob tests/tmp/server.*]}
145 puts "OK"
146 }
147
148 proc test_server_main {} {
149 cleanup
150 # Open a listening socket, trying different ports in order to find a
151 # non busy one.
152 set port 11111
153 while 1 {
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."
159 exit 1
160 } else {
161 incr port
162 }
163 } else {
164 puts "Fatal error starting test server: $e"
165 exit 1
166 }
167 } else {
168 break
169 }
170 }
171
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
178 }
179
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 {}
185
186 # Enter the event loop to handle clients I/O
187 after 100 test_server_cron
188 vwait forever
189 }
190
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 {} {
195 }
196
197 proc accept_test_clients {fd addr port} {
198 fileevent $fd readable [list read_from_test_client $fd]
199 }
200
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
203 # status types are:
204 #
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
207 # clients.
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 {
215 set bytes [gets $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}
235 }
236 exit 1
237 } elseif {$status eq {testing}} {
238 # No op
239 } else {
240 puts "\[$status\]: $data"
241 }
242 }
243
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
256 incr ::next_test
257 } else {
258 lappend ::idle_clients $fd
259 if {[llength $::active_clients] == 0} {
260 the_end
261 }
262 }
263 }
264
265 # The the_end funciton gets called when all the test units were already
266 # executed, so the test finished.
267 proc the_end {} {
268 # TODO: print the status, exit with the rigth exit code.
269 puts "The End\n"
270 puts "Execution time of different units:"
271 foreach {time name} $::clients_time_history {
272 puts " $time seconds - $name"
273 }
274 exit 1
275 }
276
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]
282 while 1 {
283 set bytes [gets $::test_server_fd]
284 set payload [read $::test_server_fd $bytes]
285 foreach {cmd data} $payload break
286 if {$cmd eq {run}} {
287 execute_tests $data
288 } else {
289 error "Unknown test client command: $cmd"
290 }
291 }
292 }
293
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
298 flush $fd
299 }
300
301 # parse arguments
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}} {
306 foreach tag $arg {
307 if {[string index $tag 0] eq "-"} {
308 lappend ::denytags [string range $tag 1 end]
309 } else {
310 lappend ::allowtags $tag
311 }
312 }
313 incr j
314 } elseif {$opt eq {--valgrind}} {
315 set ::valgrind 1
316 } elseif {$opt eq {--file}} {
317 set ::file $arg
318 incr j
319 } elseif {$opt eq {--host}} {
320 set ::external 1
321 set ::host $arg
322 incr j
323 } elseif {$opt eq {--port}} {
324 set ::port $arg
325 incr j
326 } elseif {$opt eq {--verbose}} {
327 set ::verbose 1
328 } elseif {$opt eq {--client}} {
329 set ::client 1
330 set ::test_server_port $arg
331 incr j
332 } else {
333 puts "Wrong argument: $opt"
334 exit 1
335 }
336 }
337
338 if {$::client} {
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}]} {
342 puts $estr
343 }
344 exit 1
345 }
346 } else {
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"} {
351 puts $::errorInfo
352 }
353 exit 1
354 }
355 }
356 }