Skip to content

Commit

Permalink
Merge pull request #4 from DeFiCh/master
Browse files Browse the repository at this point in the history
Fix unit test post async
  • Loading branch information
Jouzo authored May 15, 2024
2 parents 41f6064 + 79743da commit df03127
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 26 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ members = [
"client",
"integration_test",
]

[workspace.dependencies]
tokio = { version = "1", features = ["full"] }
2 changes: 1 addition & 1 deletion client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ serde_json = "1"

[dev-dependencies]
tempfile = "3.3.0"

tokio.workspace = true
10 changes: 6 additions & 4 deletions client/examples/retry_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ extern crate jsonrpc_async;
extern crate serde;
extern crate serde_json;

use async_trait::async_trait;
use defichain_rpc::{Client, Error, Result, RpcApi};

pub struct RetryClient {
Expand All @@ -22,16 +23,17 @@ pub struct RetryClient {
const INTERVAL: u64 = 1000;
const RETRY_ATTEMPTS: u8 = 10;

#[async_trait]
impl RpcApi for RetryClient {
fn call<T: for<'a> serde::de::Deserialize<'a>>(
async fn call<T: for<'a> serde::de::Deserialize<'a>>(
&self,
cmd: &str,
args: &[serde_json::Value],
) -> Result<T> {
for _ in 0..RETRY_ATTEMPTS {
match self.client.call(cmd, args) {
match self.client.call(cmd, args).await {
Ok(ret) => return Ok(ret),
Err(Error::JsonRpc(jsonrpc::error::Error::Rpc(ref rpcerr)))
Err(Error::JsonRpc(jsonrpc_async::error::Error::Rpc(ref rpcerr)))
if rpcerr.code == -28 =>
{
::std::thread::sleep(::std::time::Duration::from_millis(INTERVAL));
Expand All @@ -40,7 +42,7 @@ impl RpcApi for RetryClient {
Err(e) => return Err(e),
}
}
self.client.call(cmd, args)
self.client.call(cmd, args).await
}
}

Expand Down
21 changes: 11 additions & 10 deletions client/examples/test_against_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ extern crate defichain_rpc;

use defichain_rpc::{bitcoin, Auth, Client, Error, RpcApi};

fn main_result() -> Result<(), Error> {
async fn main_result() -> Result<(), Error> {
let mut args = std::env::args();

let _exe_name = args.next().unwrap();
Expand All @@ -23,26 +23,27 @@ fn main_result() -> Result<(), Error> {
let user = args.next().expect("no user given");
let pass = args.next().expect("no pass given");

let rpc = Client::new(&url, Auth::UserPass(user, pass)).unwrap();
let rpc = Client::new(&url, Auth::UserPass(user, pass)).await.unwrap();

let _blockchain_info = rpc.get_blockchain_info()?;
let _blockchain_info = rpc.get_blockchain_info().await?;

let best_block_hash = rpc.get_best_block_hash()?;
let best_block_hash = rpc.get_best_block_hash().await?;
println!("best block hash: {}", best_block_hash);
let bestblockcount = rpc.get_block_count()?;
let bestblockcount = rpc.get_block_count().await?;
println!("best block height: {}", bestblockcount);
let best_block_hash_by_height = rpc.get_block_hash(bestblockcount)?;
let best_block_hash_by_height = rpc.get_block_hash(bestblockcount).await?;
println!("best block hash by height: {}", best_block_hash_by_height);
assert_eq!(best_block_hash_by_height, best_block_hash);

let bitcoin_block: bitcoin::Block = rpc.get_by_id(&best_block_hash)?;
let bitcoin_block: bitcoin::Block = rpc.get_by_id(&best_block_hash).await?;
println!("best block hash by `get`: {}", bitcoin_block.header.prev_blockhash);
let bitcoin_tx: bitcoin::Transaction = rpc.get_by_id(&bitcoin_block.txdata[0].txid())?;
let bitcoin_tx: bitcoin::Transaction = rpc.get_by_id(&bitcoin_block.txdata[0].txid()).await?;
println!("tx by `get`: {}", bitcoin_tx.txid());

Ok(())
}

fn main() {
main_result().unwrap();
#[tokio::main]
async fn main() {
main_result().await.unwrap();
}
22 changes: 11 additions & 11 deletions client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1410,16 +1410,16 @@ mod tests {
use crate::bitcoin;
use serde_json;

#[test]
#[tokio::test]
async fn test_raw_tx() {
use crate::bitcoin::consensus::encode;
let client = Client::new("http://localhost/".into(), Auth::None).unwrap();
let client = Client::new("http://localhost/".into(), Auth::None).await.unwrap();
let tx: bitcoin::Transaction = encode::deserialize(&Vec::<u8>::from_hex("0200000001586bd02815cf5faabfec986a4e50d25dbee089bd2758621e61c5fab06c334af0000000006b483045022100e85425f6d7c589972ee061413bcf08dc8c8e589ce37b217535a42af924f0e4d602205c9ba9cb14ef15513c9d946fa1c4b797883e748e8c32171bdf6166583946e35c012103dae30a4d7870cd87b45dd53e6012f71318fdd059c1c2623b8cc73f8af287bb2dfeffffff021dc4260c010000001976a914f602e88b2b5901d8aab15ebe4a97cf92ec6e03b388ac00e1f505000000001976a914687ffeffe8cf4e4c038da46a9b1d37db385a472d88acfd211500").unwrap()).unwrap();

assert!(client.send_raw_transaction(&tx).is_err());
assert!(client.send_raw_transaction(&encode::serialize(&tx)).is_err());
assert!(client.send_raw_transaction("deadbeef").is_err());
assert!(client.send_raw_transaction("deadbeef".to_owned()).is_err());
assert!(client.send_raw_transaction(&tx).await.is_err());
assert!(client.send_raw_transaction(&encode::serialize(&tx)).await.is_err());
assert!(client.send_raw_transaction("deadbeef").await.is_err());
assert!(client.send_raw_transaction("deadbeef".to_owned()).await.is_err());
}

async fn test_handle_defaults_inner() -> Result<()> {
Expand Down Expand Up @@ -1474,12 +1474,12 @@ mod tests {
Ok(())
}

#[test]
#[tokio::test]
async fn test_handle_defaults() {
test_handle_defaults_inner().unwrap();
test_handle_defaults_inner().await.unwrap();
}

#[test]
#[tokio::test]
async fn auth_cookie_file_ignores_newline() {
let tempdir = tempfile::tempdir().unwrap();
let path = tempdir.path().join("cookie");
Expand All @@ -1490,7 +1490,7 @@ mod tests {
);
}

#[test]
#[tokio::test]
async fn auth_cookie_file_ignores_additional_lines() {
let tempdir = tempfile::tempdir().unwrap();
let path = tempdir.path().join("cookie");
Expand All @@ -1501,7 +1501,7 @@ mod tests {
);
}

#[test]
#[tokio::test]
async fn auth_cookie_file_fails_if_colon_isnt_present() {
let tempdir = tempfile::tempdir().unwrap();
let path = tempdir.path().join("cookie");
Expand Down

0 comments on commit df03127

Please sign in to comment.