]> git.saurik.com Git - redis.git/blob - tests/test_helper.tcl
the test runs less iterations of slow tests if no --accurate is given.
[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 set ::accurate 0; # If true runs fuzz tests with more iterations
49
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.
55 set ::client 0
56 set ::numclients 16
57
58 proc execute_tests name {
59 set path "tests/$name.tcl"
60 set ::curfile $path
61 source $path
62 send_data_packet $::test_server_fd done "$name"
63 }
64
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.
68 set ::servers {}
69 proc srv {args} {
70 set level 0
71 if {[string is integer [lindex $args 0]]} {
72 set level [lindex $args 0]
73 set property [lindex $args 1]
74 } else {
75 set property [lindex $args 0]
76 }
77 set srv [lindex $::servers end+$level]
78 dict get $srv $property
79 }
80
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.
84 proc r {args} {
85 set level 0
86 if {[string is integer [lindex $args 0]]} {
87 set level [lindex $args 0]
88 set args [lrange $args 1 end]
89 }
90 [srv $level "client"] {*}$args
91 }
92
93 proc reconnect {args} {
94 set level [lindex $args 0]
95 if {[string length $level] == 0 || ![string is integer $level]} {
96 set level 0
97 }
98
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
105
106 # select the right db when we don't have to authenticate
107 if {![dict exists $config "requirepass"]} {
108 $client select 9
109 }
110
111 # re-set $srv in the servers list
112 set ::servers [lreplace $::servers end+$level 1 $srv]
113 }
114
115 proc redis_deferring_client {args} {
116 set level 0
117 if {[llength $args] > 0 && [string is integer [lindex $args 0]]} {
118 set level [lindex $args 0]
119 set args [lrange $args 1 end]
120 }
121
122 # create client that defers reading reply
123 set client [redis [srv $level "host"] [srv $level "port"] 1]
124
125 # select the right db and read the response (OK)
126 $client select 9
127 $client read
128 return $client
129 }
130
131 # Provide easy access to INFO properties. Same semantic as "proc r".
132 proc s {args} {
133 set level 0
134 if {[string is integer [lindex $args 0]]} {
135 set level [lindex $args 0]
136 set args [lrange $args 1 end]
137 }
138 status [srv $level "client"] [lindex $args 0]
139 }
140
141 proc cleanup {} {
142 puts -nonewline "Cleanup: warning may take some time... "
143 flush stdout
144 catch {exec rm -rf {*}[glob tests/tmp/redis.conf.*]}
145 catch {exec rm -rf {*}[glob tests/tmp/server.*]}
146 puts "OK"
147 }
148
149 proc test_server_main {} {
150 cleanup
151 # Open a listening socket, trying different ports in order to find a
152 # non busy one.
153 set port 11111
154 while 1 {
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."
160 exit 1
161 } else {
162 incr port
163 }
164 } else {
165 puts "Fatal error starting test server: $e"
166 exit 1
167 }
168 } else {
169 break
170 }
171 }
172
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
179 }
180
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 {}
186
187 # Enter the event loop to handle clients I/O
188 after 100 test_server_cron
189 vwait forever
190 }
191
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 {} {
196 }
197
198 proc accept_test_clients {fd addr port} {
199 fileevent $fd readable [list read_from_test_client $fd]
200 }
201
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
204 # status types are:
205 #
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
208 # clients.
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 {
216 set bytes [gets $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}
236 }
237 exit 1
238 } elseif {$status eq {testing}} {
239 # No op
240 } else {
241 puts "\[$status\]: $data"
242 }
243 }
244
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
257 incr ::next_test
258 } else {
259 lappend ::idle_clients $fd
260 if {[llength $::active_clients] == 0} {
261 the_end
262 }
263 }
264 }
265
266 # The the_end funciton gets called when all the test units were already
267 # executed, so the test finished.
268 proc the_end {} {
269 # TODO: print the status, exit with the rigth exit code.
270 puts "The End\n"
271 puts "Execution time of different units:"
272 foreach {time name} $::clients_time_history {
273 puts " $time seconds - $name"
274 }
275 exit 1
276 }
277
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]
283 while 1 {
284 set bytes [gets $::test_server_fd]
285 set payload [read $::test_server_fd $bytes]
286 foreach {cmd data} $payload break
287 if {$cmd eq {run}} {
288 execute_tests $data
289 } else {
290 error "Unknown test client command: $cmd"
291 }
292 }
293 }
294
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
299 flush $fd
300 }
301
302 # parse arguments
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}} {
307 foreach tag $arg {
308 if {[string index $tag 0] eq "-"} {
309 lappend ::denytags [string range $tag 1 end]
310 } else {
311 lappend ::allowtags $tag
312 }
313 }
314 incr j
315 } elseif {$opt eq {--valgrind}} {
316 set ::valgrind 1
317 } elseif {$opt eq {--file}} {
318 set ::file $arg
319 incr j
320 } elseif {$opt eq {--host}} {
321 set ::external 1
322 set ::host $arg
323 incr j
324 } elseif {$opt eq {--port}} {
325 set ::port $arg
326 incr j
327 } elseif {$opt eq {--verbose}} {
328 set ::verbose 1
329 } elseif {$opt eq {--accurate}} {
330 set ::accurate 1
331 } elseif {$opt eq {--single}} {
332 set ::all_tests $arg
333 incr j
334 } elseif {$opt eq {--list-tests}} {
335 foreach t $::all_tests {
336 puts $t
337 }
338 exit 0
339 } elseif {$opt eq {--client}} {
340 set ::client 1
341 set ::test_server_port $arg
342 incr j
343 } elseif {$opt eq {--help}} {
344 puts "TODO print an help screen"
345 exit 0
346 } else {
347 puts "Wrong argument: $opt"
348 exit 1
349 }
350 }
351
352 if {$::client} {
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}]} {
356 puts $estr
357 }
358 exit 1
359 }
360 } else {
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"} {
365 puts $::errorInfo
366 }
367 exit 1
368 }
369 }
370 }