|
76 | 76 | parallel_installer.call |
77 | 77 | end |
78 | 78 | end |
| 79 | + |
| 80 | + describe "connect to make jobserver" do |
| 81 | + before do |
| 82 | + unless Gem::Installer.private_method_defined?(:build_jobs) |
| 83 | + skip "This example is runnable when RubyGems::Installer implements `build_jobs`" |
| 84 | + end |
| 85 | + |
| 86 | + require "support/artifice/compact_index" |
| 87 | + |
| 88 | + @previous_client = Gem::Request::ConnectionPools.client |
| 89 | + Gem::Request::ConnectionPools.client = Gem::Net::HTTP |
| 90 | + Gem::RemoteFetcher.fetcher.close_all |
| 91 | + |
| 92 | + build_repo2 do |
| 93 | + build_gem "one", &:add_c_extension |
| 94 | + build_gem "two", &:add_c_extension |
| 95 | + end |
| 96 | + |
| 97 | + gemfile <<~G |
| 98 | + source "https://gem.repo2" |
| 99 | +
|
| 100 | + gem "one" |
| 101 | + gem "two" |
| 102 | + G |
| 103 | + lockfile <<~L |
| 104 | + GEM |
| 105 | + remote: https://gem.repo2/ |
| 106 | + specs: |
| 107 | + one (1.0) |
| 108 | + two (1.0) |
| 109 | +
|
| 110 | + DEPENDENCIES |
| 111 | + one |
| 112 | + two |
| 113 | + L |
| 114 | + |
| 115 | + @old_ui = Bundler.ui |
| 116 | + Bundler.ui = Bundler::UI::Silent.new |
| 117 | + end |
| 118 | + |
| 119 | + after do |
| 120 | + Bundler.ui = @old_ui |
| 121 | + Gem::Request::ConnectionPools.client = @previous_client |
| 122 | + Artifice.deactivate |
| 123 | + end |
| 124 | + |
| 125 | + let(:definition) do |
| 126 | + allow(Bundler).to receive(:root) { bundled_app } |
| 127 | + |
| 128 | + definition = Bundler::Definition.build(bundled_app.join("Gemfile"), bundled_app.join("Gemfile.lock"), false) |
| 129 | + definition.tap(&:setup_domain!) |
| 130 | + end |
| 131 | + let(:installer) { Bundler::Installer.new(bundled_app, definition) } |
| 132 | + let(:gem_one) { definition.specs.find {|spec| spec.name == "one" } } |
| 133 | + let(:gem_two) { definition.specs.find {|spec| spec.name == "two" } } |
| 134 | + |
| 135 | + it "takes all available slots" do |
| 136 | + redefine_build_jobs do |
| 137 | + Bundler::ParallelInstaller.call(installer, definition.specs, 5, false, true) |
| 138 | + end |
| 139 | + |
| 140 | + # Take 3 slots out of the 5 available. |
| 141 | + expect(File.read(File.join(gem_one.extension_dir, "gem_make.out"))).to include("make -j3") |
| 142 | + # Take the remaining 2 slots. |
| 143 | + expect(File.read(File.join(gem_two.extension_dir, "gem_make.out"))).to include("make -j2") |
| 144 | + end |
| 145 | + |
| 146 | + it "fallback to non parallel when no slots are available" do |
| 147 | + redefine_build_jobs do |
| 148 | + Bundler::ParallelInstaller.call(installer, definition.specs, 3, false, true) |
| 149 | + end |
| 150 | + |
| 151 | + # Take 3 slots out of the 3 available. |
| 152 | + expect(File.read(File.join(gem_one.extension_dir, "gem_make.out"))).to include("make -j3") |
| 153 | + # Fallback to one slot (non parallel). |
| 154 | + expect(File.read(File.join(gem_two.extension_dir, "gem_make.out"))).to_not include("make -j") |
| 155 | + end |
| 156 | + |
| 157 | + it "uses one jobs when installing serially" do |
| 158 | + Bundler.settings.temporary(jobs: 1) do |
| 159 | + Bundler::ParallelInstaller.call(installer, definition.specs, 1, false, true) |
| 160 | + end |
| 161 | + |
| 162 | + expect(File.read(File.join(gem_one.extension_dir, "gem_make.out"))).to_not include("make -j") |
| 163 | + expect(File.read(File.join(gem_two.extension_dir, "gem_make.out"))).to_not include("make -j") |
| 164 | + end |
| 165 | + |
| 166 | + it "release the job slots" do |
| 167 | + build_repo2 do |
| 168 | + build_gem "one", &:add_c_extension |
| 169 | + build_gem "two" do |spec| |
| 170 | + spec.add_c_extension |
| 171 | + spec.add_dependency(:one) # ParallelInstaller will wait for `one` to be fully installed. |
| 172 | + end |
| 173 | + end |
| 174 | + |
| 175 | + Bundler::ParallelInstaller.call(installer, definition.specs, 3, false, true) |
| 176 | + |
| 177 | + # Take 3 slots out of the 3 available. |
| 178 | + expect(File.read(File.join(gem_one.extension_dir, "gem_make.out"))).to include("make -j3") |
| 179 | + # Take 3 slots that were released. |
| 180 | + expect(File.read(File.join(gem_two.extension_dir, "gem_make.out"))).to include("make -j3") |
| 181 | + end |
| 182 | + |
| 183 | + def redefine_build_jobs |
| 184 | + old_method = Bundler::RubyGemsGemInstaller.instance_method(:build_jobs) |
| 185 | + Bundler::RubyGemsGemInstaller.remove_method(:build_jobs) |
| 186 | + |
| 187 | + # Rendezvous so that "one" grabs its slots first and keeps holding them |
| 188 | + # until "two" has grabbed the rest. Blocking on a queue avoids the |
| 189 | + # busy-wait and makes the ordering deterministic. |
| 190 | + one_acquired = Thread::Queue.new |
| 191 | + two_acquired = Thread::Queue.new |
| 192 | + |
| 193 | + Bundler::RubyGemsGemInstaller.define_method(:build_jobs) do |
| 194 | + if spec.name == "one" |
| 195 | + value = old_method.bind(self).call |
| 196 | + one_acquired << true |
| 197 | + two_acquired.pop |
| 198 | + elsif spec.name == "two" |
| 199 | + one_acquired.pop |
| 200 | + value = old_method.bind(self).call |
| 201 | + two_acquired << true |
| 202 | + end |
| 203 | + |
| 204 | + value |
| 205 | + end |
| 206 | + |
| 207 | + yield |
| 208 | + ensure |
| 209 | + Bundler::RubyGemsGemInstaller.remove_method(:build_jobs) |
| 210 | + Bundler::RubyGemsGemInstaller.define_method(:build_jobs, old_method) |
| 211 | + end |
| 212 | + end |
79 | 213 | end |
0 commit comments