Skip to content

Commit 5278a2a

Browse files
committed
Add tests and make sure it works with a custom runtime
1 parent 6ae51f8 commit 5278a2a

File tree

3 files changed

+41
-7
lines changed

3 files changed

+41
-7
lines changed

src/async.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use crate::{
3434
};
3535

3636
#[derive(Debug, Clone)]
37-
pub struct AsyncClient<R: Runtime = DefaultRuntime> {
37+
pub struct AsyncClient<R = DefaultRuntime> {
3838
/// The URL of the Esplora Server.
3939
url: String,
4040
/// The inner [`reqwest::Client`] to make HTTP requests.

src/lib.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ pub fn convert_fee_rate(target: usize, estimates: HashMap<u16, f64>) -> Option<f
114114
}
115115

116116
#[derive(Debug, Clone)]
117-
pub struct Builder<R: Runtime = DefaultRuntime> {
117+
pub struct Builder<R = DefaultRuntime> {
118118
/// The URL of the Esplora server.
119119
pub base_url: String,
120120
/// Optional URL of the proxy to use to make requests to the Esplora server
@@ -1016,4 +1016,42 @@ mod test {
10161016
let tx_async = async_client.get_tx(&txid).await.unwrap();
10171017
assert_eq!(tx, tx_async);
10181018
}
1019+
1020+
#[cfg(feature = "tokio")]
1021+
#[test]
1022+
fn use_builder_with_tokio_as_normal() {
1023+
let builder = Builder::new("https://blockstream.info/testnet/api");
1024+
let client = builder.build_async();
1025+
assert!(client.is_ok());
1026+
}
1027+
1028+
#[cfg(feature = "async-std")]
1029+
#[test]
1030+
fn use_async_std_runtime() {
1031+
let builder = Builder::new("https://blockstream.info/testnet/api");
1032+
let client = builder.build_async();
1033+
assert!(client.is_ok());
1034+
}
1035+
1036+
#[cfg(not(feature = "tokio"))]
1037+
struct TestRuntime;
1038+
1039+
#[cfg(not(feature = "tokio"))]
1040+
impl Runtime for TestRuntime {
1041+
async fn sleep(duration: Duration) {
1042+
tokio::time::sleep(duration).await;
1043+
}
1044+
}
1045+
1046+
#[cfg(not(feature = "tokio"))]
1047+
#[test]
1048+
fn use_with_custom_runtime() {
1049+
let builder = Builder::<TestRuntime>::new_with_runtime(
1050+
"https://blockstream.info/testnet/api",
1051+
TestRuntime,
1052+
);
1053+
1054+
let client = builder.build_async();
1055+
assert!(client.is_ok());
1056+
}
10191057
}

src/runtime.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#[cfg(feature = "tokio")]
21
use std::time::Duration;
32

43
pub trait Runtime {
@@ -15,10 +14,7 @@ impl Runtime for DefaultRuntime {
1514
}
1615

1716
#[cfg(feature = "async-std")]
18-
pub struct AsyncStd;
19-
20-
#[cfg(feature = "async-std")]
21-
impl Runtime for AsyncStd {
17+
impl Runtime for DefaultRuntime {
2218
async fn sleep(duration: Duration) {
2319
async_std::task::sleep(duration).await;
2420
}

0 commit comments

Comments
 (0)