about summary refs log tree commit homepage
path: root/test/test_last_data_recv_unicorn.rb
diff options
context:
space:
mode:
Diffstat (limited to 'test/test_last_data_recv_unicorn.rb')
-rw-r--r--test/test_last_data_recv_unicorn.rb65
1 files changed, 65 insertions, 0 deletions
diff --git a/test/test_last_data_recv_unicorn.rb b/test/test_last_data_recv_unicorn.rb
new file mode 100644
index 0000000..19aba0d
--- /dev/null
+++ b/test/test_last_data_recv_unicorn.rb
@@ -0,0 +1,65 @@
+# -*- encoding: binary -*-
+require "./test/rack_unicorn"
+require "tempfile"
+require "net/http"
+
+$stderr.sync = $stdout.sync = true
+pmq = begin
+  Raindrops::Aggregate::PMQ
+rescue => LoadError
+  warn "W: #{e} skipping test"
+  false
+end
+
+class TestLastDataRecvUnicorn < Test::Unit::TestCase
+  def setup
+    @queue = "/test.#{rand}"
+    @host = ENV["UNICORN_TEST_ADDR"] || "127.0.0.1"
+    @sock = TCPServer.new @host, 0
+    @port = @sock.addr[1]
+    ENV["UNICORN_FD"] = @sock.fileno.to_s
+    @host_with_port = "#@host:#@port"
+    @cfg = Tempfile.new 'unicorn_config_file'
+    @cfg.puts "require 'raindrops'"
+    @cfg.puts "preload_app true"
+      ENV['RAINDROPS_MQUEUE'] = @queue
+    # @cfg.puts "worker_processes 4"
+    @opts = { :listeners => [ @host_with_port ], :config_file => @cfg.path }
+  end
+
+  def test_auto_listener
+    @srv = fork {
+      Thread.abort_on_exception = true
+      app = %q!Rack::Builder.new do
+        map("/ldr") { run Raindrops::LastDataRecv.new }
+        map("/") { run Rack::Lobster.new }
+      end.to_app!
+      def app.arity; 0; end
+      def app.call; eval self; end
+      Unicorn.run(app, @opts)
+    }
+    400.times { assert_kind_of Net::HTTPSuccess, get("/") }
+    resp = get("/ldr")
+    # # p(resp.methods - Object.methods)
+    # resp.each_header { |k,v| p [k, "=" , v] }
+    assert resp.header["x-count"]
+    assert resp.header["x-min"]
+    assert resp.header["x-max"]
+    assert resp.header["x-mean"]
+    assert resp.header["x-std-dev"]
+    assert resp.header["x-outliers-low"]
+    assert resp.header["x-outliers-high"]
+    assert resp.body.size > 0
+  end
+
+  def get(path)
+    Net::HTTP.start(@host, @port) { |http| http.get path }
+  end
+
+  def teardown
+    Process.kill :QUIT, @srv
+    _, status = Process.waitpid2 @srv
+    assert status.success?
+    POSIX_MQ.unlink @queue
+  end
+end if defined?(Unicorn) && RUBY_PLATFORM =~ /linux/ && pmq