@@ -513,7 +513,7 @@ def reset_config
513513
514514describe org . jruby . rack . rails . RailsRackApplicationFactory do
515515
516- java_import org . jruby . rack . rails . RailsRackApplicationFactory
516+ require ' jruby/ rack/rails_booter'
517517
518518 before :each do
519519 @app_factory = RailsRackApplicationFactory . new
@@ -662,7 +662,7 @@ def createRackServletWrapper(runtime, rackup, filename)
662662 allow ( @factory ) . to receive ( :newApplication ) do
663663 app = double "app"
664664 allow ( app ) . to receive ( :init ) do
665- sleep ( 0.10 )
665+ sleep ( 0.05 )
666666 end
667667 app
668668 end
@@ -676,6 +676,7 @@ def createRackServletWrapper(runtime, rackup, filename)
676676
677677 it "throws an exception from getApplication when an app failed to initialize " +
678678 "(even when only a single application initialization fails)" do
679+ app_init_secs = 0.05
679680 allow ( @factory ) . to receive ( :init )
680681 app_count = java . util . concurrent . atomic . AtomicInteger . new ( 0 )
681682 allow ( @factory ) . to receive ( :newApplication ) do
@@ -684,7 +685,7 @@ def createRackServletWrapper(runtime, rackup, filename)
684685 if app_count . addAndGet ( 1 ) == 2
685686 raise org . jruby . rack . RackInitializationException . new ( 'failed app init' )
686687 end
687- sleep ( 0.05 )
688+ sleep ( app_init_secs )
688689 end
689690 app
690691 end
@@ -698,7 +699,7 @@ def createRackServletWrapper(runtime, rackup, filename)
698699 rescue org . jruby . rack . RackInitializationException
699700 # ignore - sometimes initialization happens fast enough that the init error is thrown already
700701 end
701- sleep ( 0.20 )
702+ sleep ( num_runtimes * app_init_secs + 0.07 ) # sleep with a buffer
702703
703704 failed = 0
704705 num_runtimes . times do
@@ -714,28 +715,29 @@ def createRackServletWrapper(runtime, rackup, filename)
714715 end
715716
716717 it "wait until pool is filled when invoking getApplication (with wait set to false)" do
718+ app_init_millis = 200
717719 allow ( @factory ) . to receive ( :init )
718720 allow ( @factory ) . to receive ( :newApplication ) do
719721 app = double "app"
720- allow ( app ) . to receive ( :init ) { sleep ( 0.2 ) }
722+ allow ( app ) . to receive ( :init ) { sleep ( app_init_millis . to_f / 1000 ) }
721723 app
722724 end
723725 allow ( @rack_config ) . to receive ( :getBooleanProperty ) . with ( "jruby.runtime.init.wait" ) . and_return false
724726 expect ( @rack_config ) . to receive ( :getInitialRuntimes ) . and_return 3
725727 expect ( @rack_config ) . to receive ( :getMaximumRuntimes ) . and_return 4
726728
727729 @pooling_factory . init ( @rack_context )
728- millis = java . lang . System . currentTimeMillis
730+ start = java . lang . System . currentTimeMillis
729731 expect ( @pooling_factory . getApplication ) . not_to be nil
730- millis = java . lang . System . currentTimeMillis - millis
731- expect ( millis ) . to be >= 150 # getApplication waited ~ 0.2 secs
732+ expect ( java . lang . System . currentTimeMillis - start ) . to be_within ( 70 ) . of ( app_init_millis ) # getApplication waited ~ sleep time
732733 end
733734
734735 it "waits acquire timeout till an application is available from the pool (than raises)" do
736+ app_init_millis = 200
735737 allow ( @factory ) . to receive ( :init )
736738 expect ( @factory ) . to receive ( :newApplication ) . twice do
737739 app = double "app"
738- expect ( app ) . to receive ( :init ) { sleep ( 0.2 ) }
740+ expect ( app ) . to receive ( :init ) { sleep ( app_init_millis . to_f / 1000 ) }
739741 app
740742 end
741743 allow ( @rack_config ) . to receive ( :getBooleanProperty ) . with ( "jruby.runtime.init.wait" ) . and_return false
@@ -744,54 +746,53 @@ def createRackServletWrapper(runtime, rackup, filename)
744746
745747 @pooling_factory . init ( @rack_context )
746748 @pooling_factory . acquire_timeout = 1 . to_java # second
747- millis = java . lang . System . currentTimeMillis
749+ start = java . lang . System . currentTimeMillis
748750 expect ( @pooling_factory . getApplication ) . not_to be nil
749- millis = java . lang . System . currentTimeMillis - millis
750- expect ( millis ) . to be >= 150 # getApplication waited ~ 0.2 secs
751+ expect ( java . lang . System . currentTimeMillis - start ) . to be_within ( 70 ) . of ( app_init_millis )
751752
752753 app2 = @pooling_factory . getApplication # now the pool is empty
753-
754- @pooling_factory . acquire_timeout = 0.1 . to_java # second
755- millis = java . lang . System . currentTimeMillis
754+ timeout_millis = 100
755+ @pooling_factory . acquire_timeout = ( timeout_millis . to_f / 1000 ) . to_java
756+ start = java . lang . System . currentTimeMillis
756757 expect { @pooling_factory . getApplication } . to raise_error ( org . jruby . rack . AcquireTimeoutException )
757- millis = java . lang . System . currentTimeMillis - millis
758- expect ( millis ) . to be >= 90 # waited about ~ 0.1 secs
758+ expect ( java . lang . System . currentTimeMillis - start ) . to be_within ( 20 ) . of ( timeout_millis )
759759
760760 @pooling_factory . finishedWithApplication ( app2 ) # gets back to the pool
761761 expect ( @pooling_factory . getApplication ) . to eq app2
762762 end
763763
764764 it "gets and initializes new applications until maximum allows to create more" do
765+ app_init_millis = 100
765766 allow ( @factory ) . to receive ( :init )
766767 expect ( @factory ) . to receive ( :newApplication ) . twice do
767768 app = double "app (new)"
768- expect ( app ) . to receive ( :init ) { sleep ( 0.1 ) }
769+ expect ( app ) . to receive ( :init ) { sleep ( app_init_millis . to_f / 1000 ) }
769770 app
770771 end
771772 allow ( @rack_config ) . to receive ( :getBooleanProperty ) . with ( "jruby.runtime.init.wait" ) . and_return false
772773 allow ( @rack_config ) . to receive ( :getInitialRuntimes ) . and_return 2
773774 allow ( @rack_config ) . to receive ( :getMaximumRuntimes ) . and_return 4
774775
776+ timeout_millis = 100
775777 @pooling_factory . init ( @rack_context )
776- @pooling_factory . acquire_timeout = 0.10 . to_java # second
778+ @pooling_factory . acquire_timeout = ( timeout_millis . to_f / 1000 ) . to_java # second
777779
778780 2 . times { expect ( @pooling_factory . getApplication ) . not_to be nil }
779781
782+ app_get_millis = 150
780783 expect ( @factory ) . to receive ( :getApplication ) . twice do
781- app = double "app (get)" ; sleep ( 0.15 ) ; app
784+ app = double "app (get)" ; sleep ( app_get_millis . to_f / 1000 ) ; app
782785 end
783786
784- millis = java . lang . System . currentTimeMillis
787+ start = java . lang . System . currentTimeMillis
785788 2 . times { expect ( @pooling_factory . getApplication ) . not_to be nil }
786- millis = java . lang . System . currentTimeMillis - millis
787- expect ( millis ) . to be >= 300 # waited about 2 x 0.15 secs
789+ expect ( java . lang . System . currentTimeMillis - start ) . to be_within ( 70 ) . of ( 2 * app_get_millis )
788790
789- millis = java . lang . System . currentTimeMillis
791+ start = java . lang . System . currentTimeMillis
790792 expect {
791793 @pooling_factory . getApplication
792794 } . to raise_error ( org . jruby . rack . AcquireTimeoutException )
793- millis = java . lang . System . currentTimeMillis - millis
794- expect ( millis ) . to be >= 90 # waited about ~ 0.10 secs
795+ expect ( java . lang . System . currentTimeMillis - start ) . to be_within ( 20 ) . of ( timeout_millis )
795796 end
796797
797798 it "initializes initial runtimes in paralel (with wait set to false)" do
@@ -818,12 +819,11 @@ def createRackServletWrapper(runtime, rackup, filename)
818819 end
819820
820821 it "throws from init when application initialization in thread failed" do
822+ app_init_secs = 0.05
821823 allow ( @factory ) . to receive ( :init )
822824 allow ( @factory ) . to receive ( :newApplication ) do
823825 app = double "app"
824- allow ( app ) . to receive ( :init ) do
825- sleep ( 0.05 ) ; raise "app.init raising"
826- end
826+ allow ( app ) . to receive ( :init ) { sleep ( app_init_secs ) ; raise "app.init raising" }
827827 app
828828 end
829829 allow ( @rack_config ) . to receive ( :getInitialRuntimes ) . and_return 2
@@ -842,9 +842,6 @@ def createRackServletWrapper(runtime, rackup, filename)
842842
843843 expect { @pooling_factory . init ( @rack_context ) } . to raise_error org . jruby . rack . RackInitializationException
844844 expect ( raise_error_logged ) . to eql 1 # logs same init exception once
845-
846- # NOTE: seems it's not such a good idea to return empty on init error
847- # expect(@pooling_factory.getManagedApplications).to be_empty
848845 end
849846
850847end
0 commit comments