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

Allow using Browsermob server in parallel #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,27 @@ driver.quit

proxy_listener.hars #=> [#<HAR::Archive:0x-27066c42d7e75fa6>, #<HAR::Archive:0x-d7e75fa627066c42>]
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
Expand Down
20 changes: 12 additions & 8 deletions lib/browsermob/proxy/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -108,4 +112,4 @@ class ServerDiedError < StandardError

end # Server
end # Proxy
end # BrowserMob
end # BrowserMob
31 changes: 31 additions & 0 deletions spec/e2e/server_spec.rb
Original file line number Diff line number Diff line change
@@ -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
30 changes: 30 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,43 @@ 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
end

RSpec.configure do |c|
c.include(BrowserMob::Proxy::SpecHelper)
c.include(BrowserMob::Proxy::ServerSpecHelper)
c.after(:suite) { $_bm_server.stop if $_bm_server }
end