license: use LGPLv2.1 or later (was LGPL (2.1|3.0)-only)
[raindrops.git] / test / test_last_data_recv_unicorn.rb
blobca48715e7aa0d665a0ba7814c8c6c4ec599240ea
1 # -*- encoding: binary -*-
2 require "./test/rack_unicorn"
3 require "tempfile"
4 require "net/http"
6 $stderr.sync = $stdout.sync = true
7 pmq = begin
8   Raindrops::Aggregate::PMQ
9 rescue LoadError => e
10   warn "W: #{e} skipping test"
11   false
12 end
13 if RUBY_VERSION.to_f < 1.9
14   pmq = false
15   warn "W: skipping test=#{__FILE__}, only Ruby 1.9 supported for now"
16 end
18 class TestLastDataRecvUnicorn < Test::Unit::TestCase
19   def setup
20     @queue = "/test.#{rand}"
21     @host = ENV["UNICORN_TEST_ADDR"] || "127.0.0.1"
22     @sock = TCPServer.new @host, 0
23     @port = @sock.addr[1]
24     ENV["UNICORN_FD"] = @sock.fileno.to_s
25     @host_with_port = "#@host:#@port"
26     @cfg = Tempfile.new 'unicorn_config_file'
27     @cfg.puts "require 'raindrops'"
28     @cfg.puts "preload_app true"
29       ENV['RAINDROPS_MQUEUE'] = @queue
30     # @cfg.puts "worker_processes 4"
31     @opts = { :listeners => [ @host_with_port ], :config_file => @cfg.path }
32   end
34   def test_auto_listener
35     @srv = fork {
36       Thread.abort_on_exception = true
37       app = %q!Rack::Builder.new do
38         map("/ldr") { run Raindrops::LastDataRecv.new }
39         map("/") { run Rack::Lobster.new }
40       end.to_app!
41       def app.arity; 0; end
42       def app.call; eval self; end
43       Unicorn::HttpServer.new(app, @opts).start.join
44     }
45     400.times { assert_kind_of Net::HTTPSuccess, get("/") }
46     resp = get("/ldr")
47     # # p(resp.methods - Object.methods)
48     # resp.each_header { |k,v| p [k, "=" , v] }
49     assert resp.header["x-count"]
50     assert resp.header["x-min"]
51     assert resp.header["x-max"]
52     assert resp.header["x-mean"]
53     assert resp.header["x-std-dev"]
54     assert resp.header["x-outliers-low"]
55     assert resp.header["x-outliers-high"]
56     assert resp.body.size > 0
57   end
59   def get(path)
60     Net::HTTP.start(@host, @port) { |http| http.get path }
61   end
63   def teardown
64     Process.kill :QUIT, @srv
65     _, status = Process.waitpid2 @srv
66     assert status.success?
67     POSIX_MQ.unlink @queue
68   end
69 end if defined?(Unicorn) && RUBY_PLATFORM =~ /linux/ && pmq