Skip to content

Commit

Permalink
Merge pull request #567 from thesimplekid/fix_mint_melt_test
Browse files Browse the repository at this point in the history
fix: add time out to wait invoice in tests
  • Loading branch information
thesimplekid authored Jan 30, 2025
2 parents afb7633 + 0c6108f commit 017f88e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 26 deletions.
2 changes: 1 addition & 1 deletion crates/cdk-integration-tests/src/init_pure_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ pub async fn fund_wallet(wallet: Arc<Wallet>, amount: u64) -> anyhow::Result<Amo
let desired_amount = Amount::from(amount);
let quote = wallet.mint_quote(desired_amount, None).await?;

wait_for_mint_to_be_paid(&wallet, &quote.id).await?;
wait_for_mint_to_be_paid(&wallet, &quote.id, 60).await?;

Ok(wallet
.mint(&quote.id, SplitTarget::default(), None)
Expand Down
28 changes: 20 additions & 8 deletions crates/cdk-integration-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use cdk::wallet::client::{HttpClient, MintConnector};
use cdk::wallet::subscription::SubscriptionManager;
use cdk::wallet::WalletSubscription;
use cdk::Wallet;
use tokio::time::{timeout, Duration};

pub mod init_fake_wallet;
pub mod init_mint;
Expand Down Expand Up @@ -153,21 +154,32 @@ pub async fn attempt_to_swap_pending(wallet: &Wallet) -> Result<()> {
Ok(())
}

// Keep polling the state of the mint quote id until it's paid
pub async fn wait_for_mint_to_be_paid(wallet: &Wallet, mint_quote_id: &str) -> Result<()> {
pub async fn wait_for_mint_to_be_paid(
wallet: &Wallet,
mint_quote_id: &str,
timeout_secs: u64,
) -> Result<()> {
let mut subscription = wallet
.subscribe(WalletSubscription::Bolt11MintQuoteState(vec![
mint_quote_id.to_owned(),
]))
.await;

while let Some(msg) = subscription.recv().await {
if let NotificationPayload::MintQuoteBolt11Response(response) = msg {
if response.state == MintQuoteState::Paid {
break;
// Create the timeout future
let wait_future = async {
while let Some(msg) = subscription.recv().await {
if let NotificationPayload::MintQuoteBolt11Response(response) = msg {
if response.state == MintQuoteState::Paid {
return Ok(());
}
}
}
}
Ok(())
};

Ok(())
// Wait for either the payment to complete or timeout
match timeout(Duration::from_secs(timeout_secs), wait_future).await {
Ok(result) => result,
Err(_) => Err(anyhow::anyhow!("Timeout waiting for mint quote to be paid")),
}
}
21 changes: 10 additions & 11 deletions crates/cdk-integration-tests/tests/fake_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ async fn test_fake_tokens_pending() -> Result<()> {

let mint_quote = wallet.mint_quote(100.into(), None).await?;

wait_for_mint_to_be_paid(&wallet, &mint_quote.id).await?;
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60).await?;

let _mint_amount = wallet
.mint(&mint_quote.id, SplitTarget::default(), None)
Expand Down Expand Up @@ -69,7 +69,7 @@ async fn test_fake_melt_payment_fail() -> Result<()> {

let mint_quote = wallet.mint_quote(100.into(), None).await?;

wait_for_mint_to_be_paid(&wallet, &mint_quote.id).await?;
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60).await?;

let _mint_amount = wallet
.mint(&mint_quote.id, SplitTarget::default(), None)
Expand Down Expand Up @@ -132,7 +132,7 @@ async fn test_fake_melt_payment_fail_and_check() -> Result<()> {

let mint_quote = wallet.mint_quote(100.into(), None).await?;

wait_for_mint_to_be_paid(&wallet, &mint_quote.id).await?;
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60).await?;

let _mint_amount = wallet
.mint(&mint_quote.id, SplitTarget::default(), None)
Expand Down Expand Up @@ -177,7 +177,7 @@ async fn test_fake_melt_payment_return_fail_status() -> Result<()> {

let mint_quote = wallet.mint_quote(100.into(), None).await?;

wait_for_mint_to_be_paid(&wallet, &mint_quote.id).await?;
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60).await?;

let _mint_amount = wallet
.mint(&mint_quote.id, SplitTarget::default(), None)
Expand Down Expand Up @@ -237,7 +237,7 @@ async fn test_fake_melt_payment_error_unknown() -> Result<()> {

let mint_quote = wallet.mint_quote(100.into(), None).await?;

wait_for_mint_to_be_paid(&wallet, &mint_quote.id).await?;
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60).await?;

let _mint_amount = wallet
.mint(&mint_quote.id, SplitTarget::default(), None)
Expand Down Expand Up @@ -298,7 +298,7 @@ async fn test_fake_melt_payment_err_paid() -> Result<()> {

let mint_quote = wallet.mint_quote(100.into(), None).await?;

wait_for_mint_to_be_paid(&wallet, &mint_quote.id).await?;
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60).await?;

let _mint_amount = wallet
.mint(&mint_quote.id, SplitTarget::default(), None)
Expand Down Expand Up @@ -336,7 +336,7 @@ async fn test_fake_melt_change_in_quote() -> Result<()> {

let mint_quote = wallet.mint_quote(100.into(), None).await?;

wait_for_mint_to_be_paid(&wallet, &mint_quote.id).await?;
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60).await?;

let _mint_amount = wallet
.mint(&mint_quote.id, SplitTarget::default(), None)
Expand Down Expand Up @@ -388,7 +388,7 @@ async fn test_fake_mint_with_witness() -> Result<()> {
)?;
let mint_quote = wallet.mint_quote(100.into(), None).await?;

wait_for_mint_to_be_paid(&wallet, &mint_quote.id).await?;
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60).await?;

let proofs = wallet
.mint(&mint_quote.id, SplitTarget::default(), None)
Expand All @@ -413,7 +413,7 @@ async fn test_fake_mint_without_witness() -> Result<()> {

let mint_quote = wallet.mint_quote(100.into(), None).await?;

wait_for_mint_to_be_paid(&wallet, &mint_quote.id).await?;
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60).await?;

let http_client = HttpClient::new(MINT_URL.parse()?);

Expand All @@ -437,7 +437,6 @@ async fn test_fake_mint_without_witness() -> Result<()> {
}
}

// TODO: Rewrite this test to include witness wrong
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn test_fake_mint_with_wrong_witness() -> Result<()> {
let wallet = Wallet::new(
Expand All @@ -450,7 +449,7 @@ async fn test_fake_mint_with_wrong_witness() -> Result<()> {

let mint_quote = wallet.mint_quote(100.into(), None).await?;

wait_for_mint_to_be_paid(&wallet, &mint_quote.id).await?;
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60).await?;

let http_client = HttpClient::new(MINT_URL.parse()?);

Expand Down
12 changes: 6 additions & 6 deletions crates/cdk-integration-tests/tests/regtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ async fn test_regtest_mint_melt_round_trip() -> Result<()> {

lnd_client.pay_invoice(mint_quote.request).await.unwrap();

wait_for_mint_to_be_paid(&wallet, &mint_quote.id).await?;
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60).await?;

let proofs = wallet
.mint(&mint_quote.id, SplitTarget::default(), None)
Expand Down Expand Up @@ -179,7 +179,7 @@ async fn test_regtest_mint_melt() -> Result<()> {

lnd_client.pay_invoice(mint_quote.request).await?;

wait_for_mint_to_be_paid(&wallet, &mint_quote.id).await?;
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60).await?;

let proofs = wallet
.mint(&mint_quote.id, SplitTarget::default(), None)
Expand Down Expand Up @@ -209,7 +209,7 @@ async fn test_restore() -> Result<()> {

lnd_client.pay_invoice(mint_quote.request).await?;

wait_for_mint_to_be_paid(&wallet, &mint_quote.id).await?;
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60).await?;

let _mint_amount = wallet
.mint(&mint_quote.id, SplitTarget::default(), None)
Expand Down Expand Up @@ -271,7 +271,7 @@ async fn test_pay_invoice_twice() -> Result<()> {
.await
.expect("Could not pay invoice");

wait_for_mint_to_be_paid(&wallet, &mint_quote.id).await?;
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60).await?;

let proofs = wallet
.mint(&mint_quote.id, SplitTarget::default(), None)
Expand Down Expand Up @@ -327,7 +327,7 @@ async fn test_internal_payment() -> Result<()> {

lnd_client.pay_invoice(mint_quote.request).await?;

wait_for_mint_to_be_paid(&wallet, &mint_quote.id).await?;
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60).await?;

let _mint_amount = wallet
.mint(&mint_quote.id, SplitTarget::default(), None)
Expand All @@ -353,7 +353,7 @@ async fn test_internal_payment() -> Result<()> {

let _melted = wallet.melt(&melt.id).await.unwrap();

wait_for_mint_to_be_paid(&wallet, &mint_quote.id).await?;
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60).await?;

let _wallet_2_mint = wallet_2
.mint(&mint_quote.id, SplitTarget::default(), None)
Expand Down

0 comments on commit 017f88e

Please sign in to comment.