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 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}} {
97 # doesn't really belong here, but highly coupled to code in start_server
98 proc tags
{tags code
} {
99 set ::tags [concat $::tags $tags]
101 set ::tags [lrange $::tags 0 end-
[llength $tags]]
104 proc start_server
{options {code undefined
}} {
105 # If we are runnign against an external server, we just push the
106 # host/port pair in the stack the first time
108 if {[llength $::servers] == 0} {
110 dict
set srv
"host" $::host
111 dict
set srv
"port" $::port
112 set client
[redis
$::host $::port]
113 dict
set srv
"client" $client
116 # append the server to the stack
117 lappend ::servers $srv
124 set baseconfig
"default.conf"
129 foreach {option value
} $options {
132 set baseconfig
$value }
134 set overrides
$value }
137 set ::tags [concat $::tags $value] }
139 error "Unknown option $option" }
143 set data
[split [exec cat
"tests/assets/$baseconfig"] "\n"]
146 if {[string length
$line] > 0 && [string index
$line 0] ne
"#"} {
147 set elements
[split $line " "]
148 set directive
[lrange $elements 0 0]
149 set arguments
[lrange $elements 1 end
]
150 dict
set config
$directive $arguments
154 # use a different directory every time a server is started
155 dict
set config dir
[tmpdir server
]
157 # start every server on a different port
158 set ::port [find_available_port
[expr {$::port+1}]]
159 dict
set config port
$::port
161 # apply overrides from global space and arguments
162 foreach {directive arguments
} [concat $::global_overrides $overrides] {
163 dict
set config
$directive $arguments
166 # write new configuration to temporary file
167 set config_file
[tmpfile redis.conf
]
168 set fp
[open $config_file w
+]
169 foreach directive
[dict keys
$config] {
170 puts -nonewline $fp "$directive "
171 puts $fp [dict get
$config $directive]
175 set stdout
[format "%s/%s" [dict get
$config "dir"] "stdout"]
176 set stderr
[format "%s/%s" [dict get
$config "dir"] "stderr"]
179 exec valgrind
--suppressions=src
/valgrind.sup
--show-reachable
=no
--show-possibly
-lost
=no
--leak-check
=full src
/redis-server
$config_file > $stdout 2> $stderr &
181 exec src
/redis-server
$config_file > $stdout 2> $stderr &
184 # check that the server actually started
185 # ugly but tries to be as fast as possible...
186 if {$::valgrind} {set retrynum
1000} else {set retrynum
100}
190 puts -nonewline "=== ($tags) Starting server ${::host}:${::port} "
194 if {$code ne
"undefined"} {
195 while {[incr retrynum
-1]} {
197 if {[ping_server
$::host $::port]} {
201 if {$serverisup} break
214 append err
[exec cat
$stdout] "\n" [exec cat
$stderr]
215 start_server_error
$config_file $err
220 while {![info exists
pid]} {
221 regexp {\[(\d
+)\]} [exec cat
$stdout] _
pid
225 # setup properties to be able to initialize a client object
228 if {[dict exists
$config bind]} { set host
[dict get
$config bind] }
229 if {[dict exists
$config port
]} { set port
[dict get
$config port
] }
232 dict
set srv
"config_file" $config_file
233 dict
set srv
"config" $config
234 dict
set srv
"pid" $pid
235 dict
set srv
"host" $host
236 dict
set srv
"port" $port
237 dict
set srv
"stdout" $stdout
238 dict
set srv
"stderr" $stderr
240 # if a block of code is supplied, we wait for the server to become
241 # available, create a client object and kill the server afterwards
242 if {$code ne
"undefined"} {
243 set line
[exec head
-n1 $stdout]
244 if {[string match
{*already in use
*} $line]} {
245 error_and_quit
$config_file $line
249 # check that the server actually started and is ready for connections
250 if {[exec cat
$stdout | grep
"ready to accept" | wc
-l] > 0} {
256 # append the server to the stack
257 lappend ::servers $srv
259 # connect client (after server dict is put on the stack)
262 # execute provided block
263 set num_tests
$::num_tests
264 if {[catch { uplevel 1 $code } error]} {
265 set backtrace
$::errorInfo
267 # Kill the server without checking for leaks
268 dict
set srv
"skipleaks" 1
271 # Print warnings from log
272 puts [format "\nLogged warnings (pid %d):" [dict get
$srv "pid"]]
273 set warnings
[warnings_from_file
[dict get
$srv "stdout"]]
274 if {[string length
$warnings] > 0} {
281 error $error $backtrace
284 # Don't do the leak check when no tests were run
285 if {$num_tests == $::num_tests} {
286 dict
set srv
"skipleaks" 1
289 # pop the server object
290 set ::servers [lrange $::servers 0 end-1
]
292 set ::tags [lrange $::tags 0 end-
[llength $tags]]
295 set ::tags [lrange $::tags 0 end-
[llength $tags]]