From ec5efcc7e7a49ab6543393235431fd5c37a1a775 Mon Sep 17 00:00:00 2001 From: inconspicuous99 Date: Wed, 4 May 2022 23:46:31 -0500 Subject: [PATCH 1/2] Add: Configurable Ganache Startup TImeout --- ethers-core/src/utils/ganache.rs | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/ethers-core/src/utils/ganache.rs b/ethers-core/src/utils/ganache.rs index dfea10174..6ea517bcc 100644 --- a/ethers-core/src/utils/ganache.rs +++ b/ethers-core/src/utils/ganache.rs @@ -83,6 +83,7 @@ pub struct Ganache { mnemonic: Option, fork: Option, args: Vec, + startup_timeout: Option, } impl Ganache { @@ -92,6 +93,11 @@ impl Ganache { Self::default() } + pub fn startup_timeout_millis>(mut self, timeout: T) -> Self { + self.startup_timeout = Some(timeout.into()); + self + } + /// Sets the port which will be used when the `ganache-cli` instance is launched. #[must_use] pub fn port>(mut self, port: T) -> Self { @@ -176,8 +182,11 @@ impl Ganache { let mut private_keys = Vec::new(); let mut addresses = Vec::new(); let mut is_private_key = false; + + let startup_timeout = Duration::from_millis( + self.startup_timeout.unwrap_or(GANACHE_STARTUP_TIMEOUT_MILLIS)); loop { - if start + Duration::from_millis(GANACHE_STARTUP_TIMEOUT_MILLIS) <= Instant::now() { + if start + startup_timeout <= Instant::now() { panic!("Timed out waiting for ganache to start. Is ganache-cli installed?") } @@ -205,3 +214,18 @@ impl Ganache { GanacheInstance { pid: child, private_keys, addresses, port } } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn configurable_startup_timeout() { + Ganache::new().startup_timeout_millis(100000_u64).spawn(); + } + + #[test] + fn default_startup_works() { + Ganache::new().spawn(); + } +} \ No newline at end of file From abbea823347580a0994454e405b292f2f9b9f4f9 Mon Sep 17 00:00:00 2001 From: inconspicuous99 Date: Thu, 5 May 2022 00:01:04 -0500 Subject: [PATCH 2/2] fix lint, add docs. --- ethers-core/src/utils/ganache.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/ethers-core/src/utils/ganache.rs b/ethers-core/src/utils/ganache.rs index 6ea517bcc..45ef5b8d2 100644 --- a/ethers-core/src/utils/ganache.rs +++ b/ethers-core/src/utils/ganache.rs @@ -9,7 +9,7 @@ use std::{ time::{Duration, Instant}, }; -/// How long we will wait for ganache to indicate that it is ready. +/// Default amount of time we will wait for ganache to indicate that it is ready. const GANACHE_STARTUP_TIMEOUT_MILLIS: u64 = 10_000; /// A ganache CLI instance. Will close the instance when dropped. @@ -93,6 +93,8 @@ impl Ganache { Self::default() } + /// Sets the startup timeout which will be used when the `ganache-cli` instance is launched in + /// miliseconds. 10_000 miliseconds by default). pub fn startup_timeout_millis>(mut self, timeout: T) -> Self { self.startup_timeout = Some(timeout.into()); self @@ -183,8 +185,8 @@ impl Ganache { let mut addresses = Vec::new(); let mut is_private_key = false; - let startup_timeout = Duration::from_millis( - self.startup_timeout.unwrap_or(GANACHE_STARTUP_TIMEOUT_MILLIS)); + let startup_timeout = + Duration::from_millis(self.startup_timeout.unwrap_or(GANACHE_STARTUP_TIMEOUT_MILLIS)); loop { if start + startup_timeout <= Instant::now() { panic!("Timed out waiting for ganache to start. Is ganache-cli installed?") @@ -228,4 +230,4 @@ mod tests { fn default_startup_works() { Ganache::new().spawn(); } -} \ No newline at end of file +}