@@ -516,7 +516,7 @@ def reset_config
516516
517517describe org . jruby . rack . rails . RailsRackApplicationFactory do
518518
519- java_import org . jruby . rack . rails . RailsRackApplicationFactory
519+ require ' jruby/ rack/rails_booter'
520520
521521 before :each do
522522 @app_factory = RailsRackApplicationFactory . new
@@ -665,7 +665,7 @@ def createRackServletWrapper(runtime, rackup, filename)
665665 allow ( @factory ) . to receive ( :newApplication ) do
666666 app = double "app"
667667 allow ( app ) . to receive ( :init ) do
668- sleep ( 0.10 )
668+ sleep ( 0.05 )
669669 end
670670 app
671671 end
@@ -679,6 +679,7 @@ def createRackServletWrapper(runtime, rackup, filename)
679679
680680 it "throws an exception from getApplication when an app failed to initialize " +
681681 "(even when only a single application initialization fails)" do
682+ app_init_secs = 0.05
682683 allow ( @factory ) . to receive ( :init )
683684 app_count = java . util . concurrent . atomic . AtomicInteger . new ( 0 )
684685 allow ( @factory ) . to receive ( :newApplication ) do
@@ -687,7 +688,7 @@ def createRackServletWrapper(runtime, rackup, filename)
687688 if app_count . addAndGet ( 1 ) == 2
688689 raise org . jruby . rack . RackInitializationException . new ( 'failed app init' )
689690 end
690- sleep ( 0.05 )
691+ sleep ( app_init_secs )
691692 end
692693 app
693694 end
@@ -701,7 +702,7 @@ def createRackServletWrapper(runtime, rackup, filename)
701702 rescue org . jruby . rack . RackInitializationException
702703 # ignore - sometimes initialization happens fast enough that the init error is thrown already
703704 end
704- sleep ( 0.20 )
705+ sleep ( num_runtimes * app_init_secs + 0.07 ) # sleep with a buffer
705706
706707 failed = 0
707708 num_runtimes . times do
@@ -717,28 +718,29 @@ def createRackServletWrapper(runtime, rackup, filename)
717718 end
718719
719720 it "wait until pool is filled when invoking getApplication (with wait set to false)" do
721+ app_init_millis = 200
720722 allow ( @factory ) . to receive ( :init )
721723 allow ( @factory ) . to receive ( :newApplication ) do
722724 app = double "app"
723- allow ( app ) . to receive ( :init ) { sleep ( 0.2 ) }
725+ allow ( app ) . to receive ( :init ) { sleep ( app_init_millis . to_f / 1000 ) }
724726 app
725727 end
726728 allow ( @rack_config ) . to receive ( :getBooleanProperty ) . with ( "jruby.runtime.init.wait" ) . and_return false
727729 expect ( @rack_config ) . to receive ( :getInitialRuntimes ) . and_return 3
728730 expect ( @rack_config ) . to receive ( :getMaximumRuntimes ) . and_return 4
729731
730732 @pooling_factory . init ( @rack_context )
731- millis = java . lang . System . currentTimeMillis
733+ start = java . lang . System . currentTimeMillis
732734 expect ( @pooling_factory . getApplication ) . not_to be nil
733- millis = java . lang . System . currentTimeMillis - millis
734- expect ( millis ) . to be >= 150 # getApplication waited ~ 0.2 secs
735+ expect ( java . lang . System . currentTimeMillis - start ) . to be_within ( 70 ) . of ( app_init_millis ) # getApplication waited ~ sleep time
735736 end
736737
737738 it "waits acquire timeout till an application is available from the pool (than raises)" do
739+ app_init_millis = 200
738740 allow ( @factory ) . to receive ( :init )
739741 expect ( @factory ) . to receive ( :newApplication ) . twice do
740742 app = double "app"
741- expect ( app ) . to receive ( :init ) { sleep ( 0.2 ) }
743+ expect ( app ) . to receive ( :init ) { sleep ( app_init_millis . to_f / 1000 ) }
742744 app
743745 end
744746 allow ( @rack_config ) . to receive ( :getBooleanProperty ) . with ( "jruby.runtime.init.wait" ) . and_return false
@@ -747,54 +749,53 @@ def createRackServletWrapper(runtime, rackup, filename)
747749
748750 @pooling_factory . init ( @rack_context )
749751 @pooling_factory . acquire_timeout = 1 . to_java # second
750- millis = java . lang . System . currentTimeMillis
752+ start = java . lang . System . currentTimeMillis
751753 expect ( @pooling_factory . getApplication ) . not_to be nil
752- millis = java . lang . System . currentTimeMillis - millis
753- expect ( millis ) . to be >= 150 # getApplication waited ~ 0.2 secs
754+ expect ( java . lang . System . currentTimeMillis - start ) . to be_within ( 70 ) . of ( app_init_millis )
754755
755756 app2 = @pooling_factory . getApplication # now the pool is empty
756-
757- @pooling_factory . acquire_timeout = 0.1 . to_java # second
758- millis = java . lang . System . currentTimeMillis
757+ timeout_millis = 100
758+ @pooling_factory . acquire_timeout = ( timeout_millis . to_f / 1000 ) . to_java
759+ start = java . lang . System . currentTimeMillis
759760 expect { @pooling_factory . getApplication } . to raise_error ( org . jruby . rack . AcquireTimeoutException )
760- millis = java . lang . System . currentTimeMillis - millis
761- expect ( millis ) . to be >= 90 # waited about ~ 0.1 secs
761+ expect ( java . lang . System . currentTimeMillis - start ) . to be_within ( 20 ) . of ( timeout_millis )
762762
763763 @pooling_factory . finishedWithApplication ( app2 ) # gets back to the pool
764764 expect ( @pooling_factory . getApplication ) . to eq app2
765765 end
766766
767767 it "gets and initializes new applications until maximum allows to create more" do
768+ app_init_millis = 100
768769 allow ( @factory ) . to receive ( :init )
769770 expect ( @factory ) . to receive ( :newApplication ) . twice do
770771 app = double "app (new)"
771- expect ( app ) . to receive ( :init ) { sleep ( 0.1 ) }
772+ expect ( app ) . to receive ( :init ) { sleep ( app_init_millis . to_f / 1000 ) }
772773 app
773774 end
774775 allow ( @rack_config ) . to receive ( :getBooleanProperty ) . with ( "jruby.runtime.init.wait" ) . and_return false
775776 allow ( @rack_config ) . to receive ( :getInitialRuntimes ) . and_return 2
776777 allow ( @rack_config ) . to receive ( :getMaximumRuntimes ) . and_return 4
777778
779+ timeout_millis = 100
778780 @pooling_factory . init ( @rack_context )
779- @pooling_factory . acquire_timeout = 0.10 . to_java # second
781+ @pooling_factory . acquire_timeout = ( timeout_millis . to_f / 1000 ) . to_java # second
780782
781783 2 . times { expect ( @pooling_factory . getApplication ) . not_to be nil }
782784
785+ app_get_millis = 150
783786 expect ( @factory ) . to receive ( :getApplication ) . twice do
784- app = double "app (get)" ; sleep ( 0.15 ) ; app
787+ app = double "app (get)" ; sleep ( app_get_millis . to_f / 1000 ) ; app
785788 end
786789
787- millis = java . lang . System . currentTimeMillis
790+ start = java . lang . System . currentTimeMillis
788791 2 . times { expect ( @pooling_factory . getApplication ) . not_to be nil }
789- millis = java . lang . System . currentTimeMillis - millis
790- expect ( millis ) . to be >= 300 # waited about 2 x 0.15 secs
792+ expect ( java . lang . System . currentTimeMillis - start ) . to be_within ( 70 ) . of ( 2 * app_get_millis )
791793
792- millis = java . lang . System . currentTimeMillis
794+ start = java . lang . System . currentTimeMillis
793795 expect {
794796 @pooling_factory . getApplication
795797 } . to raise_error ( org . jruby . rack . AcquireTimeoutException )
796- millis = java . lang . System . currentTimeMillis - millis
797- expect ( millis ) . to be >= 90 # waited about ~ 0.10 secs
798+ expect ( java . lang . System . currentTimeMillis - start ) . to be_within ( 20 ) . of ( timeout_millis )
798799 end
799800
800801 it "initializes initial runtimes in paralel (with wait set to false)" do
@@ -821,12 +822,11 @@ def createRackServletWrapper(runtime, rackup, filename)
821822 end
822823
823824 it "throws from init when application initialization in thread failed" do
825+ app_init_secs = 0.05
824826 allow ( @factory ) . to receive ( :init )
825827 allow ( @factory ) . to receive ( :newApplication ) do
826828 app = double "app"
827- allow ( app ) . to receive ( :init ) do
828- sleep ( 0.05 ) ; raise "app.init raising"
829- end
829+ allow ( app ) . to receive ( :init ) { sleep ( app_init_secs ) ; raise "app.init raising" }
830830 app
831831 end
832832 allow ( @rack_config ) . to receive ( :getInitialRuntimes ) . and_return 2
@@ -845,9 +845,6 @@ def createRackServletWrapper(runtime, rackup, filename)
845845
846846 expect { @pooling_factory . init ( @rack_context ) } . to raise_error org . jruby . rack . RackInitializationException
847847 expect ( raise_error_logged ) . to eql 1 # logs same init exception once
848-
849- # NOTE: seems it's not such a good idea to return empty on init error
850- # expect(@pooling_factory.getManagedApplications).to be_empty
851848 end
852849
853850end
0 commit comments