Skip to content

Commit 840725b

Browse files
committed
[tests] Experiment with fixing test race condition on servlet_context
1 parent 9174510 commit 840725b

File tree

3 files changed

+11
-13
lines changed

3 files changed

+11
-13
lines changed

src/spec/ruby/jruby/rack/rails_booter_spec.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,6 @@
122122
end
123123
end
124124

125-
after :all do
126-
$servlet_context = nil
127-
end
128-
129125
let(:railtie_class) { Class.new(Rails::Railtie) }
130126

131127
it "should have loaded the railtie" do

src/spec/ruby/rack/application_spec.rb

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ def it_should_rewind_body
104104

105105
before :each do
106106
@app_factory = DefaultRackApplicationFactory.new
107+
reset_booter
108+
reset_servlet_context_global
107109
end
108110

109111
it "should receive a rackup script via the 'rackup' parameter" do
@@ -161,11 +163,6 @@ def it_should_rewind_body
161163
expect(input_stream.getMaximumBufferSize).to eq 420
162164
end
163165

164-
before do
165-
reset_booter
166-
JRuby::Rack.context = $servlet_context = nil
167-
end
168-
169166
it "should init and create application object without a rackup script" do
170167
$servlet_context = @servlet_context
171168
# NOTE: a workaround to be able to mock it :
@@ -340,7 +337,7 @@ def newRuntime()
340337

341338
it "initializes the $servlet_context global variable" do
342339
@runtime = app_factory.new_runtime
343-
should_not_eval_as_nil "defined?($servlet_context)"
340+
expect(@runtime.evalScriptlet("defined?($servlet_context)")).to be_truthy
344341
end
345342

346343
it "clears environment variables if the configuration ignores the environment" do

src/spec/ruby/spec_helper.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ def servlet_context
5151
mock_servlet_context
5252
end
5353

54+
def reset_servlet_context_global
55+
$servlet_context = nil if defined? $servlet_context
56+
JRuby::Rack.context = nil if defined?(JRuby::Rack) and JRuby::Rack.respond_to?(:context=)
57+
end
58+
5459
def silence_warnings(&block)
5560
JRuby::Rack::Helpers.silence_warnings(&block)
5661
end
@@ -125,7 +130,7 @@ def should_not_eval_as_nil(code, runtime = @runtime)
125130

126131
STUB_DIR = File.expand_path('../stub', File.dirname(__FILE__))
127132

128-
WD_START = Dir.getwd
133+
ORIGINAL_WORKING_DIR = Dir.getwd
129134

130135
begin
131136
# NOTE: only if running with a `bundle exec` to better isolate
@@ -154,8 +159,8 @@ def should_not_eval_as_nil(code, runtime = @runtime)
154159
config.after(:each) do
155160
(ENV.keys - @env_save.keys).each { |k| ENV.delete k }
156161
@env_save.each { |k, v| ENV[k] = v }
157-
Dir.chdir(WD_START) unless Dir.getwd == WD_START
158-
$servlet_context = nil if defined? $servlet_context
162+
Dir.chdir(ORIGINAL_WORKING_DIR) unless Dir.getwd == ORIGINAL_WORKING_DIR
163+
reset_servlet_context_global
159164
end
160165

161166
# NOTE: only works when no other example filtering is in place: e.g. `rspec ... --example=logger` won't filter here

0 commit comments

Comments
 (0)