Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Commit 824fdd9

Browse files
committed
Fix DRb specs that were failing for me locally.
As the comment explains, I have had to configure my network adapter in a non-standard way, and it causes `IPSocket.getaddress(Socket.gethostname)` to raise an error for me. A couple of the DRb specs were failing as a result. The use of `return` from `ensure` caused the error to be ignored (the fact that `local_drb` was not set caused it to return false). This was confusing, and using `return` from `ensure` is a horrible practice. I have refactored to avoid the issue, rescuing errors explicitly.
1 parent c095c70 commit 824fdd9

File tree

2 files changed

+42
-11
lines changed

2 files changed

+42
-11
lines changed

lib/rspec/core/runner.rb

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -150,19 +150,28 @@ def self.installed_at_exit?
150150
end
151151

152152
# @private
153-
# rubocop:disable Lint/EnsureReturn
154153
def self.running_in_drb?
155-
if defined?(DRb) && DRb.current_server && DRb.current_server.alive?
156-
require 'socket'
157-
require 'uri'
158-
local_ipv4 = IPSocket.getaddress(Socket.gethostname)
159-
local_drb = ["127.0.0.1", "localhost", local_ipv4].any? { |addr| addr == URI(DRb.current_server.uri).host }
160-
end
161-
rescue DRb::DRbServerNotFound
162-
ensure
163-
return local_drb || false
154+
return false unless defined?(DRb)
155+
156+
server = begin
157+
DRb.current_server
158+
rescue DRb::DRbServerNotFound
159+
return false
160+
end
161+
162+
return false unless server && server.alive?
163+
164+
require 'socket'
165+
require 'uri'
166+
167+
local_ipv4 = begin
168+
IPSocket.getaddress(Socket.gethostname)
169+
rescue SocketError
170+
return false
171+
end
172+
173+
["127.0.0.1", "localhost", local_ipv4].any? { |addr| addr == URI(DRb.current_server.uri).host }
164174
end
165-
# rubocop:enable Lint/EnsureReturn
166175

167176
# @private
168177
def self.trap_interrupt

spec/rspec/core/runner_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,14 @@ def interrupt
162162

163163
before do
164164
allow(::DRb).to receive(:current_server) { drb_server }
165+
166+
# To deal with some network weirdness at my workplace, I had to
167+
# configure my network adapter in a non-standard way that causes
168+
# `IPSocket.getaddress(Socket.gethostname)` to raise
169+
# `SocketError: getaddrinfo: nodename nor servname provided, or not known`
170+
# I'm not sure why this happens, but to keep the specs here passing,
171+
# I have to stub this out :(.
172+
allow(IPSocket).to receive(:getaddress) { "127.0.0.1" }
165173
end
166174

167175
context "when drb server is started with 127.0.0.1" do
@@ -200,6 +208,20 @@ def interrupt
200208
it { should be_falsey }
201209
end
202210

211+
context "when IPSocket cannot resolve the current hostname" do
212+
let(:drb_server) do
213+
instance_double(::DRb::DRbServer, :uri => "druby://localhost:0000/", :alive? => true)
214+
end
215+
216+
before do
217+
allow(::IPSocket).to receive(:getaddress).and_raise(
218+
SocketError, "getaddrinfo: nodename nor servname provided, or not known"
219+
)
220+
end
221+
222+
it { should be_falsey }
223+
end
224+
203225
context "when no drb server is running" do
204226
let(:drb_server) do
205227
raise ::DRb::DRbServerNotFound

0 commit comments

Comments
 (0)