]> git.saurik.com Git - redis.git/blame - tests/test_helper.tcl
the test runs less iterations of slow tests if no --accurate is given.
[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
34}
35# Index to the next test to run in the ::all_tests list.
36set ::next_test 0
37
98578b57 38set ::host 127.0.0.1
47bebf15 39set ::port 16379
e59a64b8 40set ::traceleaks 0
c4669d25 41set ::valgrind 0
322ea972 42set ::verbose 0
6e0e5bed
PN
43set ::denytags {}
44set ::allowtags {}
7d04fc75 45set ::external 0; # If "1" this means, we are running against external instance
9f1ae9ab 46set ::file ""; # If set, runs only the tests in this comma separated list
6f8a32d5 47set ::curfile ""; # Hold the filename of the current suite
524d515f 48set ::accurate 0; # If true runs fuzz tests with more iterations
13566085 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.
55set ::client 0
56set ::numclients 16
98578b57
PN
57
58proc execute_tests name {
6f8a32d5
PN
59 set path "tests/$name.tcl"
60 set ::curfile $path
61 source $path
36e790a0 62 send_data_packet $::test_server_fd done "$name"
98578b57
PN
63}
64
1c4114be
PN
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.
68set ::servers {}
f2dd4769
PN
69proc 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 }
1c4114be
PN
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.
98578b57 84proc r {args} {
1c4114be
PN
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
941c9fa2
PN
93proc 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
5eedc9c6
PN
115proc 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
1c4114be
PN
131# Provide easy access to INFO properties. Same semantic as "proc r".
132proc 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]
98578b57
PN
139}
140
f166bb1d 141proc cleanup {} {
13566085 142 puts -nonewline "Cleanup: warning may take some time... "
143 flush stdout
c4669d25 144 catch {exec rm -rf {*}[glob tests/tmp/redis.conf.*]}
145 catch {exec rm -rf {*}[glob tests/tmp/server.*]}
13566085 146 puts "OK"
f166bb1d
PN
147}
148
13566085 149proc 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 }
3fe40d6e 171 }
172
13566085 173 # Start the client instances
569f84aa 174 set ::clients_pids {}
13566085 175 for {set j 0} {$j < $::numclients} {incr j} {
569f84aa 176 set p [exec tclsh8.5 [info script] {*}$::argv \
177 --client $port --port [expr {$::port+($j*10)}] &]
178 lappend ::clients_pids $p
13566085 179 }
9f1ae9ab 180
13566085 181 # Setup global state for the test server
182 set ::idle_clients {}
183 set ::active_clients {}
36e790a0 184 array set ::clients_start_time {}
185 set ::clients_time_history {}
13566085 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.
195proc test_server_cron {} {
196}
197
198proc 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.
215proc read_from_test_client fd {
216 set bytes [gets $fd]
217 set payload [read $fd $bytes]
218 foreach {status data} $payload break
13566085 219 if {$status eq {ready}} {
82e5dd35 220 puts "\[$status\]: $data"
13566085 221 signal_idle_client $fd
222 } elseif {$status eq {done}} {
36e790a0 223 set elapsed [expr {[clock seconds]-$::clients_start_time($fd)}]
82e5dd35 224 puts "\[[colorstr yellow $status]\]: $data ($elapsed seconds)"
569f84aa 225 puts "+++ [expr {[llength $::active_clients]-1}] units still in execution."
36e790a0 226 lappend ::clients_time_history $elapsed $data
13566085 227 signal_idle_client $fd
3744824c 228 } elseif {$status eq {ok}} {
82e5dd35 229 puts "\[[colorstr green $status]\]: $data"
3744824c 230 } elseif {$status eq {err}} {
82e5dd35 231 puts "\[[colorstr red $status]\]: $data"
569f84aa 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
daab1599 238 } elseif {$status eq {testing}} {
239 # No op
3744824c 240 } else {
82e5dd35 241 puts "\[$status\]: $data"
13566085 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.
247proc 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]} {
82e5dd35 253 puts [colorstr bold-white "Testing [lindex $::all_tests $::next_test]"]
36e790a0 254 set ::clients_start_time($fd) [clock seconds]
13566085 255 send_data_packet $fd run [lindex $::all_tests $::next_test]
256 lappend ::active_clients $fd
257 incr ::next_test
9f1ae9ab 258 } else {
13566085 259 lappend ::idle_clients $fd
260 if {[llength $::active_clients] == 0} {
261 the_end
262 }
9f1ae9ab 263 }
13566085 264}
e39c8b50 265
13566085 266# The the_end funciton gets called when all the test units were already
267# executed, so the test finished.
268proc the_end {} {
269 # TODO: print the status, exit with the rigth exit code.
36e790a0 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 }
13566085 275 exit 1
276}
6f8a32d5 277
13566085 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.
280proc 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"
6f8a32d5 291 }
98578b57 292 }
13566085 293}
cabe03eb 294
13566085 295proc 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
98578b57
PN
300}
301
73bd6c58
PN
302# parse arguments
303for {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
4b918769 315 } elseif {$opt eq {--valgrind}} {
316 set ::valgrind 1
9f1ae9ab
PN
317 } elseif {$opt eq {--file}} {
318 set ::file $arg
319 incr j
7d04fc75 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
6f8a32d5
PN
327 } elseif {$opt eq {--verbose}} {
328 set ::verbose 1
524d515f 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
13566085 339 } elseif {$opt eq {--client}} {
340 set ::client 1
341 set ::test_server_port $arg
342 incr j
524d515f 343 } elseif {$opt eq {--help}} {
344 puts "TODO print an help screen"
345 exit 0
73bd6c58
PN
346 } else {
347 puts "Wrong argument: $opt"
348 exit 1
349 }
350}
351
13566085 352if {$::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
436f18b6
PN
357 }
358 exit 1
359 }
13566085 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 }
436f18b6 370}