1 set ::global_overrides {}
3 set ::valgrind_errors {}
5 proc error_and_quit
{config_file
error} {
6 puts "!!COULD NOT START REDIS-SERVER\n"
8 puts [exec cat
$config_file]
10 puts [string trim
$error]
14 proc check_valgrind_errors stderr
{
19 if {![regexp -- {ERROR SUMMARY
: 0 errors
} $buf] ||
20 (![regexp -- {definitely lost
: 0 bytes
} $buf] &&
21 ![regexp -- {no leaks are possible
} $buf])} {
22 puts "*** VALGRIND ERRORS ***"
24 puts "-----------------------"
25 append ::valgrind_errors "$buf\n\n"
29 proc kill_server config
{
30 # nothing to kill when running against external server
31 if {$::external} return
33 # nevermind if its already dead
34 if {![is_alive
$config]} { return }
35 set pid [dict get
$config pid]
38 if {![dict exists
$config "skipleaks"]} {
40 if {[string match
{*Darwin
*} [exec uname
-a]]} {
42 test
"Check for memory leaks (pid $pid)" {
50 # kill server and wait for the process to be totally exited
51 while {[is_alive
$config]} {
52 if {[incr wait
10] % 1000 == 0} {
53 puts "Waiting for process $pid to exit..."
55 catch {exec kill
$pid}
59 # Check valgrind errors if needed
61 check_valgrind_errors
[dict get
$config stderr
]
65 proc is_alive config
{
66 set pid [dict get
$config pid]
67 if {[catch {exec ps
-p $pid} err
]} {
74 proc ping_server
{host port
} {
77 set fd
[socket $::host $::port]
78 fconfigure $fd -translation binary
82 if {[string range
$reply 0 4] eq
{+PONG
} ||
83 [string range
$reply 0 3] eq
{-ERR}} {
99 # doesn't really belong here, but highly coupled to code in start_server
100 proc tags
{tags code
} {
101 set ::tags [concat $::tags $tags]
103 set ::tags [lrange $::tags 0 end-
[llength $tags]]
106 proc start_server
{options {code undefined
}} {
107 # If we are runnign against an external server, we just push the
108 # host/port pair in the stack the first time
110 if {[llength $::servers] == 0} {
112 dict
set srv
"host" $::host
113 dict
set srv
"port" $::port
114 set client
[redis
$::host $::port]
115 dict
set srv
"client" $client
118 # append the server to the stack
119 lappend ::servers $srv
126 set baseconfig
"default.conf"
131 foreach {option value
} $options {
134 set baseconfig
$value }
136 set overrides
$value }
139 set ::tags [concat $::tags $value] }
141 error "Unknown option $option" }
145 set data
[split [exec cat
"tests/assets/$baseconfig"] "\n"]
148 if {[string length
$line] > 0 && [string index
$line 0] ne
"#"} {
149 set elements
[split $line " "]
150 set directive
[lrange $elements 0 0]
151 set arguments
[lrange $elements 1 end
]
152 dict
set config
$directive $arguments
156 # use a different directory every time a server is started
157 dict
set config dir
[tmpdir server
]
159 # start every server on a different port
160 dict
set config port
[incr ::port]
162 # apply overrides from global space and arguments
163 foreach {directive arguments
} [concat $::global_overrides $overrides] {
164 dict
set config
$directive $arguments
167 # write new configuration to temporary file
168 set config_file
[tmpfile redis.conf
]
169 set fp
[open $config_file w
+]
170 foreach directive
[dict keys
$config] {
171 puts -nonewline $fp "$directive "
172 puts $fp [dict get
$config $directive]
176 set stdout
[format "%s/%s" [dict get
$config "dir"] "stdout"]
177 set stderr
[format "%s/%s" [dict get
$config "dir"] "stderr"]
180 exec valgrind
--suppressions=src
/valgrind.sup src
/redis-server
$config_file > $stdout 2> $stderr &
182 exec src
/redis-server
$config_file > $stdout 2> $stderr &
185 # check that the server actually started
186 # ugly but tries to be as fast as possible...
191 puts -nonewline "=== ($tags) Starting server ${::host}:${::port} "
195 if {$code ne
"undefined"} {
196 while {[incr retrynum
-1]} {
198 if {[ping_server
$::host $::port]} {
202 if {$serverisup} break
214 error_and_quit
$config_file [exec cat
$stderr]
218 while {![info exists
pid]} {
219 regexp {\[(\d
+)\]} [exec cat
$stdout] _
pid
223 # setup properties to be able to initialize a client object
226 if {[dict exists
$config bind]} { set host
[dict get
$config bind] }
227 if {[dict exists
$config port
]} { set port
[dict get
$config port
] }
230 dict
set srv
"config_file" $config_file
231 dict
set srv
"config" $config
232 dict
set srv
"pid" $pid
233 dict
set srv
"host" $host
234 dict
set srv
"port" $port
235 dict
set srv
"stdout" $stdout
236 dict
set srv
"stderr" $stderr
238 # if a block of code is supplied, we wait for the server to become
239 # available, create a client object and kill the server afterwards
240 if {$code ne
"undefined"} {
241 set line
[exec head
-n1 $stdout]
242 if {[string match
{*already in use
*} $line]} {
243 error_and_quit
$config_file $line
247 # check that the server actually started and is ready for connections
248 if {[exec cat
$stdout | grep
"ready to accept" | wc
-l] > 0} {
254 # append the server to the stack
255 lappend ::servers $srv
257 # connect client (after server dict is put on the stack)
260 # execute provided block
261 set num_tests
$::num_tests
262 if {[catch { uplevel 1 $code } error]} {
263 set backtrace
$::errorInfo
265 # Kill the server without checking for leaks
266 dict
set srv
"skipleaks" 1
269 # Print warnings from log
270 puts [format "\nLogged warnings (pid %d):" [dict get
$srv "pid"]]
271 set warnings
[warnings_from_file
[dict get
$srv "stdout"]]
272 if {[string length
$warnings] > 0} {
279 error $error $backtrace
282 # Don't do the leak check when no tests were run
283 if {$num_tests == $::num_tests} {
284 dict
set srv
"skipleaks" 1
287 # pop the server object
288 set ::servers [lrange $::servers 0 end-1
]
290 set ::tags [lrange $::tags 0 end-
[llength $tags]]
293 set ::tags [lrange $::tags 0 end-
[llength $tags]]