1 proc error_and_quit
{config_file
error} {
2 puts "!!COULD NOT START REDIS-SERVER\n"
4 puts [exec cat
$config_file]
6 puts [string trim
$error]
10 proc check_valgrind_errors stderr
{
15 if {![regexp -- {ERROR SUMMARY
: 0 errors
} $buf] ||
16 ![regexp -- {definitely lost
: 0 bytes
} $buf]} {
17 puts "*** VALGRIND ERRORS ***"
19 puts "--- press enter to continue ---"
24 proc kill_server config
{
25 # nevermind if its already dead
26 if {![is_alive
$config]} { return }
27 set pid [dict get
$config pid]
30 if {![dict exists
$config "skipleaks"]} {
32 if {[string match
{*Darwin
*} [exec uname
-a]]} {
33 test
"Check for memory leaks (pid $pid)" {
40 # kill server and wait for the process to be totally exited
41 while {[is_alive
$config]} {
42 if {[incr wait
10] % 1000 == 0} {
43 puts "Waiting for process $pid to exit..."
45 catch {exec kill
$pid}
49 # Check valgrind errors if needed
51 check_valgrind_errors
[dict get
$config stderr
]
55 proc is_alive config
{
56 set pid [dict get
$config pid]
57 if {[catch {exec ps
-p $pid} err
]} {
64 proc ping_server
{host port
} {
67 set fd
[socket $::host $::port]
68 fconfigure $fd -translation binary
72 if {[string range
$reply 0 4] eq
{+PONG
} ||
73 [string range
$reply 0 3] eq
{-ERR}} {
78 puts "Can't PING server at $host:$port... $e"
83 set ::global_overrides {}
84 proc start_server
{filename overrides
{code undefined
}} {
85 set data
[split [exec cat
"tests/assets/$filename"] "\n"]
88 if {[string length
$line] > 0 && [string index
$line 0] ne
"#"} {
89 set elements
[split $line " "]
90 set directive
[lrange $elements 0 0]
91 set arguments
[lrange $elements 1 end
]
92 dict
set config
$directive $arguments
96 # use a different directory every time a server is started
97 dict
set config dir
[tmpdir server
]
99 # start every server on a different port
100 dict
set config port
[incr ::port]
102 # apply overrides from global space and arguments
103 foreach override
[concat $::global_overrides $overrides] {
104 set directive
[lrange $override 0 0]
105 set arguments
[lrange $override 1 end
]
106 dict
set config
$directive $arguments
109 # write new configuration to temporary file
110 set config_file
[tmpfile redis.conf
]
111 set fp
[open $config_file w
+]
112 foreach directive
[dict keys
$config] {
113 puts -nonewline $fp "$directive "
114 puts $fp [dict get
$config $directive]
118 set stdout
[format "%s/%s" [dict get
$config "dir"] "stdout"]
119 set stderr
[format "%s/%s" [dict get
$config "dir"] "stderr"]
122 exec valgrind .
/redis-server
$config_file > $stdout 2> $stderr &
125 exec .
/redis-server
$config_file > $stdout 2> $stderr &
129 # check that the server actually started
130 if {$code ne
"undefined" && ![ping_server
$::host $::port]} {
131 error_and_quit
$config_file [exec cat
$stderr]
135 while {![info exists
pid]} {
136 regexp {^
\[(\d
+)\]} [exec head
-n1 $stdout] _
pid
140 # setup properties to be able to initialize a client object
143 if {[dict exists
$config bind]} { set host
[dict get
$config bind] }
144 if {[dict exists
$config port
]} { set port
[dict get
$config port
] }
147 dict
set srv
"config" $config_file
148 dict
set srv
"pid" $pid
149 dict
set srv
"host" $host
150 dict
set srv
"port" $port
151 dict
set srv
"stdout" $stdout
152 dict
set srv
"stderr" $stderr
154 # if a block of code is supplied, we wait for the server to become
155 # available, create a client object and kill the server afterwards
156 if {$code ne
"undefined"} {
157 set line
[exec head
-n1 $stdout]
158 if {[string match
{*already in use
*} $line]} {
159 error_and_quit
$config_file $line
163 # check that the server actually started and is ready for connections
164 if {[exec cat
$stdout | grep
"ready to accept" | wc
-l] > 0} {
170 set client
[redis
$host $port]
171 dict
set srv
"client" $client
173 # select the right db when we don't have to authenticate
174 if {![dict exists
$config requirepass
]} {
178 # append the server to the stack
179 lappend ::servers $srv
181 # execute provided block
182 catch { uplevel 1 $code } err
184 # pop the server object
185 set ::servers [lrange $::servers 0 end-1
]
187 # allow an exception to bubble up the call chain but still kill this
188 # server, because we want to reuse the ports when the tests are re-run
189 if {$err eq
"exception"} {
190 puts [format "Logged warnings (pid %d):" [dict get
$srv "pid"]]
191 set warnings
[warnings_from_file
[dict get
$srv "stdout"]]
192 if {[string length
$warnings] > 0} {
197 # kill this server without checking for leaks
198 dict
set srv
"skipleaks" 1
201 } elseif
{[string length
$err] > 0} {
202 puts "Error executing the suite, aborting..."