Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add listen timeouts to iroha cli #5241

Merged
merged 20 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions crates/iroha_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,6 @@ futures = { workspace = true }
[build-dependencies]
vergen = { version = "8.3.1", default-features = false }
color-eyre = "0.6.3"

[dev-dependencies]
iroha_test_network = { workspace = true }
aoyako marked this conversation as resolved.
Show resolved Hide resolved
85 changes: 83 additions & 2 deletions crates/iroha_cli/src/main.rs
aoyako marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ mod events {
}
}

fn listen(
pub fn listen(
filter: impl Into<EventFilterBox>,
context: &mut dyn RunContext,
timeout: Option<Duration>,
Expand Down Expand Up @@ -417,7 +417,7 @@ mod blocks {
}
}

fn listen(
pub fn listen(
height: NonZeroU64,
context: &mut dyn RunContext,
timeout: Option<Duration>,
Expand Down Expand Up @@ -1451,8 +1451,13 @@ mod multisig {
Ok(())
}
}

#[cfg(test)]
mod tests {
use iroha::crypto::KeyPair;
use iroha_test_network::*;
aoyako marked this conversation as resolved.
Show resolved Hide resolved
use serde_json::to_string;

use super::*;

#[test]
Expand All @@ -1477,4 +1482,80 @@ mod tests {
let json_str = r#"{"Vec":[{"String":"a"},{"String":"b"}]}"#;
case!(json_str, serde_json::from_str(json_str).unwrap());
}

struct MockContext {
network: Network,
config: Config,
datastream: String,
}

impl MockContext {
fn test_config() -> Config {
return Config{
chain: ChainId::from("00000000-0000-0000-0000-000000000000"),
account: "ed0120CE7FA46C9DCE7EA4B125E2E36BDB63EA33073E7590AC92816AE1E861B7048B03@wonderland".parse().expect("Can't parse mock account"),
key_pair: KeyPair::random(),
basic_auth: None,
torii_api_url: "http://127.0.0.1:8080/".parse().expect("Can't parse mock url"),
transaction_ttl: Duration::from_millis(100_000),
transaction_status_timeout: Duration::from_millis(100_000),
transaction_add_nonce: false,
};
}
}

impl RunContext for MockContext {
fn configuration(&self) -> &Config {
return &self.config;
}

fn client_from_config(&self) -> Client {
self.network.client()
}

fn print_data(&mut self, data: &dyn Serialize) -> Result<()> {
self.datastream.push_str(&to_string(data)?);
Ok(())
}
}

#[test]
fn listen_events_timeouts() {
let (network, _rt) = NetworkBuilder::new()
.start_blocking()
.expect("Failed to start network.");
let mut tc = MockContext {
network,
config: MockContext::test_config(),
datastream: String::new(),
};

assert!(events::listen(
ExecuteTriggerEventFilter::new(),
&mut tc,
Some(Duration::from_secs(1))
)
.is_ok());
}

#[test]
fn listen_blocks_timeouts() {
use std::num::NonZeroU64;

let (network, _rt) = NetworkBuilder::new()
.start_blocking()
.expect("Failed to start network.");
let mut tc = MockContext {
network,
config: MockContext::test_config(),
datastream: String::new(),
};

assert!(blocks::listen(
NonZeroU64::new(1).expect("Blocks cannot be zero"),
&mut tc,
Some(Duration::from_secs(1))
)
.is_ok());
}
}
Loading