Skip to content

Commit bfce62c

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

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

src/spec/ruby/rack/application_spec.rb

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ def createRackServletWrapper(runtime, rackup, filename)
558558
describe org.jruby.rack.PoolingRackApplicationFactory do
559559

560560
before :each do
561-
@factory = double "factory"
561+
@factory = double("factory").as_null_object
562562
@pooling_factory = org.jruby.rack.PoolingRackApplicationFactory.new @factory
563563
@pooling_factory.context = @rack_context
564564
end
@@ -641,7 +641,7 @@ def createRackServletWrapper(runtime, rackup, filename)
641641
it "forces the maximum size to be greater or equal to the initial size" do
642642
allow(@factory).to receive(:init)
643643
allow(@factory).to receive(:newApplication) do
644-
app = double "app"
644+
app = double("app").as_null_object
645645
expect(app).to receive(:init)
646646
app
647647
end
@@ -655,15 +655,15 @@ def createRackServletWrapper(runtime, rackup, filename)
655655
end
656656

657657
it "retrieves the error application from the delegate factory" do
658-
app = double("app")
658+
app = double("app").as_null_object
659659
expect(@factory).to receive(:getErrorApplication).and_return app
660660
expect(@pooling_factory.getErrorApplication).to eq app
661661
end
662662

663663
it "waits till initial runtimes get initialized (with wait set to true)" do
664664
allow(@factory).to receive(:init)
665665
allow(@factory).to receive(:newApplication) do
666-
app = double "app"
666+
app = double("app").as_null_object
667667
allow(app).to receive(:init) do
668668
sleep(0.05)
669669
end
@@ -683,7 +683,7 @@ def createRackServletWrapper(runtime, rackup, filename)
683683
allow(@factory).to receive(:init)
684684
app_count = java.util.concurrent.atomic.AtomicInteger.new(0)
685685
allow(@factory).to receive(:newApplication) do
686-
app = double "app"
686+
app = double("app").as_null_object
687687
allow(app).to receive(:init) do
688688
if app_count.addAndGet(1) == 2
689689
raise org.jruby.rack.RackInitializationException.new('failed app init')
@@ -799,33 +799,37 @@ def createRackServletWrapper(runtime, rackup, filename)
799799
end
800800

801801
it "initializes initial runtimes in parallel (with wait set to false)" do
802+
app_init_secs = 0.15
802803
allow(@factory).to receive(:init)
803804
allow(@factory).to receive(:newApplication) do
804805
app = double "app"
805-
allow(app).to receive(:init) do
806-
sleep(0.15)
807-
end
806+
allow(app).to receive(:init) { sleep(app_init_secs) }
808807
app
809808
end
809+
810+
init_threads = 4
811+
init_runtimes = 6
810812
allow(@rack_config).to receive(:getBooleanProperty).with("jruby.runtime.init.wait").and_return false
811-
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
812815
allow(@rack_config).to receive(:getMaximumRuntimes).and_return 8
813816

817+
expected_initial_init_time = 1.1 * (init_runtimes.to_f / init_threads.to_f).ceil * app_init_secs # 10% margin
814818
@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
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
819823

820824
expect(@pooling_factory.getManagedApplications).to_not be_empty
821-
expect(@pooling_factory.getManagedApplications.size).to eql 6
825+
expect(@pooling_factory.getManagedApplications.size).to eql init_runtimes
822826
end
823827

824828
it "throws from init when application initialization in thread failed" do
825829
app_init_secs = 0.05
826830
allow(@factory).to receive(:init)
827831
allow(@factory).to receive(:newApplication) do
828-
app = double "app"
832+
app = double("app").as_null_object
829833
allow(app).to receive(:init) { sleep(app_init_secs); raise "app.init raising" }
830834
app
831835
end

0 commit comments

Comments
 (0)