Fix reqwest blockchain test

- add back await_or_block! to bdk-macros
- use await_or_block! in reqwest tests
This commit is contained in:
rajarshimaitra 2021-08-31 14:26:52 +05:30
parent 2459740f72
commit a41a0030dc
No known key found for this signature in database
GPG Key ID: 558ACE7DBB4377C8
2 changed files with 33 additions and 10 deletions

View File

@ -121,3 +121,26 @@ pub fn maybe_await(expr: TokenStream) -> TokenStream {
quoted.into() quoted.into()
} }
/// Awaits if target_arch is "wasm32", uses `tokio::Runtime::block_on()` otherwise
///
/// Requires the `tokio` crate as a dependecy with `rt-core` or `rt-threaded` to build on non-wasm32 platforms.
#[proc_macro]
pub fn await_or_block(expr: TokenStream) -> TokenStream {
let expr: proc_macro2::TokenStream = expr.into();
let quoted = quote! {
{
#[cfg(all(not(target_arch = "wasm32"), not(feature = "async-interface")))]
{
tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(#expr)
}
#[cfg(any(target_arch = "wasm32", feature = "async-interface"))]
{
#expr.await
}
}
};
quoted.into()
}

View File

@ -106,19 +106,19 @@ impl Blockchain for EsploraBlockchain {
} }
fn get_tx(&self, txid: &Txid) -> Result<Option<Transaction>, Error> { fn get_tx(&self, txid: &Txid) -> Result<Option<Transaction>, Error> {
Ok(self.url_client._get_tx(txid).await?) Ok(await_or_block!(self.url_client._get_tx(txid))?)
} }
fn broadcast(&self, tx: &Transaction) -> Result<(), Error> { fn broadcast(&self, tx: &Transaction) -> Result<(), Error> {
Ok(self.url_client._broadcast(tx).await?) Ok(await_or_block!(self.url_client._broadcast(tx))?)
} }
fn get_height(&self) -> Result<u32, Error> { fn get_height(&self) -> Result<u32, Error> {
Ok(self.url_client._get_height().await?) Ok(await_or_block!(self.url_client._get_height())?)
} }
fn estimate_fee(&self, target: usize) -> Result<FeeRate, Error> { fn estimate_fee(&self, target: usize) -> Result<FeeRate, Error> {
let estimates = self.url_client._get_fee_estimates().await?; let estimates = await_or_block!(self.url_client._get_fee_estimates())?;
super::into_fee_rate(target, estimates) super::into_fee_rate(target, estimates)
} }
} }
@ -287,10 +287,10 @@ impl ElectrumLikeSync for UrlClient {
for script in chunk { for script in chunk {
futs.push(self._script_get_history(script)); futs.push(self._script_get_history(script));
} }
let partial_results: Vec<Vec<ElsGetHistoryRes>> = futs.try_collect().await?; let partial_results: Vec<Vec<ElsGetHistoryRes>> = await_or_block!(futs.try_collect())?;
results.extend(partial_results); results.extend(partial_results);
} }
Ok(stream::iter(results).collect().await) Ok(await_or_block!(stream::iter(results).collect()))
} }
fn els_batch_transaction_get<'s, I: IntoIterator<Item = &'s Txid>>( fn els_batch_transaction_get<'s, I: IntoIterator<Item = &'s Txid>>(
@ -303,10 +303,10 @@ impl ElectrumLikeSync for UrlClient {
for txid in chunk { for txid in chunk {
futs.push(self._get_tx_no_opt(txid)); futs.push(self._get_tx_no_opt(txid));
} }
let partial_results: Vec<Transaction> = futs.try_collect().await?; let partial_results: Vec<Transaction> = await_or_block!(futs.try_collect())?;
results.extend(partial_results); results.extend(partial_results);
} }
Ok(stream::iter(results).collect().await) Ok(await_or_block!(stream::iter(results).collect()))
} }
fn els_batch_block_header<I: IntoIterator<Item = u32>>( fn els_batch_block_header<I: IntoIterator<Item = u32>>(
@ -319,10 +319,10 @@ impl ElectrumLikeSync for UrlClient {
for height in chunk { for height in chunk {
futs.push(self._get_header(height)); futs.push(self._get_header(height));
} }
let partial_results: Vec<BlockHeader> = futs.try_collect().await?; let partial_results: Vec<BlockHeader> = await_or_block!(futs.try_collect())?;
results.extend(partial_results); results.extend(partial_results);
} }
Ok(stream::iter(results).collect().await) Ok(await_or_block!(stream::iter(results).collect()))
} }
} }