Skip to content

Commit 5c324f2

Browse files
authored
chore(tests): don't use .wait() and use block_on instead (#2220)
Fixes an issue regarding a tokio tcp stream that breaks because it was spawned in a non-tokio runtime. the `.wait` methods were using a different runtime - from futures - to run the future. This commit uses `block_on(fut.compat())` from KDF's tokio runtime instead.
1 parent 4fc8388 commit 5c324f2

File tree

18 files changed

+1446
-1651
lines changed

18 files changed

+1446
-1651
lines changed

clippy.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[[disallowed-methods]]
2+
path = "futures::future::Future::wait"
3+
replacement = "common::block_on_f01"
4+
reason = "Use the default KDF async executor."

mm2src/coins/eth/eth_tests.rs

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::*;
22
use crate::IguanaPrivKey;
3-
use common::block_on;
3+
use common::{block_on, block_on_f01};
44
use mm2_core::mm_ctx::MmCtxBuilder;
55

66
cfg_native!(
@@ -191,18 +191,16 @@ fn test_wait_for_payment_spend_timeout() {
191191
184, 42, 106,
192192
];
193193

194-
assert!(coin
195-
.wait_for_htlc_tx_spend(WaitForHTLCTxSpendArgs {
196-
tx_bytes: &tx_bytes,
197-
secret_hash: &[],
198-
wait_until,
199-
from_block,
200-
swap_contract_address: &coin.swap_contract_address(),
201-
check_every: TAKER_PAYMENT_SPEND_SEARCH_INTERVAL,
202-
watcher_reward: false
203-
})
204-
.wait()
205-
.is_err());
194+
assert!(block_on_f01(coin.wait_for_htlc_tx_spend(WaitForHTLCTxSpendArgs {
195+
tx_bytes: &tx_bytes,
196+
secret_hash: &[],
197+
wait_until,
198+
from_block,
199+
swap_contract_address: &coin.swap_contract_address(),
200+
check_every: TAKER_PAYMENT_SPEND_SEARCH_INTERVAL,
201+
watcher_reward: false
202+
}))
203+
.is_err());
206204
}
207205

208206
#[cfg(not(target_arch = "wasm32"))]
@@ -229,7 +227,7 @@ fn test_withdraw_impl_manual_fee() {
229227
memo: None,
230228
ibc_source_channel: None,
231229
};
232-
coin.get_balance().wait().unwrap();
230+
block_on_f01(coin.get_balance()).unwrap();
233231

