|
26 | 26 | //! Here is an example of how to create an asynchronous client. |
27 | 27 | //! |
28 | 28 | //! ```no_run |
29 | | -//! # #[cfg(feature = "async")] |
| 29 | +//! # #[cfg(all(feature = "async", feature = "tokio"))] |
30 | 30 | //! # { |
31 | 31 | //! use esplora_client::Builder; |
32 | 32 | //! let builder = Builder::new("https://blockstream.info/testnet/api"); |
@@ -178,11 +178,17 @@ impl Builder { |
178 | 178 | BlockingClient::from_builder(self) |
179 | 179 | } |
180 | 180 |
|
181 | | - // Build an asynchronous client from builder |
182 | | - #[cfg(feature = "async")] |
| 181 | + /// Build an asynchronous client from builder |
| 182 | + #[cfg(all(feature = "async", feature = "tokio"))] |
183 | 183 | pub fn build_async(self) -> Result<AsyncClient, Error> { |
184 | 184 | AsyncClient::from_builder(self) |
185 | 185 | } |
| 186 | + |
| 187 | + /// Build an asynchronous client from builder |
| 188 | + #[cfg(feature = "async")] |
| 189 | + pub fn build_async_with_sleeper<S: r#async::Sleeper>(self) -> Result<AsyncClient<S>, Error> { |
| 190 | + AsyncClient::from_builder(self) |
| 191 | + } |
186 | 192 | } |
187 | 193 |
|
188 | 194 | /// Errors that can happen during a request to `Esplora` servers. |
@@ -253,6 +259,8 @@ mod test { |
253 | 259 | use electrsd::{bitcoind, bitcoind::BitcoinD, ElectrsD}; |
254 | 260 | use lazy_static::lazy_static; |
255 | 261 | use std::env; |
| 262 | + #[cfg(all(feature = "async", not(feature = "tokio")))] |
| 263 | + use std::{future::Future, pin::Pin}; |
256 | 264 | use tokio::sync::Mutex; |
257 | 265 | #[cfg(all(feature = "blocking", feature = "async"))] |
258 | 266 | use { |
@@ -320,8 +328,15 @@ mod test { |
320 | 328 | let blocking_client = builder.build_blocking(); |
321 | 329 |
|
322 | 330 | let builder_async = Builder::new(&format!("http://{}", esplora_url)); |
| 331 | + |
| 332 | + #[cfg(feature = "tokio")] |
323 | 333 | let async_client = builder_async.build_async().unwrap(); |
324 | 334 |
|
| 335 | + #[cfg(not(feature = "tokio"))] |
| 336 | + let async_client = builder_async |
| 337 | + .build_async_with_sleeper::<r#async::DefaultSleeper>() |
| 338 | + .unwrap(); |
| 339 | + |
325 | 340 | (blocking_client, async_client) |
326 | 341 | } |
327 | 342 |
|
@@ -992,4 +1007,31 @@ mod test { |
992 | 1007 | let tx_async = async_client.get_tx(&txid).await.unwrap(); |
993 | 1008 | assert_eq!(tx, tx_async); |
994 | 1009 | } |
| 1010 | + |
| 1011 | + #[cfg(all(feature = "async", feature = "tokio"))] |
| 1012 | + #[test] |
| 1013 | + fn test_default_tokio_sleeper() { |
| 1014 | + let builder = Builder::new("https://blockstream.info/testnet/api"); |
| 1015 | + let client = builder.build_async(); |
| 1016 | + assert!(client.is_ok()); |
| 1017 | + } |
| 1018 | + #[cfg(all(feature = "async", not(feature = "tokio")))] |
| 1019 | + struct CustomRuntime; |
| 1020 | + |
| 1021 | + #[cfg(all(feature = "async", not(feature = "tokio")))] |
| 1022 | + impl r#async::Sleeper for CustomRuntime { |
| 1023 | + type Sleep = Pin<Box<dyn Future<Output = ()>>>; |
| 1024 | + |
| 1025 | + fn sleep(dur: std::time::Duration) -> Self::Sleep { |
| 1026 | + Box::pin(async_std::task::sleep(dur)) |
| 1027 | + } |
| 1028 | + } |
| 1029 | + |
| 1030 | + #[cfg(all(feature = "async", not(feature = "tokio")))] |
| 1031 | + #[test] |
| 1032 | + fn test_custom_runtime() { |
| 1033 | + let builder = Builder::new("https://blockstream.info/testnet/api"); |
| 1034 | + let client = builder.build_async_with_sleeper::<CustomRuntime>(); |
| 1035 | + assert!(client.is_ok()); |
| 1036 | + } |
995 | 1037 | } |
0 commit comments