Skip to content

Commit

Permalink
patching the setup of the on close callback instead
Browse files Browse the repository at this point in the history
the previous patch allowed the callback to be called only once, whereas this one will be long-lived for the duration of the connection
  • Loading branch information
HoneyryderChuck committed Nov 3, 2023
1 parent ec7b845 commit e273010
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/httpx/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,9 @@ def init_connection(type, uri, options)
connection.on(:open) do
emit(:connection_opened, connection.origin, connection.io.socket)
# only run close callback if it opened
connection.once(:close) { emit(:connection_closed, connection.origin, connection.io.socket) }
end
connection.on(:close) do
emit(:connection_closed, connection.origin, connection.io.socket) if connection.used?
end
catch(:coalesced) do
pool.init_connection(connection, options)
Expand Down
30 changes: 30 additions & 0 deletions regression_tests/bug_1_1_1_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

require "test_helper"
require "support/http_helpers"

class Bug_1_1_1_Test < Minitest::Test
include HTTPHelpers

def test_conection_callbacks_fire_setup_once
uri = build_uri("/get")

connected = 0

HTTPX.on_connection_opened { |*| connected += 1 }
.on_connection_closed { |*| connected -= 1 }
.wrap do |session|
3.times.each do
response = session.get(uri)
verify_status(response, 200)
assert connected.zero?
end
end
end

private

def scheme
"http://"
end
end

0 comments on commit e273010

Please sign in to comment.