Skip to content

Commit 6609c22

Browse files
committed
fix flakiness
Signed-off-by: Andrei Gherghescu <[email protected]>
1 parent 6724a78 commit 6609c22

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

plotly_static/src/lib.rs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,8 +1047,12 @@ impl AsyncStaticExporter {
10471047

10481048
async fn extract(&mut self, html_content: &str, plot: &PlotData<'_>) -> Result<String> {
10491049
let caps = self.build_webdriver_caps()?;
1050-
debug!("Use WebDriver and headless browser to export static plot");
1050+
debug!(
1051+
"Use WebDriver and headless browser to export static plot (offline_mode={}, port={})",
1052+
self.offline_mode, self.webdriver_port
1053+
);
10511054
let webdriver_url = format!("{}:{}", self.webdriver_url, self.webdriver_port);
1055+
debug!("Connecting to WebDriver at {webdriver_url}");
10521056

10531057
// Reuse existing client or create new one
10541058
let client = if let Some(ref client) = self.webdriver_client {
@@ -1058,8 +1062,8 @@ impl AsyncStaticExporter {
10581062
debug!("Creating new WebDriver session");
10591063
let new_client = ClientBuilder::native()
10601064
.capabilities(caps)
1061-
.connect(&webdriver_url)
1062-
.await
1065+
.connect(&webdriver_url)
1066+
.await
10631067
.with_context(|| "WebDriver session error")?;
10641068
self.webdriver_client = Some(new_client.clone());
10651069
new_client
@@ -1191,20 +1195,30 @@ impl AsyncStaticExporter {
11911195

11921196
#[cfg(test)]
11931197
mod tests {
1194-
use std::path::PathBuf;
1195-
use std::sync::atomic::{AtomicU32, Ordering};
1196-
11971198
use super::*;
1199+
use std::path::PathBuf;
11981200

11991201
fn init() {
12001202
let _ = env_logger::try_init();
12011203
}
12021204

12031205
// Helper to generate unique ports for parallel tests
1204-
static PORT_COUNTER: AtomicU32 = AtomicU32::new(4444);
1205-
12061206
fn get_unique_port() -> u32 {
1207-
PORT_COUNTER.fetch_add(1, Ordering::SeqCst)
1207+
use std::sync::atomic::{AtomicU32, Ordering};
1208+
static PORT_COUNTER: AtomicU32 = AtomicU32::new(4444);
1209+
1210+
// Before we used this counter to generate unique ports.
1211+
// >>> PORT_COUNTER.fetch_add(1, Ordering::SeqCst)
1212+
// However, sometimes the webdriver process is not stopped immediately
1213+
// and we get port conflicts.
1214+
// We try to give some time for other webdriver processes to stop so that we don't
1215+
// get port conflicts.
1216+
loop {
1217+
let p = PORT_COUNTER.fetch_add(1, Ordering::SeqCst);
1218+
if !webdriver::WebDriver::is_webdriver_running(p) {
1219+
return p;
1220+
}
1221+
}
12081222
}
12091223

12101224
fn create_test_plot() -> serde_json::Value {

plotly_static/src/webdriver.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const WEBDRIVER_BIN: &str = "chromedriver";
3232
/// Default WebDriver port
3333
pub(crate) const WEBDRIVER_PORT: u32 = 4444;
3434
/// Default WebDriver URL
35-
pub(crate) const WEBDRIVER_URL: &str = "http://localhost";
35+
pub(crate) const WEBDRIVER_URL: &str = "http://127.0.0.1";
3636

3737
#[cfg(all(feature = "chromedriver", not(target_os = "windows")))]
3838
pub(crate) fn chrome_default_caps() -> Vec<&'static str> {

0 commit comments

Comments
 (0)