Skip to content

Commit

Permalink
Fix running with JRuby
Browse files Browse the repository at this point in the history
  • Loading branch information
twalpole committed Aug 15, 2019
1 parent ad1a0d1 commit 9043f02
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 40 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ rvm:
- 2.6
- 2.5
- 2.4
- jruby-9.2.8.0
gemfile:
- Gemfile
env:
Expand All @@ -25,8 +26,10 @@ matrix:
- gemfile: gemfiles/Gemfile.base
rvm: 2.4
- gemfile: gemfiles/Gemfile.edge
rvm: 2.6
allow_failures:
- gemfile: gemfiles/Gemfile.edge
- rvm: jruby-9.2.8.0

before_install:
- gem update --system
Expand Down
10 changes: 8 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,11 @@ source 'https://rubygems.org'

gemspec

gem 'capybara', github: 'teamcapybara/capybara'
gem 'puma'
gem 'capybara'

if RUBY_ENGINE == 'jruby'
# nio4r <= 2.4.0 used by puma 4.x has a bug with JRuby - use puma 3.x for now
gem 'puma', '~>3.0'
else
gem 'puma'
end
2 changes: 1 addition & 1 deletion apparition.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Gem::Specification.new do |s|
s.add_runtime_dependency 'capybara', '~> 3.13', '< 4'
s.add_runtime_dependency 'websocket-driver', '>=0.6.5'

s.add_development_dependency 'byebug'
# s.add_development_dependency 'byebug'
s.add_development_dependency 'chunky_png'
s.add_development_dependency 'fastimage'
s.add_development_dependency 'irb'
Expand Down
9 changes: 8 additions & 1 deletion gemfiles/Gemfile.edge
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,11 @@ source 'https://rubygems.org'
gemspec :path => '..'

gem 'capybara', github: 'teamcapybara/capybara'
gem 'puma'

if RUBY_ENGINE == 'jruby'
# nio4r <= 2.4.0 used by puma 4.x has a bug with JRuby - use puma 3.x for now
gem 'puma', '~>3.0'
else
gem 'puma'
end

54 changes: 19 additions & 35 deletions lib/capybara/apparition/driver/launcher.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require 'open3'

module Capybara::Apparition
class Browser
class Launcher
Expand Down Expand Up @@ -44,23 +46,23 @@ def initialize(headless:, **options)

def start
@output = Queue.new
@read_io, @write_io = IO.pipe

process_options = {}
process_options[:pgroup] = true unless Capybara::Apparition.windows?
cmd = [path] + @options.map { |k, v| v.nil? ? "--#{k}" : "--#{k}=#{v}" }

stdin, @stdout_stderr, wait_thr = Open3.popen2e(*cmd, process_options)
stdin.close

@pid = wait_thr.pid

@out_thread = Thread.new do
while !@read_io.eof? && (data = @read_io.readpartial(512))
while !@stdout_stderr.eof? && (data = @stdout_stderr.readpartial(512))
@output << data
end
end

process_options = { in: File::NULL }
process_options[:pgroup] = true unless Capybara::Apparition.windows?
process_options[:out] = process_options[:err] = @write_io if Capybara::Apparition.mri?

redirect_stdout do
cmd = [path] + @options.map { |k, v| v.nil? ? "--#{k}" : "--#{k}=#{v}" }
@pid = ::Process.spawn(*cmd, process_options)
ObjectSpace.define_finalizer(self, self.class.process_killer(@pid))
end
ObjectSpace.define_finalizer(self, self.class.process_killer(@pid))
end

def stop
Expand All @@ -87,47 +89,29 @@ def ws_url
@ws_url ||= begin
regexp = %r{DevTools listening on (ws://.*)}
url = nil

sleep 3
loop do
break if (url = @output.pop.scan(regexp)[0])
end
@out_thread.kill
@out_thread.join # wait for thread to end before closing io
close_io
Addressable::URI.parse(url[0])
end
end

private

def redirect_stdout
if Capybara::Apparition.mri?
yield
else
begin
prev = STDOUT.dup
$stdout = @write_io
STDOUT.reopen(@write_io)
yield
ensure
STDOUT.reopen(prev)
$stdout = STDOUT
prev.close
end
end
end

def kill
self.class.process_killer(@pid).call
@pid = nil
end

def close_io
[@write_io, @read_io].each do |io|
begin
io.close unless io.closed?
rescue IOError
raise unless RUBY_ENGINE == 'jruby'
end
end
@stdout_stderr.close unless @stdout_stderr.closed?
rescue IOError
raise unless RUBY_ENGINE == 'jruby'
end

def path
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
$LOAD_PATH.unshift(APPARITION_ROOT + '/lib')

require 'bundler/setup'
require 'byebug'
# require 'byebug'
require 'rspec'
require 'capybara/spec/spec_helper'
require 'capybara/apparition'
Expand Down

0 comments on commit 9043f02

Please sign in to comment.