Skip to content
This repository

[FEATURE] Pull in any Java or JRuby environment variables that are already set #102

Merged
merged 5 commits into from over 1 year ago

4 participants

Taylor Carpenter Matt Wynne Ben Langfeld Myron Marston
Taylor Carpenter

Aruba kills java and jruby environment variables currently.

I think the environment variables should be set in everyones features/env.rb file rather than in the lib, but this patch makes it so at least they are merged.

I did not try to handle conflicts.

Matt Wynne
Owner

I think the environment variables should be set in everyones features/env.rb file rather than in the lib

Are you saying you think the auto-require of 'aruba/jruby' is too helpful? I don't use this code myself, so I'd be happy to change it to whatever you think is best.

Matt Wynne
Owner

Ping @myronmarston, you may be interested in this thread.

Taylor Carpenter

@mattwynne yes, I find it too helpful. I think that should be documented in the wiki as recommended settings. The end-user/developer can then add that to env.rb.

If it is going to be auto-required then it needs this patch or the user loses any environment settings they have.

Ben Langfeld

My thoughts on the ideal approach:

1) Document these particular flags as useful, probably in the README for maximum visibility.
2) Allow aruba/jruby to be required in env.rb, but do not auto-require it.
3) Merge this patch so that if aruba/jruby is required, it does not kill other environment vars.
4) Document the fact that aruba/jruby does not handle conflicting flags and these should be set manually if anything more complex is required.

Matt Wynne
Owner

That sounds sensible to me @benlangfeld. The only other thing I'd like to see is a spec/aruba/jruby_spec.rb that pins this behaviour down.

Would anyone like to update this pull request with the changes to the README etc as Ben has suggested? I'll happily merge it if you do.

Ben Langfeld

@taylor Please go ahead and complete 1,2&4. Please also add test coverage if it's not significant work.

Matt Wynne
Owner

What are you, his boss? ;)

Ben Langfeld

PM :)

Myron Marston

I'm cool with this approach as well.

added some commits January 04, 2012
Taylor Carpenter [UPDATE] JRuby auto-require removed; Added tips for JRuby
  * Tips for require or manual env.rb setup added to README
  * Updated jruby.rb to only load config for Java
0ebaf3f
Taylor Carpenter Merge branch 'master' of git://github.com/cucumber/aruba 15174f6
Taylor Carpenter [UPDATE] added spec for testing jruby configuration
  * Also added some helper methods for setting and re-setting global constants
d4dca41
Taylor Carpenter

The first helper method is for setting constants like RUBY_PLATFORM and ENV to something useful during a test and then setting them back to their previous values after the block.

The second helper is used to silence warnings about changing the constant. It can be used to silence other warnings if desired though.

Ben Langfeld

Thanks Taylor, looks good.

@mattwynne All yours :)

Ben Langfeld

Any chance this might be merged?

Ben Langfeld

@mattwynne Anything we need to do before this can be merged?

Matt Wynne
Owner

Can you move that with_constants method into a module please and either include it from the spec? Adding global methods like this is a bad idea.

Matt Wynne
Owner

Sorry I went to sleep on that. Thanks for your contributions guys.

I've added one comment on the specs, otherwise I'd be happy to merge it in.

Ben Langfeld

@taylor Can I have write access to your fork, or would you mind making this change?

Taylor Carpenter
Matt Wynne
Owner

Thanks guys, I'd do it myself but I'm craaazy busy.

Ben Langfeld
Matt Wynne mattwynne merged commit 4b70f78 into from October 09, 2012
Matt Wynne mattwynne closed this October 09, 2012
Matt Wynne
Owner

Finally!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 5 unique commits by 1 author.

Dec 31, 2011
Taylor Carpenter [FEATURE] Pull in any Java or JRuby environment variables that are al…
…ready set
7944596
Jan 04, 2012
Taylor Carpenter [UPDATE] JRuby auto-require removed; Added tips for JRuby
  * Tips for require or manual env.rb setup added to README
  * Updated jruby.rb to only load config for Java
0ebaf3f
Taylor Carpenter Merge branch 'master' of git://github.com/cucumber/aruba 15174f6
Taylor Carpenter [UPDATE] added spec for testing jruby configuration
  * Also added some helper methods for setting and re-setting global constants
d4dca41
Taylor Carpenter [UPDATE] tip for process timeout just for java 4b70f78
This page is out of date. Refresh to see the latest.
31  README.md
Source Rendered
@@ -98,6 +98,37 @@ make assertions about coloured output. Still, there might be cases where you wan
98 98
 scenario with `@ansi`. Alternatively you can add your own Before
99 99
 hook that sets `@aruba_keep_ansi = true`.
100 100
 
  101
+### JRuby Tips
  102
+
  103
+Improve startup time by disabling JIT and forcing client JVM mode.  This can be accomplished by adding
  104
+
  105
+    require 'aruba/java'
  106
+
  107
+or setting a hook like this example:
  108
+
  109
+```
  110
+    Aruba.configure do |config|
  111
+      config.before_cmd do |cmd|
  112
+        set_env('JRUBY_OPTS', "-X-C #{ENV['JRUBY_OPTS']}") # disable JIT since these processes are so short lived
  113
+        set_env('JAVA_OPTS', "-d32 #{ENV['JAVA_OPTS']}") # force jRuby to use client JVM for faster startup times
  114
+      end
  115
+    end if RUBY_PLATFORM == 'java'
  116
+```
  117
