Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mullermp committed Dec 14, 2024
1 parent d79bb30 commit 4c79449
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 16 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ name: CI
on:
push:
branches:
- main
- decaf

pull_request:
branches:
- main
- decaf

env:
ruby_version: 3.3
Expand Down
18 changes: 5 additions & 13 deletions gems/smithy-client/lib/smithy-client/net_http/connection_pool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def session_for(endpoint)
session.read_timeout = http_read_timeout if http_read_timeout
session.continue_timeout = http_continue_timeout if http_continue_timeout
yield(session)
rescue
rescue StandardError
session&.finish
raise
else
Expand Down Expand Up @@ -251,26 +251,18 @@ def _clean
# Helper methods extended onto Net::HTTPSession objects opened by the
# connection pool.
# @api private
class ExtendedSession < Delegator
class ExtendedSession < SimpleDelegator
def initialize(http)
super
@http = http
end

# @return [Integer,nil]
# @return [Integer, nil]
attr_reader :last_used

def __getobj__
@http
end

def __setobj__(obj)
@http = obj
end

# Sends the request and tracks that this session has been used.
def request(*args, &block)
@http.request(*args, &block)
def request(...)
@http.request(...)
@last_used = Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module NetHTTP
session = double(
'Net::HTTPSession',
last_used: Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond),
keep_alive_timeout: 2,
keep_alive_timeout: 2
).as_null_object
pool = ConnectionPool.for({})
expect(pool).to receive(:start_session)
Expand Down Expand Up @@ -138,6 +138,31 @@ module NetHTTP
expect(ConnectionPool.pools).to eq(ConnectionPool.pools)
end
end

describe ConnectionPool::ExtendedSession do
let(:request) { Net::HTTP::Get.new('/') }
let(:session) { ConnectionPool::ExtendedSession.new(net_http) }
let(:net_http) { Net::HTTP.new('https://example.com') }

it 'delegates to Net::HTTP' do
expect(session).to be_a(Delegator)
expect(session.__getobj__).to be(net_http)
end

describe '#request' do
it 'sets the last used time' do
expect(net_http).to receive(:request).and_return(double('response', body: ''))
expect { session.request(request) }.to(change { session.last_used })
end
end

describe '#finish' do
it 'closes the connection without errors' do
expect(net_http).to receive(:finish).and_raise(IOError)
expect { session.finish }.not_to raise_error
end
end
end
end
end
end
Expand Down

0 comments on commit 4c79449

Please sign in to comment.