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

Commit 6350b3a

Browse files
committed
Merge pull request #2165 from rspec/fix-drb-weirdness
Fix drb weirdness
2 parents 59b0335 + 824fdd9 commit 6350b3a

File tree

2 files changed

+39
-21
lines changed

2 files changed

+39
-21
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: 19 additions & 10 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
@@ -192,27 +200,28 @@ def interrupt
192200
it { should be_truthy }
193201
end
194202

195-
context "when drb server is started with another local ip address" do
203+
context "when drb server is started with 127.0.0.1 but not alive" do
196204
let(:drb_server) do
197-
instance_double(::DRb::DRbServer, :uri => "druby://192.168.0.1:0000/", :alive? => true)
198-
end
199-
200-
before do
201-
allow(::IPSocket).to receive(:getaddress).and_return("192.168.0.1")
205+
instance_double(::DRb::DRbServer, :uri => "druby://127.0.0.1:0000/", :alive? => false)
202206
end
203207

204-
it { should be_truthy }
208+
it { should be_falsey }
205209
end
206210

207-
context "when drb server is started with 127.0.0.1 but not alive" do
211+
context "when IPSocket cannot resolve the current hostname" do
208212
let(:drb_server) do
209-
instance_double(::DRb::DRbServer, :uri => "druby://127.0.0.1:0000/", :alive? => false)
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+
)
210220
end
211221

212222
it { should be_falsey }
213223
end
214224

215-
216225
context "when no drb server is running" do
217226
let(:drb_server) do
218227
raise ::DRb::DRbServerNotFound

0 commit comments

Comments
 (0)