From 00ffc0d7bbefcb1f6b786c4cf85738412bfcc825 Mon Sep 17 00:00:00 2001 From: Marcus <20TRIES@users.noreply.github.com> Date: Thu, 9 Nov 2023 06:23:29 +0000 Subject: [PATCH] Stop process if error during initialize (#414) --- 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 75288671..364b0fa4 100644 --- a/lib/ferrum/browser.rb +++ b/lib/ferrum/browser.rb @@ -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 diff --git a/spec/browser_spec.rb b/spec/browser_spec.rb index 38ef049a..270b13a9 100644 --- a/spec/browser_spec.rb +++ b/spec/browser_spec.rb @@ -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