Skip to content

Commit

Permalink
fix unit test
Browse files Browse the repository at this point in the history
Signed-off-by: yongman <[email protected]>
  • Loading branch information
yongman committed Mar 11, 2024
1 parent 6d318ca commit 45b5fae
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions src/region_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,11 +497,15 @@ mod test {
async fn test_get_region_by_key() -> Result<()> {
let retry_client = Arc::new(MockRetryClient::default());
let cache = RegionCache::new(retry_client.clone());
let leader = Some(metapb::Peer {
store_id: 1,
..Default::default()
});

let region1 = region(1, vec![], vec![10]);
let region2 = region(2, vec![10], vec![20]);
let region1 = region_with_leader(1, vec![], vec![10], leader.clone());
let region2 = region_with_leader(2, vec![10], vec![20], leader.clone());
let region3 = region(3, vec![30], vec![40]);
let region4 = region(4, vec![50], vec![]);
let region4 = region_with_leader(4, vec![50], vec![], leader.clone());
cache.add_region(region1.clone()).await;
cache.add_region(region2.clone()).await;
cache.add_region(region3.clone()).await;
Expand All @@ -521,6 +525,8 @@ mod test {
);
assert!(cache.get_region_by_key(&vec![20].into()).await.is_err());
assert!(cache.get_region_by_key(&vec![25].into()).await.is_err());
// region3 in cache has no leader, the cache is invalid.
assert!(cache.get_region_by_key(&vec![35].into()).await.is_err());
assert_eq!(cache.get_region_by_key(&vec![60].into()).await?, region4);
Ok(())
}
Expand Down Expand Up @@ -557,6 +563,26 @@ mod test {
region
}

fn region_with_leader(
id: RegionId,
start_key: Vec<u8>,
end_key: Vec<u8>,
leader: Option<metapb::Peer>,
) -> RegionWithLeader {
let mut region = RegionWithLeader::default();
region.region.id = id;
region.region.start_key = start_key;
region.region.end_key = end_key;
region.region.region_epoch = Some(RegionEpoch {
conf_ver: 0,
version: 0,
});
// We don't care about other fields here

region.leader = leader;
region
}

#[test]
fn test_is_valid_tikv_store() {
let mut store = metapb::Store::default();
Expand Down

0 comments on commit 45b5fae

Please sign in to comment.