Skip to content

Commit

Permalink
fix: use random ports instead of hardcoded ones
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexD10S committed Dec 2, 2024
1 parent 3302333 commit 0f1605e
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 15 deletions.
3 changes: 2 additions & 1 deletion crates/pop-cli/tests/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use url::Url;
/// Test the contract lifecycle: new, build, up, call
#[tokio::test]
async fn contract_lifecycle() -> Result<()> {
const DEFAULT_PORT: u16 = 9944;
let temp = tempfile::tempdir().unwrap();
let temp_dir = temp.path();
//let temp_dir = Path::new("./"); //For testing locally
Expand Down Expand Up @@ -45,7 +46,7 @@ async fn contract_lifecycle() -> Result<()> {
let binary = contracts_node_generator(temp_dir.to_path_buf().clone(), None).await?;
binary.source(false, &(), true).await?;
set_executable_permission(binary.path())?;
let process = run_contracts_node(binary.path(), None, 9944).await?;
let process = run_contracts_node(binary.path(), None, DEFAULT_PORT).await?;

// Only upload the contract
// pop up contract --upload-only
Expand Down
15 changes: 8 additions & 7 deletions crates/pop-contracts/src/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ mod tests {
use crate::{
contracts_node_generator, dry_run_gas_estimate_instantiate, errors::Error,
instantiate_smart_contract, mock_build_process, new_environment, run_contracts_node,
set_up_deployment, UpOpts,
set_up_deployment, testing::find_free_port, UpOpts,
};
use anyhow::Result;
use pop_common::set_executable_permission;
Expand Down Expand Up @@ -336,7 +336,8 @@ mod tests {

#[tokio::test]
async fn call_works() -> Result<()> {
const LOCALHOST_URL: &str = "ws://127.0.0.1:9945";
let random_port = find_free_port();
let localhost_url = format!("ws://127.0.0.1:{}", random_port);
let temp_dir = new_environment("testing")?;
let current_dir = env::current_dir().expect("Failed to get current directory");
mock_build_process(
Expand All @@ -350,7 +351,7 @@ mod tests {
let binary = contracts_node_generator(cache.clone(), None).await?;
binary.source(false, &(), true).await?;
set_executable_permission(binary.path())?;
let process = run_contracts_node(binary.path(), None, 9945).await?;
let process = run_contracts_node(binary.path(), None, random_port).await?;
// Wait 5 secs more to give time for the node to be ready
sleep(Duration::from_millis(5000)).await;
// Instantiate a Smart Contract.
Expand All @@ -362,7 +363,7 @@ mod tests {
gas_limit: None,
proof_size: None,
salt: Some(Bytes::from(vec![0x00])),
url: Url::parse(LOCALHOST_URL)?,
url: Url::parse(&localhost_url)?,
suri: "//Alice".to_string(),
})
.await?;
Expand All @@ -377,7 +378,7 @@ mod tests {
value: "0".to_string(),
gas_limit: None,
proof_size: None,
url: Url::parse(LOCALHOST_URL)?,
url: Url::parse(&localhost_url)?,
suri: "//Alice".to_string(),
execute: false,
})
Expand All @@ -393,15 +394,15 @@ mod tests {
value: "0".to_string(),
gas_limit: None,
proof_size: None,
url: Url::parse(LOCALHOST_URL)?,
url: Url::parse(&localhost_url)?,
suri: "//Alice".to_string(),
execute: false,
})
.await?;
let weight = dry_run_gas_estimate_call(&call_exec).await?;
assert!(weight.ref_time() > 0);
assert!(weight.proof_size() > 0);
call_smart_contract(call_exec, weight, &Url::parse(LOCALHOST_URL)?).await?;
call_smart_contract(call_exec, weight, &Url::parse(&localhost_url)?).await?;
// Assert that the value has been flipped.
query = dry_run_call(&query_exec).await?;
assert_eq!(query, "Ok(true)");
Expand Down
2 changes: 1 addition & 1 deletion crates/pop-contracts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub use new::{create_smart_contract, is_valid_contract_name};
pub use node::{contracts_node_generator, is_chain_alive, run_contracts_node};
pub use templates::{Contract, ContractType};
pub use test::{test_e2e_smart_contract, test_smart_contract};
pub use testing::{mock_build_process, new_environment};
pub use testing::{find_free_port, mock_build_process, new_environment};
pub use up::{
dry_run_gas_estimate_instantiate, dry_run_upload, instantiate_smart_contract,
set_up_deployment, set_up_upload, upload_smart_contract, UpOpts,
Expand Down
6 changes: 5 additions & 1 deletion crates/pop-contracts/src/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ fn release_directory_by_target(tag: Option<&str>) -> Result<&'static str, Error>

#[cfg(test)]
mod tests {
use crate::testing::find_free_port;

use super::*;
use anyhow::{Error, Result};
use std::process::Command;
Expand Down Expand Up @@ -221,7 +223,9 @@ mod tests {
#[ignore = "Works fine locally but is causing issues when running tests in parallel in the CI environment."]
#[tokio::test]
async fn run_contracts_node_works() -> Result<(), Error> {
let local_url = url::Url::parse("ws://localhost:9947")?;
let random_port = find_free_port();
let localhost_url = format!("ws://127.0.0.1:{}", random_port);
let local_url = url::Url::parse(&localhost_url)?;

let temp_dir = tempfile::tempdir().expect("Could not create temp dir");
let cache = temp_dir.path().join("");
Expand Down
10 changes: 10 additions & 0 deletions crates/pop-contracts/src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{create_smart_contract, Contract};
use anyhow::Result;
use std::{
fs::{copy, create_dir},
net::TcpListener,
path::Path,
};

Expand Down Expand Up @@ -37,3 +38,12 @@ where
copy(metadata_file, target_contract_dir.join("ink/testing.json"))?;
Ok(())
}

/// Finds an available port by binding to port 0 and retrieving the assigned port.
pub fn find_free_port() -> u16 {
TcpListener::bind("127.0.0.1:0")
.expect("Failed to bind to an available port")
.local_addr()
.expect("Failed to retrieve local address")
.port()
}
11 changes: 6 additions & 5 deletions crates/pop-contracts/src/up.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ mod tests {
use super::*;
use crate::{
contracts_node_generator, errors::Error, mock_build_process, new_environment,
run_contracts_node,
run_contracts_node, testing::find_free_port,
};
use anyhow::Result;
use pop_common::set_executable_permission;
Expand Down Expand Up @@ -366,7 +366,8 @@ mod tests {

#[tokio::test]
async fn instantiate_and_upload() -> Result<()> {
const LOCALHOST_URL: &str = "ws://127.0.0.1:9946";
let random_port = find_free_port();
let localhost_url = format!("ws://127.0.0.1:{}", random_port);
let temp_dir = new_environment("testing")?;
let current_dir = env::current_dir().expect("Failed to get current directory");
mock_build_process(
Expand All @@ -380,7 +381,7 @@ mod tests {
let binary = contracts_node_generator(cache.clone(), None).await?;
binary.source(false, &(), true).await?;
set_executable_permission(binary.path())?;
let process = run_contracts_node(binary.path(), None, 9946).await?;
let process = run_contracts_node(binary.path(), None, random_port).await?;
// Wait 5 secs more to give time for the node to be ready
sleep(Duration::from_millis(5000)).await;
let upload_exec = set_up_upload(UpOpts {
Expand All @@ -391,7 +392,7 @@ mod tests {
gas_limit: None,
proof_size: None,
salt: None,
url: Url::parse(LOCALHOST_URL)?,
url: Url::parse(&localhost_url)?,
suri: "//Alice".to_string(),
})
.await?;
Expand All @@ -415,7 +416,7 @@ mod tests {
gas_limit: None,
proof_size: None,
salt: Some(Bytes::from(vec![0x00])),
url: Url::parse(LOCALHOST_URL)?,
url: Url::parse(&localhost_url)?,
suri: "//Alice".to_string(),
})
.await?;
Expand Down

0 comments on commit 0f1605e

Please sign in to comment.