1
0
Fork 0
mirror of https://github.com/thoughtbot/capybara-webkit synced 2023-03-27 23:22:28 -04:00

General clean up of Connection

* Use instance variables over private methods with arguments
* Use a constant for the server path
* Reorganize methods
This commit is contained in:
Joe Ferris 2012-07-08 08:39:27 -07:00
parent 784b9b7f63
commit eaef841d45

View file

@ -4,15 +4,14 @@ require 'thread'
class Capybara::Driver::Webkit class Capybara::Driver::Webkit
class Connection class Connection
SERVER_PATH = File.expand_path("../../../../../bin/webkit_server", __FILE__)
WEBKIT_SERVER_START_TIMEOUT = 15 WEBKIT_SERVER_START_TIMEOUT = 15
attr_reader :port attr_reader :port
def initialize(options = {}) def initialize(options = {})
@socket_class = options[:socket_class] || TCPSocket @socket_class = options[:socket_class] || TCPSocket
@stdout = options.has_key?(:stdout) ? @stdout = options.has_key?(:stdout) ? options[:stdout] : $stdout
options[:stdout] :
$stdout
start_server start_server
connect connect
end end
@ -36,52 +35,50 @@ class Capybara::Driver::Webkit
private private
def start_server def start_server
pipe = fork_server open_pipe
@port = discover_port(pipe) discover_port
@stdout_thread = Thread.new do forward_stdout_in_background_thread
Thread.current.abort_on_exception = true
forward_stdout(pipe)
end
end end
def fork_server def open_pipe
server_path = File.expand_path("../../../../../bin/webkit_server", __FILE__) @pipe = IO.popen(SERVER_PATH)
pipe, @pid = server_pipe_and_pid(server_path) @pid = @pipe.pid
register_shutdown_hook register_shutdown_hook
pipe
end
def kill_process(pid)
if RUBY_PLATFORM =~ /mingw32/
Process.kill(9, pid)
else
Process.kill("INT", pid)
end
end end
def register_shutdown_hook def register_shutdown_hook
@owner_pid = Process.pid @owner_pid = Process.pid
at_exit do at_exit do
if Process.pid == @owner_pid if Process.pid == @owner_pid
kill_process(@pid) kill_process
end end
end end
end end
def server_pipe_and_pid(server_path) def kill_process
cmdline = [server_path] if RUBY_PLATFORM =~ /mingw32/
pipe = IO.popen(cmdline.join(" ")) Process.kill(9, @pid)
[pipe, pipe.pid] else
Process.kill("INT", @pid)
end
end end
def discover_port(read_pipe) def discover_port
return unless IO.select([read_pipe], nil, nil, WEBKIT_SERVER_START_TIMEOUT) if IO.select([@pipe], nil, nil, WEBKIT_SERVER_START_TIMEOUT)
((read_pipe.first || '').match(/listening on port: (\d+)/) || [])[1].to_i @port = ((@pipe.first || '').match(/listening on port: (\d+)/) || [])[1].to_i
end
end end
def forward_stdout(pipe) def forward_stdout_in_background_thread
while pipe_readable?(pipe) @stdout_thread = Thread.new do
line = pipe.readline Thread.current.abort_on_exception = true
forward_stdout
end
end
def forward_stdout
while pipe_readable?
line = @pipe.readline
if @stdout if @stdout
@stdout.write(line) @stdout.write(line)
@stdout.flush @stdout.flush
@ -90,6 +87,20 @@ class Capybara::Driver::Webkit
rescue EOFError rescue EOFError
end end
if !defined?(RUBY_ENGINE) || (RUBY_ENGINE == "ruby" && RUBY_VERSION <= "1.8")
# please note the use of IO::select() here, as it is used specifically to
# preserve correct signal handling behavior in ruby 1.8.
# https://github.com/thibaudgg/rb-fsevent/commit/d1a868bf8dc72dbca102bedbadff76c7e6c2dc21
# https://github.com/thibaudgg/rb-fsevent/blob/1ca42b987596f350ee7b19d8f8210b7b6ae8766b/ext/fsevent/fsevent_watch.c#L171
def pipe_readable?
IO.select([@pipe])
end
else
def pipe_readable?
!@pipe.eof?
end
end
def connect def connect
Timeout.timeout(5) do Timeout.timeout(5) do
while @socket.nil? while @socket.nil?
@ -102,19 +113,5 @@ class Capybara::Driver::Webkit
@socket = @socket_class.open("127.0.0.1", @port) @socket = @socket_class.open("127.0.0.1", @port)
rescue Errno::ECONNREFUSED rescue Errno::ECONNREFUSED
end end
if !defined?(RUBY_ENGINE) || (RUBY_ENGINE == "ruby" && RUBY_VERSION <= "1.8")
# please note the use of IO::select() here, as it is used specifically to
# preserve correct signal handling behavior in ruby 1.8.
# https://github.com/thibaudgg/rb-fsevent/commit/d1a868bf8dc72dbca102bedbadff76c7e6c2dc21
# https://github.com/thibaudgg/rb-fsevent/blob/1ca42b987596f350ee7b19d8f8210b7b6ae8766b/ext/fsevent/fsevent_watch.c#L171
def pipe_readable?(pipe)
IO.select([pipe])
end
else
def pipe_readable?(pipe)
!pipe.eof?
end
end
end end
end end