Merge pull request #30 from puppetlabs/update_timeout_method

(maint) Use timeout builtin to TCPSocket when opening sockets.
This commit is contained in:
Jake Spain 2023-03-06 13:59:21 -05:00 committed by GitHub
commit f7c797a4f4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 31 additions and 35 deletions

View file

@ -73,7 +73,7 @@ jobs:
- name: Install Ruby jruby-9.3.6.0 - name: Install Ruby jruby-9.3.6.0
uses: ruby/setup-ruby@v1 uses: ruby/setup-ruby@v1
with: with:
ruby-version: 'jruby-9.3.6.0' ruby-version: 'jruby-9.4.1.0'
- name: Build gem - name: Build gem
run: gem build *.gemspec run: gem build *.gemspec

View file

@ -18,8 +18,7 @@ jobs:
strategy: strategy:
matrix: matrix:
ruby-version: ruby-version:
- 'jruby-9.3.6.0' - 'jruby-9.4.1.0'
- 'jruby-9.4.0.0'
steps: steps:
- uses: actions/checkout@v3 - uses: actions/checkout@v3
- name: Set up Ruby - name: Set up Ruby
@ -35,8 +34,7 @@ jobs:
strategy: strategy:
matrix: matrix:
ruby-version: ruby-version:
- 'jruby-9.3.6.0' - 'jruby-9.4.1.0'
- 'jruby-9.4.0.0'
steps: steps:
- uses: actions/checkout@v3 - uses: actions/checkout@v3
- name: Set up Ruby - name: Set up Ruby

View file

@ -9,11 +9,11 @@ GEM
remote: https://rubygems.org/ remote: https://rubygems.org/
specs: specs:
ast (2.4.2) ast (2.4.2)
bindata (2.4.14) bindata (2.4.15)
builder (3.2.4) builder (3.2.4)
climate_control (1.2.0) climate_control (1.2.0)
coderay (1.1.3) coderay (1.1.3)
concurrent-ruby (1.2.0) concurrent-ruby (1.2.2)
connection_pool (2.3.0) connection_pool (2.3.0)
deep_merge (1.2.2) deep_merge (1.2.2)
diff-lcs (1.5.0) diff-lcs (1.5.0)
@ -33,7 +33,7 @@ GEM
ruby2_keywords (~> 0.0.1) ruby2_keywords (~> 0.0.1)
net-ldap (0.17.1) net-ldap (0.17.1)
nio4r (2.5.8-java) nio4r (2.5.8-java)
nokogiri (1.13.10-java) nokogiri (1.14.2-java)
racc (~> 1.4) racc (~> 1.4)
opentelemetry-api (1.1.0) opentelemetry-api (1.1.0)
opentelemetry-common (0.19.6) opentelemetry-common (0.19.6)
@ -74,7 +74,7 @@ GEM
opentelemetry-api (~> 1.0) opentelemetry-api (~> 1.0)
optimist (3.0.1) optimist (3.0.1)
parallel (1.22.1) parallel (1.22.1)
parser (3.2.0.0) parser (3.2.1.0)
ast (~> 2.4.1) ast (~> 2.4.1)
pickup (0.0.11) pickup (0.0.11)
prometheus-client (2.1.0) prometheus-client (2.1.0)
@ -97,14 +97,14 @@ GEM
json (~> 2.3) json (~> 2.3)
nokogiri (~> 1.12, >= 1.12.5) nokogiri (~> 1.12, >= 1.12.5)
optimist (~> 3.0) optimist (~> 3.0)
redis (4.8.0) redis (4.8.1)
regexp_parser (2.6.2) regexp_parser (2.7.0)
rexml (3.2.5) rexml (3.2.5)
rspec (3.12.0) rspec (3.12.0)
rspec-core (~> 3.12.0) rspec-core (~> 3.12.0)
rspec-expectations (~> 3.12.0) rspec-expectations (~> 3.12.0)
rspec-mocks (~> 3.12.0) rspec-mocks (~> 3.12.0)
rspec-core (3.12.0) rspec-core (3.12.1)
rspec-support (~> 3.12.0) rspec-support (~> 3.12.0)
rspec-expectations (3.12.2) rspec-expectations (3.12.2)
diff-lcs (>= 1.2.0, < 2.0) diff-lcs (>= 1.2.0, < 2.0)
@ -122,9 +122,9 @@ GEM
rubocop-ast (>= 1.17.0, < 2.0) rubocop-ast (>= 1.17.0, < 2.0)
ruby-progressbar (~> 1.7) ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 3.0) unicode-display_width (>= 1.4.0, < 3.0)
rubocop-ast (1.24.1) rubocop-ast (1.27.0)
parser (>= 3.1.1.0) parser (>= 3.2.1.0)
ruby-progressbar (1.11.0) ruby-progressbar (1.12.0)
ruby2_keywords (0.0.5) ruby2_keywords (0.0.5)
simplecov (0.22.0) simplecov (0.22.0)
docile (~> 1.1) docile (~> 1.1)
@ -143,8 +143,8 @@ GEM
ffi ffi
statsd-ruby (1.5.0) statsd-ruby (1.5.0)
thor (1.2.1) thor (1.2.1)
thrift (0.17.0) thrift (0.18.1)
tilt (2.0.11) tilt (2.1.0)
unicode-display_width (2.4.2) unicode-display_width (2.4.2)
vmpooler (2.4.0) vmpooler (2.4.0)
concurrent-ruby (~> 1.1) concurrent-ruby (~> 1.1)

