Skip to content

Commit

Permalink
Add: Make Ganache Startup Timeout *Configurable*. (gakonst#1224)
Browse files Browse the repository at this point in the history
* Add: Configurable Ganache Startup TImeout

* fix lint, add docs.
  • Loading branch information
inconspicuous99 committed May 5, 2022
1 parent 16e1707 commit f5c5468
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions ethers-core/src/utils/ganache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -83,6 +83,7 @@ pub struct Ganache {
mnemonic: Option<String>,
fork: Option<String>,
args: Vec<String>,
startup_timeout: Option<u64>,
}

impl Ganache {
Expand All @@ -92,6 +93,13 @@ 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<T: Into<u64>>(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<T: Into<u16>>(mut self, port: T) -> Self {
Expand Down Expand Up @@ -176,8 +184,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?")
}

Expand Down Expand Up @@ -205,3 +216,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();
}
}

0 comments on commit f5c5468

Please sign in to comment.