Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix JRuby before :command helper hook #612

Merged
merged 1 commit into from
May 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion features/08_other/improve_performance_if_using_jruby.feature
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,34 @@ Feature: Support for JRuby
be accomplished by adding

```ruby
require 'aruba/jruby'
require 'aruba/config/jruby'
```

*Note* - no conflict resolution on the JAVA/JRuby environment options is
done; only merging. For more complex settings please manually set the
environment variables in the hook or externally.

Refer to http://blog.headius.com/2010/03/jruby-startup-time-tips.html for other tips on startup time.

Background:
Given I use a fixture named "cli-app"

@requires-ruby-platform-java
Scenario:
Given a file named "spec/jruby_env_spec.rb" with:
"""
require 'spec_helper'
require 'aruba/config/jruby'

RSpec.describe 'running commands with before :command hook', :type => :aruba do
it 'sets up for efficient JRuby startup' do
with_environment 'JRUBY_OPTS' => '-d' do
run_command_and_stop('env')

expect(last_command_started.output).to include 'JRUBY_OPTS=--dev -X-C -d'
end
end
end
"""
When I run `rspec`
Then the specs should all pass
2 changes: 0 additions & 2 deletions lib/aruba/api/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
require 'aruba/errors'
require 'aruba/setup'

require 'aruba/config/jruby'

# Aruba
module Aruba
# Api
Expand Down
12 changes: 7 additions & 5 deletions lib/aruba/config/jruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,26 @@

# ideas taken from: http://blog.headius.com/2010/03/jruby-startup-time-tips.html
Aruba.configure do |config|
config.before :command do
config.before :command do |command|
next unless RUBY_PLATFORM == 'java'

jruby_opts = aruba.environment['JRUBY_OPTS'] || ''
env = command.environment

jruby_opts = env['JRUBY_OPTS'] || ''

# disable JIT since these processes are so short lived
jruby_opts = "-X-C #{jruby_opts}" unless jruby_opts.include? '-X-C'

# Faster startup for jruby
jruby_opts = "--dev #{jruby_opts}" unless jruby_opts.include? '--dev'

set_environment_variable('JRUBY_OPTS', jruby_opts)
env['JRUBY_OPTS'] = jruby_opts

if RbConfig::CONFIG['host_os'] =~ /solaris|sunos/i
java_opts = aruba.environment['JAVA_OPTS'] || ''
java_opts = env['JAVA_OPTS'] || ''

# force jRuby to use client JVM for faster startup times
set_environment_variable('JAVA_OPTS', "-d32 #{java_opts}") unless java_opts.include?('-d32')
env['JAVA_OPTS'] = "-d32 #{java_opts}" unless java_opts.include?('-d32')
end
end
end
37 changes: 17 additions & 20 deletions spec/aruba/jruby_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@

describe "Aruba JRuby Startup Helper" do
include Aruba::Api
include_context 'uses aruba API'

let(:rb_config) { double('RbConfig::CONFIG') }
let(:command) do
instance_double(Aruba::Processes::BasicProcess, :environment => command_environment)
end
let(:command_environment) { { 'JRUBY_OPTS' => '--1.9', 'JAVA_OPTS' => '-Xdebug' } }

before do
Aruba.config.reset
Expand All @@ -14,24 +17,18 @@
load 'aruba/config/jruby.rb'
end

around do |example|
with_environment('JRUBY_OPTS' => '--1.9', 'JAVA_OPTS' => '-Xdebug') do
example.run
end
end

context 'when running under some MRI Ruby' do
before do
stub_const('RUBY_PLATFORM', 'x86_64-chocolate')
end

it 'keeps the existing JRuby and Java option values' do
it 'keeps the existing JRUBY_OPTS and JAVA_OPTS environment values' do
# Run defined before :command hook
Aruba.config.before :command, self
Aruba.config.before :command, self, command

with_environment do
expect(ENV['JRUBY_OPTS']).to eq '--1.9'
expect(ENV['JAVA_OPTS']).to eq '-Xdebug'
aggregate_failures do
expect(command_environment['JRUBY_OPTS']).to eq '--1.9'
expect(command_environment['JAVA_OPTS']).to eq '-Xdebug'
end
end
end
Expand All @@ -46,11 +43,11 @@

it 'updates the existing JRuby but not Java option values' do
# Run defined before :command hook
Aruba.config.before :command, self
Aruba.config.before :command, self, command

with_environment do
expect(ENV['JRUBY_OPTS']).to eq '--dev -X-C --1.9'
expect(ENV['JAVA_OPTS']).to eq '-Xdebug'
aggregate_failures do
expect(command_environment['JRUBY_OPTS']).to eq '--dev -X-C --1.9'
expect(command_environment['JAVA_OPTS']).to eq '-Xdebug'
end
end
end
Expand All @@ -65,11 +62,11 @@

it 'keeps the existing JRuby and Java option values' do
# Run defined before :command hook
Aruba.config.before :command, self
Aruba.config.before :command, self, command

with_environment do
expect(ENV['JRUBY_OPTS']).to eq '--dev -X-C --1.9'
expect(ENV['JAVA_OPTS']).to eq '-d32 -Xdebug'
aggregate_failures do
expect(command_environment['JRUBY_OPTS']).to eq '--dev -X-C --1.9'
expect(command_environment['JAVA_OPTS']).to eq '-d32 -Xdebug'
end
end
end
Expand Down