Move run_until is_target_state comparison functions into testutils

This commit is contained in:
Daniel Karzel 2021-01-21 19:37:52 +11:00
parent 3593f5323a
commit 33a9057b1f
10 changed files with 55 additions and 53 deletions

View File

@ -49,20 +49,6 @@ pub fn is_complete(state: &AliceState) -> bool {
) )
} }
pub fn is_xmr_locked(state: &AliceState) -> bool {
matches!(
state,
AliceState::XmrLocked{..}
)
}
pub fn is_encsig_learned(state: &AliceState) -> bool {
matches!(
state,
AliceState::EncSigLearned{..}
)
}
pub async fn run(swap: alice::Swap) -> Result<AliceState> { pub async fn run(swap: alice::Swap) -> Result<AliceState> {
run_until(swap, is_complete).await run_until(swap, is_complete).await
} }

View File

@ -24,22 +24,6 @@ pub fn is_complete(state: &BobState) -> bool {
) )
} }
pub fn is_btc_locked(state: &BobState) -> bool {
matches!(state, BobState::BtcLocked(..))
}
pub fn is_lock_proof_received(state: &BobState) -> bool {
matches!(state, BobState::XmrLockProofReceived { .. })
}
pub fn is_xmr_locked(state: &BobState) -> bool {
matches!(state, BobState::XmrLocked(..))
}
pub fn is_encsig_sent(state: &BobState) -> bool {
matches!(state, BobState::EncSigSent(..))
}
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
pub async fn run(swap: bob::Swap) -> Result<BobState> { pub async fn run(swap: bob::Swap) -> Result<BobState> {
run_until(swap, is_complete).await run_until(swap, is_complete).await

View File

@ -1,6 +1,7 @@
pub mod testutils; pub mod testutils;
use swap::protocol::{alice, alice::AliceState, bob}; use swap::protocol::{alice, alice::AliceState, bob};
use testutils::alice_run_until::is_encsig_learned;
#[tokio::test] #[tokio::test]
async fn given_alice_restarts_after_encsig_is_learned_resume_swap() { async fn given_alice_restarts_after_encsig_is_learned_resume_swap() {
@ -11,7 +12,7 @@ async fn given_alice_restarts_after_encsig_is_learned_resume_swap() {
let bob = bob::run(bob_swap); let bob = bob::run(bob_swap);
let bob_handle = tokio::spawn(bob); let bob_handle = tokio::spawn(bob);
let alice_state = alice::run_until(alice_swap, alice::swap::is_encsig_learned) let alice_state = alice::run_until(alice_swap, is_encsig_learned)
.await .await
.unwrap(); .unwrap();
assert!(matches!(alice_state, AliceState::EncSigLearned {..})); assert!(matches!(alice_state, AliceState::EncSigLearned {..}));

View File

@ -1,6 +1,7 @@
pub mod testutils; pub mod testutils;
use swap::protocol::{alice, bob, bob::BobState}; use swap::protocol::{alice, bob, bob::BobState};
use testutils::bob_run_until::is_encsig_sent;
#[tokio::test] #[tokio::test]
async fn given_bob_restarts_after_encsig_is_sent_resume_swap() { async fn given_bob_restarts_after_encsig_is_sent_resume_swap() {
@ -11,9 +12,7 @@ async fn given_bob_restarts_after_encsig_is_sent_resume_swap() {
let alice = alice::run(alice_swap); let alice = alice::run(alice_swap);
let alice_handle = tokio::spawn(alice); let alice_handle = tokio::spawn(alice);
let bob_state = bob::run_until(bob_swap, bob::swap::is_encsig_sent) let bob_state = bob::run_until(bob_swap, is_encsig_sent).await.unwrap();
.await
.unwrap();
assert!(matches!(bob_state, BobState::EncSigSent {..})); assert!(matches!(bob_state, BobState::EncSigSent {..}));

View File

@ -1,10 +1,8 @@
use swap::protocol::{
alice, bob,
bob::{swap::is_lock_proof_received, BobState},
};
pub mod testutils; pub mod testutils;
use swap::protocol::{alice, bob, bob::BobState};
use testutils::bob_run_until::is_lock_proof_received;
#[tokio::test] #[tokio::test]
async fn given_bob_restarts_after_lock_proof_received_resume_swap() { async fn given_bob_restarts_after_lock_proof_received_resume_swap() {
testutils::setup_test(|mut ctx| async move { testutils::setup_test(|mut ctx| async move {

View File

@ -1,9 +1,7 @@
pub mod testutils; pub mod testutils;
use swap::protocol::{ use swap::protocol::{alice, bob, bob::BobState};
alice, bob, use testutils::bob_run_until::is_xmr_locked;
bob::{swap::is_xmr_locked, BobState},
};
#[tokio::test] #[tokio::test]
async fn given_bob_restarts_after_xmr_is_locked_resume_swap() { async fn given_bob_restarts_after_xmr_is_locked_resume_swap() {

View File

@ -1,9 +1,7 @@
pub mod testutils; pub mod testutils;
use swap::protocol::{ use swap::protocol::{alice, bob, bob::BobState};
alice, bob, use testutils::bob_run_until::is_btc_locked;
bob::{swap::is_btc_locked, BobState},
};
/// Bob locks Btc and Alice locks Xmr. Bob does not act; he fails to send Alice /// Bob locks Btc and Alice locks Xmr. Bob does not act; he fails to send Alice
/// the encsig and fail to refund or redeem. Alice punishes. /// the encsig and fail to refund or redeem. Alice punishes.

View File

@ -1,6 +1,7 @@
pub mod testutils; pub mod testutils;
use swap::protocol::{alice, alice::AliceState, bob}; use swap::protocol::{alice, alice::AliceState, bob};
use testutils::alice_run_until::is_xmr_locked;
/// Bob locks btc and Alice locks xmr. Alice fails to act so Bob refunds. Alice /// Bob locks btc and Alice locks xmr. Alice fails to act so Bob refunds. Alice
/// then also refunds. /// then also refunds.
@ -13,9 +14,7 @@ async fn given_alice_restarts_after_xmr_is_locked_refund_swap() {
let bob = bob::run(bob_swap); let bob = bob::run(bob_swap);
let bob_handle = tokio::spawn(bob); let bob_handle = tokio::spawn(bob);
let alice_state = alice::run_until(alice_swap, alice::swap::is_xmr_locked) let alice_state = alice::run_until(alice_swap, is_xmr_locked).await.unwrap();
.await
.unwrap();
assert!(matches!(alice_state, AliceState::XmrLocked {..})); assert!(matches!(alice_state, AliceState::XmrLocked {..}));
// Alice does not act, Bob refunds // Alice does not act, Bob refunds

View File

@ -1,7 +1,8 @@
use swap::protocol::{alice, alice::AliceState, bob};
pub mod testutils; pub mod testutils;
use swap::protocol::{alice, alice::AliceState, bob};
use testutils::alice_run_until::is_encsig_learned;
/// Bob locks btc and Alice locks xmr. Alice fails to act so Bob refunds. Alice /// Bob locks btc and Alice locks xmr. Alice fails to act so Bob refunds. Alice
/// is forced to refund even though she learned the secret and would be able to /// is forced to refund even though she learned the secret and would be able to
/// redeem had the timelock not expired. /// redeem had the timelock not expired.
@ -14,7 +15,7 @@ async fn given_alice_restarts_after_enc_sig_learned_and_bob_already_cancelled_re
let bob = bob::run(bob_swap); let bob = bob::run(bob_swap);
let bob_handle = tokio::spawn(bob); let bob_handle = tokio::spawn(bob);
let alice_state = alice::run_until(alice_swap, alice::swap::is_encsig_learned) let alice_state = alice::run_until(alice_swap, is_encsig_learned)
.await .await
.unwrap(); .unwrap();
assert!(matches!(alice_state, AliceState::EncSigLearned {..})); assert!(matches!(alice_state, AliceState::EncSigLearned {..}));

View File

@ -460,3 +460,41 @@ fn init_tracing() -> DefaultGuard {
)) ))
.set_default() .set_default()
} }
pub mod alice_run_until {
use swap::protocol::alice::AliceState;
pub fn is_xmr_locked(state: &AliceState) -> bool {
matches!(
state,
AliceState::XmrLocked{..}
)
}
pub fn is_encsig_learned(state: &AliceState) -> bool {
matches!(
state,
AliceState::EncSigLearned{..}
)
}
}
pub mod bob_run_until {
use swap::protocol::bob::BobState;
pub fn is_btc_locked(state: &BobState) -> bool {
matches!(state, BobState::BtcLocked(..))
}
pub fn is_lock_proof_received(state: &BobState) -> bool {
matches!(state, BobState::XmrLockProofReceived { .. })
}
pub fn is_xmr_locked(state: &BobState) -> bool {
matches!(state, BobState::XmrLocked(..))
}
pub fn is_encsig_sent(state: &BobState) -> bool {
matches!(state, BobState::EncSigSent(..))
}
}