1 set ::global_overrides {}
4 proc error_and_quit
{config_file
error} {
5 puts "!!COULD NOT START REDIS-SERVER\n"
7 puts [exec cat
$config_file]
9 puts [string trim
$error]
13 proc check_valgrind_errors stderr
{
18 if {![regexp -- {ERROR SUMMARY
: 0 errors
} $buf] ||
19 ![regexp -- {definitely lost
: 0 bytes
} $buf]} {
20 puts "*** VALGRIND ERRORS ***"
22 puts "--- press enter to continue ---"
27 proc kill_server config
{
28 # nothing to kill when running against external server
29 if {$::external} return
31 # nevermind if its already dead
32 if {![is_alive
$config]} { return }
33 set pid [dict get
$config pid]
36 if {![dict exists
$config "skipleaks"]} {
38 if {[string match
{*Darwin
*} [exec uname
-a]]} {
40 test
"Check for memory leaks (pid $pid)" {
48 # kill server and wait for the process to be totally exited
49 while {[is_alive
$config]} {
50 if {[incr wait
10] % 1000 == 0} {
51 puts "Waiting for process $pid to exit..."
53 catch {exec kill
$pid}
57 # Check valgrind errors if needed
59 check_valgrind_errors
[dict get
$config stderr
]
63 proc is_alive config
{
64 set pid [dict get
$config pid]
65 if {[catch {exec ps
-p $pid} err
]} {
72 proc ping_server
{host port
} {
75 set fd
[socket $::host $::port]
76 fconfigure $fd -translation binary
80 if {[string range
$reply 0 4] eq
{+PONG
} ||
81 [string range
$reply 0 3] eq
{-ERR}} {
93 # doesn't really belong here, but highly coupled to code in start_server
94 proc tags
{tags code
} {
95 set ::tags [concat $::tags $tags]
97 set ::tags [lrange $::tags 0 end-
[llength $tags]]
100 proc start_server
{options {code undefined
}} {
101 # If we are runnign against an external server, we just push the
102 # host/port pair in the stack the first time
104 if {[llength $::servers] == 0} {
106 dict
set srv
"host" $::host
107 dict
set srv
"port" $::port
108 set client
[redis
$::host $::port]
109 dict
set srv
"client" $client
112 # append the server to the stack
113 lappend ::servers $srv
120 set baseconfig
"default.conf"
125 foreach {option value
} $options {
128 set baseconfig
$value }
130 set overrides
$value }
133 set ::tags [concat $::tags $value] }
135 error "Unknown option $option" }
139 set data
[split [exec cat
"tests/assets/$baseconfig"] "\n"]
142 if {[string length
$line] > 0 && [string index
$line 0] ne
"#"} {
143 set elements
[split $line " "]
144 set directive
[lrange $elements 0 0]
145 set arguments
[lrange $elements 1 end
]
146 dict
set config
$directive $arguments
150 # use a different directory every time a server is started
151 dict
set config dir
[tmpdir server
]
153 # start every server on a different port
154 dict
set config port
[incr ::port]
156 # apply overrides from global space and arguments
157 foreach {directive arguments
} [concat $::global_overrides $overrides] {
158 dict
set config
$directive $arguments
161 # write new configuration to temporary file
162 set config_file
[tmpfile redis.conf
]
163 set fp
[open $config_file w
+]
164 foreach directive
[dict keys
$config] {
165 puts -nonewline $fp "$directive "
166 puts $fp [dict get
$config $directive]
170 set stdout
[format "%s/%s" [dict get
$config "dir"] "stdout"]
171 set stderr
[format "%s/%s" [dict get
$config "dir"] "stderr"]
174 exec valgrind src
/redis-server
$config_file > $stdout 2> $stderr &
176 exec src
/redis-server
$config_file > $stdout 2> $stderr &
179 # check that the server actually started
180 # ugly but tries to be as fast as possible...
184 puts -nonewline "=== ($tags) Starting server ${::host}:${::port} "
186 if {$code ne
"undefined"} {
187 while {[incr retrynum
-1]} {
189 if {[ping_server
$::host $::port]} {
193 if {$serverisup} break
202 error_and_quit
$config_file [exec cat
$stderr]
206 while {![info exists
pid]} {
207 regexp {^
\[(\d
+)\]} [exec head
-n1 $stdout] _
pid
211 # setup properties to be able to initialize a client object
214 if {[dict exists
$config bind]} { set host
[dict get
$config bind] }
215 if {[dict exists
$config port
]} { set port
[dict get
$config port
] }
218 dict
set srv
"config" $config_file
219 dict
set srv
"pid" $pid
220 dict
set srv
"host" $host
221 dict
set srv
"port" $port
222 dict
set srv
"stdout" $stdout
223 dict
set srv
"stderr" $stderr
225 # if a block of code is supplied, we wait for the server to become
226 # available, create a client object and kill the server afterwards
227 if {$code ne
"undefined"} {
228 set line
[exec head
-n1 $stdout]
229 if {[string match
{*already in use
*} $line]} {
230 error_and_quit
$config_file $line
234 # check that the server actually started and is ready for connections
235 if {[exec cat
$stdout | grep
"ready to accept" | wc
-l] > 0} {
241 set client
[redis
$host $port]
242 dict
set srv
"client" $client
244 # select the right db when we don't have to authenticate
245 if {![dict exists
$config requirepass
]} {
249 # append the server to the stack
250 lappend ::servers $srv
252 # execute provided block
253 set curnum
$::testnum
254 if {![catch { uplevel 1 $code } err
]} {
255 # zero exit status is good
259 if {$curnum == $::testnum} {
260 # don't check for leaks when no tests were executed
261 dict
set srv
"skipleaks" 1
264 # pop the server object
265 set ::servers [lrange $::servers 0 end-1
]
267 # allow an exception to bubble up the call chain but still kill this
268 # server, because we want to reuse the ports when the tests are re-run
269 if {[info exists err
]} {
270 if {$err eq
"exception"} {
271 puts [format "Logged warnings (pid %d):" [dict get
$srv "pid"]]
272 set warnings
[warnings_from_file
[dict get
$srv "stdout"]]
273 if {[string length
$warnings] > 0} {
278 # kill this server without checking for leaks
279 dict
set srv
"skipleaks" 1
282 } elseif
{[string length
$err] > 0} {
283 puts "Error executing the suite, aborting..."
289 set ::tags [lrange $::tags 0 end-
[llength $tags]]
292 set ::tags [lrange $::tags 0 end-
[llength $tags]]