Skip to content

Commit 3c40908

Browse files
authored
test(tap-agent): retry logic test (#198)
* test(tap-agent): retry logic test Signed-off-by: Gustavo Inacio <[email protected]> * chore(tap-agent): rename retry_duration to retry_interval Signed-off-by: Gustavo Inacio <[email protected]> --------- Signed-off-by: Gustavo Inacio <[email protected]>
1 parent f430708 commit 3c40908

File tree

2 files changed

+72
-11
lines changed

2 files changed

+72
-11
lines changed

tap-agent/src/agent/sender_account.rs

Lines changed: 70 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ pub struct SenderAccountArgs {
6767
pub sender_aggregator_endpoint: String,
6868
pub allocation_ids: HashSet<Address>,
6969
pub prefix: Option<String>,
70+
71+
pub retry_interval: Duration,
7072
}
7173
pub struct State {
7274
prefix: Option<String>,
@@ -82,6 +84,7 @@ pub struct State {
8284
// Deny reasons
8385
denied: bool,
8486
sender_balance: U256,
87+
retry_interval: Duration,
8588

8689
//Eventuals
8790
escrow_accounts: Eventual<EscrowAccounts>,
@@ -251,6 +254,7 @@ impl Actor for SenderAccount {
251254
sender_aggregator_endpoint,
252255
allocation_ids,
253256
prefix,
257+
retry_interval,
254258
}: Self::Arguments,
255259
) -> std::result::Result<Self::State, ActorProcessingErr> {
256260
let myself_clone = myself.clone();
@@ -419,6 +423,7 @@ impl Actor for SenderAccount {
419423
sender: sender_id,
420424
denied,
421425
sender_balance,
426+
retry_interval,
422427
scheduled_rav_request: None,
423428
};
424429

@@ -502,7 +507,7 @@ impl Actor for SenderAccount {
502507
(true, true) => {
503508
// retry in a moment
504509
state.scheduled_rav_request =
505-
Some(myself.send_after(Duration::from_secs(30), move || {
510+
Some(myself.send_after(state.retry_interval, move || {
506511
SenderAccountMessage::UpdateReceiptFees(
507512
allocation_id,
508513
unaggregated_fees,
@@ -692,7 +697,7 @@ pub mod tests {
692697
use serde_json::json;
693698
use sqlx::PgPool;
694699
use std::collections::{HashMap, HashSet};
695-
use std::sync::atomic::{AtomicBool, AtomicU32};
700+
use std::sync::atomic::AtomicU32;
696701
use std::sync::{Arc, Mutex};
697702
use std::time::Duration;
698703
use wiremock::matchers::{body_string_contains, method};
@@ -773,6 +778,7 @@ pub mod tests {
773778
sender_aggregator_endpoint: DUMMY_URL.to_string(),
774779
allocation_ids: HashSet::new(),
775780
prefix: Some(prefix.clone()),
781+
retry_interval: Duration::from_millis(10),
776782
};
777783

778784
let (sender, handle) = SenderAccount::spawn(Some(prefix.clone()), SenderAccount, args)
@@ -823,14 +829,14 @@ pub mod tests {
823829
}
824830

825831
pub struct MockSenderAllocation {
826-
triggered_rav_request: Arc<AtomicBool>,
832+
triggered_rav_request: Arc<AtomicU32>,
827833
next_rav_value: Arc<Mutex<u128>>,
828834
receipts: Arc<Mutex<Vec<NewReceiptNotification>>>,
829835
}
830836

831837
impl MockSenderAllocation {
832-
pub fn new_with_triggered_rav_request() -> (Self, Arc<AtomicBool>) {
833-
let triggered_rav_request = Arc::new(AtomicBool::new(false));
838+
pub fn new_with_triggered_rav_request() -> (Self, Arc<AtomicU32>) {
839+
let triggered_rav_request = Arc::new(AtomicU32::new(0));
834840
(
835841
Self {
836842
triggered_rav_request: triggered_rav_request.clone(),
@@ -845,7 +851,7 @@ pub mod tests {
845851
let next_rav_value = Arc::new(Mutex::new(0));
846852
(
847853
Self {
848-
triggered_rav_request: Arc::new(AtomicBool::new(false)),
854+
triggered_rav_request: Arc::new(AtomicU32::new(0)),
849855
receipts: Arc::new(Mutex::new(Vec::new())),
850856
next_rav_value: next_rav_value.clone(),
851857
},
@@ -857,7 +863,7 @@ pub mod tests {
857863
let receipts = Arc::new(Mutex::new(Vec::new()));
858864
(
859865
Self {
860-
triggered_rav_request: Arc::new(AtomicBool::new(false)),
866+
triggered_rav_request: Arc::new(AtomicU32::new(0)),
861867
receipts: receipts.clone(),
862868
next_rav_value: Arc::new(Mutex::new(0)),
863869
},
@@ -889,7 +895,7 @@ pub mod tests {
889895
match message {
890896
SenderAllocationMessage::TriggerRAVRequest(reply) => {
891897
self.triggered_rav_request
892-
.store(true, std::sync::atomic::Ordering::SeqCst);
898+
.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
893899
let signed_rav = create_rav(
894900
*ALLOCATION_ID_0,
895901
SIGNER.0.clone(),
@@ -912,7 +918,7 @@ pub mod tests {
912918
sender: Address,
913919
allocation: Address,
914920
) -> (
915-
Arc<AtomicBool>,
921+
Arc<AtomicU32>,
916922
ActorRef<SenderAllocationMessage>,
917923
JoinHandle<()>,
918924
) {
@@ -954,7 +960,10 @@ pub mod tests {
954960

955961
tokio::time::sleep(Duration::from_millis(10)).await;
956962

957-
assert!(!triggered_rav_request.load(std::sync::atomic::Ordering::SeqCst));
963+
assert_eq!(
964+
triggered_rav_request.load(std::sync::atomic::Ordering::SeqCst),
965+
0
966+
);
958967

959968
allocation.stop_and_wait(None, None).await.unwrap();
960969
allocation_handle.await.unwrap();
@@ -990,7 +999,10 @@ pub mod tests {
990999

9911000
tokio::time::sleep(Duration::from_millis(10)).await;
9921001

993-
assert!(triggered_rav_request.load(std::sync::atomic::Ordering::SeqCst));
1002+
assert_eq!(
1003+
triggered_rav_request.load(std::sync::atomic::Ordering::SeqCst),
1004+
1
1005+
);
9941006

9951007
allocation.stop_and_wait(None, None).await.unwrap();
9961008
allocation_handle.await.unwrap();
@@ -1065,6 +1077,53 @@ pub mod tests {
10651077
assert!(deny);
10661078
}
10671079

1080+
#[sqlx::test(migrations = "../migrations")]
1081+
async fn test_retry_unaggregated_fees(pgpool: PgPool) {
1082+
// we set to zero to block the sender, no matter the fee
1083+
let max_unaggregated_fees_per_sender: u128 = 0;
1084+
1085+
let (sender_account, handle, prefix, _) = create_sender_account(
1086+
pgpool,
1087+
HashSet::new(),
1088+
TRIGGER_VALUE,
1089+
max_unaggregated_fees_per_sender,
1090+
DUMMY_URL,
1091+
)
1092+
.await;
1093+
1094+
let (triggered_rav_request, allocation, allocation_handle) =
1095+
create_mock_sender_allocation(prefix, SENDER.1, *ALLOCATION_ID_0).await;
1096+
1097+
assert_eq!(
1098+
triggered_rav_request.load(std::sync::atomic::Ordering::SeqCst),
1099+
0
1100+
);
1101+
sender_account
1102+
.cast(SenderAccountMessage::UpdateReceiptFees(
1103+
*ALLOCATION_ID_0,
1104+
UnaggregatedReceipts {
1105+
value: TRIGGER_VALUE,
1106+
last_id: 11,
1107+
},
1108+
))
1109+
.unwrap();
1110+
tokio::time::sleep(Duration::from_millis(30)).await;
1111+
1112+
let retry_value = triggered_rav_request.load(std::sync::atomic::Ordering::SeqCst);
1113+
assert!(retry_value > 1, "It didn't retry more than once");
1114+
1115+
tokio::time::sleep(Duration::from_millis(30)).await;
1116+
1117+
let new_value = triggered_rav_request.load(std::sync::atomic::Ordering::SeqCst);
1118+
assert!(new_value > retry_value, "It didn't retry anymore");
1119+
1120+
allocation.stop_and_wait(None, None).await.unwrap();
1121+
allocation_handle.await.unwrap();
1122+
1123+
sender_account.stop_and_wait(None, None).await.unwrap();
1124+
handle.await.unwrap();
1125+
}
1126+
10681127
#[sqlx::test(migrations = "../migrations")]
10691128
async fn test_deny_allow(pgpool: PgPool) {
10701129
async fn get_deny_status(sender_account: &ActorRef<SenderAccountMessage>) -> bool {

tap-agent/src/agent/sender_accounts_manager.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
use std::collections::HashSet;
5+
use std::time::Duration;
56
use std::{collections::HashMap, str::FromStr};
67

78
use crate::agent::sender_allocation::SenderAllocationMessage;
@@ -436,6 +437,7 @@ impl State {
436437
.clone(),
437438
allocation_ids,
438439
prefix: self.prefix.clone(),
440+
retry_interval: Duration::from_secs(30),
439441
})
440442
}
441443
}

0 commit comments

Comments
 (0)