diff --git a/lib/net/http/persistent/timed_stack_multi.rb b/lib/net/http/persistent/timed_stack_multi.rb index d242e9d..9924a0a 100644 --- a/lib/net/http/persistent/timed_stack_multi.rb +++ b/lib/net/http/persistent/timed_stack_multi.rb @@ -63,7 +63,8 @@ def try_create options = {} # :nodoc: if @created >= @max && @enqueued >= 1 oldest, = @lru.first @lru.delete oldest - @ques[oldest].pop + connection = @ques[oldest].pop + connection.close if connection.respond_to?(:close) @created -= 1 end diff --git a/test/test_net_http_persistent_timed_stack_multi.rb b/test/test_net_http_persistent_timed_stack_multi.rb index 9bfa410..4f2c205 100644 --- a/test/test_net_http_persistent_timed_stack_multi.rb +++ b/test/test_net_http_persistent_timed_stack_multi.rb @@ -4,10 +4,15 @@ class TestNetHttpPersistentTimedStackMulti < Minitest::Test class Connection - attr_reader :host + attr_reader :host, :closed def initialize(host) @host = host + @closed = false + end + + def close + @closed = true end end @@ -69,6 +74,18 @@ def test_pop_full assert_empty stack end + def test_pop_closes_extra_connections + stack = Net::HTTP::Persistent::TimedStackMulti.new(1) { |host| Connection.new(host) } + + a_conn = stack.pop connection_args: 'a.example' + stack.push a_conn, connection_args: 'a.example' + + b_conn = stack.pop connection_args: 'b.example' + + assert a_conn.closed + refute b_conn.closed + end + def test_pop_wait thread = Thread.start do @stack.pop