Skip to content

Commit

Permalink
Add tests and make sure it works with a custom runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
praveenperera committed Oct 15, 2024
1 parent f74ff21 commit 32eb107
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use crate::{
};

#[derive(Debug, Clone)]
pub struct AsyncClient<R: Runtime = DefaultRuntime> {
pub struct AsyncClient<R = DefaultRuntime> {
/// The URL of the Esplora Server.
url: String,
/// The inner [`reqwest::Client`] to make HTTP requests.
Expand Down Expand Up @@ -434,7 +434,7 @@ where

/// Sends a GET request to the given `url`, retrying failed attempts
/// for retryable error codes until max retries hit.
async fn get_with_retry(&self, url: &str) -> Result<Response, Error> {
pub async fn get_with_retry(&self, url: &str) -> Result<Response, Error> {
let mut delay = BASE_BACKOFF_MILLIS;
let mut attempts = 0;

Expand Down
40 changes: 39 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ pub fn convert_fee_rate(target: usize, estimates: HashMap<u16, f64>) -> Option<f
}

#[derive(Debug, Clone)]
pub struct Builder<R: Runtime = DefaultRuntime> {
pub struct Builder<R = DefaultRuntime> {
/// The URL of the Esplora server.
pub base_url: String,
/// Optional URL of the proxy to use to make requests to the Esplora server
Expand Down Expand Up @@ -1016,4 +1016,42 @@ mod test {
let tx_async = async_client.get_tx(&txid).await.unwrap();
assert_eq!(tx, tx_async);
}

#[cfg(feature = "tokio")]
#[test]
fn use_builder_with_tokio_as_normal() {
let builder = Builder::new("https://blockstream.info/testnet/api");
let client = builder.build_async();
assert!(client.is_ok());
}

#[cfg(feature = "async-std")]
#[test]
fn use_async_std_runtime() {
let builder = Builder::new("https://blockstream.info/testnet/api");
let client = builder.build_async();
assert!(client.is_ok());
}

#[cfg(not(feature = "tokio"))]
struct TestRuntime;

#[cfg(not(feature = "tokio"))]
impl Runtime for TestRuntime {
async fn sleep(duration: Duration) {
tokio::time::sleep(duration).await;
}
}

#[cfg(not(feature = "tokio"))]
#[test]
fn use_with_custom_runtime() {
let builder = Builder::<TestRuntime>::new_with_runtime(
"https://blockstream.info/testnet/api",
TestRuntime,
);

let client = builder.build_async();
assert!(client.is_ok());
}
}
6 changes: 1 addition & 5 deletions src/runtime.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#[cfg(feature = "tokio")]
use std::time::Duration;

pub trait Runtime {
Expand All @@ -15,10 +14,7 @@ impl Runtime for DefaultRuntime {
}

#[cfg(feature = "async-std")]
pub struct AsyncStd;

#[cfg(feature = "async-std")]
impl Runtime for AsyncStd {
impl Runtime for DefaultRuntime {
async fn sleep(duration: Duration) {
async_std::task::sleep(duration).await;
}
Expand Down

0 comments on commit 32eb107

Please sign in to comment.