Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
HaoranYi committed Jan 31, 2025
1 parent 74a6a46 commit 443a37f
Showing 1 changed file with 46 additions and 1 deletion.
47 changes: 46 additions & 1 deletion accounts-db/src/accounts_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,6 @@ impl<T: IndexValue, U: DiskIndexValue + From<T> + Into<T>> Iterator
)
}
};

for (count, (pubkey, account_map_entry)) in range.iter().enumerate() {
if chunk.len() >= ITER_BATCH_SIZE && !self.collect_all_unsorted {
range.drain(0..count);
Expand Down Expand Up @@ -3891,6 +3890,52 @@ pub mod tests {
}
}

#[test]
fn test_account_index_iter() {
let index = AccountsIndex::<bool, bool>::default_for_tests();
// Setup an account index for test.
// Two bins. First bin has 2000 accounts, second bin has 0 accounts.
let num_pubkeys = 2000;
let pubkeys = (0..num_pubkeys)
.map(|_| Pubkey::new_unique())
.collect::<Vec<_>>();

for key in pubkeys {
let slot = 0;
let value = true;
let mut gc = Vec::new();
index.upsert(
slot,
slot,
&key,
&AccountSharedData::default(),
&AccountSecondaryIndexes::default(),
value,
&mut gc,
UPSERT_POPULATE_RECLAIMS,
);
}

// Create an iterator for the whole pubkey range.
let mut iter = index.iter(None::<&Range<Pubkey>>, COLLECT_ALL_UNSORTED_FALSE);
// First iter.next() should return the first batch of pubkeys (1000
// pubkeys) out of the 2000 pubkeys in the first bin. And the remaining
// 1000 pubkeys from the first bin should be cached in
// self.last_bin_range, so that the second iter.next() don't need to
// load/filter/sort the first bin again.
let x = iter.next().unwrap();
assert_eq!(x.len(), 1000);
assert!(x.is_sorted_by(|a, b| a.0 < b.0)); // The result should be sorted by pubkey.
assert!(iter.last_bin_range.is_some()); // last_bin_range should be cached.
assert_eq!(iter.last_bin_range.as_ref().unwrap().0, 0); // This is the first bin.
assert_eq!(iter.last_bin_range.as_ref().unwrap().1.len(), 1000); // Contains the remaining 1000 items.

// Second iter.next() should return the second batch of pubkeys - the remaining 1000 pubkeys.
let x = iter.next().unwrap();
assert!(x.is_sorted_by(|a, b| a.0 < b.0));
assert_eq!(x.len(), 1000);
}

#[test]
fn test_bin_start_and_range() {
let index = AccountsIndex::<bool, bool>::default_for_tests();
Expand Down

0 comments on commit 443a37f

Please sign in to comment.