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