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