diff options
author | Eric Wong <normalperson@yhbt.net> | 2010-12-20 00:14:52 +0000 |
---|---|---|
committer | Eric Wong <normalperson@yhbt.net> | 2010-12-20 00:14:52 +0000 |
commit | 7ad59e0c48e12febae2a2fe86b76116c05977c6f (patch) | |
tree | 9fe5ba22397adc11adb1b62aa4ae221b1bead596 /test | |
parent | 82ea9b442a9edaae6dc3b06a5c61035b2c2924c9 (diff) | |
download | unicorn-7ad59e0c48e12febae2a2fe86b76116c05977c6f.tar.gz |
This limits the number of keepalive requests of a single connection to prevent a single client from monopolizing server resources. On multi-process servers (e.g. Rainbows!) with many keepalive clients per worker process, this can force a client to reconnect and increase its chances of being accepted on a less-busy worker process. This directive is named after the nginx directive which is identical in function.
Diffstat (limited to 'test')
-rw-r--r-- | test/unit/test_http_parser_ng.rb | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/test/unit/test_http_parser_ng.rb b/test/unit/test_http_parser_ng.rb index ea46ea8..8bcb724 100644 --- a/test/unit/test_http_parser_ng.rb +++ b/test/unit/test_http_parser_ng.rb @@ -8,9 +8,81 @@ include Unicorn class HttpParserNgTest < Test::Unit::TestCase def setup + HttpParser.keepalive_requests = HttpParser::KEEPALIVE_REQUESTS_DEFAULT @parser = HttpParser.new end + def test_keepalive_requests_default_constant + assert_kind_of Integer, HttpParser::KEEPALIVE_REQUESTS_DEFAULT + assert HttpParser::KEEPALIVE_REQUESTS_DEFAULT >= 0 + end + + def test_keepalive_requests_setting + HttpParser.keepalive_requests = 0 + assert_equal 0, HttpParser.keepalive_requests + HttpParser.keepalive_requests = nil + assert HttpParser.keepalive_requests >= 0xffffffff + HttpParser.keepalive_requests = 1 + assert_equal 1, HttpParser.keepalive_requests + HttpParser.keepalive_requests = 666 + assert_equal 666, HttpParser.keepalive_requests + + assert_raises(TypeError) { HttpParser.keepalive_requests = "666" } + assert_raises(TypeError) { HttpParser.keepalive_requests = [] } + end + + def test_keepalive_requests_with_next? + req = "GET / HTTP/1.1\r\nHost: example.com\r\n\r\n".freeze + expect = { + "SERVER_NAME" => "example.com", + "HTTP_HOST" => "example.com", + "rack.url_scheme" => "http", + "REQUEST_PATH" => "/", + "SERVER_PROTOCOL" => "HTTP/1.1", + "PATH_INFO" => "/", + "HTTP_VERSION" => "HTTP/1.1", + "REQUEST_URI" => "/", + "SERVER_PORT" => "80", + "REQUEST_METHOD" => "GET", + "QUERY_STRING" => "" + }.freeze + HttpParser::KEEPALIVE_REQUESTS_DEFAULT.times do |nr| + @parser.buf << req + assert_equal expect, @parser.parse + assert @parser.next? + end + @parser.buf << req + assert_equal expect, @parser.parse + assert ! @parser.next? + end + + def test_fewer_keepalive_requests_with_next? + HttpParser.keepalive_requests = 5 + @parser = HttpParser.new + req = "GET / HTTP/1.1\r\nHost: example.com\r\n\r\n".freeze + expect = { + "SERVER_NAME" => "example.com", + "HTTP_HOST" => "example.com", + "rack.url_scheme" => "http", + "REQUEST_PATH" => "/", + "SERVER_PROTOCOL" => "HTTP/1.1", + "PATH_INFO" => "/", + "HTTP_VERSION" => "HTTP/1.1", + "REQUEST_URI" => "/", + "SERVER_PORT" => "80", + "REQUEST_METHOD" => "GET", + "QUERY_STRING" => "" + }.freeze + 5.times do |nr| + @parser.buf << req + assert_equal expect, @parser.parse + assert @parser.next? + end + @parser.buf << req + assert_equal expect, @parser.parse + assert ! @parser.next? + end + def test_default_keepalive_is_off assert ! @parser.keepalive? assert ! @parser.next? |