Skip to content

Commit

Permalink
Stop process if error during initialize (#414)
Browse files Browse the repository at this point in the history
  • Loading branch information
20TRIES authored Nov 9, 2023
1 parent afd29cc commit 00ffc0d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
20 changes: 15 additions & 5 deletions lib/ferrum/browser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -273,11 +273,21 @@ def headless_new?

def start
Utils::ElapsedTime.start
@process = Process.start(options)
@client = Client.new(@process.ws_url, self,
logger: options.logger,
ws_max_receive_size: options.ws_max_receive_size)
@contexts = Contexts.new(self)
@process = Process.new(options)

begin
@process.start
@client = Client.new(
@process.ws_url,
self,
logger: options.logger,
ws_max_receive_size: options.ws_max_receive_size
)
@contexts = Contexts.new(self)
rescue
@process.stop
raise
end
end
end
end
29 changes: 29 additions & 0 deletions spec/browser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,35 @@
end
end
end

it "stops process if an error is raised during process start" do
process = Ferrum::Browser::Process.new(Ferrum::Browser::Options.new(process_timeout: 0))
allow(Ferrum::Browser::Process).to receive(:new).and_return(process)
expect { Ferrum::Browser.new }.to raise_error(Ferrum::ProcessTimeoutError)
expect(process.pid).to be(nil)
end

it "stops process if an error is raised during client creation" do
process = Ferrum::Browser::Process.new(Ferrum::Browser::Options.new)
allow(Ferrum::Browser::Process).to receive(:new).and_return(process)

error = StandardError.new
allow(Ferrum::Browser::Client).to receive(:new).and_raise(error)

expect { Ferrum::Browser.new }.to raise_error(error)
expect(process.pid).to be(nil)
end

it "stops process if an error is raised during contexts creation" do
process = Ferrum::Browser::Process.new(Ferrum::Browser::Options.new)
allow(Ferrum::Browser::Process).to receive(:new).and_return(process)

error = StandardError.new
allow(Ferrum::Contexts).to receive(:new).and_raise(error)

expect { Ferrum::Browser.new }.to raise_error(error)
expect(process.pid).to be(nil)
end
end

describe "#crash" do
Expand Down

0 comments on commit 00ffc0d

Please sign in to comment.