Skip to content

Commit fee1b99

Browse files
committed
[tests] Fix threading issue with parallel testing with non-threadsafe rspec mocks
1 parent 2588463 commit fee1b99

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

src/spec/ruby/rack/application_spec.rb

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -798,27 +798,35 @@ def createRackServletWrapper(runtime, rackup, filename)
798798
expect(java.lang.System.currentTimeMillis - start).to be_within(20).of(timeout_millis)
799799
end
800800

801-
it "initializes initial runtimes in paralel (with wait set to false)" do
801+
it "initializes initial runtimes in parallel (with wait set to false)" do
802+
@factory = double("factory (non-tracking)").as_null_object
803+
@pooling_factory = org.jruby.rack.PoolingRackApplicationFactory.new @factory
804+
@pooling_factory.context = @rack_context
805+
806+
app_init_secs = 0.05
802807
allow(@factory).to receive(:init)
803808
allow(@factory).to receive(:newApplication) do
804-
app = double "app"
805-
allow(app).to receive(:init) do
806-
sleep(0.15)
807-
end
809+
app = double("app").as_null_object
810+
allow(app).to receive(:init) { sleep(app_init_secs) }
808811
app
809812
end
813+
814+
init_threads = 4
815+
init_runtimes = 6
810816
allow(@rack_config).to receive(:getBooleanProperty).with("jruby.runtime.init.wait").and_return false
811-
allow(@rack_config).to receive(:getInitialRuntimes).and_return 6
817+
allow(@rack_config).to receive(:getRuntimeInitThreads).and_return init_threads
818+
allow(@rack_config).to receive(:getInitialRuntimes).and_return init_runtimes
812819
allow(@rack_config).to receive(:getMaximumRuntimes).and_return 8
813820

821+
expected_initial_init_time = 1.1 * (init_runtimes.to_f / init_threads.to_f).ceil * app_init_secs # 10% margin
814822
@pooling_factory.init(@rack_context)
815-
sleep(0.10)
816-
expect(@pooling_factory.getApplicationPool.size).to be < 6
817-
sleep(0.9)
818-
expect(@pooling_factory.getApplicationPool.size).to be >= 6
823+
sleep(app_init_secs) # wait for at some (but not possibly all) to finish
824+
expect(@pooling_factory.getApplicationPool.size).to be < init_runtimes
825+
sleep(expected_initial_init_time - app_init_secs) # remaining time
826+
expect(@pooling_factory.getApplicationPool.size).to be >= init_runtimes
819827

820828
expect(@pooling_factory.getManagedApplications).to_not be_empty
821-
expect(@pooling_factory.getManagedApplications.size).to eql 6
829+
expect(@pooling_factory.getManagedApplications.size).to eql init_runtimes
822830
end
823831

824832
it "throws from init when application initialization in thread failed" do

0 commit comments

Comments
 (0)