1 set ::global_overrides {}
3 set ::valgrind_errors {}
5 proc start_server_error
{config_file
error} {
7 append err
"Cant' start the Redis server\n"
8 append err
"CONFIGURATION:"
9 append err
[exec cat
$config_file]
11 append err
[string trim
$error]
12 send_data_packet
$::test_server_fd err
$err
15 proc check_valgrind_errors stderr
{
20 if {[regexp -- { at
0x
} $buf] ||
21 (![regexp -- {definitely lost
: 0 bytes
} $buf] &&
22 ![regexp -- {no leaks are possible
} $buf])} {
23 send_data_packet
$::test_server_fd err
"Valgrind error: $buf\n"
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)" {
42 catch {exec leaks
$pid} output
50 # kill server and wait for the process to be totally exited
51 catch {exec kill
$pid}
52 while {[is_alive
$config]} {
56 puts "Forcing process $pid to exit..."
57 catch {exec kill
-KILL $pid}
58 } elseif
{$wait % 1000 == 0} {
59 puts "Waiting for process $pid to exit..."
64 # Check valgrind errors if needed
66 check_valgrind_errors
[dict get
$config stderr
]
70 proc is_alive config
{
71 set pid [dict get
$config pid]
72 if {[catch {exec ps
-p $pid} err
]} {
79 proc ping_server
{host port
} {
82 set fd
[socket $::host $::port]
83 fconfigure $fd -translation binary
87 if {[string range
$reply 0 4] eq
{+PONG
} ||
88 [string range
$reply 0 3] eq
{-ERR}} {
104 # doesn't really belong here, but highly coupled to code in start_server
105 proc tags
{tags code
} {
106 set ::tags [concat $::tags $tags]
108 set ::tags [lrange $::tags 0 end-
[llength $tags]]
111 proc start_server
{options {code undefined
}} {
112 # If we are running against an external server, we just push the
113 # host/port pair in the stack the first time
115 if {[llength $::servers] == 0} {
117 dict
set srv
"host" $::host
118 dict
set srv
"port" $::port
119 set client
[redis
$::host $::port]
120 dict
set srv
"client" $client
123 # append the server to the stack
124 lappend ::servers $srv
131 set baseconfig
"default.conf"
136 foreach {option value
} $options {
139 set baseconfig
$value }
141 set overrides
$value }
144 set ::tags [concat $::tags $value] }
146 error "Unknown option $option" }
150 set data
[split [exec cat
"tests/assets/$baseconfig"] "\n"]
153 if {[string length
$line] > 0 && [string index
$line 0] ne
"#"} {
154 set elements
[split $line " "]
155 set directive
[lrange $elements 0 0]
156 set arguments
[lrange $elements 1 end
]
157 dict
set config
$directive $arguments
161 # use a different directory every time a server is started
162 dict
set config dir
[tmpdir server
]
164 # start every server on a different port
165 set ::port [find_available_port
[expr {$::port+1}]]
166 dict
set config port
$::port
168 # apply overrides from global space and arguments
169 foreach {directive arguments
} [concat $::global_overrides $overrides] {
170 dict
set config
$directive $arguments
173 # write new configuration to temporary file
174 set config_file
[tmpfile redis.conf
]
175 set fp
[open $config_file w
+]
176 foreach directive
[dict keys
$config] {
177 puts -nonewline $fp "$directive "
178 puts $fp [dict get
$config $directive]
182 set stdout
[format "%s/%s" [dict get
$config "dir"] "stdout"]
183 set stderr
[format "%s/%s" [dict get
$config "dir"] "stderr"]
186 exec valgrind
--suppressions=src
/valgrind.sup
--show-reachable
=no
--show-possibly
-lost
=no
--leak-check
=full src
/redis-server
$config_file > $stdout 2> $stderr &
188 exec src
/redis-server
$config_file > $stdout 2> $stderr &
191 # check that the server actually started
192 # ugly but tries to be as fast as possible...
193 if {$::valgrind} {set retrynum
1000} else {set retrynum
100}
197 puts -nonewline "=== ($tags) Starting server ${::host}:${::port} "
201 if {$code ne
"undefined"} {
202 while {[incr retrynum
-1]} {
204 if {[ping_server
$::host $::port]} {
208 if {$serverisup} break
221 append err
[exec cat
$stdout] "\n" [exec cat
$stderr]
222 start_server_error
$config_file $err
227 while {![info exists
pid]} {
228 regexp {\[(\d
+)\]} [exec cat
$stdout] _
pid
232 # setup properties to be able to initialize a client object
235 if {[dict exists
$config bind]} { set host
[dict get
$config bind] }
236 if {[dict exists
$config port
]} { set port
[dict get
$config port
] }
239 dict
set srv
"config_file" $config_file
240 dict
set srv
"config" $config
241 dict
set srv
"pid" $pid
242 dict
set srv
"host" $host
243 dict
set srv
"port" $port
244 dict
set srv
"stdout" $stdout
245 dict
set srv
"stderr" $stderr
247 # if a block of code is supplied, we wait for the server to become
248 # available, create a client object and kill the server afterwards
249 if {$code ne
"undefined"} {
250 set line
[exec head
-n1 $stdout]
251 if {[string match
{*already in use
*} $line]} {
252 error_and_quit
$config_file $line
256 # check that the server actually started and is ready for connections
257 if {[exec grep
"ready to accept" | wc
-l < $stdout] > 0} {
263 # append the server to the stack
264 lappend ::servers $srv
266 # connect client (after server dict is put on the stack)
269 # execute provided block
270 set num_tests
$::num_tests
271 if {[catch { uplevel 1 $code } error]} {
272 set backtrace
$::errorInfo
274 # Kill the server without checking for leaks
275 dict
set srv
"skipleaks" 1
278 # Print warnings from log
279 puts [format "\nLogged warnings (pid %d):" [dict get
$srv "pid"]]
280 set warnings
[warnings_from_file
[dict get
$srv "stdout"]]
281 if {[string length
$warnings] > 0} {
288 error $error $backtrace
291 # Don't do the leak check when no tests were run
292 if {$num_tests == $::num_tests} {
293 dict
set srv
"skipleaks" 1
296 # pop the server object
297 set ::servers [lrange $::servers 0 end-1
]
299 set ::tags [lrange $::tags 0 end-
[llength $tags]]
302 set ::tags [lrange $::tags 0 end-
[llength $tags]]