]>
git.saurik.com Git - redis.git/blob - test/support/server.tcl
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 start_server
{filename overrides
{code undefined
}} {
11 set data
[split [exec cat
"test/assets/$filename"] "\n"]
14 if {[string length
$line] > 0 && [string index
$line 0] ne
"#"} {
15 set elements
[split $line " "]
16 set directive
[lrange $elements 0 0]
17 set arguments
[lrange $elements 1 end
]
18 dict
set config
$directive $arguments
22 # use a different directory every time a server is started
23 dict
set config dir
[tmpdir server
]
25 # start every server on a different port
26 dict
set config port
[incr ::port]
28 # apply overrides from arguments
29 foreach override
$overrides {
30 set directive
[lrange $override 0 0]
31 set arguments
[lrange $override 1 end
]
32 dict
set config
$directive $arguments
35 # write new configuration to temporary file
36 set config_file
[tmpfile redis.conf
]
37 set fp
[open $config_file w
+]
38 foreach directive
[dict keys
$config] {
39 puts -nonewline $fp "$directive "
40 puts $fp [dict get
$config $directive]
44 set stdout
[format "%s/%s" [dict get
$config "dir"] "stdout"]
45 set stderr
[format "%s/%s" [dict get
$config "dir"] "stderr"]
46 exec .
/redis-server
$config_file > $stdout 2> $stderr &
49 # check that the server actually started
50 if {[file size
$stderr] > 0} {
51 error_and_quit
$config_file [exec cat
$stderr]
54 set line
[exec head
-n1 $stdout]
55 if {[string match
{*already in use
*} $line]} {
56 error_and_quit
$config_file $line
60 regexp {^
\[(\d
+)\]} [exec head
-n1 $stdout] _
pid
62 # create the client object
65 if {[dict exists
$config bind]} { set host
[dict get
$config bind] }
66 if {[dict exists
$config port
]} { set port
[dict get
$config port
] }
67 set client
[redis
$host $port]
69 # select the right db when we don't have to authenticate
70 if {![dict exists
$config requirepass
]} {
74 if {$code ne
"undefined"} {
75 # append the client to the client stack
76 lappend ::clients $client
78 # execute provided block
79 catch { uplevel 1 $code } err
81 # pop the client object
82 set ::clients [lrange $::clients 0 end-1
]
84 # kill server and wait for the process to be totally exited
87 if {[catch {exec ps
-p $pid | grep redis-server
} result
]} {
88 # non-zero exis status, process is gone
94 if {[string length
$err] > 0} {
95 puts "Error executing the suite, aborting..."
100 dict
set ret
"config" $config_file
101 dict
set ret
"pid" $pid
102 dict
set ret
"stdout" $stdout
103 dict
set ret
"stderr" $stderr
104 dict
set ret
"client" $client
109 proc kill_server config
{
110 set pid [dict get
$config pid]