added regression test to fix misbehaviour spotted before releasing 0.14.4

This commit is contained in:
HoneyryderChuck 2021-06-02 22:49:47 +01:00
parent c4e8d7f55f
commit 68955c14b0
3 changed files with 66 additions and 1 deletions

View File

@ -0,0 +1,55 @@
# frozen_string_literal: true
require "test_helper"
require "support/http_helpers"
class Bug_0_14_4_Test < Minitest::Test
include HTTPHelpers
def test_http1_keep_alive_persistent_requests_failed_to_start_new_connection_after_max_requests_reached
server = KeepAliveServer.new
th = Thread.new { server.start }
begin
uri = "#{server.origin}/"
uris = [uri] * 400
HTTPX.plugin(SessionWithPool).with(max_requests: 100, max_concurrent_requests: 1).wrap do |http|
responses = http.get(*uris)
assert responses.size == 400
responses.each_with_index do |response, idx|
verify_status(response, 200)
conn_header = ((idx + 1) % 100).zero? ? "close" : "Keep-Alive"
assert verify_header(response.headers, "connection", conn_header)
end
connection_count = http.pool.connection_count
assert connection_count == 4, "expected to have 4 connections (+ an idle one), instead have #{connection_count}"
end
ensure
server.shutdown
th.join
end
end
def test_http1_keep_alive_persistent_requests_failed_to_start_new_connection_after_server_max_reached
server = KeepAliveServer.new
th = Thread.new { server.start }
begin
uri = "#{server.origin}/2"
uris = [uri] * 200
HTTPX.plugin(SessionWithPool).with(max_requests: 100, max_concurrent_requests: 1).wrap do |http|
responses = http.get(*uris)
assert responses.size == 200
responses.each_with_index do |response, idx|
verify_status(response, 200)
conn_header = ((idx + 1) % 2).zero? ? "close" : "Keep-Alive"
assert verify_header(response.headers, "connection", conn_header)
end
connection_count = http.pool.connection_count
assert connection_count == 100, "expected to have 100 connections (+ an idle one), instead have #{connection_count}"
end
ensure
server.shutdown
th.join
end
end
end

View File

@ -52,7 +52,7 @@ class HTTPTest < Minitest::Test
server = KeepAliveServer.new
th = Thread.new { server.start }
begin
uri = "#{server.origin}/"
uri = "#{server.origin}/2"
HTTPX.plugin(SessionWithPool).with(max_concurrent_requests: 1).wrap do |http|
responses = http.get(uri, uri, uri)
assert responses.size == 3, "expected 3 responses, got #{responses.size}"

View File

@ -4,6 +4,15 @@ require_relative "test"
class KeepAliveServer < TestServer
class KeepAliveApp < WEBrick::HTTPServlet::AbstractServlet
def do_GET(_req, res) # rubocop:disable Naming/MethodName
res.status = 200
res["Connection"] = "Keep-Alive"
res["Content-Type"] = "application/json"
res.body = "{\"counter\": infinity}"
end
end
class KeepAliveMax2App < WEBrick::HTTPServlet::AbstractServlet
def do_GET(_req, res) # rubocop:disable Naming/MethodName
res.status = 200
res["Connection"] = "Keep-Alive"
@ -16,5 +25,6 @@ class KeepAliveServer < TestServer
def initialize(options = {})
super
mount("/", KeepAliveApp)
mount("/2", KeepAliveMax2App)
end
end