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