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)" {
48 # kill server and wait for the process to be totally exited
49 catch {exec kill
$pid}
50 while {[is_alive
$config]} {
54 puts "Forcing process $pid to exit..."
55 catch {exec kill
-KILL $pid}
56 } elseif
{$wait % 1000 == 0} {
57 puts "Waiting for process $pid to exit..."
62 # Check valgrind errors if needed
64 check_valgrind_errors
[dict get
$config stderr
]
68 proc is_alive config
{
69 set pid [dict get
$config pid]
70 if {[catch {exec ps
-p $pid} err
]} {
77 proc ping_server
{host port
} {
80 set fd
[socket $::host $::port]
81 fconfigure $fd -translation binary
85 if {[string range
$reply 0 4] eq
{+PONG
} ||
86 [string range
$reply 0 3] eq
{-ERR}} {
102 # doesn't really belong here, but highly coupled to code in start_server
103 proc tags
{tags code
} {
104 set ::tags [concat $::tags $tags]
106 set ::tags [lrange $::tags 0 end-
[llength $tags]]
109 proc start_server
{options {code undefined
}} {
110 # If we are running against an external server, we just push the
111 # host/port pair in the stack the first time
113 if {[llength $::servers] == 0} {
115 dict
set srv
"host" $::host
116 dict
set srv
"port" $::port
117 set client
[redis
$::host $::port]
118 dict
set srv
"client" $client
121 # append the server to the stack
122 lappend ::servers $srv
129 set baseconfig
"default.conf"
134 foreach {option value
} $options {
137 set baseconfig
$value }
139 set overrides
$value }
142 set ::tags [concat $::tags $value] }
144 error "Unknown option $option" }
148 set data
[split [exec cat
"tests/assets/$baseconfig"] "\n"]
151 if {[string length
$line] > 0 && [string index
$line 0] ne
"#"} {
152 set elements
[split $line " "]
153 set directive
[lrange $elements 0 0]
154 set arguments
[lrange $elements 1 end
]
155 dict
set config
$directive $arguments
159 # use a different directory every time a server is started
160 dict
set config dir
[tmpdir server
]
162 # start every server on a different port
163 set ::port [find_available_port
[expr {$::port+1}]]
164 dict
set config port
$::port
166 # apply overrides from global space and arguments
167 foreach {directive arguments
} [concat $::global_overrides $overrides] {
168 dict
set config
$directive $arguments
171 # write new configuration to temporary file
172 set config_file
[tmpfile redis.conf
]
173 set fp
[open $config_file w
+]
174 foreach directive
[dict keys
$config] {
175 puts -nonewline $fp "$directive "
176 puts $fp [dict get
$config $directive]
180 set stdout
[format "%s/%s" [dict get
$config "dir"] "stdout"]
181 set stderr
[format "%s/%s" [dict get
$config "dir"] "stderr"]
184 exec valgrind
--suppressions=src
/valgrind.sup
--show-reachable
=no
--show-possibly
-lost
=no
--leak-check
=full src
/redis-server
$config_file > $stdout 2> $stderr &
186 exec src
/redis-server
$config_file > $stdout 2> $stderr &
189 # check that the server actually started
190 # ugly but tries to be as fast as possible...
191 if {$::valgrind} {set retrynum
1000} else {set retrynum
100}
195 puts -nonewline "=== ($tags) Starting server ${::host}:${::port} "
199 if {$code ne
"undefined"} {
200 while {[incr retrynum
-1]} {
202 if {[ping_server
$::host $::port]} {
206 if {$serverisup} break
219 append err
[exec cat
$stdout] "\n" [exec cat
$stderr]
220 start_server_error
$config_file $err
225 while {![info exists
pid]} {
226 regexp {\[(\d
+)\]} [exec cat
$stdout] _
pid
230 # setup properties to be able to initialize a client object
233 if {[dict exists
$config bind]} { set host
[dict get
$config bind] }
234 if {[dict exists
$config port
]} { set port
[dict get
$config port
] }
237 dict
set srv
"config_file" $config_file
238 dict
set srv
"config" $config
239 dict
set srv
"pid" $pid
240 dict
set srv
"host" $host
241 dict
set srv
"port" $port
242 dict
set srv
"stdout" $stdout
243 dict
set srv
"stderr" $stderr
245 # if a block of code is supplied, we wait for the server to become
246 # available, create a client object and kill the server afterwards
247 if {$code ne
"undefined"} {
248 set line
[exec head
-n1 $stdout]
249 if {[string match
{*already in use
*} $line]} {
250 error_and_quit
$config_file $line
254 # check that the server actually started and is ready for connections
255 if {[exec grep
"ready to accept" | wc
-l < $stdout] > 0} {
261 # append the server to the stack
262 lappend ::servers $srv
264 # connect client (after server dict is put on the stack)
267 # execute provided block
268 set num_tests
$::num_tests
269 if {[catch { uplevel 1 $code } error]} {
270 set backtrace
$::errorInfo
272 # Kill the server without checking for leaks
273 dict
set srv
"skipleaks" 1
276 # Print warnings from log
277 puts [format "\nLogged warnings (pid %d):" [dict get
$srv "pid"]]
278 set warnings
[warnings_from_file
[dict get
$srv "stdout"]]
279 if {[string length
$warnings] > 0} {
286 error $error $backtrace
289 # Don't do the leak check when no tests were run
290 if {$num_tests == $::num_tests} {
291 dict
set srv
"skipleaks" 1
294 # pop the server object
295 set ::servers [lrange $::servers 0 end-1
]
297 set ::tags [lrange $::tags 0 end-
[llength $tags]]
300 set ::tags [lrange $::tags 0 end-
[llength $tags]]