View file

@ -632,15 +632,13 @@ module Vmpooler
# This should supercede the open_socket method in the Pool Manager # This should supercede the open_socket method in the Pool Manager
def open_socket(host, domain = nil, timeout = 5, port = 22, &_block) def open_socket(host, domain = nil, timeout = 5, port = 22, &_block)
Timeout.timeout(timeout) do target_host = host
target_host = host target_host = "#{host}.#{domain}" if domain
target_host = "#{host}.#{domain}" if domain sock = TCPSocket.new(target_host, port, connect_timeout: timeout)
sock = TCPSocket.new target_host, port begin
begin yield sock if block_given?
yield sock if block_given? ensure
ensure sock.close
sock.close
end
end end
end end

View file

@ -1399,44 +1399,44 @@ EOT
end end
it 'opens socket with defaults' do it 'opens socket with defaults' do
expect(TCPSocket).to receive(:new).with(hostname,default_socket).and_return(socket) expect(TCPSocket).to receive(:new).with(hostname,default_socket,{connect_timeout: 5}).and_return(socket)
expect(subject.open_socket(hostname)).to eq(nil) expect(subject.open_socket(hostname)).to eq(nil)
end end
it 'yields the socket if a block is given' do it 'yields the socket if a block is given' do
expect(TCPSocket).to receive(:new).with(hostname,default_socket).and_return(socket) expect(TCPSocket).to receive(:new).with(hostname,default_socket,{connect_timeout: nil}).and_return(socket)
expect{ |socket| subject.open_socket(hostname,nil,nil,default_socket,&socket) }.to yield_control.exactly(1).times expect{ |socket| subject.open_socket(hostname,nil,nil,default_socket,&socket) }.to yield_control.exactly(1).times
end end
it 'closes the opened socket' do it 'closes the opened socket' do
expect(TCPSocket).to receive(:new).with(hostname,default_socket).and_return(socket) expect(TCPSocket).to receive(:new).with(hostname,default_socket,{connect_timeout: 5}).and_return(socket)
expect(socket).to receive(:close) expect(socket).to receive(:close)
expect(subject.open_socket(hostname)).to eq(nil) expect(subject.open_socket(hostname)).to eq(nil)
end end
it 'opens a specific socket' do it 'opens a specific socket' do
expect(TCPSocket).to receive(:new).with(hostname,80).and_return(socket) expect(TCPSocket).to receive(:new).with(hostname,80,{connect_timeout: nil}).and_return(socket)
expect(subject.open_socket(hostname,nil,nil,80)).to eq(nil) expect(subject.open_socket(hostname,nil,nil,80)).to eq(nil)
end end
it 'uses a specific domain with the hostname' do it 'uses a specific domain with the hostname' do
expect(TCPSocket).to receive(:new).with("#{hostname}.#{domain}",default_socket).and_return(socket) expect(TCPSocket).to receive(:new).with("#{hostname}.#{domain}",default_socket,{connect_timeout: 5}).and_return(socket)
expect(subject.open_socket(hostname,domain)).to eq(nil) expect(subject.open_socket(hostname,domain)).to eq(nil)
end end
it 'raises error if host is not resolvable' do it 'raises error if host is not resolvable' do
expect(TCPSocket).to receive(:new).with(hostname,default_socket).and_raise(SocketError,'getaddrinfo: No such host is known') expect(TCPSocket).to receive(:new).with(hostname,default_socket,{connect_timeout: 1}).and_raise(SocketError,'getaddrinfo: No such host is known')
expect { subject.open_socket(hostname,nil,1) }.to raise_error(SocketError) expect { subject.open_socket(hostname,nil,1) }.to raise_error(SocketError)
end end
it 'raises error if socket is not listening' do it 'raises error if socket is not listening' do
expect(TCPSocket).to receive(:new).with(hostname,default_socket).and_raise(SocketError,'No connection could be made because the target machine actively refused it') expect(TCPSocket).to receive(:new).with(hostname,default_socket,{connect_timeout: 1}).and_raise(SocketError,'No connection could be made because the target machine actively refused it')
expect { subject.open_socket(hostname,nil,1) }.to raise_error(SocketError) expect { subject.open_socket(hostname,nil,1) }.to raise_error(SocketError)
end end

View file

@ -3,5 +3,5 @@
# The container tag should closely match what is used in `docker/Dockerfile` in vmpooler-deployment # The container tag should closely match what is used in `docker/Dockerfile` in vmpooler-deployment
docker run -it --rm \ docker run -it --rm \
-v $(pwd):/app \ -v $(pwd):/app \
jruby:9.3.6-jdk \ jruby:9.4.1.0-jdk11 \
/bin/bash -c 'apt-get update -qq && apt-get install -y --no-install-recommends make git && cd /app && gem install bundler && bundle install --jobs 3 && bundle update; echo "LOCK_FILE_UPDATE_EXIT_CODE=$?"' /bin/bash -c 'apt-get update -qq && apt-get install -y --no-install-recommends git make netbase && cd /app && gem install bundler && bundle install --jobs 3 && bundle update; echo "LOCK_FILE_UPDATE_EXIT_CODE=$?"'