1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
# -*- encoding: binary -*-
require "test/unit"
require "rack"
require "raindrops"
begin
require 'aggregate'
rescue LoadError => e
warn "W: #{e} skipping #{__FILE__}"
end
class TestWatcher < Test::Unit::TestCase
TEST_ADDR = ENV['UNICORN_TEST_ADDR'] || '127.0.0.1'
def check_headers(headers)
%w(X-Count X-Std-Dev X-Min X-Max X-Mean
X-Outliers-Low X-Outliers-Low X-Last-Reset).each { |x|
assert_kind_of String, headers[x], "#{x} missing"
}
end
def teardown
@app.shutdown
@ios.each { |io| io.close unless io.closed? }
end
def setup
@ios = []
@srv = TCPServer.new TEST_ADDR, 0
@ios << @srv
@port = @srv.addr[1]
@client = TCPSocket.new TEST_ADDR, @port
@addr = "#{TEST_ADDR}:#{@port}"
@ios << @client
@app = Raindrops::Watcher.new :delay => 0.001
@req = Rack::MockRequest.new @app
end
def test_index
resp = @req.get "/"
assert_equal 200, resp.status.to_i
t = Time.parse resp.headers["Last-Modified"]
assert_in_delta Time.now.to_f, t.to_f, 2.0
end
def test_active_txt
resp = @req.get "/active/#@addr.txt"
assert_equal 200, resp.status.to_i
assert_equal "text/plain", resp.headers["Content-Type"]
check_headers(resp.headers)
end
def test_invalid
assert_nothing_raised do
@req.get("/active/666.666.666.666%3A666.txt")
@req.get("/queued/666.666.666.666%3A666.txt")
@req.get("/active/666.666.666.666%3A666.html")
@req.get("/queued/666.666.666.666%3A666.html")
end
addr = @app.instance_eval do
@peak_active.keys + @peak_queued.keys +
@resets.keys + @active.keys + @queued.keys
end
assert addr.grep(/666\.666\.666\.666/).empty?, addr.inspect
end
def test_active_html
resp = @req.get "/active/#@addr.html"
assert_equal 200, resp.status.to_i
assert_equal "text/html", resp.headers["Content-Type"]
check_headers(resp.headers)
end
def test_queued_txt
resp = @req.get "/queued/#@addr.txt"
assert_equal 200, resp.status.to_i
assert_equal "text/plain", resp.headers["Content-Type"]
check_headers(resp.headers)
end
def test_queued_html
resp = @req.get "/queued/#@addr.html"
assert_equal 200, resp.status.to_i
assert_equal "text/html", resp.headers["Content-Type"]
check_headers(resp.headers)
end
def test_reset
resp = @req.post "/reset/#@addr"
assert_equal 302, resp.status.to_i
end
def test_tail
env = @req.class.env_for "/tail/#@addr.txt"
status, headers, body = @app.call env
assert_equal "text/plain", headers["Content-Type"]
assert_equal 200, status.to_i
tmp = []
body.each do |x|
assert_kind_of String, x
tmp << x
break if tmp.size > 1
end
end
def test_tail_queued_min
env = @req.class.env_for "/tail/#@addr.txt?queued_min=1"
status, headers, body = @app.call env
assert_equal "text/plain", headers["Content-Type"]
assert_equal 200, status.to_i
tmp = []
body.each do |x|
tmp = TCPSocket.new TEST_ADDR, @port
@ios << tmp
assert_kind_of String, x
assert_equal 1, x.strip.split(/\s+/).last.to_i
break
end
end
def test_x_current_header
env = @req.class.env_for "/active/#@addr.txt"
_status, headers, _body = @app.call(env)
assert_equal "0", headers["X-Current"], headers.inspect
env = @req.class.env_for "/queued/#@addr.txt"
_status, headers, _body = @app.call(env)
assert_equal "1", headers["X-Current"], headers.inspect
@ios << @srv.accept
sleep 0.1
env = @req.class.env_for "/queued/#@addr.txt"
_status, headers, _body = @app.call(env)
assert_equal "0", headers["X-Current"], headers.inspect
env = @req.class.env_for "/active/#@addr.txt"
_status, headers, _body = @app.call(env)
assert_equal "1", headers["X-Current"], headers.inspect
end
def test_peaks
env = @req.class.env_for "/active/#@addr.txt"
_status, headers, _body = @app.call(env.dup)
start = headers["X-First-Peak-At"]
assert headers["X-First-Peak-At"], headers.inspect
assert headers["X-Last-Peak-At"], headers.inspect
assert_nothing_raised { Time.parse(headers["X-First-Peak-At"]) }
assert_nothing_raised { Time.parse(headers["X-Last-Peak-At"]) }
before = headers["X-Last-Peak-At"]
env = @req.class.env_for "/queued/#@addr.txt"
_status, headers, _body = @app.call(env)
assert_nothing_raised { Time.parse(headers["X-First-Peak-At"]) }
assert_nothing_raised { Time.parse(headers["X-Last-Peak-At"]) }
assert_equal before, headers["X-Last-Peak-At"], "should not change"
sleep 2
env = @req.class.env_for "/active/#@addr.txt"
_status, headers, _body = @app.call(env.dup)
assert_equal before, headers["X-Last-Peak-At"], headers.inspect
@ios << @srv.accept
begin
@srv.accept_nonblock
assert false, "we should not get here"
rescue => e
assert_kind_of Errno::EAGAIN, e
end
sleep 0.1
env = @req.class.env_for "/queued/#@addr.txt"
_status, headers, _body = @app.call(env.dup)
assert headers["X-Last-Peak-At"], headers.inspect
assert_nothing_raised { Time.parse(headers["X-Last-Peak-At"]) }
assert before != headers["X-Last-Peak-At"]
queued_before = headers["X-Last-Peak-At"]
sleep 2
env = @req.class.env_for "/queued/#@addr.txt"
_status, headers, _body = @app.call(env)
assert_equal "0", headers["X-Current"]
assert_nothing_raised { Time.parse(headers["X-Last-Peak-At"]) }
assert_equal queued_before, headers["X-Last-Peak-At"], "should not change"
assert_equal start, headers["X-First-Peak-At"]
end
end if RUBY_PLATFORM =~ /linux/ && defined?(Aggregate)
|