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]]} {
36 test
"Check for memory leaks (pid $pid)" {
43 # kill server and wait for the process to be totally exited
44 while {[is_alive
$config]} {
45 if {[incr wait
10] % 1000 == 0} {
46 puts "Waiting for process $pid to exit..."
48 catch {exec kill
$pid}
52 # Check valgrind errors if needed
54 check_valgrind_errors
[dict get
$config stderr
]
58 proc is_alive config
{
59 set pid [dict get
$config pid]
60 if {[catch {exec ps
-p $pid} err
]} {
67 proc ping_server
{host port
} {
70 set fd
[socket $::host $::port]
71 fconfigure $fd -translation binary
75 if {[string range
$reply 0 4] eq
{+PONG
} ||
76 [string range
$reply 0 3] eq
{-ERR}} {
81 puts "Can't PING server at $host:$port... $e"
86 # doesn't really belong here, but highly coupled to code in start_server
87 proc tags
{tags code
} {
88 set ::tags [concat $::tags $tags]
90 set ::tags [lrange $::tags 0 end-
[llength $tags]]
93 proc start_server
{options {code undefined
}} {
95 set baseconfig
"default.conf"
100 foreach {option value
} $options {
103 set baseconfig
$value }
105 set overrides
$value }
108 set ::tags [concat $::tags $value] }
110 error "Unknown option $option" }
114 set data
[split [exec cat
"tests/assets/$baseconfig"] "\n"]
117 if {[string length
$line] > 0 && [string index
$line 0] ne
"#"} {
118 set elements
[split $line " "]
119 set directive
[lrange $elements 0 0]
120 set arguments
[lrange $elements 1 end
]
121 dict
set config
$directive $arguments
125 # use a different directory every time a server is started
126 dict
set config dir
[tmpdir server
]
128 # start every server on a different port
129 dict
set config port
[incr ::port]
131 # apply overrides from global space and arguments
132 foreach {directive arguments
} [concat $::global_overrides $overrides] {
133 dict
set config
$directive $arguments
136 # write new configuration to temporary file
137 set config_file
[tmpfile redis.conf
]
138 set fp
[open $config_file w
+]
139 foreach directive
[dict keys
$config] {
140 puts -nonewline $fp "$directive "
141 puts $fp [dict get
$config $directive]
145 set stdout
[format "%s/%s" [dict get
$config "dir"] "stdout"]
146 set stderr
[format "%s/%s" [dict get
$config "dir"] "stderr"]
149 exec valgrind .
/redis-server
$config_file > $stdout 2> $stderr &
152 exec .
/redis-server
$config_file > $stdout 2> $stderr &
156 # check that the server actually started
157 if {$code ne
"undefined" && ![ping_server
$::host $::port]} {
158 error_and_quit
$config_file [exec cat
$stderr]
162 while {![info exists
pid]} {
163 regexp {^
\[(\d
+)\]} [exec head
-n1 $stdout] _
pid
167 # setup properties to be able to initialize a client object
170 if {[dict exists
$config bind]} { set host
[dict get
$config bind] }
171 if {[dict exists
$config port
]} { set port
[dict get
$config port
] }
174 dict
set srv
"config" $config_file
175 dict
set srv
"pid" $pid
176 dict
set srv
"host" $host
177 dict
set srv
"port" $port
178 dict
set srv
"stdout" $stdout
179 dict
set srv
"stderr" $stderr
181 # if a block of code is supplied, we wait for the server to become
182 # available, create a client object and kill the server afterwards
183 if {$code ne
"undefined"} {
184 set line
[exec head
-n1 $stdout]
185 if {[string match
{*already in use
*} $line]} {
186 error_and_quit
$config_file $line
190 # check that the server actually started and is ready for connections
191 if {[exec cat
$stdout | grep
"ready to accept" | wc
-l] > 0} {
197 set client
[redis
$host $port]
198 dict
set srv
"client" $client
200 # select the right db when we don't have to authenticate
201 if {![dict exists
$config requirepass
]} {
205 # append the server to the stack
206 lappend ::servers $srv
208 # execute provided block
209 set curnum
$::testnum
210 catch { uplevel 1 $code } err
211 if {$curnum == $::testnum} {
212 # don't check for leaks when no tests were executed
213 dict
set srv
"skipleaks" 1
216 # pop the server object
217 set ::servers [lrange $::servers 0 end-1
]
219 # allow an exception to bubble up the call chain but still kill this
220 # server, because we want to reuse the ports when the tests are re-run
221 if {$err eq
"exception"} {
222 puts [format "Logged warnings (pid %d):" [dict get
$srv "pid"]]
223 set warnings
[warnings_from_file
[dict get
$srv "stdout"]]
224 if {[string length
$warnings] > 0} {
229 # kill this server without checking for leaks
230 dict
set srv
"skipleaks" 1
233 } elseif
{[string length
$err] > 0} {
234 puts "Error executing the suite, aborting..."
245 set ::tags [lrange $::tags 0 end-
[llength $tags]]