raindrops.git  about / heads / tags
real-time stats for preforking Rack servers
blob 86c97d840f0d9091cad6b337906fe7f2c7e985c7 2637 bytes (raw)
$ git show v0.7.0:test/test_watcher.rb	# shows this blob on the CLI

 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
 
# -*- encoding: binary -*-
require "test/unit"
require "rack"
require "raindrops"

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_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
end if RUBY_PLATFORM =~ /linux/

git clone https://yhbt.net/raindrops.git