From 891e532041a29c64231459c83f6533a2fc36769b Mon Sep 17 00:00:00 2001 From: Ales Tsurko Date: Wed, 22 Jun 2022 15:52:51 +0300 Subject: [PATCH] [fix] #2137: Refactor with new version of unique_port crate Signed-off-by: Ales Tsurko --- Cargo.lock | 17 ++++- cli/Cargo.toml | 1 + cli/src/torii/tests.rs | 49 +++++++------- client/Cargo.toml | 1 + client/tests/integration/add_account.rs | 5 +- client/tests/integration/add_asset.rs | 11 ++-- client/tests/integration/add_domain.rs | 5 +- client/tests/integration/asset_propagation.rs | 5 +- client/tests/integration/burn_public_keys.rs | 5 +- client/tests/integration/connected_peers.rs | 7 +- client/tests/integration/events/pipeline.rs | 7 +- client/tests/integration/offline_peers.rs | 5 +- client/tests/integration/queries/role.rs | 11 ++-- client/tests/integration/restart_peer.rs | 5 +- core/test_network/Cargo.toml | 1 + core/test_network/src/lib.rs | 2 - core/test_network/src/unique_port.rs | 65 ------------------- data_model/Cargo.toml | 1 + data_model/tests/data_model.rs | 5 +- 19 files changed, 85 insertions(+), 123 deletions(-) delete mode 100644 core/test_network/src/unique_port.rs diff --git a/Cargo.lock b/Cargo.lock index 9fdb87ffb93..224e75b7759 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1703,6 +1703,7 @@ dependencies = [ "test_network", "thiserror", "tokio", + "unique_port 0.2.0", "warp", ] @@ -1776,6 +1777,7 @@ dependencies = [ "tracing-flame", "tracing-subscriber 0.3.11", "tungstenite 0.16.0", + "unique_port 0.2.0", ] [[package]] @@ -1850,7 +1852,7 @@ dependencies = [ "thiserror", "tokio", "tokio-stream", - "unique_port", + "unique_port 0.1.1", "wasmtime", ] @@ -1897,6 +1899,7 @@ dependencies = [ "thiserror", "tokio", "trybuild", + "unique_port 0.2.0", "warp", ] @@ -2014,7 +2017,7 @@ dependencies = [ "rand 0.8.5", "thiserror", "tokio", - "unique_port", + "unique_port 0.1.1", ] [[package]] @@ -3586,6 +3589,7 @@ dependencies = [ "rand 0.8.5", "tempfile", "tokio", + "unique_port 0.2.0", ] [[package]] @@ -4029,6 +4033,15 @@ dependencies = [ "once_cell", ] +[[package]] +name = "unique_port" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "addcbf13a0059acde3897f2187f88d74bf7d71a4f3073a1a788bf4d266825c79" +dependencies = [ + "once_cell", +] + [[package]] name = "universal-hash" version = "0.4.1" diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 4fbb67951c5..66d9cca1a36 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -64,3 +64,4 @@ color-eyre = "0.5.11" [dev-dependencies] test_network = { version = "=2.0.0-pre-rc.5", path = "../core/test_network" } +unique_port = "0.2.0" diff --git a/cli/src/torii/tests.rs b/cli/src/torii/tests.rs index 1edaccc5d5b..8bcd06dabfe 100644 --- a/cli/src/torii/tests.rs +++ b/cli/src/torii/tests.rs @@ -20,9 +20,10 @@ use iroha_core::{ }; use iroha_data_model::{account::GENESIS_ACCOUNT_NAME, predicate::PredicateBox, prelude::*}; use iroha_version::prelude::*; +// use test_network::*; use tokio::time; +use unique_port::generate_unique_start_port; use warp::test::WsClient; -use test_network::{generate_offset, *}; use super::{routing::*, *}; use crate::{ @@ -84,7 +85,7 @@ async fn create_torii() -> (Torii, KeyPair) { #[tokio::test(flavor = "multi_thread")] async fn create_and_start_torii() { - unique_port::set_offset(generate_offset!()).unwrap(); + unique_port::set_port_index(generate_unique_start_port!()); let (torii, _) = create_torii().await; let result = time::timeout(Duration::from_millis(50), torii.start()).await; @@ -94,7 +95,7 @@ async fn create_and_start_torii() { #[tokio::test(flavor = "multi_thread")] async fn torii_pagination() { - unique_port::set_offset(generate_offset!()).unwrap(); + unique_port::set_port_index(generate_unique_start_port!()); let (torii, keys) = create_torii().await; let get_domains = |start, limit| { @@ -315,7 +316,7 @@ fn asset_id(name: &str, account: &str) -> AssetId { #[tokio::test] async fn find_asset() { - unique_port::set_offset(generate_offset!()).unwrap(); + unique_port::set_port_index(generate_unique_start_port!()); QuerySet::new() .given(register_domain()) .given(register_account("alice")) @@ -342,7 +343,7 @@ async fn find_asset() { #[tokio::test] async fn find_asset_with_no_mint() { - unique_port::set_offset(generate_offset!()).unwrap(); + unique_port::set_port_index(generate_unique_start_port!()); QuerySet::new() .given(register_domain()) .given(register_account("alice")) @@ -365,7 +366,7 @@ async fn find_asset_with_no_mint() { #[tokio::test] async fn find_asset_with_no_asset_definition() { - unique_port::set_offset(generate_offset!()).unwrap(); + unique_port::set_port_index(generate_unique_start_port!()); QuerySet::new() .given(register_domain()) .given(register_account("alice")) @@ -388,7 +389,7 @@ async fn find_asset_with_no_asset_definition() { #[tokio::test] async fn find_asset_with_no_account() { - unique_port::set_offset(generate_offset!()).unwrap(); + unique_port::set_port_index(generate_unique_start_port!()); QuerySet::new() .given(register_domain()) // .given(register_account("alice")) @@ -411,7 +412,7 @@ async fn find_asset_with_no_account() { #[tokio::test] async fn find_asset_with_no_domain() { - unique_port::set_offset(generate_offset!()).unwrap(); + unique_port::set_port_index(generate_unique_start_port!()); QuerySet::new() // .given(register_domain()) // .given(register_account("alice")) @@ -434,7 +435,7 @@ async fn find_asset_with_no_domain() { #[tokio::test] async fn find_asset_definition() { - unique_port::set_offset(generate_offset!()).unwrap(); + unique_port::set_port_index(generate_unique_start_port!()); QuerySet::new() .given(register_domain()) .given(register_asset_definition("rose")) @@ -448,7 +449,7 @@ async fn find_asset_definition() { #[tokio::test] async fn find_asset_definition_with_no_asset_definition() { - unique_port::set_offset(generate_offset!()).unwrap(); + unique_port::set_port_index(generate_unique_start_port!()); QuerySet::new() .given(register_domain()) // .given(register_asset_definition("rose")) @@ -469,7 +470,7 @@ async fn find_asset_definition_with_no_asset_definition() { #[tokio::test] async fn find_asset_definition_with_no_domain() { - unique_port::set_offset(generate_offset!()).unwrap(); + unique_port::set_port_index(generate_unique_start_port!()); QuerySet::new() // .given(register_domain()) // .given(register_asset_definition("rose")) @@ -490,7 +491,7 @@ async fn find_asset_definition_with_no_domain() { #[tokio::test] async fn find_account() { - unique_port::set_offset(generate_offset!()).unwrap(); + unique_port::set_port_index(generate_unique_start_port!()); QuerySet::new() .given(register_domain()) .given(register_account("alice")) @@ -507,7 +508,7 @@ async fn find_account() { #[tokio::test] async fn find_account_with_no_account() { - unique_port::set_offset(generate_offset!()).unwrap(); + unique_port::set_port_index(generate_unique_start_port!()); QuerySet::new() .given(register_domain()) // .given(register_account("alice")) @@ -528,7 +529,7 @@ async fn find_account_with_no_account() { #[tokio::test] async fn find_account_with_no_domain() { - unique_port::set_offset(generate_offset!()).unwrap(); + unique_port::set_port_index(generate_unique_start_port!()); QuerySet::new() // .given(register_domain()) // .given(register_account("alice")) @@ -549,7 +550,7 @@ async fn find_account_with_no_domain() { #[tokio::test] async fn find_domain() { - unique_port::set_offset(generate_offset!()).unwrap(); + unique_port::set_port_index(generate_unique_start_port!()); QuerySet::new() .given(register_domain()) .query(QueryBox::FindDomainById(FindDomainById::new( @@ -562,7 +563,7 @@ async fn find_domain() { #[tokio::test] async fn find_domain_with_no_domain() { - unique_port::set_offset(generate_offset!()).unwrap(); + unique_port::set_port_index(generate_unique_start_port!()); QuerySet::new() // .given(register_domain()) .query(QueryBox::FindDomainById(FindDomainById::new( @@ -589,7 +590,7 @@ fn query() -> QueryBox { #[tokio::test] async fn query_with_wrong_signatory() { - unique_port::set_offset(generate_offset!()).unwrap(); + unique_port::set_port_index(generate_unique_start_port!()); QuerySet::new() .given(register_domain()) .given(register_account("alice")) @@ -618,7 +619,7 @@ async fn query_with_wrong_signature() { #[tokio::test] async fn query_with_wrong_signature_and_no_permission() { - unique_port::set_offset(generate_offset!()).unwrap(); + unique_port::set_port_index(generate_unique_start_port!()); QuerySet::new() .given(register_domain()) .given(register_account("alice")) @@ -633,7 +634,7 @@ async fn query_with_wrong_signature_and_no_permission() { #[tokio::test] async fn query_with_no_permission() { - unique_port::set_offset(generate_offset!()).unwrap(); + unique_port::set_port_index(generate_unique_start_port!()); QuerySet::new() .given(register_domain()) .given(register_account("alice")) @@ -648,7 +649,7 @@ async fn query_with_no_permission() { #[tokio::test] async fn query_with_no_permission_and_no_find() { - unique_port::set_offset(generate_offset!()).unwrap(); + unique_port::set_port_index(generate_unique_start_port!()); QuerySet::new() .given(register_domain()) // .given(register_account("alice")) @@ -663,7 +664,7 @@ async fn query_with_no_permission_and_no_find() { #[tokio::test] async fn query_with_no_find() { - unique_port::set_offset(generate_offset!()).unwrap(); + unique_port::set_port_index(generate_unique_start_port!()); QuerySet::new() .given(register_domain()) // .given(register_account("alice")) @@ -701,7 +702,7 @@ fn new_dummy() -> ValidBlock { #[tokio::test] async fn blocks_stream() { - unique_port::set_offset(generate_offset!()).unwrap(); + unique_port::set_port_index(generate_unique_start_port!()); const BLOCK_COUNT: usize = 4; let (torii, _) = create_torii().await; @@ -769,7 +770,7 @@ fn domains( #[test] fn hash_should_be_the_same() { - unique_port::set_offset(generate_offset!()).unwrap(); + unique_port::set_port_index(generate_unique_start_port!()); let key_pair = KeyPair::generate().expect("Failed to generate key pair."); let mut config = get_config( get_trusted_peers(Some(key_pair.public_key())), @@ -812,7 +813,7 @@ fn hash_should_be_the_same() { #[tokio::test] async fn test_subscription_websocket_clean_closing() { - unique_port::set_offset(generate_offset!()).unwrap(); + unique_port::set_port_index(generate_unique_start_port!()); use iroha_data_model::events::{pipeline, FilterBox}; use warp::filters::ws; diff --git a/client/Cargo.toml b/client/Cargo.toml index b5cc6af2c23..086d64f8f6a 100644 --- a/client/Cargo.toml +++ b/client/Cargo.toml @@ -42,6 +42,7 @@ iroha_permissions_validators = { version = "=2.0.0-pre-rc.5", path = "../permiss iroha = { path = "../cli", features = ["dev-telemetry", "telemetry"] } test_network = { version = "=2.0.0-pre-rc.5", path = "../core/test_network" } +unique_port = "0.2.0" parity-scale-codec = { version = "2.3.1", default-features = false, features = ["derive"] } tokio = { version = "1.6.0", features = ["rt", "rt-multi-thread"]} diff --git a/client/tests/integration/add_account.rs b/client/tests/integration/add_account.rs index b44449a43e3..efed7d3bd3a 100644 --- a/client/tests/integration/add_account.rs +++ b/client/tests/integration/add_account.rs @@ -5,12 +5,13 @@ use std::thread; use eyre::Result; use iroha_client::client; use iroha_data_model::prelude::*; -use test_network::{generate_offset, *}; +use test_network::*; +use unique_port::generate_unique_start_port; #[test] fn client_add_account_with_name_length_more_than_limit_should_not_commit_transaction() -> Result<()> { - unique_port::set_offset(generate_offset!()).unwrap(); + unique_port::set_port_index(generate_unique_start_port!()); let (_rt, _peer, test_client) = ::new().start_with_runtime(); wait_for_genesis_committed(&vec![test_client.clone()], 0); diff --git a/client/tests/integration/add_asset.rs b/client/tests/integration/add_asset.rs index 95047853dd6..05be8a7562f 100644 --- a/client/tests/integration/add_asset.rs +++ b/client/tests/integration/add_asset.rs @@ -5,13 +5,14 @@ use std::{str::FromStr as _, thread}; use eyre::Result; use iroha_client::client; use iroha_data_model::{fixed::Fixed, prelude::*}; -use test_network::{generate_offset, *}; +use test_network::*; +use unique_port::generate_unique_start_port; use super::Configuration; #[test] fn client_add_asset_quantity_to_existing_asset_should_increase_asset_amount() -> Result<()> { - unique_port::set_offset(generate_offset!()).unwrap(); + unique_port::set_port_index(generate_unique_start_port!()); let (_rt, _peer, mut test_client) = ::new().start_with_runtime(); wait_for_genesis_committed(&vec![test_client.clone()], 0); @@ -43,7 +44,7 @@ fn client_add_asset_quantity_to_existing_asset_should_increase_asset_amount() -> #[test] fn client_add_big_asset_quantity_to_existing_asset_should_increase_asset_amount() -> Result<()> { - unique_port::set_offset(generate_offset!()).unwrap(); + unique_port::set_port_index(generate_unique_start_port!()); let (_rt, _peer, mut test_client) = ::new().start_with_runtime(); wait_for_genesis_committed(&vec![test_client.clone()], 0); @@ -75,7 +76,7 @@ fn client_add_big_asset_quantity_to_existing_asset_should_increase_asset_amount( #[test] fn client_add_asset_with_decimal_should_increase_asset_amount() -> Result<()> { - unique_port::set_offset(generate_offset!()).unwrap(); + unique_port::set_port_index(generate_unique_start_port!()); let (_rt, _peer, mut test_client) = ::new().start_with_runtime(); // Given @@ -128,7 +129,7 @@ fn client_add_asset_with_decimal_should_increase_asset_amount() -> Result<()> { #[test] fn client_add_asset_with_name_length_more_than_limit_should_not_commit_transaction() -> Result<()> { - unique_port::set_offset(generate_offset!()).unwrap(); + unique_port::set_port_index(generate_unique_start_port!()); let (_rt, _peer, test_client) = ::new().start_with_runtime(); let pipeline_time = Configuration::pipeline_time(); diff --git a/client/tests/integration/add_domain.rs b/client/tests/integration/add_domain.rs index b04e43d3238..9169310a9ea 100644 --- a/client/tests/integration/add_domain.rs +++ b/client/tests/integration/add_domain.rs @@ -5,14 +5,15 @@ use std::thread; use eyre::Result; use iroha_client::client; use iroha_data_model::prelude::*; -use test_network::{generate_offset, *}; +use test_network::*; +use unique_port::generate_unique_start_port; use super::Configuration; #[test] fn client_add_domain_with_name_length_more_than_limit_should_not_commit_transaction() -> Result<()> { - unique_port::set_offset(generate_offset!()).unwrap(); + unique_port::set_port_index(generate_unique_start_port!()); let (_rt, _peer, test_client) = ::new().start_with_runtime(); wait_for_genesis_committed(&vec![test_client.clone()], 0); let pipeline_time = Configuration::pipeline_time(); diff --git a/client/tests/integration/asset_propagation.rs b/client/tests/integration/asset_propagation.rs index 03ca1689c1c..49f0a593707 100644 --- a/client/tests/integration/asset_propagation.rs +++ b/client/tests/integration/asset_propagation.rs @@ -6,14 +6,15 @@ use eyre::Result; use iroha_client::client; use iroha_core::prelude::*; use iroha_data_model::prelude::*; -use test_network::{generate_offset, *}; +use test_network::*; +use unique_port::generate_unique_start_port; use super::Configuration; #[test] fn client_add_asset_quantity_to_existing_asset_should_increase_asset_amount_on_another_peer( ) -> Result<()> { - unique_port::set_offset(generate_offset!()).unwrap(); + unique_port::set_port_index(generate_unique_start_port!()); // Given let (_rt, network, iroha_client) = ::start_test_with_runtime(4, 1); wait_for_genesis_committed(&network.clients(), 0); diff --git a/client/tests/integration/burn_public_keys.rs b/client/tests/integration/burn_public_keys.rs index 5cbb7c0f107..20eecdf4663 100644 --- a/client/tests/integration/burn_public_keys.rs +++ b/client/tests/integration/burn_public_keys.rs @@ -5,7 +5,8 @@ use std::thread; use iroha_client::client::{account, transaction, Client}; use iroha_core::prelude::*; use iroha_data_model::prelude::*; -use test_network::{generate_offset, *}; +use test_network::*; +use unique_port::generate_unique_start_port; use super::Configuration; @@ -27,7 +28,7 @@ fn account_keys_count(client: &mut Client, account_id: AccountId) -> usize { #[test] fn public_keys_cannot_be_burned_to_nothing() { - unique_port::set_offset(generate_offset!()).unwrap(); + unique_port::set_port_index(generate_unique_start_port!()); const KEYS_COUNT: usize = 3; let bob_id: AccountId = "bob@wonderland".parse().expect("Valid"); let bob_keys_count = |client: &mut Client| account_keys_count(client, bob_id.clone()); diff --git a/client/tests/integration/connected_peers.rs b/client/tests/integration/connected_peers.rs index e9a91ad221a..ae59adcb479 100644 --- a/client/tests/integration/connected_peers.rs +++ b/client/tests/integration/connected_peers.rs @@ -4,19 +4,20 @@ use std::thread; use iroha_client::client::Client; use iroha_data_model::{peer::Peer as DataModelPeer, prelude::*}; -use test_network::{generate_offset, *}; +use test_network::*; +use unique_port::generate_unique_start_port; use super::Configuration; #[test] fn connected_peers_with_f_2_1_2() { - unique_port::set_offset(generate_offset!()).unwrap(); + unique_port::set_port_index(generate_unique_start_port!()); connected_peers_with_f(2) } #[test] fn connected_peers_with_f_1_0_1() { - unique_port::set_offset(generate_offset!()).unwrap(); + unique_port::set_port_index(generate_unique_start_port!()); connected_peers_with_f(1) } diff --git a/client/tests/integration/events/pipeline.rs b/client/tests/integration/events/pipeline.rs index 82fb20b4bf3..e23f00f2ec3 100644 --- a/client/tests/integration/events/pipeline.rs +++ b/client/tests/integration/events/pipeline.rs @@ -4,7 +4,8 @@ use std::thread::{self, JoinHandle}; use eyre::Result; use iroha_data_model::prelude::*; -use test_network::{generate_offset, *}; +use test_network::*; +use unique_port::generate_unique_start_port; use super::Configuration; @@ -12,13 +13,13 @@ const PEER_COUNT: usize = 7; #[test] fn transaction_with_no_instructions_should_be_committed() -> Result<()> { - unique_port::set_offset(generate_offset!()).unwrap(); + unique_port::set_port_index(generate_unique_start_port!()); test_with_instruction_and_status(None, PipelineStatusKind::Committed) } #[test] fn transaction_with_fail_instruction_should_be_rejected() -> Result<()> { - unique_port::set_offset(generate_offset!()).unwrap(); + unique_port::set_port_index(generate_unique_start_port!()); let fail = FailBox::new("Should be rejected"); test_with_instruction_and_status(Some(fail.into()), PipelineStatusKind::Rejected) } diff --git a/client/tests/integration/offline_peers.rs b/client/tests/integration/offline_peers.rs index 2fc5ed73c87..7472abf636a 100644 --- a/client/tests/integration/offline_peers.rs +++ b/client/tests/integration/offline_peers.rs @@ -2,12 +2,13 @@ use iroha_client::client; use iroha_data_model::prelude::*; -use test_network::{generate_offset, *}; +use test_network::*; use tokio::runtime::Runtime; +use unique_port::generate_unique_start_port; #[test] fn genesis_block_is_commited_with_some_offline_peers() { - unique_port::set_offset(generate_offset!()).unwrap(); + unique_port::set_port_index(generate_unique_start_port!()); // Given let rt = Runtime::test(); diff --git a/client/tests/integration/queries/role.rs b/client/tests/integration/queries/role.rs index b9546873d13..5e90dc005b0 100644 --- a/client/tests/integration/queries/role.rs +++ b/client/tests/integration/queries/role.rs @@ -8,6 +8,7 @@ use iroha_core::smartcontracts::isi::query::Error as QueryError; use iroha_data_model::prelude::*; use iroha_permissions_validators::public_blockchain::key_value::CanSetKeyValueInUserMetadata; use test_network::*; +use unique_port::generate_unique_start_port; fn create_role_ids() -> [::Id; 5] { [ @@ -21,7 +22,7 @@ fn create_role_ids() -> [::Id; 5] { #[test] fn find_roles() -> Result<()> { - unique_port::set_offset(generate_offset!()).unwrap(); + unique_port::set_port_index(generate_unique_start_port!()); let (_rt, _peer, test_client) = ::new().start_with_runtime(); wait_for_genesis_committed(&vec![test_client.clone()], 0); @@ -51,7 +52,7 @@ fn find_roles() -> Result<()> { #[test] fn find_role_ids() -> Result<()> { - unique_port::set_offset(generate_offset!()).unwrap(); + unique_port::set_port_index(generate_unique_start_port!()); let (_rt, _peer, test_client) = ::new().start_with_runtime(); wait_for_genesis_committed(&vec![test_client.clone()], 0); @@ -78,7 +79,7 @@ fn find_role_ids() -> Result<()> { #[test] fn find_role_by_id() -> Result<()> { - unique_port::set_offset(generate_offset!()).unwrap(); + unique_port::set_port_index(generate_unique_start_port!()); let (_rt, _peer, test_client) = ::new().start_with_runtime(); wait_for_genesis_committed(&vec![test_client.clone()], 0); @@ -99,7 +100,7 @@ fn find_role_by_id() -> Result<()> { #[test] fn find_unregistered_role_by_id() { - unique_port::set_offset(generate_offset!()).unwrap(); + unique_port::set_port_index(generate_unique_start_port!()); let (_rt, _peer, test_client) = ::new().start_with_runtime(); wait_for_genesis_committed(&vec![test_client.clone()], 0); @@ -117,7 +118,7 @@ fn find_unregistered_role_by_id() { #[test] fn find_roles_by_account_id() -> Result<()> { - unique_port::set_offset(generate_offset!()).unwrap(); + unique_port::set_port_index(generate_unique_start_port!()); let (_rt, _peer, test_client) = ::new().start_with_runtime(); wait_for_genesis_committed(&vec![test_client.clone()], 0); diff --git a/client/tests/integration/restart_peer.rs b/client/tests/integration/restart_peer.rs index b95cfd7bbf0..3c9514f27b8 100644 --- a/client/tests/integration/restart_peer.rs +++ b/client/tests/integration/restart_peer.rs @@ -7,14 +7,15 @@ use iroha_client::client; use iroha_core::prelude::*; use iroha_data_model::prelude::*; use tempfile::TempDir; -use test_network::{generate_offset, Peer as TestPeer, *}; +use test_network::{Peer as TestPeer, *}; use tokio::runtime::Runtime; +use unique_port::generate_unique_start_port; use super::Configuration; #[test] fn restarted_peer_should_have_the_same_asset_amount() -> Result<()> { - unique_port::set_offset(generate_offset!()).unwrap(); + unique_port::set_port_index(generate_unique_start_port!()); let temp_dir = Arc::new(TempDir::new()?); let mut configuration = Configuration::test(); diff --git a/core/test_network/Cargo.toml b/core/test_network/Cargo.toml index ed943438203..d6961d8d186 100644 --- a/core/test_network/Cargo.toml +++ b/core/test_network/Cargo.toml @@ -24,6 +24,7 @@ once_cell = "1.8.0" rand = "0.8" tempfile = "3" tokio = { version = "1.6.0", features = ["rt", "rt-multi-thread", "macros"]} +unique_port = "0.2.0" [dev-dependencies] async-trait = "0.1" diff --git a/core/test_network/src/lib.rs b/core/test_network/src/lib.rs index 96a208045c2..fae3d088d9d 100644 --- a/core/test_network/src/lib.rs +++ b/core/test_network/src/lib.rs @@ -28,8 +28,6 @@ use tokio::{ time, }; -pub mod unique_port; - #[derive(Debug, Clone, Copy)] struct ShutdownRuntime; diff --git a/core/test_network/src/unique_port.rs b/core/test_network/src/unique_port.rs deleted file mode 100644 index 58c94e9085a..00000000000 --- a/core/test_network/src/unique_port.rs +++ /dev/null @@ -1,65 +0,0 @@ -#![allow(clippy::restriction)] - -use once_cell::sync::Lazy; -use std::net::{Ipv4Addr, SocketAddrV4, TcpListener}; -use std::ops::Range; -use std::sync::Mutex; - -static PORT_IDX: Lazy> = Lazy::new(|| Mutex::new(1000)); - -#[macro_export] -macro_rules! generate_offset { - () => {{ - use std::collections::hash_map::DefaultHasher; - use std::hash::{Hash, Hasher}; - - fn f() {} - fn type_name_of(_: T) -> &'static str { - std::any::type_name::() - } - let name = type_name_of(f); - let name = &name[..name.len() - 3]; - let mut hasher = DefaultHasher::new(); - name.hash(&mut hasher); - 1000 + (hasher.finish() % ((u16::MAX - 1000) as u64)) as u16 - }}; -} - -pub fn set_offset(offset: u16) -> Result<(), String> { - let mut port_idx = PORT_IDX - .lock() - .map_err(|_| "Failed to aquire the lock".to_owned())?; - *port_idx = offset; - - Ok(()) -} - -/// Returns a free unique local port. Every time a call to this function during one run should -/// return a unique address. -/// -/// # Examples -/// ``` -/// use unique_port::get_unique_free_port; -/// -/// let port_1 = get_unique_free_port().unwrap(); -/// let port_2 = get_unique_free_port().unwrap(); -/// assert_ne!(port_1, port_2); -/// ``` -pub fn get_unique_free_port() -> Result { - let mut port_idx = PORT_IDX - .lock() - .map_err(|_| "Failed to aquire the lock".to_owned())?; - let result = get_free_port(*port_idx..u16::MAX); - if let Ok(port) = result { - *port_idx = port + 1; - } - result -} - -/// Returns empty port from range. Can be not unique -fn get_free_port(ports: Range) -> Result { - ports - .into_iter() - .find(|port| TcpListener::bind(SocketAddrV4::new(Ipv4Addr::LOCALHOST, *port)).is_ok()) - .ok_or_else(|| "Failed to get empty port".to_owned()) -} diff --git a/data_model/Cargo.toml b/data_model/Cargo.toml index c4bc821a3d0..5574506e529 100644 --- a/data_model/Cargo.toml +++ b/data_model/Cargo.toml @@ -54,6 +54,7 @@ iroha = { path = "../cli" } hex = { version = "0.4.0", default-features = false, features = ["alloc", "serde"] } test_network = { path = "../core/test_network", version = "=2.0.0-pre-rc.5" } +unique_port = "0.2.0" tokio = { version = "1.6.0", features = ["rt", "rt-multi-thread"]} trybuild = "1.0.53" diff --git a/data_model/tests/data_model.rs b/data_model/tests/data_model.rs index 11e88da58e5..f25de48bae0 100644 --- a/data_model/tests/data_model.rs +++ b/data_model/tests/data_model.rs @@ -10,8 +10,9 @@ use iroha_core::{ }; use iroha_data_model::{prelude::*, ParseError}; use small::SmallStr; -use test_network::{generate_offset, unique_port, Peer as TestPeer, PeerBuilder, TestRuntime}; +use test_network::{Peer as TestPeer, PeerBuilder, TestRuntime}; use tokio::runtime::Runtime; +use unique_port::generate_unique_start_port; fn asset_id_new( definition_name: &str, @@ -143,7 +144,7 @@ mod register { #[allow(unused_must_use)] #[test] fn find_rate_and_make_exchange_isi_should_succeed() { - unique_port::set_offset(generate_offset!()).unwrap(); + unique_port::set_port_index(generate_unique_start_port!()); let kp = KeyPair::new( PublicKey::from_str( r#"ed01207233bfc89dcbd68c19fde6ce6158225298ec1131b6a130d1aeb454c1ab5183c0"#,