Skip to content

Commit 06e334d

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

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
@@ -794,27 +794,35 @@ def createRackServletWrapper(runtime, rackup, filename)
794794
expect(java.lang.System.currentTimeMillis - start).to be_within(20).of(timeout_millis)
795795
end
796796

797-
it "initializes initial runtimes in paralel (with wait set to false)" do
797+
it "initializes initial runtimes in parallel (with wait set to false)" do
798+
@factory = double("factory (non-tracking)").as_null_object
799+
@pooling_factory = org.jruby.rack.PoolingRackApplicationFactory.new @factory
800+
@pooling_factory.context = @rack_context
801+
802+
app_init_secs = 0.05
798803
allow(@factory).to receive(:init)
799804
allow(@factory).to receive(:newApplication) do
800-
app = double "app"
801-
allow(app).to receive(:init) do
802-
sleep(0.15)
803-
end
805+
app = double("app").as_null_object
806+
allow(app).to receive(:init) { sleep(app_init_secs) }
804807
app
805808
end
809+
810+
init_threads = 4
811+
init_runtimes = 6
806812
allow(@rack_config).to receive(:getBooleanProperty).with("jruby.runtime.init.wait").and_return false
807-
allow(@rack_config).to receive(:getInitialRuntimes).and_return 6
813+
allow(@rack_config).to receive(:getRuntimeInitThreads).and_return init_threads
814+
allow(@rack_config).to receive(:getInitialRuntimes).and_return init_runtimes
808815
allow(@rack_config).to receive(:getMaximumRuntimes).and_return 8
809816

817+
expected_initial_init_time = 1.1 * (init_runtimes.to_f / init_threads.to_f).ceil * app_init_secs # 10% margin
810818
@pooling_factory.init(@rack_context)
811-
sleep(0.10)
812-
expect(@pooling_factory.getApplicationPool.size).to be < 6
813-
sleep(0.9)
814-
expect(@pooling_factory.getApplicationPool.size).to be >= 6
819+
sleep(app_init_secs) # wait for at some (but not possibly all) to finish
820+
expect(@pooling_factory.getApplicationPool.size).to be < init_runtimes
821+
sleep(expected_initial_init_time - app_init_secs) # remaining time
822+
expect(@pooling_factory.getApplicationPool.size).to be >= init_runtimes
815823

816824
expect(@pooling_factory.getManagedApplications).to_not be_empty
817-
expect(@pooling_factory.getManagedApplications.size).to eql 6
825+
expect(@pooling_factory.getManagedApplications.size).to eql init_runtimes
818826
end
819827

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

0 commit comments

Comments
 (0)