Skip to content

Commit 7c115b8

Browse files
deivid-rodriguezhsbt
authored andcommitted
[rubygems/rubygems] Fix flaky test failures in mirror probing specs
The mirror probing spec file was moved to our regular suite, which runs in parallel, recently. These specs rely on starting and stopping actual servers in localhost, but this does not play nice with parallelization, because a spec may kill the webserver another spec has created. This commit moves mirror probing specs to not need to start servers in localhost and do something more similar to what the other specs do. ruby/rubygems@ca9a19706f
1 parent 353fa6f commit 7c115b8

File tree

4 files changed

+54
-80
lines changed

4 files changed

+54
-80
lines changed
Lines changed: 18 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,23 @@
11
# frozen_string_literal: true
22

33
RSpec.describe "fetching dependencies with a not available mirror" do
4-
let(:host) { "127.0.0.1" }
5-
64
before do
7-
require_rack_test
8-
setup_server
9-
setup_mirror
10-
end
5+
build_repo2
116

12-
after do
13-
Artifice.deactivate
14-
@server_thread.kill
15-
@server_thread.join
7+
gemfile <<-G
8+
source "https://gem.repo2"
9+
gem 'weakling'
10+
G
1611
end
1712

1813
context "with a specific fallback timeout" do
1914
before do
20-
global_config("BUNDLE_MIRROR__HTTP://127__0__0__1:#{@server_port}/__FALLBACK_TIMEOUT/" => "true",
21-
"BUNDLE_MIRROR__HTTP://127__0__0__1:#{@server_port}/" => @mirror_uri)
15+
global_config("BUNDLE_MIRROR__HTTPS://GEM__REPO2/__FALLBACK_TIMEOUT/" => "true",
16+
"BUNDLE_MIRROR__HTTPS://GEM__REPO2/" => "https://gem.mirror")
2217
end
2318

2419
it "install a gem using the original uri when the mirror is not responding" do
25-
gemfile <<-G
26-
source "#{@server_uri}"
27-
gem 'weakling'
28-
G
29-
30-
bundle :install, artifice: nil
20+
bundle :install, env: { "BUNDLER_SPEC_FAKE_RESOLVE" => "gem.mirror" }, verbose: true
3121

3222
expect(out).to include("Installing weakling")
3323
expect(out).to include("Bundle complete")
@@ -38,16 +28,11 @@
3828
context "with a global fallback timeout" do
3929
before do
4030
global_config("BUNDLE_MIRROR__ALL__FALLBACK_TIMEOUT/" => "1",
41-
"BUNDLE_MIRROR__ALL" => @mirror_uri)
31+
"BUNDLE_MIRROR__ALL" => "https://gem.mirror")
4232
end
4333

4434
it "install a gem using the original uri when the mirror is not responding" do
45-
gemfile <<-G
46-
source "#{@server_uri}"
47-
gem 'weakling'
48-
G
49-
50-
bundle :install, artifice: nil
35+
bundle :install, env: { "BUNDLER_SPEC_FAKE_RESOLVE" => "gem.mirror" }
5136

5237
expect(out).to include("Installing weakling")
5338
expect(out).to include("Bundle complete")
@@ -57,73 +42,27 @@
5742

5843
context "with a specific mirror without a fallback timeout" do
5944
before do
60-
global_config("BUNDLE_MIRROR__HTTP://127__0__0__1:#{@server_port}/" => @mirror_uri)
45+
global_config("BUNDLE_MIRROR__HTTPS://GEM__REPO2/" => "https://gem.mirror")
6146
end
6247

6348
it "fails to install the gem with a timeout error when the mirror is not responding" do
64-
gemfile <<-G
65-
source "#{@server_uri}"
66-
gem 'weakling'
67-
G
49+
bundle :install, artifice: "compact_index_mirror_down", raise_on_error: false
6850

