Skip to content

Commit

Permalink
fix: fix flakey test_coinbase_abandoned integration test (#3866)
Browse files Browse the repository at this point in the history
Description
---
This test has been flakey on CI and I think it is because when running release mode it is fast enough that the next stage in the test start before the last validation protocol in complete because it only waited for the cancellation events. This PR updates the test to wait for the validation completed event too to be sure that the previous validation protocol is complete before starting the next stage.


How Has This Been Tested?
---
cargo test
  • Loading branch information
philipr-za authored Feb 22, 2022
1 parent 0d96ea3 commit ab52f5e
Showing 1 changed file with 76 additions and 38 deletions.
114 changes: 76 additions & 38 deletions base_layer/wallet/tests/transaction_service_tests/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3606,32 +3606,43 @@ fn test_coinbase_abandoned() {
.unwrap();
assert_eq!(balance.pending_incoming_balance, fees1 + reward1);

runtime
let validation_id = runtime
.block_on(alice_ts_interface.transaction_service_handle.validate_transactions())
.expect("Validation should start");

runtime.block_on(async {
let delay = sleep(Duration::from_secs(30));
tokio::pin!(delay);
let mut count = 0usize;
let mut cancelled = false;
let mut completed = false;
loop {
tokio::select! {
event = alice_event_stream.recv() => {
if let TransactionEvent::TransactionCancelled(tx_id, _) = &*event.unwrap() {
if tx_id == &tx_id1 {
count += 1;
}
if count == 1 {
break;
}
match &*event.unwrap() {
TransactionEvent::TransactionValidationCompleted(id) => {
if id == &validation_id {
completed = true;
}
},
TransactionEvent::TransactionCancelled(tx_id, _) => {
if tx_id == &tx_id1 {
cancelled = true;
}
},
_ => (),
}

if cancelled && completed {
break;
}
},
() = &mut delay => {
break;
},
}
}
assert_eq!(count, 1, "Expected a TransactionCancelled event");
assert!(cancelled, "Expected a TransactionCancelled event");
assert!(completed, "Expected a TransactionValidationCompleted event");
});

let txs = runtime
Expand Down Expand Up @@ -3727,31 +3738,43 @@ fn test_coinbase_abandoned() {
}
alice_ts_interface.base_node_rpc_mock_state.set_blocks(block_headers);

runtime
let validation_id = runtime
.block_on(alice_ts_interface.transaction_service_handle.validate_transactions())
.expect("Validation should start");

runtime.block_on(async {
let delay = sleep(Duration::from_secs(30));
tokio::pin!(delay);
let mut count = 0usize;
let mut completed = false;
let mut mined_unconfirmed = false;
loop {
tokio::select! {
event = alice_event_stream.recv() => {
if let TransactionEvent::TransactionMinedUnconfirmed{tx_id, num_confirmations:_, is_valid: _} = &*event.unwrap() { if tx_id == &tx_id2 {
count += 1;
}
if count == 1 {
break;
}
match &*event.unwrap() {
TransactionEvent::TransactionValidationCompleted(id) => {
if id == &validation_id {
completed = true;
}
},
TransactionEvent::TransactionMinedUnconfirmed{tx_id, num_confirmations:_, is_valid: _} => {
if tx_id == &tx_id2 {
mined_unconfirmed = true;
}
},
_ => (),
}

if mined_unconfirmed && completed {
break;
}
},
() = &mut delay => {
break;
},
}
}
assert_eq!(count, 1, "Expected a TransactionMinedUnconfirmed event");
assert!(mined_unconfirmed, "Expected a TransactionMinedUnconfirmed event");
assert!(completed, "Expected a TransactionValidationCompleted event");
});

let tx = runtime
Expand Down Expand Up @@ -3800,44 +3823,50 @@ fn test_coinbase_abandoned() {
}
alice_ts_interface.base_node_rpc_mock_state.set_blocks(block_headers);

runtime
let validation_id = runtime
.block_on(alice_ts_interface.transaction_service_handle.validate_transactions())
.expect("Validation should start");

runtime.block_on(async {
let delay = sleep(Duration::from_secs(30));
tokio::pin!(delay);
let mut count = 0usize;
let mut completed = false;
let mut broadcast = false;
let mut cancelled = false;
loop {
tokio::select! {
event = alice_event_stream.recv() => {
match &*event.unwrap() {
TransactionEvent::TransactionBroadcast(tx_id) => {
if tx_id == &tx_id2 {
count += 1;
broadcast = true;
}
},
TransactionEvent::TransactionCancelled(tx_id, _) => {
if tx_id == &tx_id2 {
count += 1;
cancelled = true;
}
},
TransactionEvent::TransactionValidationCompleted(id) => {
if id == &validation_id {
completed = true;
}
},
_ => (),
}

if count == 2 {
break;
}
if cancelled && broadcast && completed {
break;
}
},
() = &mut delay => {
break;
},
}
}
assert_eq!(
count, 2,
"Expected a TransactionBroadcast and Transaction Cancelled event"
);
assert!(cancelled, "Expected a TransactionCancelled event");
assert!(broadcast, "Expected a TransactionBroadcast event");
assert!(completed, "Expected a TransactionValidationCompleted event");
});

let txs = runtime
Expand Down Expand Up @@ -3900,41 +3929,50 @@ fn test_coinbase_abandoned() {
.base_node_rpc_mock_state
.set_transaction_query_batch_responses(batch_query_response);

runtime
let validation_id = runtime
.block_on(alice_ts_interface.transaction_service_handle.validate_transactions())
.expect("Validation should start");

runtime.block_on(async {
let delay = sleep(Duration::from_secs(60));
tokio::pin!(delay);
let mut count = 0usize;
let mut mined = false;
let mut cancelled = false;
let mut completed = false;
loop {
tokio::select! {
event = alice_event_stream.recv() => {
match &*event.unwrap() {
TransactionEvent::TransactionMined { tx_id, is_valid: _ } => {
if tx_id == &tx_id2 {
count += 1;
mined = true;
}
},
TransactionEvent::TransactionCancelled(tx_id, _) => {
if tx_id == &tx_id1 {
count += 1;
cancelled = true;
}
},
TransactionEvent::TransactionValidationCompleted(id) => {
if id == &validation_id {
completed = true;
}
},
_ => (),
}

if count == 2 {
break;
}
if mined && cancelled && completed {
break;
}
},
() = &mut delay => {
break;
},
}
}
assert_eq!(count, 2, "Expected a TransactionMined and TransactionCancelled event");
assert!(mined, "Expected to received TransactionMined event");
assert!(cancelled, "Expected to received TransactionCancelled event");
assert!(completed, "Expected a TransactionValidationCompleted event");
});
}

Expand Down

0 comments on commit ab52f5e

Please sign in to comment.