+
  118
+*Note* - no conflict resolution on the JAVA/JRuby environment options is
  119
+done; only merging.  For more complex settings please manually set the
  120
+environment variables in the hook or externally.
  121
+
  122
+A larger process timeout for java may be needed
  123
+
  124
+```
  125
+    Before do
  126
+      @aruba_timeout_seconds = RUBY_PLATFORM == 'java' ? 60 : 10
  127
+    end
  128
+```
  129
+
  130
+Refer to http://blog.headius.com/2010/03/jruby-startup-time-tips.html for other tips on startup time.
  131
+
101 132
 ## Reporting
102 133
 
103 134
 *Important* - you need [Pygments](http://pygments.org/) installed to use this feature.
1  lib/aruba/api.rb
@@ -3,7 +3,6 @@
3 3
 require 'rspec/expectations'
4 4
 require 'aruba/process'
5 5
 require 'aruba/config'
6  
-require 'aruba/jruby' if RUBY_PLATFORM == 'java'
7 6
 
8 7
 module Aruba
9 8
   module Api
6  lib/aruba/jruby.rb
... ...
@@ -1,7 +1,7 @@
1 1
 Aruba.configure do |config|
2 2
   config.before_cmd do
3 3
     # ideas taken from: http://blog.headius.com/2010/03/jruby-startup-time-tips.html
4  
-    set_env('JRUBY_OPTS', '-X-C') # disable JIT since these processes are so short lived
5  
-    set_env('JAVA_OPTS', '-d32')  # force jRuby to use client JVM for faster startup times
  4
+    set_env('JRUBY_OPTS', "-X-C #{ENV['JRUBY_OPTS']}") # disable JIT since these processes are so short lived
  5
+    set_env('JAVA_OPTS', "-d32 #{ENV['JAVA_OPTS']}") # force jRuby to use client JVM for faster startup times
6 6
   end
7  
-end
  7
+end if RUBY_PLATFORM == 'java'
33  spec/aruba/jruby_spec.rb
... ...
@@ -0,0 +1,33 @@
  1
+require 'spec_helper'
  2
+require 'aruba/config'
  3
+require 'aruba/api'
  4
+include Aruba::Api
  5
+
  6
+describe "Aruba JRuby Startup Helper"  do
  7
+  before(:all) do
  8
+    @fake_env = ENV.clone
  9
+  end
  10
+  before(:each) do
  11
+    Aruba.config = Aruba::Config.new
  12
+    @fake_env['JRUBY_OPTS'] = "--1.9"
  13
+    @fake_env['JAVA_OPTS'] = "-Xdebug"
  14
+  end
  15
+   
  16
+  it 'configuration does not load when RUBY_PLATFORM is not java' do
  17
+    with_constants :ENV => @fake_env, :RUBY_PLATFORM => 'x86_64-chocolate' do
  18
+      load 'aruba/jruby.rb'
  19
+      Aruba.config.hooks.execute :before_cmd, self
  20
+      ENV['JRUBY_OPTS'].should  == "--1.9"
  21
+      ENV['JAVA_OPTS'].should  == "-Xdebug"
  22
+    end
  23
+  end
  24
+
  25
+  it 'configuration loads for java and merges existing environment variables' do
  26
+    with_constants :ENV => @fake_env, :RUBY_PLATFORM => 'java'  do
  27
+      load 'aruba/jruby.rb'
  28
+      Aruba.config.hooks.execute :before_cmd, self
  29
+      ENV['JRUBY_OPTS'].should  == "-X-C --1.9"
  30
+      ENV['JAVA_OPTS'].should  == "-d32 -Xdebug"
  31
+    end 
  32
+  end
  33
+end
32  spec/spec_helper.rb
@@ -6,3 +6,35 @@
6 6
   config.run_all_when_everything_filtered = true
7 7
   config.treat_symbols_as_metadata_keys_with_true_values = true
8 8
 end
  9
+
  10
+# http://digitaldumptruck.jotabout.com/?p=551
  11
+def with_constants(constants, &block)
  12
+  saved_constants = {}
  13
+  constants.each do |constant, val|
  14
+    saved_constants[ constant ] = Object.const_get( constant )
  15
+    Kernel::silence_warnings { Object.const_set( constant, val ) }
  16
+  end
  17
+ 
  18
+  begin
  19
+    block.call
  20
+  ensure
  21
+    constants.each do |constant, val|
  22
+      Kernel::silence_warnings { Object.const_set( constant, saved_constants[ constant ] ) }
  23
+    end
  24
+  end
  25
+end
  26
+
  27
+# http://mislav.uniqpath.com/2011/06/ruby-verbose-mode/
  28
+# these methods are already present in Active Support
  29
+module Kernel
  30
+  def silence_warnings
  31
+    with_warnings(nil) { yield }
  32
+  end
  33
+
  34
+  def with_warnings(flag)
  35
+    old_verbose, $VERBOSE = $VERBOSE, flag
  36
+    yield
  37
+  ensure
  38
+    $VERBOSE = old_verbose
  39
+  end
  40
+end unless Kernel.respond_to? :silence_warnings
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.