Skip to content

Commit

Permalink
Merge pull request #4 from CloudCannon/fix/chrome-instability
Browse files Browse the repository at this point in the history
Improve resilience launching a Chrome instance
  • Loading branch information
bglw authored Oct 2, 2024
2 parents 9e41a02 + a29f7a2 commit 9d5f5f4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

## Unreleased

* Improve resilience launching a Chrome instance

## v0.4.0 (September 27, 2024)

* Adds `-n <name>` / `--name <name>` arguments to the CLI to run a specific test
Expand Down
22 changes: 17 additions & 5 deletions toolproof/src/definitions/browser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ use std::sync::Arc;

use async_trait::async_trait;
use chromiumoxide::cdp::browser_protocol::target::CreateTargetParams;
use chromiumoxide::error::CdpError;
use futures::StreamExt;
use tokio::task::JoinHandle;

use crate::civilization::Civilization;
use crate::errors::{ToolproofInputError, ToolproofStepError};
use crate::errors::{ToolproofInputError, ToolproofInternalError, ToolproofStepError};
use crate::options::ToolproofParams;

use super::{SegmentArgs, ToolproofInstruction, ToolproofRetriever};
Expand All @@ -29,14 +30,25 @@ pub enum BrowserTester {
},
}

async fn try_launch_browser(mut max: usize) -> (Browser, chromiumoxide::Handler) {
let mut launch = Err(CdpError::NotFound);
while launch.is_err() && max > 0 {
max -= 1;
launch = Browser::launch(BrowserConfig::builder().build().unwrap()).await;
}
match launch {
Ok(res) => res,
Err(e) => {
panic!("Failed to launch browser due to error: {e}");
}
}
}

impl BrowserTester {
async fn initialize(params: &ToolproofParams) -> Self {
match params.browser {
crate::options::ToolproofBrowserImpl::Chrome => {
let (browser, mut handler) =
Browser::launch(BrowserConfig::builder().build().unwrap())
.await
.unwrap();
let (browser, mut handler) = try_launch_browser(3).await;

BrowserTester::Chrome {
browser: Arc::new(browser),
Expand Down

0 comments on commit 9d5f5f4

Please sign in to comment.