diff options
-rw-r--r-- | lib/bundler/mirror.rb | 36 | ||||
-rw-r--r-- | spec/bundler/mirror_spec.rb | 33 |
2 files changed, 36 insertions, 33 deletions
diff --git a/lib/bundler/mirror.rb b/lib/bundler/mirror.rb index a18d3528..6a653487 100644 --- a/lib/bundler/mirror.rb +++ b/lib/bundler/mirror.rb @@ -4,7 +4,7 @@ module Bundler def initialize(prober = nil) @all = Mirror.new @prober = prober || TCPSocketProbe.new - @mirrors = Hash.new + @mirrors = {} end def for(uri) @@ -31,7 +31,7 @@ module Bundler config.update_mirror(mirror) end - private + private def fetch_valid_mirror_for(uri) mirror = (@mirrors[uri] || Mirror.new(uri)).validate!(@prober) @@ -53,10 +53,10 @@ module Bundler def uri=(uri) @uri = if uri.nil? - uri = nil - else - URI(uri.to_s) - end + nil + else + URI(uri.to_s) + end @valid = nil end @@ -72,8 +72,8 @@ module Bundler @valid = nil end - def ==(o) - o != nil && self.uri == o.uri && self.fallback_timeout == o.fallback_timeout + def ==(other) + !other.nil? && uri == other.uri && fallback_timeout == other.fallback_timeout end def valid? @@ -91,14 +91,12 @@ module Bundler end end - private - class MirrorConfig attr_accessor :uri, :value def initialize(config_line, value) uri, fallback = - config_line.match(/^mirror\.(all|.+?)(\.fallback_timeout)?\/?$/).captures + config_line.match(%r{^mirror\.(all|.+?)(\.fallback_timeout)?\/?$}).captures @fallback = !fallback.nil? @all = false if uri == "all" @@ -135,7 +133,7 @@ module Bundler end end - private + private def wait_for_writtable_socket(socket, address, timeout) if IO.select(nil, [socket], nil, timeout) @@ -146,13 +144,11 @@ module Bundler end def probe_writtable_socket(socket, address) - begin - socket.connect_nonblock(address) - rescue Errno::EISCONN - true - rescue => e # Connection failed - false - end + socket.connect_nonblock(address) + rescue Errno::EISCONN + true + rescue # Connection failed + false end end end @@ -174,7 +170,7 @@ module Bundler value end - private + private def socket_address Socket.pack_sockaddr_in(@port, @host) diff --git a/spec/bundler/mirror_spec.rb b/spec/bundler/mirror_spec.rb index 6e5cede0..88436fd0 100644 --- a/spec/bundler/mirror_spec.rb +++ b/spec/bundler/mirror_spec.rb @@ -59,7 +59,7 @@ describe Bundler::Settings::Mirror do end context "when probed" do - let(:probe) { double() } + let(:probe) { double } context "with a replying mirror" do before do @@ -93,7 +93,7 @@ describe Bundler::Settings::Mirror do end context "when probed" do - let(:probe) { double() } + let(:probe) { double } context "with a replying mirror" do before do @@ -138,7 +138,7 @@ describe Bundler::Settings::Mirrors do context "with a just created mirror" do let(:mirrors) do - probe = double() + probe = double allow(probe).to receive(:replies?).and_return(true) Bundler::Settings::Mirrors.new(probe) end @@ -175,7 +175,7 @@ describe Bundler::Settings::Mirrors do context "with a mirror prober that replies on time" do let(:mirrors) do - probe = double() + probe = double allow(probe).to receive(:replies?).and_return(true) Bundler::Settings::Mirrors.new(probe) end @@ -221,7 +221,7 @@ describe Bundler::Settings::Mirrors do context "with a mirror prober that does not reply on time" do let(:mirrors) do - probe = double() + probe = double allow(probe).to receive(:replies?).and_return(false) Bundler::Settings::Mirrors.new(probe) end @@ -276,23 +276,30 @@ describe Bundler::Settings::TCPSocketProbe do let(:probe) { Bundler::Settings::TCPSocketProbe.new } context "with a listening TCP Server" do - let(:server) { TCPServer.new("127.0.0.1", 0) } - let(:port) { server.addr[1] } - let(:mirror) { Bundler::Settings::Mirror.new(uri="http://localhost:#{port}", fallback_timeout=true) } + def with_server_and_mirror + server = TCPServer.new("127.0.0.1", 0) + mirror = Bundler::Settings::Mirror.new("http://localhost:#{server.addr[1]}", 1) + yield server, mirror + server.close unless server.closed? + end it "probes the server correctly" do - expect(probe.replies?(mirror)).to be_truthy + with_server_and_mirror do |server, mirror| + expect(server.closed?).to be_falsey + expect(probe.replies?(mirror)).to be_truthy + end end it "probes falsey when the server is down" do - my_mirror = mirror - server.close - expect(probe.replies?(my_mirror)).to be_falsey + with_server_and_mirror do |server, mirror| + server.close + expect(probe.replies?(mirror)).to be_falsey + end end end context "with an invalid mirror" do - let(:mirror) { Bundler::Settings::Mirror.new(uri="http://127.0.0.127:#{9292}", fallback_timeout=true) } + let(:mirror) { Bundler::Settings::Mirror.new("http://127.0.0.127:9292", true) } it "fails with a timeout when there is nothing to tcp handshake" do expect(probe.replies?(mirror)).to be_falsey |