|
3 | 3 |
|
4 | 4 | #[cfg(test)] |
5 | 5 | mod receipt_storage_adapter_unit_test { |
| 6 | + use rand::seq::SliceRandom; |
| 7 | + use rand::thread_rng; |
6 | 8 | use std::collections::HashMap; |
7 | 9 | use std::str::FromStr; |
8 | 10 | use std::sync::Arc; |
@@ -179,4 +181,72 @@ mod receipt_storage_adapter_unit_test { |
179 | 181 | .is_err()); |
180 | 182 | } |
181 | 183 | } |
| 184 | + |
| 185 | + /// The test code will shuffle the input timestamps prior to calling safe_truncate_receipts. |
| 186 | + #[rstest] |
| 187 | + #[case(vec![1, 2, 3, 4, 5], 3, vec![1, 2, 3])] |
| 188 | + #[case(vec![1, 2, 3, 3, 4, 5], 3, vec![1, 2])] |
| 189 | + #[case(vec![1, 2, 3, 4, 4, 4], 3, vec![1, 2, 3])] |
| 190 | + #[case(vec![1, 1, 1, 1, 2, 3], 3, vec![])] |
| 191 | + #[tokio::test] |
| 192 | + async fn safe_truncate_receipts_test( |
| 193 | + domain_separator: Eip712Domain, |
| 194 | + #[case] input: Vec<u64>, |
| 195 | + #[case] limit: u64, |
| 196 | + #[case] expected: Vec<u64>, |
| 197 | + ) { |
| 198 | + let wallet: LocalWallet = MnemonicBuilder::<English>::default() |
| 199 | + .phrase("abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about") |
| 200 | + .build() |
| 201 | + .unwrap(); |
| 202 | + |
| 203 | + // Vec of (id, receipt) |
| 204 | + let mut receipts_orig: Vec<(u64, ReceivedReceipt)> = Vec::new(); |
| 205 | + |
| 206 | + for (i, timestamp) in input.iter().enumerate() { |
| 207 | + // The contents of the receipt only need to be unique for this test (so we can check) |
| 208 | + receipts_orig.push(( |
| 209 | + i as u64, |
| 210 | + ReceivedReceipt::new( |
| 211 | + EIP712SignedMessage::new( |
| 212 | + &domain_separator, |
| 213 | + Receipt { |
| 214 | + allocation_id: Address::ZERO, |
| 215 | + timestamp_ns: *timestamp, |
| 216 | + nonce: 0, |
| 217 | + value: 0, |
| 218 | + }, |
| 219 | + &wallet, |
| 220 | + ) |
| 221 | + .await |
| 222 | + .unwrap(), |
| 223 | + i as u64, // Will use that to check the IDs |
| 224 | + &get_full_list_of_checks(), |
| 225 | + ), |
| 226 | + )); |
| 227 | + } |
| 228 | + |
| 229 | + let mut receipts_truncated = receipts_orig.clone(); |
| 230 | + |
| 231 | + // shuffle the input receipts |
| 232 | + receipts_truncated.shuffle(&mut thread_rng()); |
| 233 | + |
| 234 | + crate::adapters::receipt_storage_adapter::safe_truncate_receipts( |
| 235 | + &mut receipts_truncated, |
| 236 | + limit, |
| 237 | + ); |
| 238 | + |
| 239 | + assert_eq!(receipts_truncated.len(), expected.len()); |
| 240 | + |
| 241 | + for (elem_trun, expected_timestamp) in receipts_truncated.iter().zip(expected.iter()) { |
| 242 | + // Check timestamps |
| 243 | + assert_eq!( |
| 244 | + elem_trun.1.signed_receipt.message.timestamp_ns, |
| 245 | + *expected_timestamp |
| 246 | + ); |
| 247 | + |
| 248 | + // Check that the IDs are fine |
| 249 | + assert_eq!(elem_trun.0, elem_trun.1.query_id); |
| 250 | + } |
| 251 | + } |
182 | 252 | } |
0 commit comments