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 # nevermind if its already dead
29 if {![is_alive
$config]} { return }
30 set pid [dict get
$config pid]
33 if {![dict exists
$config "skipleaks"]} {
35 if {[string match
{*Darwin
*} [exec uname
-a]]} {
37 test
"Check for memory leaks (pid $pid)" {
45 # kill server and wait for the process to be totally exited
46 while {[is_alive
$config]} {
47 if {[incr wait
10] % 1000 == 0} {
48 puts "Waiting for process $pid to exit..."
50 catch {exec kill
$pid}
54 # Check valgrind errors if needed
56 check_valgrind_errors
[dict get
$config stderr
]
60 proc is_alive config
{
61 set pid [dict get
$config pid]
62 if {[catch {exec ps
-p $pid} err
]} {
69 proc ping_server
{host port
} {
72 set fd
[socket $::host $::port]
73 fconfigure $fd -translation binary
77 if {[string range
$reply 0 4] eq
{+PONG
} ||
78 [string range
$reply 0 3] eq
{-ERR}} {
83 puts "Can't PING server at $host:$port... $e"
88 # doesn't really belong here, but highly coupled to code in start_server
89 proc tags
{tags code
} {
90 set ::tags [concat $::tags $tags]
92 set ::tags [lrange $::tags 0 end-
[llength $tags]]
95 proc start_server
{options {code undefined
}} {
97 set baseconfig
"default.conf"
102 foreach {option value
} $options {
105 set baseconfig
$value }
107 set overrides
$value }
110 set ::tags [concat $::tags $value] }
112 error "Unknown option $option" }
116 set data
[split [exec cat
"tests/assets/$baseconfig"] "\n"]
119 if {[string length
$line] > 0 && [string index
$line 0] ne
"#"} {
120 set elements
[split $line " "]
121 set directive
[lrange $elements 0 0]
122 set arguments
[lrange $elements 1 end
]
123 dict
set config
$directive $arguments
127 # use a different directory every time a server is started
128 dict
set config dir
[tmpdir server
]
130 # start every server on a different port
131 dict
set config port
[incr ::port]
133 # apply overrides from global space and arguments
134 foreach {directive arguments
} [concat $::global_overrides $overrides] {
135 dict
set config
$directive $arguments
138 # write new configuration to temporary file
139 set config_file
[tmpfile redis.conf
]
140 set fp
[open $config_file w
+]
141 foreach directive
[dict keys
$config] {
142 puts -nonewline $fp "$directive "
143 puts $fp [dict get
$config $directive]
147 set stdout
[format "%s/%s" [dict get
$config "dir"] "stdout"]
148 set stderr
[format "%s/%s" [dict get
$config "dir"] "stderr"]
151 exec valgrind .
/redis-server
$config_file > $stdout 2> $stderr &
154 exec .
/redis-server
$config_file > $stdout 2> $stderr &
158 # check that the server actually started
159 if {$code ne
"undefined" && ![ping_server
$::host $::port]} {
160 error_and_quit
$config_file [exec cat
$stderr]
164 while {![info exists
pid]} {
165 regexp {^
\[(\d
+)\]} [exec head
-n1 $stdout] _
pid
169 # setup properties to be able to initialize a client object
172 if {[dict exists
$config bind]} { set host
[dict get
$config bind] }
173 if {[dict exists
$config port
]} { set port
[dict get
$config port
] }
176 dict
set srv
"config" $config_file
177 dict
set srv
"pid" $pid
178 dict
set srv
"host" $host
179 dict
set srv
"port" $port
180 dict
set srv
"stdout" $stdout
181 dict
set srv
"stderr" $stderr
183 # if a block of code is supplied, we wait for the server to become
184 # available, create a client object and kill the server afterwards
185 if {$code ne
"undefined"} {
186 set line
[exec head
-n1 $stdout]
187 if {[string match
{*already in use
*} $line]} {
188 error_and_quit
$config_file $line
192 # check that the server actually started and is ready for connections
193 if {[exec cat
$stdout | grep
"ready to accept" | wc
-l] > 0} {
199 set client
[redis
$host $port]
200 dict
set srv
"client" $client
202 # select the right db when we don't have to authenticate
203 if {![dict exists
$config requirepass
]} {
207 # append the server to the stack
208 lappend ::servers $srv
210 # execute provided block
211 set curnum
$::testnum
212 catch { uplevel 1 $code } err
213 if {$curnum == $::testnum} {
214 # don't check for leaks when no tests were executed
215 dict
set srv
"skipleaks" 1
218 # pop the server object
219 set ::servers [lrange $::servers 0 end-1
]
221 # allow an exception to bubble up the call chain but still kill this
222 # server, because we want to reuse the ports when the tests are re-run
223 if {$err eq
"exception"} {
224 puts [format "Logged warnings (pid %d):" [dict get
$srv "pid"]]
225 set warnings
[warnings_from_file
[dict get
$srv "stdout"]]
226 if {[string length
$warnings] > 0} {
231 # kill this server without checking for leaks
232 dict
set srv
"skipleaks" 1
235 } elseif
{[string length
$err] > 0} {
236 puts "Error executing the suite, aborting..."
241 set ::tags [lrange $::tags 0 end-
[llength $tags]]
244 set ::tags [lrange $::tags 0 end-
[llength $tags]]