]> git.saurik.com Git - redis.git/blame - tests/test_helper.tcl
fixed assert proc on Redis test
[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
13566085 35}
36# Index to the next test to run in the ::all_tests list.
37set ::next_test 0
38
98578b57 39set ::host 127.0.0.1
47bebf15 40set ::port 16379
e59a64b8 41set ::traceleaks 0
c4669d25 42set ::valgrind 0
322ea972 43set ::verbose 0
6e0e5bed
PN
44set ::denytags {}
45set ::allowtags {}
7d04fc75 46set ::external 0; # If "1" this means, we are running against external instance
9f1ae9ab 47set ::file ""; # If set, runs only the tests in this comma separated list
6f8a32d5 48set ::curfile ""; # Hold the filename of the current suite
524d515f 49set ::accurate 0; # If true runs fuzz tests with more iterations
04e2410d 50set ::force_failure 0
13566085 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.
57set ::client 0
58set ::numclients 16
98578b57
PN
59
60proc execute_tests name {
6f8a32d5
PN
61 set path "tests/$name.tcl"
62 set ::curfile $path
63 source $path
36e790a0 64 send_data_packet $::test_server_fd done "$name"
98578b57
PN
65}
66
1c4114be
PN
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.
70set ::servers {}
f2dd4769
PN
71proc 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 }
1c4114be
PN
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.
98578b57 86proc r {args} {
1c4114be
PN
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
941c9fa2
PN
95proc 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
5eedc9c6
PN
117proc 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
1c4114be
PN
133# Provide easy access to INFO properties. Same semantic as "proc r".
134proc 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]
98578b57
PN
141}
142
f166bb1d 143proc cleanup {} {
c7c16a32 144 puts -nonewline "Cleanup: may take some time... "
13566085 145 flush stdout
c4669d25 146 catch {exec rm -rf {*}[glob tests/tmp/redis.conf.*]}
147 catch {exec rm -rf {*}[glob tests/tmp/server.*]}
13566085 148 puts "OK"
f166bb1d
PN
149}
150
13566085 151proc test_server_main {} {
9f1ae9ab 152 cleanup
13566085 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 }
3fe40d6e 173 }
174
13566085 175 # Start the client instances
569f84aa 176 set ::clients_pids {}
13566085 177 for {set j 0} {$j < $::numclients} {incr j} {
569f84aa 178 set p [exec tclsh8.5 [info script] {*}$::argv \
179 --client $port --port [expr {$::port+($j*10)}] &]
180 lappend ::clients_pids $p
13566085 181 }
9f1ae9ab 182
13566085 183 # Setup global state for the test server
184 set ::idle_clients {}
185 set ::active_clients {}
36e790a0 186 array set ::clients_start_time {}
187 set ::clients_time_history {}
04e2410d 188 set ::failed_tests {}
13566085 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.
198proc test_server_cron {} {
199}
200
201proc 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.
218proc read_from_test_client fd {
219 set bytes [gets $fd]
220 set payload [read $fd $bytes]
221 foreach {status data} $payload break
13566085 222 if {$status eq {ready}} {
82e5dd35 223 puts "\[$status\]: $data"
13566085 224 signal_idle_client $fd
225 } elseif {$status eq {done}} {
36e790a0 226 set elapsed [expr {[clock seconds]-$::clients_start_time($fd)}]
82e5dd35 227 puts "\[[colorstr yellow $status]\]: $data ($elapsed seconds)"
569f84aa 228 puts "+++ [expr {[llength $::active_clients]-1}] units still in execution."
36e790a0 229 lappend ::clients_time_history $elapsed $data
13566085 230 signal_idle_client $fd
3744824c 231 } elseif {$status eq {ok}} {
82e5dd35 232 puts "\[[colorstr green $status]\]: $data"
3744824c 233 } elseif {$status eq {err}} {
04e2410d 234 set err "\[[colorstr red $status]\]: $data"
235 puts $err
236 lappend ::failed_tests $err
569f84aa 237 } elseif {$status eq {exception}} {
238 puts "\[[colorstr red $status]\]: $data"
239 foreach p $::clients_pids {
240 catch {exec kill -9 $p}
9f1ae9ab 241 }
569f84aa 242 exit 1
daab1599 243 } elseif {$status eq {testing}} {
244 # No op
9f1ae9ab 245 } else {
82e5dd35 246 puts "\[$status\]: $data"
9f1ae9ab 247 }
13566085 248}
e39c8b50 249
13566085 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.
252proc 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]} {
82e5dd35 258 puts [colorstr bold-white "Testing [lindex $::all_tests $::next_test]"]
36e790a0 259 set ::clients_start_time($fd) [clock seconds]
13566085 260 send_data_packet $fd run [lindex $::all_tests $::next_test]
261 lappend ::active_clients $fd
262 incr ::next_test
9f1ae9ab 263 } else {
13566085 264 lappend ::idle_clients $fd
265 if {[llength $::active_clients] == 0} {
266 the_end
6f8a32d5 267 }
9f1ae9ab 268 }
13566085 269}
6f8a32d5 270
13566085 271# The the_end funciton gets called when all the test units were already
272# executed, so the test finished.
273proc the_end {} {
274 # TODO: print the status, exit with the rigth exit code.
04e2410d 275 puts "\n The End\n"
36e790a0 276 puts "Execution time of different units:"
277 foreach {time name} $::clients_time_history {
278 puts " $time seconds - $name"
279 }
04e2410d 280 if {[llength $::failed_tests]} {
121ffc85 281 puts "\n[colorstr bold-red {!!! WARNING}] The following tests failed:\n"
04e2410d 282 foreach failed $::failed_tests {
283 puts "*** $failed"
284 }
c7c16a32 285 cleanup
e39c8b50 286 exit 1
04e2410d 287 } else {
288 puts "\n[colorstr bold-white {\o/}] [colorstr bold-green {All tests passed without errors!}]\n"
c7c16a32 289 cleanup
04e2410d 290 exit 0
98578b57 291 }
98578b57
PN
292}
293
13566085 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.
296proc 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"
6f8a32d5 307 }
98578b57 308 }
13566085 309}
cabe03eb 310
13566085 311proc 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
98578b57
PN
316}
317
e4715f00 318proc 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
73bd6c58
PN
329# parse arguments
330for {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
4b918769 342 } elseif {$opt eq {--valgrind}} {
343 set ::valgrind 1
7d04fc75 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
524d515f 351 } elseif {$opt eq {--accurate}} {
352 set ::accurate 1
04e2410d 353 } elseif {$opt eq {--force-failure}} {
354 set ::force_failure 1
524d515f 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
13566085 363 } elseif {$opt eq {--client}} {
364 set ::client 1
365 set ::test_server_port $arg
366 incr j
524d515f 367 } elseif {$opt eq {--help}} {
e4715f00 368 print_help_screen
524d515f 369 exit 0
73bd6c58
PN
370 } else {
371 puts "Wrong argument: $opt"
372 exit 1
373 }
374}
375
13566085 376if {$::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
436f18b6
PN
381 }
382 exit 1
383 }
13566085 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 }
436f18b6 394}