Skip to content

Commit 6d2aa18

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

File tree

1 file changed

+28
-21
lines changed

1 file changed

+28
-21
lines changed

src/spec/ruby/rack/application_spec.rb

Lines changed: 28 additions & 21 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')
@@ -721,7 +721,7 @@ def createRackServletWrapper(runtime, rackup, filename)
721721
app_init_millis = 200
722722
allow(@factory).to receive(:init)
723723
allow(@factory).to receive(:newApplication) do
724-
app = double "app"
724+
app = double("app").as_null_object
725725
allow(app).to receive(:init) { sleep(app_init_millis.to_f / 1000) }
726726
app
727727
end
@@ -739,7 +739,7 @@ def createRackServletWrapper(runtime, rackup, filename)
739739
app_init_millis = 200
740740
allow(@factory).to receive(:init)
741741
expect(@factory).to receive(:newApplication).twice do
742-
app = double "app"
742+
app = double("app").as_null_object
743743
expect(app).to receive(:init) { sleep(app_init_millis.to_f / 1000) }
744744
app
745745
end
@@ -768,7 +768,7 @@ def createRackServletWrapper(runtime, rackup, filename)
768768
app_init_millis = 100
769769
allow(@factory).to receive(:init)
770770
expect(@factory).to receive(:newApplication).twice do
771-
app = double "app (new)"
771+
app = double("app (new)").as_null_object
772772
expect(app).to receive(:init) { sleep(app_init_millis.to_f / 1000) }
773773
app
774774
end
@@ -784,7 +784,7 @@ def createRackServletWrapper(runtime, rackup, filename)
784784

785785
app_get_millis = 150
786786
expect(@factory).to receive(:getApplication).twice do
787-
app = double "app (get)"; sleep(app_get_millis.to_f / 1000); app
787+
app = double("app (get)").as_null_object; sleep(app_get_millis.to_f / 1000); app
788788
end
789789

790790
start = java.lang.System.currentTimeMillis
@@ -798,34 +798,41 @@ 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+
@pooling_factory = org.jruby.rack.PoolingRackApplicationFactory.new @factory
803+
@pooling_factory.context = @rack_context
804+
805+
app_init_secs = 0.15
802806
allow(@factory).to receive(:init)
803807
allow(@factory).to receive(:newApplication) do
804-
app = double "app"
805-
allow(app).to receive(:init) do
806-
sleep(0.15)
807-
end
808+
app = double("app").as_null_object
809+
allow(app).to receive(:init) { sleep(app_init_secs) }
808810
app
809811
end
812+
813+
init_threads = 4
814+
init_runtimes = 6
810815
allow(@rack_config).to receive(:getBooleanProperty).with("jruby.runtime.init.wait").and_return false
811-
allow(@rack_config).to receive(:getInitialRuntimes).and_return 6
816+
allow(@rack_config).to receive(:getRuntimeInitThreads).and_return init_threads
817+
allow(@rack_config).to receive(:getInitialRuntimes).and_return init_runtimes
812818
allow(@rack_config).to receive(:getMaximumRuntimes).and_return 8
813819

820+
expected_initial_init_time = 1.1 * (init_runtimes.to_f / init_threads.to_f).ceil * app_init_secs # 10% margin
814821
@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
822+
sleep(app_init_secs) # wait for at some (but not possibly all) to finish
823+
expect(@pooling_factory.getApplicationPool.size).to be < init_runtimes
824+
sleep(expected_initial_init_time - app_init_secs) # remaining time
825+
expect(@pooling_factory.getApplicationPool.size).to be >= init_runtimes
819826

820827
expect(@pooling_factory.getManagedApplications).to_not be_empty
821-
expect(@pooling_factory.getManagedApplications.size).to eql 6
828+
expect(@pooling_factory.getManagedApplications.size).to eql init_runtimes
822829
end
823830

824831
it "throws from init when application initialization in thread failed" do
825832
app_init_secs = 0.05
826833
allow(@factory).to receive(:init)
827834
allow(@factory).to receive(:newApplication) do
828-
app = double "app"
835+
app = double("app").as_null_object
829836
allow(app).to receive(:init) { sleep(app_init_secs); raise "app.init raising" }
830837
app
831838
end

0 commit comments

Comments
 (0)