]>
git.saurik.com Git - redis.git/blob - tests/support/server.tcl
40f21925a3b645c4d1b2ebd8c106be009a06a45b
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 kill_server config
{
11 set pid [dict get
$config pid]
15 if {[string match
{*Darwin
*} [exec uname
-a]]} {
16 test
"Check for memory leaks (pid $pid)" {
22 # kill server and wait for the process to be totally exited
25 # with a non-zero exit status, the process is gone
26 if {[catch {exec ps
-p $pid | grep redis-server
} result
]} {
33 proc start_server
{filename overrides
{code undefined
}} {
34 set data
[split [exec cat
"tests/assets/$filename"] "\n"]
37 if {[string length
$line] > 0 && [string index
$line 0] ne
"#"} {
38 set elements
[split $line " "]
39 set directive
[lrange $elements 0 0]
40 set arguments
[lrange $elements 1 end
]
41 dict
set config
$directive $arguments
45 # use a different directory every time a server is started
46 dict
set config dir
[tmpdir server
]
48 # start every server on a different port
49 dict
set config port
[incr ::port]
51 # apply overrides from arguments
52 foreach override
$overrides {
53 set directive
[lrange $override 0 0]
54 set arguments
[lrange $override 1 end
]
55 dict
set config
$directive $arguments
58 # write new configuration to temporary file
59 set config_file
[tmpfile redis.conf
]
60 set fp
[open $config_file w
+]
61 foreach directive
[dict keys
$config] {
62 puts -nonewline $fp "$directive "
63 puts $fp [dict get
$config $directive]
67 set stdout
[format "%s/%s" [dict get
$config "dir"] "stdout"]
68 set stderr
[format "%s/%s" [dict get
$config "dir"] "stderr"]
69 exec .
/redis-server
$config_file > $stdout 2> $stderr &
72 # check that the server actually started
73 if {[file size
$stderr] > 0} {
74 error_and_quit
$config_file [exec cat
$stderr]
77 set line
[exec head
-n1 $stdout]
78 if {[string match
{*already in use
*} $line]} {
79 error_and_quit
$config_file $line
83 # check that the server actually started and is ready for connections
84 if {[exec cat
$stdout | grep
"ready to accept" | wc
-l] > 0} {
91 regexp {^
\[(\d
+)\]} [exec head
-n1 $stdout] _
pid
93 # create the client object
96 if {[dict exists
$config bind]} { set host
[dict get
$config bind] }
97 if {[dict exists
$config port
]} { set port
[dict get
$config port
] }
98 set client
[redis
$host $port]
100 # select the right db when we don't have to authenticate
101 if {![dict exists
$config requirepass
]} {
106 dict
set srv
"config" $config_file
107 dict
set srv
"pid" $pid
108 dict
set srv
"host" $host
109 dict
set srv
"port" $port
110 dict
set srv
"stdout" $stdout
111 dict
set srv
"stderr" $stderr
112 dict
set srv
"client" $client
114 if {$code ne
"undefined"} {
115 # append the server to the stack
116 lappend ::servers $srv
118 # execute provided block
119 catch { uplevel 1 $code } err
121 # pop the server object
122 set ::servers [lrange $::servers 0 end-1
]
126 if {[string length
$err] > 0} {
127 puts "Error executing the suite, aborting..."