Skip to content

Commit 8b865b7

Browse files
carlosvdrgusinacio
andcommitted
test: added unit tests for trigger count
Co-authored-by: Gustavo Inacio <[email protected]>
1 parent bdd31ec commit 8b865b7

File tree

2 files changed

+107
-18
lines changed

2 files changed

+107
-18
lines changed

tap-agent/src/agent/sender_account.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,7 +1378,7 @@ pub mod tests {
13781378
))
13791379
.unwrap();
13801380

1381-
tokio::time::sleep(Duration::from_millis(20)).await;
1381+
tokio::time::sleep(Duration::from_millis(BUFFER_MS)).await;
13821382

13831383
assert_eq!(
13841384
triggered_rav_request.load(std::sync::atomic::Ordering::SeqCst),
@@ -1400,27 +1400,39 @@ pub mod tests {
14001400
TRIGGER_VALUE,
14011401
TRIGGER_VALUE,
14021402
DUMMY_URL,
1403-
1,
1403+
2,
14041404
)
14051405
.await;
14061406

14071407
let (triggered_rav_request, _, allocation, allocation_handle) =
1408-
create_mock_sender_allocation(prefix, SENDER.1, *ALLOCATION_ID_0).await;
1408+
create_mock_sender_allocation(
1409+
prefix,
1410+
SENDER.1,
1411+
*ALLOCATION_ID_0,
1412+
sender_account.clone(),
1413+
)
1414+
.await;
14091415

14101416
// create a fake sender allocation
14111417
sender_account
14121418
.cast(SenderAccountMessage::UpdateReceiptFees(
14131419
*ALLOCATION_ID_0,
1414-
ReceiptFees::NewReceipt(TRIGGER_VALUE - 1),
1420+
ReceiptFees::NewReceipt(1),
14151421
))
14161422
.unwrap();
14171423

1418-
tokio::time::sleep(Duration::from_millis(20)).await;
1424+
tokio::time::sleep(Duration::from_millis(BUFFER_MS)).await;
14191425

14201426
assert_eq!(
14211427
triggered_rav_request.load(std::sync::atomic::Ordering::SeqCst),
14221428
0
14231429
);
1430+
sender_account
1431+
.cast(SenderAccountMessage::UpdateReceiptFees(
1432+
*ALLOCATION_ID_0,
1433+
ReceiptFees::NewReceipt(1),
1434+
))
1435+
.unwrap();
14241436

14251437
// wait for it to be outside buffer
14261438
tokio::time::sleep(Duration::from_millis(BUFFER_MS)).await;

tap-agent/src/agent/sender_fee_tracker.rs

Lines changed: 90 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,25 @@ struct ExpiringSum {
1616

1717
impl ExpiringSum {
1818
fn get_sum(&mut self, duration: &Duration) -> u128 {
19-
let now = Instant::now();
20-
while let Some(&(timestamp, value)) = self.entries.front() {
21-
if now.duration_since(timestamp) >= *duration {
22-
self.entries.pop_front();
23-
self.sum -= value;
24-
} else {
25-
break;
26-
}
27-
}
19+
self.cleanup(duration);
2820
self.sum
2921
}
3022

3123
fn get_count(&mut self, duration: &Duration) -> usize {
24+
self.cleanup(duration);
25+
self.entries.len()
26+
}
27+
28+
fn cleanup(&mut self, duration: &Duration) {
3229
let now = Instant::now();
33-
while let Some(&(timestamp, _)) = self.entries.front() {
30+
while let Some(&(timestamp, value)) = self.entries.front() {
3431
if now.duration_since(timestamp) >= *duration {
3532
self.entries.pop_front();
33+
self.sum -= value;
3634
} else {
3735
break;
3836
}
3937
}
40-
self.entries.len()
4138
}
4239
}
4340

@@ -219,13 +216,13 @@ impl SenderFeeTracker {
219216
pub fn start_rav_request(&mut self, allocation_id: Address) {
220217
let current_fee = self.id_to_fee.entry(allocation_id).or_default();
221218
self.ids_requesting.insert(allocation_id);
222-
self.fees_requesting += *current_fee;
219+
self.fees_requesting += current_fee.fee;
223220
}
224221

225222
/// Should be called before `update`
226223
pub fn finish_rav_request(&mut self, allocation_id: Address) {
227224
let current_fee = self.id_to_fee.entry(allocation_id).or_default();
228-
self.fees_requesting -= *current_fee;
225+
self.fees_requesting -= current_fee.fee;
229226
self.ids_requesting.remove(&allocation_id);
230227
}
231228

@@ -442,4 +439,84 @@ mod tests {
442439
assert_eq!(tracker.get_total_fee(), 60);
443440
assert_eq!(tracker.get_total_fee_outside_buffer(), 60);
444441
}
442+
443+
#[test]
444+
fn check_counter_and_fee_outside_buffer_unordered() {
445+
let allocation_id_0 = address!("abababababababababababababababababababab");
446+
let allocation_id_2 = address!("cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd");
447+
448+
const BUFFER_WINDOW: Duration = Duration::from_millis(20);
449+
let mut tracker = SenderFeeTracker::new(BUFFER_WINDOW);
450+
assert_eq!(tracker.get_total_fee_outside_buffer(), 0);
451+
assert_eq!(
452+
tracker.get_total_counter_outside_buffer_for_allocation(&allocation_id_0),
453+
0
454+
);
455+
456+
tracker.add(allocation_id_0, 10);
457+
assert_eq!(tracker.get_total_fee_outside_buffer(), 0);
458+
assert_eq!(
459+
tracker.get_total_counter_outside_buffer_for_allocation(&allocation_id_0),
460+
0
461+
);
462+
463+
sleep(BUFFER_WINDOW);
464+
465+
assert_eq!(tracker.get_total_fee_outside_buffer(), 10);
466+
assert_eq!(
467+
tracker.get_total_counter_outside_buffer_for_allocation(&allocation_id_0),
468+
1
469+
);
470+
471+
tracker.add(allocation_id_2, 20);
472+
assert_eq!(
473+
tracker.get_total_counter_outside_buffer_for_allocation(&allocation_id_2),
474+
0
475+
);
476+
assert_eq!(tracker.get_total_fee_outside_buffer(), 10);
477+
478+
sleep(BUFFER_WINDOW);
479+
480+
tracker.block_allocation_id(allocation_id_2);
481+
assert_eq!(
482+
tracker.get_total_counter_outside_buffer_for_allocation(&allocation_id_2),
483+
1
484+
);
485+
assert_eq!(tracker.get_total_fee_outside_buffer(), 30);
486+
}
487+
488+
#[test]
489+
fn check_get_count_updates_sum() {
490+
let allocation_id_0 = address!("abababababababababababababababababababab");
491+
492+
const BUFFER_WINDOW: Duration = Duration::from_millis(20);
493+
let mut tracker = SenderFeeTracker::new(BUFFER_WINDOW);
494+
495+
tracker.add(allocation_id_0, 10);
496+
let expiring_sum = tracker
497+
.buffer_window_fee
498+
.get_mut(&allocation_id_0)
499+
.expect("there should be something here");
500+
assert_eq!(expiring_sum.get_sum(&BUFFER_WINDOW), 10);
501+
assert_eq!(expiring_sum.get_count(&BUFFER_WINDOW), 1);
502+
503+
sleep(BUFFER_WINDOW);
504+
505+
assert_eq!(expiring_sum.get_sum(&BUFFER_WINDOW), 0);
506+
assert_eq!(expiring_sum.get_count(&BUFFER_WINDOW), 0);
507+
508+
tracker.add(allocation_id_0, 10);
509+
let expiring_sum = tracker
510+
.buffer_window_fee
511+
.get_mut(&allocation_id_0)
512+
.expect("there should be something here");
513+
514+
assert_eq!(expiring_sum.get_count(&BUFFER_WINDOW), 1);
515+
assert_eq!(expiring_sum.get_sum(&BUFFER_WINDOW), 10);
516+
517+
sleep(BUFFER_WINDOW);
518+
519+
assert_eq!(expiring_sum.get_count(&BUFFER_WINDOW), 0);
520+
assert_eq!(expiring_sum.get_sum(&BUFFER_WINDOW), 0);
521+
}
445522
}

0 commit comments

Comments
 (0)