@@ -534,7 +534,7 @@ def reset_config
534534
535535describe org . jruby . rack . rails . RailsRackApplicationFactory do
536536
537- java_import org . jruby . rack . rails . RailsRackApplicationFactory
537+ require ' jruby/ rack/rails_booter'
538538
539539 before :each do
540540 @app_factory = RailsRackApplicationFactory . new
@@ -683,7 +683,7 @@ def createRackServletWrapper(runtime, rackup, filename)
683683 allow ( @factory ) . to receive ( :newApplication ) do
684684 app = double "app"
685685 allow ( app ) . to receive ( :init ) do
686- sleep ( 0.10 )
686+ sleep ( 0.05 )
687687 end
688688 app
689689 end
@@ -697,6 +697,7 @@ def createRackServletWrapper(runtime, rackup, filename)
697697
698698 it "throws an exception from getApplication when an app failed to initialize " +
699699 "(even when only a single application initialization fails)" do
700+ app_init_secs = 0.05
700701 allow ( @factory ) . to receive ( :init )
701702 app_count = java . util . concurrent . atomic . AtomicInteger . new ( 0 )
702703 allow ( @factory ) . to receive ( :newApplication ) do
@@ -705,7 +706,7 @@ def createRackServletWrapper(runtime, rackup, filename)
705706 if app_count . addAndGet ( 1 ) == 2
706707 raise org . jruby . rack . RackInitializationException . new ( 'failed app init' )
707708 end
708- sleep ( 0.05 )
709+ sleep ( app_init_secs )
709710 end
710711 app
711712 end
@@ -719,7 +720,7 @@ def createRackServletWrapper(runtime, rackup, filename)
719720 rescue org . jruby . rack . RackInitializationException
720721 # ignore - sometimes initialization happens fast enough that the init error is thrown already
721722 end
722- sleep ( 0.20 )
723+ sleep ( num_runtimes * app_init_secs + 0.07 ) # sleep with a buffer
723724
724725 failed = 0
725726 num_runtimes . times do
@@ -735,28 +736,29 @@ def createRackServletWrapper(runtime, rackup, filename)
735736 end
736737
737738 it "wait until pool is filled when invoking getApplication (with wait set to false)" do
739+ app_init_secs = 0.2
738740 allow ( @factory ) . to receive ( :init )
739741 allow ( @factory ) . to receive ( :newApplication ) do
740742 app = double "app"
741- allow ( app ) . to receive ( :init ) { sleep ( 0.2 ) }
743+ allow ( app ) . to receive ( :init ) { sleep ( app_init_secs ) }
742744 app
743745 end
744746 allow ( @rack_config ) . to receive ( :getBooleanProperty ) . with ( "jruby.runtime.init.wait" ) . and_return false
745747 expect ( @rack_config ) . to receive ( :getInitialRuntimes ) . and_return 3
746748 expect ( @rack_config ) . to receive ( :getMaximumRuntimes ) . and_return 4
747749
748750 @pooling_factory . init ( @rack_context )
749- millis = java . lang . System . currentTimeMillis
751+ start = java . lang . System . currentTimeMillis
750752 expect ( @pooling_factory . getApplication ) . not_to be nil
751- millis = java . lang . System . currentTimeMillis - millis
752- expect ( millis ) . to be >= 150 # getApplication waited ~ 0.2 secs
753+ expect ( java . lang . System . currentTimeMillis - start ) . to be_within ( 70 ) . of ( app_init_secs * 1000 ) # getApplication waited ~ sleep time
753754 end
754755
755756 it "waits acquire timeout till an application is available from the pool (than raises)" do
757+ app_init_secs = 0.2
756758 allow ( @factory ) . to receive ( :init )
757759 expect ( @factory ) . to receive ( :newApplication ) . twice do
758760 app = double "app"
759- expect ( app ) . to receive ( :init ) { sleep ( 0.2 ) }
761+ expect ( app ) . to receive ( :init ) { sleep ( app_init_secs ) }
760762 app
761763 end
762764 allow ( @rack_config ) . to receive ( :getBooleanProperty ) . with ( "jruby.runtime.init.wait" ) . and_return false
@@ -765,57 +767,56 @@ def createRackServletWrapper(runtime, rackup, filename)
765767
766768 @pooling_factory . init ( @rack_context )
767769 @pooling_factory . acquire_timeout = 1 . to_java # second
768- millis = java . lang . System . currentTimeMillis
770+ start = java . lang . System . currentTimeMillis
769771 expect ( @pooling_factory . getApplication ) . not_to be nil
770- millis = java . lang . System . currentTimeMillis - millis
771- expect ( millis ) . to be >= 150 # getApplication waited ~ 0.2 secs
772+ expect ( java . lang . System . currentTimeMillis - start ) . to be_within ( 70 ) . of ( app_init_secs * 1000 )
772773
773774 app2 = @pooling_factory . getApplication # now the pool is empty
774-
775- @pooling_factory . acquire_timeout = 0.1 . to_java # second
776- millis = java . lang . System . currentTimeMillis
775+ timeout_secs = 0.1
776+ @pooling_factory . acquire_timeout = ( timeout_secs ) . to_java
777+ start = java . lang . System . currentTimeMillis
777778 expect { @pooling_factory . getApplication } . to raise_error ( org . jruby . rack . AcquireTimeoutException )
778- millis = java . lang . System . currentTimeMillis - millis
779- expect ( millis ) . to be >= 90 # waited about ~ 0.1 secs
779+ expect ( java . lang . System . currentTimeMillis - start ) . to be_within ( 20 ) . of ( timeout_secs * 1000 )
780780
781781 @pooling_factory . finishedWithApplication ( app2 ) # gets back to the pool
782782 expect ( @pooling_factory . getApplication ) . to eq app2
783783 end
784784
785785 it "gets and initializes new applications until maximum allows to create more" do
786+ app_init_secs = 0.1
786787 allow ( @factory ) . to receive ( :init )
787788 expect ( @factory ) . to receive ( :newApplication ) . twice do
788789 app = double "app (new)"
789- expect ( app ) . to receive ( :init ) { sleep ( 0.1 ) }
790+ expect ( app ) . to receive ( :init ) { sleep ( app_init_secs ) }
790791 app
791792 end
792793 allow ( @rack_config ) . to receive ( :getBooleanProperty ) . with ( "jruby.runtime.init.wait" ) . and_return false
793794 allow ( @rack_config ) . to receive ( :getInitialRuntimes ) . and_return 2
794795 allow ( @rack_config ) . to receive ( :getMaximumRuntimes ) . and_return 4
795796
797+ timeout_secs = 0.1
796798 @pooling_factory . init ( @rack_context )
797- @pooling_factory . acquire_timeout = 0.10 . to_java # second
799+ @pooling_factory . acquire_timeout = ( timeout_secs ) . to_java # second
798800
799801 2 . times { expect ( @pooling_factory . getApplication ) . not_to be nil }
800802
803+ app_get_secs = 0.15
801804 expect ( @factory ) . to receive ( :getApplication ) . twice do
802- app = double "app (get)" ; sleep ( 0.15 ) ; app
805+ app = double "app (get)" ; sleep ( app_get_secs ) ; app
803806 end
804807
805- millis = java . lang . System . currentTimeMillis
808+ start = java . lang . System . currentTimeMillis
806809 2 . times { expect ( @pooling_factory . getApplication ) . not_to be nil }
807- millis = java . lang . System . currentTimeMillis - millis
808- expect ( millis ) . to be >= 300 # waited about 2 x 0.15 secs
810+ expect ( java . lang . System . currentTimeMillis - start ) . to be_within ( 70 ) . of ( 2 * app_get_secs * 1000 )
809811
810- millis = java . lang . System . currentTimeMillis
812+ start = java . lang . System . currentTimeMillis
811813 expect {
812814 @pooling_factory . getApplication
813815 } . to raise_error ( org . jruby . rack . AcquireTimeoutException )
814- millis = java . lang . System . currentTimeMillis - millis
815- expect ( millis ) . to be >= 90 # waited about ~ 0.10 secs
816+ expect ( java . lang . System . currentTimeMillis - start ) . to be_within ( 20 ) . of ( timeout_secs * 1000 )
816817 end
817818
818- it "initializes initial runtimes in paralel (with wait set to false)" do
819+ it "initializes initial runtimes in parallel (with wait set to false)" do
819820 allow ( @factory ) . to receive ( :init )
820821 allow ( @factory ) . to receive ( :newApplication ) do
821822 app = double "app"
@@ -839,12 +840,11 @@ def createRackServletWrapper(runtime, rackup, filename)
839840 end
840841
841842 it "throws from init when application initialization in thread failed" do
843+ app_init_secs = 0.05
842844 allow ( @factory ) . to receive ( :init )
843845 allow ( @factory ) . to receive ( :newApplication ) do
844846 app = double "app"
845- allow ( app ) . to receive ( :init ) do
846- sleep ( 0.05 ) ; raise "app.init raising"
847- end
847+ allow ( app ) . to receive ( :init ) { sleep ( app_init_secs ) ; raise "app.init raising" }
848848 app
849849 end
850850 allow ( @rack_config ) . to receive ( :getInitialRuntimes ) . and_return 2
@@ -863,9 +863,6 @@ def createRackServletWrapper(runtime, rackup, filename)
863863
864864 expect { @pooling_factory . init ( @rack_context ) } . to raise_error org . jruby . rack . RackInitializationException
865865 expect ( raise_error_logged ) . to eql 1 # logs same init exception once
866-
867- # NOTE: seems it's not such a good idea to return empty on init error
868- # expect(@pooling_factory.getManagedApplications).to be_empty
869866 end
870867
871868end
0 commit comments