From 153e40a76f8a24dfb36e5bfdfaa5a48f922651de Mon Sep 17 00:00:00 2001 From: Marcus Turner Date: Thu, 19 Oct 2023 12:55:17 +0000 Subject: [PATCH] Stop process if error during initialize --- lib/ferrum/browser.rb | 20 +++++++++++++++----- spec/browser_spec.rb | 29 +++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/lib/ferrum/browser.rb b/lib/ferrum/browser.rb index 850cb59a..0539f394 100644 --- a/lib/ferrum/browser.rb +++ b/lib/ferrum/browser.rb @@ -266,11 +266,21 @@ def version 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 diff --git a/spec/browser_spec.rb b/spec/browser_spec.rb index 2d4ab27e..f2637f04 100644 --- a/spec/browser_spec.rb +++ b/spec/browser_spec.rb @@ -252,6 +252,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