@@ -512,7 +512,7 @@ def reset_config
512512
513513describe org . jruby . rack . rails . RailsRackApplicationFactory do
514514
515- java_import org . jruby . rack . rails . RailsRackApplicationFactory
515+ require ' jruby/ rack/rails_booter'
516516
517517 before :each do
518518 @app_factory = RailsRackApplicationFactory . new
@@ -661,7 +661,7 @@ def createRackServletWrapper(runtime, rackup, filename)
661661 allow ( @factory ) . to receive ( :newApplication ) do
662662 app = double "app"
663663 allow ( app ) . to receive ( :init ) do
664- sleep ( 0.10 )
664+ sleep ( 0.05 )
665665 end
666666 app
667667 end
@@ -675,6 +675,7 @@ def createRackServletWrapper(runtime, rackup, filename)
675675
676676 it "throws an exception from getApplication when an app failed to initialize " +
677677 "(even when only a single application initialization fails)" do
678+ app_init_secs = 0.05
678679 allow ( @factory ) . to receive ( :init )
679680 app_count = java . util . concurrent . atomic . AtomicInteger . new ( 0 )
680681 allow ( @factory ) . to receive ( :newApplication ) do
@@ -683,7 +684,7 @@ def createRackServletWrapper(runtime, rackup, filename)
683684 if app_count . addAndGet ( 1 ) == 2
684685 raise org . jruby . rack . RackInitializationException . new ( 'failed app init' )
685686 end
686- sleep ( 0.05 )
687+ sleep ( app_init_secs )
687688 end
688689 app
689690 end
@@ -697,7 +698,7 @@ def createRackServletWrapper(runtime, rackup, filename)
697698 rescue org . jruby . rack . RackInitializationException
698699 # ignore - sometimes initialization happens fast enough that the init error is thrown already
699700 end
700- sleep ( 0.20 )
701+ sleep ( num_runtimes * app_init_secs + 0.07 ) # sleep with a buffer
701702
702703 failed = 0
703704 num_runtimes . times do
@@ -713,28 +714,29 @@ def createRackServletWrapper(runtime, rackup, filename)
713714 end
714715
715716 it "wait until pool is filled when invoking getApplication (with wait set to false)" do
717+ app_init_millis = 200
716718 allow ( @factory ) . to receive ( :init )
717719 allow ( @factory ) . to receive ( :newApplication ) do
718720 app = double "app"
719- allow ( app ) . to receive ( :init ) { sleep ( 0.2 ) }
721+ allow ( app ) . to receive ( :init ) { sleep ( app_init_millis . to_f / 1000 ) }
720722 app
721723 end
722724 allow ( @rack_config ) . to receive ( :getBooleanProperty ) . with ( "jruby.runtime.init.wait" ) . and_return false
723725 expect ( @rack_config ) . to receive ( :getInitialRuntimes ) . and_return 3
724726 expect ( @rack_config ) . to receive ( :getMaximumRuntimes ) . and_return 4
725727
726728 @pooling_factory . init ( @rack_context )
727- millis = java . lang . System . currentTimeMillis
729+ start = java . lang . System . currentTimeMillis
728730 expect ( @pooling_factory . getApplication ) . not_to be nil
729- millis = java . lang . System . currentTimeMillis - millis
730- expect ( millis ) . to be >= 150 # getApplication waited ~ 0.2 secs
731+ expect ( java . lang . System . currentTimeMillis - start ) . to be_within ( 70 ) . of ( app_init_millis ) # getApplication waited ~ sleep time
731732 end
732733
733734 it "waits acquire timeout till an application is available from the pool (than raises)" do
735+ app_init_millis = 200
734736 allow ( @factory ) . to receive ( :init )
735737 expect ( @factory ) . to receive ( :newApplication ) . twice do
736738 app = double "app"
737- expect ( app ) . to receive ( :init ) { sleep ( 0.2 ) }
739+ expect ( app ) . to receive ( :init ) { sleep ( app_init_millis . to_f / 1000 ) }
738740 app
739741 end
740742 allow ( @rack_config ) . to receive ( :getBooleanProperty ) . with ( "jruby.runtime.init.wait" ) . and_return false
@@ -743,54 +745,53 @@ def createRackServletWrapper(runtime, rackup, filename)
743745
744746 @pooling_factory . init ( @rack_context )
745747 @pooling_factory . acquire_timeout = 1 . to_java # second
746- millis = java . lang . System . currentTimeMillis
748+ start = java . lang . System . currentTimeMillis
747749 expect ( @pooling_factory . getApplication ) . not_to be nil
748- millis = java . lang . System . currentTimeMillis - millis
749- expect ( millis ) . to be >= 150 # getApplication waited ~ 0.2 secs
750+ expect ( java . lang . System . currentTimeMillis - start ) . to be_within ( 70 ) . of ( app_init_millis )
750751
751752 app2 = @pooling_factory . getApplication # now the pool is empty
752-
753- @pooling_factory . acquire_timeout = 0.1 . to_java # second
754- millis = java . lang . System . currentTimeMillis
753+ timeout_millis = 100
754+ @pooling_factory . acquire_timeout = ( timeout_millis . to_f / 1000 ) . to_java
755+ start = java . lang . System . currentTimeMillis
755756 expect { @pooling_factory . getApplication } . to raise_error ( org . jruby . rack . AcquireTimeoutException )
756- millis = java . lang . System . currentTimeMillis - millis
757- expect ( millis ) . to be >= 90 # waited about ~ 0.1 secs
757+ expect ( java . lang . System . currentTimeMillis - start ) . to be_within ( 20 ) . of ( timeout_millis )
758758
759759 @pooling_factory . finishedWithApplication ( app2 ) # gets back to the pool
760760 expect ( @pooling_factory . getApplication ) . to eq app2
761761 end
762762
763763 it "gets and initializes new applications until maximum allows to create more" do
764+ app_init_millis = 100
764765 allow ( @factory ) . to receive ( :init )
765766 expect ( @factory ) . to receive ( :newApplication ) . twice do
766767 app = double "app (new)"
767- expect ( app ) . to receive ( :init ) { sleep ( 0.1 ) }
768+ expect ( app ) . to receive ( :init ) { sleep ( app_init_millis . to_f / 1000 ) }
768769 app
769770 end
770771 allow ( @rack_config ) . to receive ( :getBooleanProperty ) . with ( "jruby.runtime.init.wait" ) . and_return false
771772 allow ( @rack_config ) . to receive ( :getInitialRuntimes ) . and_return 2
772773 allow ( @rack_config ) . to receive ( :getMaximumRuntimes ) . and_return 4
773774
775+ timeout_millis = 100
774776 @pooling_factory . init ( @rack_context )
775- @pooling_factory . acquire_timeout = 0.10 . to_java # second
777+ @pooling_factory . acquire_timeout = ( timeout_millis . to_f / 1000 ) . to_java # second
776778
777779 2 . times { expect ( @pooling_factory . getApplication ) . not_to be nil }
778780
781+ app_get_millis = 150
779782 expect ( @factory ) . to receive ( :getApplication ) . twice do
780- app = double "app (get)" ; sleep ( 0.15 ) ; app
783+ app = double "app (get)" ; sleep ( app_get_millis . to_f / 1000 ) ; app
781784 end
782785
783- millis = java . lang . System . currentTimeMillis
786+ start = java . lang . System . currentTimeMillis
784787 2 . times { expect ( @pooling_factory . getApplication ) . not_to be nil }
785- millis = java . lang . System . currentTimeMillis - millis
786- expect ( millis ) . to be >= 300 # waited about 2 x 0.15 secs
788+ expect ( java . lang . System . currentTimeMillis - start ) . to be_within ( 70 ) . of ( 2 * app_get_millis )
787789
788- millis = java . lang . System . currentTimeMillis
790+ start = java . lang . System . currentTimeMillis
789791 expect {
790792 @pooling_factory . getApplication
791793 } . to raise_error ( org . jruby . rack . AcquireTimeoutException )
792- millis = java . lang . System . currentTimeMillis - millis
793- expect ( millis ) . to be >= 90 # waited about ~ 0.10 secs
794+ expect ( java . lang . System . currentTimeMillis - start ) . to be_within ( 20 ) . of ( timeout_millis )
794795 end
795796
796797 it "initializes initial runtimes in paralel (with wait set to false)" do
@@ -817,12 +818,11 @@ def createRackServletWrapper(runtime, rackup, filename)
817818 end
818819
819820 it "throws from init when application initialization in thread failed" do
821+ app_init_secs = 0.05
820822 allow ( @factory ) . to receive ( :init )
821823 allow ( @factory ) . to receive ( :newApplication ) do
822824 app = double "app"
823- allow ( app ) . to receive ( :init ) do
824- sleep ( 0.05 ) ; raise "app.init raising"
825- end
825+ allow ( app ) . to receive ( :init ) { sleep ( app_init_secs ) ; raise "app.init raising" }
826826 app
827827 end
828828 allow ( @rack_config ) . to receive ( :getInitialRuntimes ) . and_return 2
@@ -841,9 +841,6 @@ def createRackServletWrapper(runtime, rackup, filename)
841841
842842 expect { @pooling_factory . init ( @rack_context ) } . to raise_error org . jruby . rack . RackInitializationException
843843 expect ( raise_error_logged ) . to eql 1 # logs same init exception once
844-
845- # NOTE: seems it's not such a good idea to return empty on init error
846- # expect(@pooling_factory.getManagedApplications).to be_empty
847844 end
848845
849846end
0 commit comments