]> git.saurik.com Git - redis.git/blame - tests/test_helper.tcl
commenting out aof-race test for now, waiting for a fix to issue #80
[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
be9250c8 18 unit/type/list-2
6209797d 19 unit/type/list-3
13566085 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
569f84aa 29 integration/replication-2
30 integration/replication-3
13566085 31 integration/aof
32 unit/pubsub
33 unit/slowlog
0681c5ad 34 unit/scripting
243b783f 35 unit/maxmemory
13566085 36}
37# Index to the next test to run in the ::all_tests list.
38set ::next_test 0
39
98578b57 40set ::host 127.0.0.1
47bebf15 41set ::port 16379
e59a64b8 42set ::traceleaks 0
c4669d25 43set ::valgrind 0
322ea972 44set ::verbose 0
6e0e5bed
PN
45set ::denytags {}
46set ::allowtags {}
7d04fc75 47set ::external 0; # If "1" this means, we are running against external instance
9f1ae9ab 48set ::file ""; # If set, runs only the tests in this comma separated list
6f8a32d5 49set ::curfile ""; # Hold the filename of the current suite
524d515f 50set ::accurate 0; # If true runs fuzz tests with more iterations
04e2410d 51set ::force_failure 0
13566085 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.
58set ::client 0
59set ::numclients 16
98578b57
PN
60
61proc execute_tests name {
6f8a32d5
PN
62 set path "tests/$name.tcl"
63 set ::curfile $path
64 source $path
36e790a0 65 send_data_packet $::test_server_fd done "$name"
98578b57
PN
66}
67
1c4114be
PN
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.
71set ::servers {}
f2dd4769
PN
72proc 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 }
1c4114be
PN
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.
98578b57 87proc r {args} {
1c4114be
PN
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
941c9fa2
PN
96proc 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
5eedc9c6
PN
118proc 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
1c4114be
PN
134# Provide easy access to INFO properties. Same semantic as "proc r".
135proc 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]
98578b57
PN
142}
143
f166bb1d 144proc cleanup {} {
c7c16a32 145 puts -nonewline "Cleanup: may take some time... "
13566085 146 flush stdout
c4669d25 147 catch {exec rm -rf {*}[glob tests/tmp/redis.conf.*]}
148 catch {exec rm -rf {*}[glob tests/tmp/server.*]}
13566085 149 puts "OK"
f166bb1d
PN
150}
151
13566085 152proc test_server_main {} {
9f1ae9ab 153 cleanup
13566085 154 # Open a listening socket, trying different ports in order to find a
155 # non busy one.
156 set port 11111
157 while 1 {
158 puts "Starting test server at port $port"
159 if {[catch {socket -server accept_test_clients $port} e]} {
160 if {[string match {*address already in use*} $e]} {
161 if {$port == 20000} {
162 puts "Can't find an available TCP port for test server."
163 exit 1
164 } else {
165 incr port
166 }
167 } else {
168 puts "Fatal error starting test server: $e"
169 exit 1
170 }
171 } else {
172 break
173 }
3fe40d6e 174 }
175
13566085 176 # Start the client instances
569f84aa 177 set ::clients_pids {}
13566085 178 for {set j 0} {$j < $::numclients} {incr j} {
569f84aa 179 set p [exec tclsh8.5 [info script] {*}$::argv \
180 --client $port --port [expr {$::port+($j*10)}] &]
181 lappend ::clients_pids $p
13566085 182 }
9f1ae9ab 183
13566085 184 # Setup global state for the test server
185 set ::idle_clients {}
186 set ::active_clients {}
36e790a0 187 array set ::clients_start_time {}
188 set ::clients_time_history {}
04e2410d 189 set ::failed_tests {}
13566085 190
191 # Enter the event loop to handle clients I/O
192 after 100 test_server_cron
193 vwait forever
194}
195
196# This function gets called 10 times per second, for now does nothing but
197# may be used in the future in order to detect test clients taking too much
198# time to execute the task.
199proc test_server_cron {} {
200}
201
202proc accept_test_clients {fd addr port} {
203 fileevent $fd readable [list read_from_test_client $fd]
204}
205
206# This is the readable handler of our test server. Clients send us messages
207# in the form of a status code such and additional data. Supported
208# status types are:
209#
210# ready: the client is ready to execute the command. Only sent at client
211# startup. The server will queue the client FD in the list of idle
212# clients.
213# testing: just used to signal that a given test started.
214# ok: a test was executed with success.
215# err: a test was executed with an error.
216# exception: there was a runtime exception while executing the test.
217# done: all the specified test file was processed, this test client is
218# ready to accept a new task.
219proc read_from_test_client fd {
220 set bytes [gets $fd]
221 set payload [read $fd $bytes]
222 foreach {status data} $payload break
13566085 223 if {$status eq {ready}} {
82e5dd35 224 puts "\[$status\]: $data"
13566085 225 signal_idle_client $fd
226 } elseif {$status eq {done}} {
36e790a0 227 set elapsed [expr {[clock seconds]-$::clients_start_time($fd)}]
82e5dd35 228 puts "\[[colorstr yellow $status]\]: $data ($elapsed seconds)"
569f84aa 229 puts "+++ [expr {[llength $::active_clients]-1}] units still in execution."
36e790a0 230 lappend ::clients_time_history $elapsed $data
13566085 231 signal_idle_client $fd
3744824c 232 } elseif {$status eq {ok}} {
82e5dd35 233 puts "\[[colorstr green $status]\]: $data"
3744824c 234 } elseif {$status eq {err}} {
04e2410d 235 set err "\[[colorstr red $status]\]: $data"
236 puts $err
237 lappend ::failed_tests $err
569f84aa 238 } elseif {$status eq {exception}} {
239 puts "\[[colorstr red $status]\]: $data"
240 foreach p $::clients_pids {
241 catch {exec kill -9 $p}
9f1ae9ab 242 }
569f84aa 243 exit 1
daab1599 244 } elseif {$status eq {testing}} {
245 # No op
9f1ae9ab 246 } else {
82e5dd35 247 puts "\[$status\]: $data"
9f1ae9ab 248 }
13566085 249}
e39c8b50 250
13566085 251# A new client is idle. Remove it from the list of active clients and
252# if there are still test units to run, launch them.
253proc signal_idle_client fd {
254 # Remove this fd from the list of active clients.
255 set ::active_clients \
256 [lsearch -all -inline -not -exact $::active_clients $fd]
257 # New unit to process?
258 if {$::next_test != [llength $::all_tests]} {
82e5dd35 259 puts [colorstr bold-white "Testing [lindex $::all_tests $::next_test]"]
36e790a0 260 set ::clients_start_time($fd) [clock seconds]
13566085 261 send_data_packet $fd run [lindex $::all_tests $::next_test]
262 lappend ::active_clients $fd
263 incr ::next_test
9f1ae9ab 264 } else {
13566085 265 lappend ::idle_clients $fd
266 if {[llength $::active_clients] == 0} {
267 the_end
6f8a32d5 268 }
9f1ae9ab 269 }
13566085 270}
6f8a32d5 271
13566085 272# The the_end funciton gets called when all the test units were already
273# executed, so the test finished.
274proc the_end {} {
275 # TODO: print the status, exit with the rigth exit code.
04e2410d 276 puts "\n The End\n"
36e790a0 277 puts "Execution time of different units:"
278 foreach {time name} $::clients_time_history {
279 puts " $time seconds - $name"
280 }
04e2410d 281 if {[llength $::failed_tests]} {
121ffc85 282 puts "\n[colorstr bold-red {!!! WARNING}] The following tests failed:\n"
04e2410d 283 foreach failed $::failed_tests {
284 puts "*** $failed"
285 }
c7c16a32 286 cleanup
e39c8b50 287 exit 1
04e2410d 288 } else {
289 puts "\n[colorstr bold-white {\o/}] [colorstr bold-green {All tests passed without errors!}]\n"
c7c16a32 290 cleanup
04e2410d 291 exit 0
98578b57 292 }
98578b57
PN
293}
294
13566085 295# The client is not even driven (the test server is instead) as we just need
296# to read the command, execute, reply... all this in a loop.
297proc test_client_main server_port {
298 set ::test_server_fd [socket localhost $server_port]
299 send_data_packet $::test_server_fd ready [pid]
300 while 1 {
301 set bytes [gets $::test_server_fd]
302 set payload [read $::test_server_fd $bytes]
303 foreach {cmd data} $payload break
304 if {$cmd eq {run}} {
305 execute_tests $data
306 } else {
307 error "Unknown test client command: $cmd"
6f8a32d5 308 }
98578b57 309 }
13566085 310}
cabe03eb 311
13566085 312proc send_data_packet {fd status data} {
313 set payload [list $status $data]
314 puts $fd [string length $payload]
315 puts -nonewline $fd $payload
316 flush $fd
98578b57
PN
317}
318
e4715f00 319proc print_help_screen {} {
320 puts [join {
321 "--valgrind Run the test over valgrind."
322 "--accurate Run slow randomized tests for more iterations."
323 "--single <unit> Just execute the specified unit (see next option)."
324 "--list-tests List all the available test units."
325 "--force-failure Force the execution of a test that always fails."
326 "--help Print this help screen."
327 } "\n"]
328}
329
73bd6c58
PN
330# parse arguments
331for {set j 0} {$j < [llength $argv]} {incr j} {
332 set opt [lindex $argv $j]
333 set arg [lindex $argv [expr $j+1]]
334 if {$opt eq {--tags}} {
335 foreach tag $arg {
336 if {[string index $tag 0] eq "-"} {
337 lappend ::denytags [string range $tag 1 end]
338 } else {
339 lappend ::allowtags $tag
340 }
341 }
342 incr j
4b918769 343 } elseif {$opt eq {--valgrind}} {
344 set ::valgrind 1
7d04fc75 345 } elseif {$opt eq {--host}} {
346 set ::external 1
347 set ::host $arg
348 incr j
349 } elseif {$opt eq {--port}} {
350 set ::port $arg
351 incr j
524d515f 352 } elseif {$opt eq {--accurate}} {
353 set ::accurate 1
04e2410d 354 } elseif {$opt eq {--force-failure}} {
355 set ::force_failure 1
524d515f 356 } elseif {$opt eq {--single}} {
357 set ::all_tests $arg
358 incr j
359 } elseif {$opt eq {--list-tests}} {
360 foreach t $::all_tests {
361 puts $t
362 }
363 exit 0
13566085 364 } elseif {$opt eq {--client}} {
365 set ::client 1
366 set ::test_server_port $arg
367 incr j
524d515f 368 } elseif {$opt eq {--help}} {
e4715f00 369 print_help_screen
524d515f 370 exit 0
73bd6c58
PN
371 } else {
372 puts "Wrong argument: $opt"
373 exit 1
374 }
375}
376
13566085 377if {$::client} {
378 if {[catch { test_client_main $::test_server_port } err]} {
379 set estr "Executing test client: $err.\n$::errorInfo"
380 if {[catch {send_data_packet $::test_server_fd exception $estr}]} {
381 puts $estr
436f18b6
PN
382 }
383 exit 1
384 }
13566085 385} else {
386 if {[catch { test_server_main } err]} {
387 if {[string length $err] > 0} {
388 # only display error when not generated by the test suite
389 if {$err ne "exception"} {
390 puts $::errorInfo
391 }
392 exit 1
393 }
394 }
436f18b6 395}