Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions crates/icp-cli/tests/common/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ impl TestContext {
.success();
}

// wait up to 30 seconds for descriptor path to contain valid json
// wait up for descriptor path to contain valid json
pub fn wait_for_local_network_descriptor(&self, project_dir: &Path) -> TestNetwork {
self.wait_for_network_descriptor(project_dir, "local")
}
Expand All @@ -245,23 +245,27 @@ impl TestContext {
.join(network_name)
.join("descriptor.json");
let start_time = std::time::Instant::now();
let timeout = 45;
eprintln!("Waiting for network descriptor at {descriptor_path} - limit {timeout}s");
let network_descriptor = loop {
eprintln!("Checking for network descriptor at {descriptor_path}");
let elapsed = start_time.elapsed().as_secs();
if descriptor_path.exists() && descriptor_path.is_file() {
let contents = fs::read_to_string(&descriptor_path)
.expect("Failed to read network descriptor");
let parsed = serde_json::from_str::<serde_json::Value>(&contents);
if let Ok(value) = parsed {
eprintln!("Network descriptor found at {descriptor_path}");
eprintln!("Network descriptor found at {descriptor_path} after {elapsed}s");
break value;
} else {
eprintln!(
"Network descriptor at {descriptor_path} is not valid JSON: {contents}"
);
}
}
if start_time.elapsed().as_secs() > 30 {
panic!("Timed out waiting for network descriptor at {descriptor_path}");
if elapsed > timeout {
panic!(
"Timed out waiting for network descriptor at {descriptor_path} after {elapsed}s"
);
}
std::thread::sleep(std::time::Duration::from_millis(100));
};
Expand Down
10 changes: 6 additions & 4 deletions crates/icp/src/network/managed/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,19 +196,21 @@ async fn wait_for_shutdown(child: &mut Child) -> ShutdownReason {
}

pub async fn wait_for_port_file(path: &Path) -> Result<u16, WaitForPortTimeoutError> {
let mut retries = 0;
while retries < 3000 {
let start_time = std::time::Instant::now();

loop {
if let Ok(contents) = read_to_string(path)
&& contents.ends_with('\n')
&& let Ok(port) = contents.trim().parse::<u16>()
{
return Ok(port);
}

if start_time.elapsed().as_secs() > 30 {
return Err(WaitForPortTimeoutError);
}
sleep(Duration::from_millis(100)).await;
retries += 1;
}
Err(WaitForPortTimeoutError)
}

#[derive(Debug, Snafu)]
Expand Down