Skip to content

Commit

Permalink
[fix] hyperledger-iroha#2137: Fix tests for iroha_client
Browse files Browse the repository at this point in the history
Signed-off-by: Ales Tsurko <[email protected]>
  • Loading branch information
ales-tsurko committed Jun 20, 2022
1 parent 67f8fc4 commit f5c3285
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 15 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion client/tests/integration/add_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ use std::thread;
use eyre::Result;
use iroha_client::client;
use iroha_data_model::prelude::*;
use test_network::*;
use test_network::{generate_offset, *};

#[test]
fn client_add_account_with_name_length_more_than_limit_should_not_commit_transaction() -> Result<()>
{
unique_port::set_offset(generate_offset!()).unwrap();
let (_rt, _peer, test_client) = <PeerBuilder>::new().start_with_runtime();
wait_for_genesis_committed(&vec![test_client.clone()], 0);

Expand Down
6 changes: 5 additions & 1 deletion client/tests/integration/add_asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ use std::{str::FromStr as _, thread};
use eyre::Result;
use iroha_client::client;
use iroha_data_model::{fixed::Fixed, prelude::*};
use test_network::*;
use test_network::{generate_offset, *};

use super::Configuration;

#[test]
fn client_add_asset_quantity_to_existing_asset_should_increase_asset_amount() -> Result<()> {
unique_port::set_offset(generate_offset!()).unwrap();
let (_rt, _peer, mut test_client) = <PeerBuilder>::new().start_with_runtime();
wait_for_genesis_committed(&vec![test_client.clone()], 0);

Expand Down Expand Up @@ -42,6 +43,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();
let (_rt, _peer, mut test_client) = <PeerBuilder>::new().start_with_runtime();
wait_for_genesis_committed(&vec![test_client.clone()], 0);

Expand Down Expand Up @@ -73,6 +75,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();
let (_rt, _peer, mut test_client) = <PeerBuilder>::new().start_with_runtime();

// Given
Expand Down Expand Up @@ -125,6 +128,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();
let (_rt, _peer, test_client) = <PeerBuilder>::new().start_with_runtime();
let pipeline_time = Configuration::pipeline_time();

Expand Down
3 changes: 2 additions & 1 deletion client/tests/integration/add_domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ use std::thread;
use eyre::Result;
use iroha_client::client;
use iroha_data_model::prelude::*;
use test_network::*;
use test_network::{generate_offset, *};

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();
let (_rt, _peer, test_client) = <PeerBuilder>::new().start_with_runtime();
wait_for_genesis_committed(&vec![test_client.clone()], 0);
let pipeline_time = Configuration::pipeline_time();
Expand Down
3 changes: 2 additions & 1 deletion client/tests/integration/asset_propagation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ use eyre::Result;
use iroha_client::client;
use iroha_core::prelude::*;
use iroha_data_model::prelude::*;
use test_network::*;
use test_network::{generate_offset, *};

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();
// Given
let (_rt, network, iroha_client) = <Network>::start_test_with_runtime(4, 1);
wait_for_genesis_committed(&network.clients(), 0);
Expand Down
3 changes: 2 additions & 1 deletion client/tests/integration/burn_public_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::thread;
use iroha_client::client::{account, transaction, Client};
use iroha_core::prelude::*;
use iroha_data_model::prelude::*;
use test_network::*;
use test_network::{generate_offset, *};

use super::Configuration;

Expand All @@ -27,6 +27,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();
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());
Expand Down
4 changes: 3 additions & 1 deletion client/tests/integration/connected_peers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@ use std::thread;

use iroha_client::client::Client;
use iroha_data_model::{peer::Peer as DataModelPeer, prelude::*};
use test_network::*;
use test_network::{generate_offset, *};

use super::Configuration;

#[test]
fn connected_peers_with_f_2_1_2() {
unique_port::set_offset(generate_offset!()).unwrap();
connected_peers_with_f(2)
}

#[test]
fn connected_peers_with_f_1_0_1() {
unique_port::set_offset(generate_offset!()).unwrap();
connected_peers_with_f(1)
}

Expand Down
4 changes: 3 additions & 1 deletion client/tests/integration/events/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@ use std::thread::{self, JoinHandle};

use eyre::Result;
use iroha_data_model::prelude::*;
use test_network::*;
use test_network::{generate_offset, *};

use super::Configuration;

const PEER_COUNT: usize = 7;

#[test]
fn transaction_with_no_instructions_should_be_committed() -> Result<()> {
unique_port::set_offset(generate_offset!()).unwrap();
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();
let fail = FailBox::new("Should be rejected");
test_with_instruction_and_status(Some(fail.into()), PipelineStatusKind::Rejected)
}
Expand Down
3 changes: 2 additions & 1 deletion client/tests/integration/restart_peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ use iroha_client::client;
use iroha_core::prelude::*;
use iroha_data_model::prelude::*;
use tempfile::TempDir;
use test_network::{Peer as TestPeer, *};
use test_network::{generate_offset, Peer as TestPeer, *};
use tokio::runtime::Runtime;

use super::Configuration;

#[test]
fn restarted_peer_should_have_the_same_asset_amount() -> Result<()> {
unique_port::set_offset(generate_offset!()).unwrap();
let temp_dir = Arc::new(TempDir::new()?);

let mut configuration = Configuration::test();
Expand Down
15 changes: 9 additions & 6 deletions core/test_network/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,19 @@ edition = "2021"
query = []

[dependencies]
iroha_core = { version = "=2.0.0-pre-rc.5", path = "../../core" }
iroha = { path = "../../cli", features = ["test-network"] }
iroha_actor = { version = "=2.0.0-pre-rc.5", path = "../../actor" }
iroha_client = { version = "=2.0.0-pre-rc.5", path = "../../client" }
iroha_core = { version = "=2.0.0-pre-rc.5", path = "../../core" }
iroha_data_model = { version = "=2.0.0-pre-rc.5", path = "../../data_model" }
iroha_logger = { version = "=2.0.0-pre-rc.5", path = "../../logger" }
iroha_actor = { version = "=2.0.0-pre-rc.5", path = "../../actor" }
iroha = { path = "../../cli", features = ["test-network"] }

eyre = "0.6.5"
futures = { version = "0.3.17", default-features = false, features = ["std", "async-await"] }
once_cell = "1.8.0"
rand = "0.8"
tempfile = "3"
unique_port = "0.1.0"
tokio = { version = "1.6.0", features = ["rt", "rt-multi-thread", "macros"]}
rand = "0.8"
futures = { version = "0.3.17", default-features = false, features = ["std", "async-await"] }

[dev-dependencies]
async-trait = "0.1"
2 changes: 2 additions & 0 deletions core/test_network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ use tokio::{
time,
};

pub mod unique_port;

#[derive(Debug, Clone, Copy)]
struct ShutdownRuntime;

Expand Down
65 changes: 65 additions & 0 deletions core/test_network/src/unique_port.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#![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<Mutex<u16>> = 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>(_: T) -> &'static str {
std::any::type_name::<T>()
}
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<u16, String> {
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<u16>) -> Result<u16, String> {
ports
.into_iter()
.find(|port| TcpListener::bind(SocketAddrV4::new(Ipv4Addr::LOCALHOST, *port)).is_ok())
.ok_or_else(|| "Failed to get empty port".to_owned())
}

0 comments on commit f5c3285

Please sign in to comment.