diff --git a/substrate/extrinsic-pool/src/rotator.rs b/substrate/extrinsic-pool/src/rotator.rs index 1f4b1c4e737e1..073e99fe254d4 100644 --- a/substrate/extrinsic-pool/src/rotator.rs +++ b/substrate/extrinsic-pool/src/rotator.rs @@ -174,4 +174,39 @@ mod tests { // then assert!(!rotator.is_banned(&hash)); } + + #[test] + fn should_garbage_collect() { + // given + fn tx_with(i: u64, time: Instant) -> Verified { + let hash = i.into(); + Verified { + original: i, + verified: VerifiedTransaction { + hash, + sender: Default::default(), + nonce: Default::default(), + }, + valid_till: time, + } + } + + let rotator = rotator(); + + let now = Instant::now(); + let past = now - Duration::from_secs(1); + + // when + for i in 0..2*EXPECTED_SIZE { + let tx = tx_with(i as u64, past); + assert!(rotator.ban_if_stale(&now, &tx)); + } + assert_eq!(rotator.banned_until.read().len(), 2*EXPECTED_SIZE); + + // then + let tx = tx_with(2*EXPECTED_SIZE as u64, past); + // trigger a garbage collection + assert!(rotator.ban_if_stale(&now, &tx)); + assert_eq!(rotator.banned_until.read().len(), EXPECTED_SIZE); + } }