server.tcl 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. set ::global_overrides {}
  2. set ::tags {}
  3. set ::valgrind_errors {}
  4. proc start_server_error {config_file error} {
  5. set err {}
  6. append err "Cant' start the Redis server\n"
  7. append err "CONFIGURATION:"
  8. append err [exec cat $config_file]
  9. append err "\nERROR:"
  10. append err [string trim $error]
  11. send_data_packet $::test_server_fd err $err
  12. }
  13. proc check_valgrind_errors stderr {
  14. set fd [open $stderr]
  15. set buf [read $fd]
  16. close $fd
  17. if {[regexp -- { at 0x} $buf] ||
  18. (![regexp -- {definitely lost: 0 bytes} $buf] &&
  19. ![regexp -- {no leaks are possible} $buf])} {
  20. send_data_packet $::test_server_fd err "Valgrind error: $buf\n"
  21. }
  22. }
  23. proc kill_server config {
  24. # nothing to kill when running against external server
  25. if {$::external} return
  26. # nevermind if its already dead
  27. if {![is_alive $config]} { return }
  28. set pid [dict get $config pid]
  29. # check for leaks
  30. if {![dict exists $config "skipleaks"]} {
  31. catch {
  32. if {[string match {*Darwin*} [exec uname -a]]} {
  33. tags {"leaks"} {
  34. test "Check for memory leaks (pid $pid)" {
  35. set output {0 leaks}
  36. catch {exec leaks $pid} output
  37. if {[string match {*process does not exist*} $output] ||
  38. [string match {*cannot examine*} $output]} {
  39. # In a few tests we kill the server process.
  40. set output "0 leaks"
  41. }
  42. set output
  43. } {*0 leaks*}
  44. }
  45. }
  46. }
  47. }
  48. # kill server and wait for the process to be totally exited
  49. catch {exec kill $pid}
  50. if {$::valgrind} {
  51. set max_wait 60000
  52. } else {
  53. set max_wait 10000
  54. }
  55. while {[is_alive $config]} {
  56. incr wait 10
  57. if {$wait >= $max_wait} {
  58. puts "Forcing process $pid to exit..."
  59. catch {exec kill -KILL $pid}
  60. } elseif {$wait % 1000 == 0} {
  61. puts "Waiting for process $pid to exit..."
  62. }
  63. after 10
  64. }
  65. # Check valgrind errors if needed
  66. if {$::valgrind} {
  67. check_valgrind_errors [dict get $config stderr]
  68. }
  69. # Remove this pid from the set of active pids in the test server.
  70. send_data_packet $::test_server_fd server-killed $pid
  71. }
  72. proc is_alive config {
  73. set pid [dict get $config pid]
  74. if {[catch {exec ps -p $pid} err]} {
  75. return 0
  76. } else {
  77. return 1
  78. }
  79. }
  80. proc ping_server {host port} {
  81. set retval 0
  82. if {[catch {
  83. set fd [socket $host $port]
  84. fconfigure $fd -translation binary
  85. puts $fd "PING\r\n"
  86. flush $fd
  87. set reply [gets $fd]
  88. if {[string range $reply 0 0] eq {+} ||
  89. [string range $reply 0 0] eq {-}} {
  90. set retval 1
  91. }
  92. close $fd
  93. } e]} {
  94. if {$::verbose} {
  95. puts -nonewline "."
  96. }
  97. } else {
  98. if {$::verbose} {
  99. puts -nonewline "ok"
  100. }
  101. }
  102. return $retval
  103. }
  104. # Return 1 if the server at the specified addr is reachable by PING, otherwise
  105. # returns 0. Performs a try every 50 milliseconds for the specified number
  106. # of retries.
  107. proc server_is_up {host port retrynum} {
  108. after 10 ;# Use a small delay to make likely a first-try success.
  109. set retval 0
  110. while {[incr retrynum -1]} {
  111. if {[catch {ping_server $host $port} ping]} {
  112. set ping 0
  113. }
  114. if {$ping} {return 1}
  115. after 50
  116. }
  117. return 0
  118. }
  119. # doesn't really belong here, but highly coupled to code in start_server
  120. proc tags {tags code} {
  121. set ::tags [concat $::tags $tags]
  122. uplevel 1 $code
  123. set ::tags [lrange $::tags 0 end-[llength $tags]]
  124. }
  125. proc start_server {options {code undefined}} {
  126. # If we are running against an external server, we just push the
  127. # host/port pair in the stack the first time
  128. if {$::external} {
  129. if {[llength $::servers] == 0} {
  130. set srv {}
  131. dict set srv "host" $::host
  132. dict set srv "port" $::port
  133. set client [redis $::host $::port]
  134. dict set srv "client" $client
  135. $client select 9
  136. # append the server to the stack
  137. lappend ::servers $srv
  138. }
  139. uplevel 1 $code
  140. return
  141. }
  142. # setup defaults
  143. set baseconfig "default.conf"
  144. set overrides {}
  145. set tags {}
  146. # parse options
  147. foreach {option value} $options {
  148. switch $option {
  149. "config" {
  150. set baseconfig $value }
  151. "overrides" {
  152. set overrides $value }
  153. "tags" {
  154. set tags $value
  155. set ::tags [concat $::tags $value] }
  156. default {
  157. error "Unknown option $option" }
  158. }
  159. }
  160. set data [split [exec cat "tests/assets/$baseconfig"] "\n"]
  161. set config {}
  162. foreach line $data {
  163. if {[string length $line] > 0 && [string index $line 0] ne "#"} {
  164. set elements [split $line " "]
  165. set directive [lrange $elements 0 0]
  166. set arguments [lrange $elements 1 end]
  167. dict set config $directive $arguments
  168. }
  169. }
  170. # use a different directory every time a server is started
  171. dict set config dir [tmpdir server]
  172. # start every server on a different port
  173. set ::port [find_available_port [expr {$::port+1}]]
  174. dict set config port $::port
  175. # apply overrides from global space and arguments
  176. foreach {directive arguments} [concat $::global_overrides $overrides] {
  177. dict set config $directive $arguments
  178. }
  179. # write new configuration to temporary file
  180. set config_file [tmpfile redis.conf]
  181. set fp [open $config_file w+]
  182. foreach directive [dict keys $config] {
  183. puts -nonewline $fp "$directive "
  184. puts $fp [dict get $config $directive]
  185. }
  186. close $fp
  187. set stdout [format "%s/%s" [dict get $config "dir"] "stdout"]
  188. set stderr [format "%s/%s" [dict get $config "dir"] "stderr"]
  189. if {$::valgrind} {
  190. set pid [exec valgrind --track-origins=yes --suppressions=src/valgrind.sup --show-reachable=no --show-possibly-lost=no --leak-check=full src/redis-server $config_file > $stdout 2> $stderr &]
  191. } elseif ($::stack_logging) {
  192. set pid [exec /usr/bin/env MallocStackLogging=1 MallocLogFile=/tmp/malloc_log.txt src/redis-server $config_file > $stdout 2> $stderr &]
  193. } else {
  194. set pid [exec src/redis-server $config_file > $stdout 2> $stderr &]
  195. }
  196. # Tell the test server about this new instance.
  197. send_data_packet $::test_server_fd server-spawned $pid
  198. # check that the server actually started
  199. # ugly but tries to be as fast as possible...
  200. if {$::valgrind} {set retrynum 1000} else {set retrynum 100}
  201. if {$::verbose} {
  202. puts -nonewline "=== ($tags) Starting server ${::host}:${::port} "
  203. }
  204. if {$code ne "undefined"} {
  205. set serverisup [server_is_up $::host $::port $retrynum]
  206. } else {
  207. set serverisup 1
  208. }
  209. if {$::verbose} {
  210. puts ""
  211. }
  212. if {!$serverisup} {
  213. set err {}
  214. append err [exec cat $stdout] "\n" [exec cat $stderr]
  215. start_server_error $config_file $err
  216. return
  217. }
  218. # Wait for actual startup
  219. while {![info exists _pid]} {
  220. regexp {PID:\s(\d+)} [exec cat $stdout] _ _pid
  221. after 100
  222. }
  223. # setup properties to be able to initialize a client object
  224. set host $::host
  225. set port $::port
  226. if {[dict exists $config bind]} { set host [dict get $config bind] }
  227. if {[dict exists $config port]} { set port [dict get $config port] }
  228. # setup config dict
  229. dict set srv "config_file" $config_file
  230. dict set srv "config" $config
  231. dict set srv "pid" $pid
  232. dict set srv "host" $host
  233. dict set srv "port" $port
  234. dict set srv "stdout" $stdout
  235. dict set srv "stderr" $stderr
  236. # if a block of code is supplied, we wait for the server to become
  237. # available, create a client object and kill the server afterwards
  238. if {$code ne "undefined"} {
  239. set line [exec head -n1 $stdout]
  240. if {[string match {*already in use*} $line]} {
  241. error_and_quit $config_file $line
  242. }
  243. while 1 {
  244. # check that the server actually started and is ready for connections
  245. if {[exec grep -i "Ready to accept" | wc -l < $stdout] > 0} {
  246. break
  247. }
  248. after 10
  249. }
  250. # append the server to the stack
  251. lappend ::servers $srv
  252. # connect client (after server dict is put on the stack)
  253. reconnect
  254. # execute provided block
  255. set num_tests $::num_tests
  256. if {[catch { uplevel 1 $code } error]} {
  257. set backtrace $::errorInfo
  258. # Kill the server without checking for leaks
  259. dict set srv "skipleaks" 1
  260. kill_server $srv
  261. # Print warnings from log
  262. puts [format "\nLogged warnings (pid %d):" [dict get $srv "pid"]]
  263. set warnings [warnings_from_file [dict get $srv "stdout"]]
  264. if {[string length $warnings] > 0} {
  265. puts "$warnings"
  266. } else {
  267. puts "(none)"
  268. }
  269. puts ""
  270. error $error $backtrace
  271. }
  272. # Don't do the leak check when no tests were run
  273. if {$num_tests == $::num_tests} {
  274. dict set srv "skipleaks" 1
  275. }
  276. # pop the server object
  277. set ::servers [lrange $::servers 0 end-1]
  278. set ::tags [lrange $::tags 0 end-[llength $tags]]
  279. kill_server $srv
  280. } else {
  281. set ::tags [lrange $::tags 0 end-[llength $tags]]
  282. set _ $srv
  283. }
  284. }