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