69-
bundle :install, artifice: nil, raise_on_error: false
70-
71-
expect(out).to include("Fetching source index from #{@mirror_uri}")
72-
73-
err_lines = err.split("\n")
74-
expect(err_lines).to include(%r{\ARetrying fetcher due to error \(2/4\): Bundler::HTTPError Could not fetch specs from #{@mirror_uri}/ due to underlying error <})
75-
expect(err_lines).to include(%r{\ARetrying fetcher due to error \(3/4\): Bundler::HTTPError Could not fetch specs from #{@mirror_uri}/ due to underlying error <})
76-
expect(err_lines).to include(%r{\ARetrying fetcher due to error \(4/4\): Bundler::HTTPError Could not fetch specs from #{@mirror_uri}/ due to underlying error <})
77-
expect(err_lines).to include(%r{\ACould not fetch specs from #{@mirror_uri}/ due to underlying error <})
51+
expect(out).to be_empty
52+
expect(err).to eq("Could not reach host gem.mirror. Check your network connection and try again.")
7853
end
7954
end
8055

8156
context "with a global mirror without a fallback timeout" do
8257
before do
83-
global_config("BUNDLE_MIRROR__ALL" => @mirror_uri)
58+
global_config("BUNDLE_MIRROR__ALL" => "https://gem.mirror")
8459
end
8560

8661
it "fails to install the gem with a timeout error when the mirror is not responding" do
87-
gemfile <<-G
88-
source "#{@server_uri}"
89-
gem 'weakling'
90-
G
91-
92-
bundle :install, artifice: nil, raise_on_error: false
93-
94-
expect(out).to include("Fetching source index from #{@mirror_uri}")
62+
bundle :install, artifice: "compact_index_mirror_down", raise_on_error: false
9563

96-
err_lines = err.split("\n")
97-
expect(err_lines).to include(%r{\ARetrying fetcher due to error \(2/4\): Bundler::HTTPError Could not fetch specs from #{@mirror_uri}/ due to underlying error <})
98-
expect(err_lines).to include(%r{\ARetrying fetcher due to error \(3/4\): Bundler::HTTPError Could not fetch specs from #{@mirror_uri}/ due to underlying error <})
99-
expect(err_lines).to include(%r{\ARetrying fetcher due to error \(4/4\): Bundler::HTTPError Could not fetch specs from #{@mirror_uri}/ due to underlying error <})
100-
expect(err_lines).to include(%r{\ACould not fetch specs from #{@mirror_uri}/ due to underlying error <})
64+
expect(out).to be_empty
65+
expect(err).to eq("Could not reach host gem.mirror. Check your network connection and try again.")
10166
end
10267
end
103-
104-
def setup_server
105-
@server_port = find_unused_port
106-
@server_uri = "http://#{host}:#{@server_port}"
107-
108-
require_relative "../../support/artifice/compact_index"
109-
require_relative "../../support/silent_logger"
110-
111-
require "rackup/server"
112-
113-
@server_thread = Thread.new do
114-
Rackup::Server.start(app: CompactIndexAPI,
115-
Host: host,
116-
Port: @server_port,
117-
server: "webrick",
118-
AccessLog: [],
119-
Logger: Spec::SilentLogger.new)
120-
end.run
121-
122-
wait_for_server(host, @server_port)
123-
end
124-
125-
def setup_mirror
126-
@mirror_port = find_unused_port
127-
@mirror_uri = "http://#{host}:#{@mirror_port}"
128-
end
12968
end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# frozen_string_literal: true
2+
3+
require_relative "helpers/compact_index"
4+
require_relative "helpers/artifice"
5+
require_relative "helpers/rack_request"
6+
7+
module Artifice
8+
module Net
9+
class HTTPMirrorDown < HTTP
10+
def connect
11+
raise SocketError if address == "gem.mirror"
12+
13+
super
14+
end
15+
end
16+
17+
HTTP.endpoint = CompactIndexAPI
18+
end
19+
20+
replace_net_http(Net::HTTPMirrorDown)
21+
end

spec/bundler/support/artifice/helpers/endpoint.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def self.all_requests
2727

2828
set :raise_errors, true
2929
set :show_exceptions, false
30-
set :host_authorization, permitted_hosts: [".example.org", ".local", ".repo", ".repo1", ".repo2", ".repo3", ".repo4", ".rubygems.org", ".security", ".source", ".test", "127.0.0.1"]
30+
set :host_authorization, permitted_hosts: [".example.org", ".local", ".mirror", ".repo", ".repo1", ".repo2", ".repo3", ".repo4", ".rubygems.org", ".security", ".source", ".test", "127.0.0.1"]
3131

3232
def call!(*)
3333
super.tap do

spec/bundler/support/hax.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,18 @@ def open(file, mode)
5151

5252
File.singleton_class.prepend ReadOnly
5353
end
54+
55+
if ENV["BUNDLER_SPEC_FAKE_RESOLVE"]
56+
module FakeResolv
57+
def getaddrinfo(host, port)
58+
if host == ENV["BUNDLER_SPEC_FAKE_RESOLVE"]
59+
[["AF_INET", port, "127.0.0.1", "127.0.0.1", 2, 2, 17]]
60+
else
61+
super
62+
end
63+
end
64+
end
65+
66+
Socket.singleton_class.prepend FakeResolv
67+
end
5468
end

0 commit comments

Comments
 (0)