234232
let tx_details = block_on(withdraw_impl(coin, withdraw_req)).unwrap();
235233
let expected = Some(
@@ -278,7 +276,7 @@ fn test_withdraw_impl_fee_details() {
278276
memo: None,
279277
ibc_source_channel: None,
280278
};
281-
coin.get_balance().wait().unwrap();
279+
block_on_f01(coin.get_balance()).unwrap();
282280

283281
let tx_details = block_on(withdraw_impl(coin, withdraw_req)).unwrap();
284282
let expected = Some(
@@ -486,10 +484,8 @@ fn get_receiver_trade_preimage() {
486484
paid_from_trading_vol: false,
487485
};
488486

489-
let actual = coin
490-
.get_receiver_trade_fee(FeeApproxStage::WithoutApprox)
491-
.wait()
492-
.expect("!get_sender_trade_fee");
487+
let actual =
488+
block_on_f01(coin.get_receiver_trade_fee(FeeApproxStage::WithoutApprox)).expect("!get_sender_trade_fee");
493489
assert_eq!(actual, expected_fee);
494490
}
495491

@@ -595,7 +591,9 @@ fn validate_dex_fee_invalid_sender_eth() {
595591
min_block_number: 0,
596592
uuid: &[],
597593
};
598-
let error = coin.validate_fee(validate_fee_args).wait().unwrap_err().into_inner();
594+
let error = block_on_f01(coin.validate_fee(validate_fee_args))
595+
.unwrap_err()
596+
.into_inner();
599597
match error {
600598
ValidatePaymentError::WrongPaymentTx(err) => assert!(err.contains("was sent from wrong address")),
601599
_ => panic!("Expected `WrongPaymentTx` wrong sender address, found {:?}", error),
@@ -631,7 +629,9 @@ fn validate_dex_fee_invalid_sender_erc() {
631629
min_block_number: 0,
632630
uuid: &[],
633631
};
634-
let error = coin.validate_fee(validate_fee_args).wait().unwrap_err().into_inner();
632+
let error = block_on_f01(coin.validate_fee(validate_fee_args))
633+
.unwrap_err()
634+
.into_inner();
635635
match error {
636636
ValidatePaymentError::WrongPaymentTx(err) => assert!(err.contains("was sent from wrong address")),
637637
_ => panic!("Expected `WrongPaymentTx` wrong sender address, found {:?}", error),
@@ -671,7 +671,9 @@ fn validate_dex_fee_eth_confirmed_before_min_block() {
671671
min_block_number: 11784793,
672672
uuid: &[],
673673
};
674-
let error = coin.validate_fee(validate_fee_args).wait().unwrap_err().into_inner();
674+
let error = block_on_f01(coin.validate_fee(validate_fee_args))
675+
.unwrap_err()
676+
.into_inner();
675677
match error {
676678
ValidatePaymentError::WrongPaymentTx(err) => assert!(err.contains("confirmed before min_block")),
677679
_ => panic!("Expected `WrongPaymentTx` early confirmation, found {:?}", error),
@@ -710,7 +712,9 @@ fn validate_dex_fee_erc_confirmed_before_min_block() {
710712
min_block_number: 11823975,
711713
uuid: &[],
712714
};
713-
let error = coin.validate_fee(validate_fee_args).wait().unwrap_err().into_inner();
715+
let error = block_on_f01(coin.validate_fee(validate_fee_args))
716+
.unwrap_err()
717+
.into_inner();
714718
match error {
715719
ValidatePaymentError::WrongPaymentTx(err) => assert!(err.contains("confirmed before min_block")),
716720
_ => panic!("Expected `WrongPaymentTx` early confirmation, found {:?}", error),
@@ -834,9 +838,7 @@ fn polygon_check_if_my_payment_sent() {
834838
amount: &BigDecimal::default(),
835839
payment_instructions: &None,
836840
};
837-
let my_payment = coin
838-
.check_if_my_payment_sent(if_my_payment_sent_args)
839-
.wait()
841+
let my_payment = block_on_f01(coin.check_if_my_payment_sent(if_my_payment_sent_args))
840842
.unwrap()
841843
.unwrap();
842844
let expected_hash = BytesJson::from("69a20008cea0c15ee483b5bbdff942752634aa072dfd2ff715fe87eec302de11");

mm2src/coins/lightning/ln_platform.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use bitcoin::hash_types::{BlockHash, TxMerkleNode, Txid};
1515
use bitcoin_hashes::{sha256d, Hash};
1616
use common::executor::{abortable_queue::AbortableQueue, AbortableSystem, SpawnFuture, Timer};
1717
use common::log::{debug, error, info};
18-
use common::wait_until_sec;
18+
use common::{block_on_f01, wait_until_sec};
1919
use futures::compat::Future01CompatExt;
2020
use futures::future::join_all;
2121
use keys::hash::H256;
@@ -570,17 +570,15 @@ impl FeeEstimator for Platform {
570570
ConfirmationTarget::HighPriority => self.confirmations_targets.high_priority,
571571
};
572572
let fee_per_kb = tokio::task::block_in_place(move || {
573-
self.rpc_client()
574-
.estimate_fee_sat(
575-
platform_coin.decimals(),
576-
// Todo: when implementing Native client detect_fee_method should be used for Native and
577-
// EstimateFeeMethod::Standard for Electrum
578-
&EstimateFeeMethod::Standard,
579-
&conf.estimate_fee_mode,
580-
n_blocks,
581-
)
582-
.wait()
583-
.unwrap_or(latest_fees)
573+
block_on_f01(self.rpc_client().estimate_fee_sat(
574+
platform_coin.decimals(),
575+
// Todo: when implementing Native client detect_fee_method should be used for Native and
576+
// EstimateFeeMethod::Standard for Electrum
577+
&EstimateFeeMethod::Standard,
578+
&conf.estimate_fee_mode,
579+
n_blocks,
580+
))
581+
.unwrap_or(latest_fees)
584582
});
585583

586584
// Set default fee to last known fee for the corresponding confirmation target

0 commit comments

Comments
 (0)