From 1ea6a923bcd9d606e257284ba2e4424b88942b1d Mon Sep 17 00:00:00 2001 From: Andrey Botalov Date: Wed, 25 Dec 2013 21:17:34 +0300 Subject: [PATCH] Allow using Browsermob server in parallel --- README.md | 20 ++++++++++++++++++++ lib/browsermob/proxy/server.rb | 20 ++++++++++++-------- spec/e2e/server_spec.rb | 31 +++++++++++++++++++++++++++++++ spec/spec_helper.rb | 30 ++++++++++++++++++++++++++++++ 4 files changed, 93 insertions(+), 8 deletions(-) create mode 100644 spec/e2e/server_spec.rb diff --git a/README.md b/README.md index a934695..ac0d963 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,27 @@ driver.quit proxy_listener.hars #=> [#, #] proxy.close +``` + +Parallel usage of a single Browsermob server (it will start server process only if it hasn't already been started): +```ruby +BROWSERMOB_SERVER_PORT = 8080 + +begin + TCPSocket.new("127.0.0.1", BROWSERMOB_SERVER_PORT).close +rescue + server_location = "/path/to/download/browsermob-proxy" + browsermob_server = BrowserMob::Proxy::Server.new(server_location, port: BROWSERMOB_SERVER_PORT, stop_at_exit: false) + browsermob_server.start +end + +server_url = BrowserMob::Proxy::Server.form_server_url(BROWSERMOB_SERVER_PORT) +proxy = BrowserMob::Proxy::Client.from server_url + +at_exit do + proxy.close +end ``` Viewing HARs diff --git a/lib/browsermob/proxy/server.rb b/lib/browsermob/proxy/server.rb index 7028f84..de69079 100644 --- a/lib/browsermob/proxy/server.rb +++ b/lib/browsermob/proxy/server.rb @@ -5,7 +5,11 @@ module BrowserMob module Proxy class Server - attr_reader :port + attr_reader :port, :url, :process + + def self.form_server_url(port) + "http://localhost:#{port}" + end # # Create a new server instance @@ -24,6 +28,8 @@ def initialize(path, opts = {}) @port = Integer(opts[:port] || 8080) @timeout = Integer(opts[:timeout] || 10) @log = !!opts[:log] + @stop_at_exit = (opts[:stop_at_exit].nil?) ? true : opts[:stop_at_exit] + @url = self.class.form_server_url(@port) @process = create_process end @@ -33,16 +39,14 @@ def start wait_for_startup - pid = Process.pid - at_exit { stop if Process.pid == pid } + if @stop_at_exit + pid = Process.pid + at_exit { stop if Process.pid == pid } + end self end - def url - "http://localhost:#{port}" - end - def create_proxy Client.from url end @@ -108,4 +112,4 @@ class ServerDiedError < StandardError end # Server end # Proxy -end # BrowserMob \ No newline at end of file +end # BrowserMob diff --git a/spec/e2e/server_spec.rb b/spec/e2e/server_spec.rb new file mode 100644 index 0000000..d5e3c80 --- /dev/null +++ b/spec/e2e/server_spec.rb @@ -0,0 +1,31 @@ +require 'spec_helper' + +describe "Server" do + it 'should not stop itself at exit if :stop_at_exit is false' do + server_pid = run_in_isolation do + server = BrowserMob::Proxy::Server.new( + File.join(home, "bin", "browsermob-proxy"), + port: Selenium::WebDriver::PortProber.above(3000), + log: true, + stop_at_exit: false + ).start + server.process.pid + end + + expect(process_alive?(server_pid)).to eq(true) + Process.kill 'TERM', server_pid + end + + it 'should stop itself at exit if :stop_at_exit is not set (true by default)' do + server_pid = run_in_isolation do + server = BrowserMob::Proxy::Server.new( + File.join(home, "bin", "browsermob-proxy"), + port: Selenium::WebDriver::PortProber.above(3000), + log: true + ).start + server.process.pid + end + + expect(process_alive?(server_pid)).to eq(false) + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index c9ce6a6..fe3d566 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -82,7 +82,36 @@ def call(env) @app.call(env) end end + end + end + + module ServerSpecHelper + def run_in_isolation + # See http://stackoverflow.com/a/1076445/841064 + read, write = IO.pipe + + pid = fork do + read.close + ChildProcess.close_on_exec(write) + result = yield + Marshal.dump(result, write) + exit + end + write.close + result = read.read + Process.wait(pid) + raise 'child failed' if result.empty? + Marshal.load(result) + end + + def process_alive?(pid) + begin + Process.kill(0, pid) + true + rescue Errno::ESRCH + false + end end end end @@ -90,5 +119,6 @@ def call(env) RSpec.configure do |c| c.include(BrowserMob::Proxy::SpecHelper) + c.include(BrowserMob::Proxy::ServerSpecHelper) c.after(:suite) { $_bm_server.stop if $_bm_server } end