Skip to content

Commit

Permalink
Make RoutingTable generation more robust and add basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mbjorkqvist committed Sep 17, 2024
1 parent 220a539 commit 1fe4699
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 5 deletions.
8 changes: 7 additions & 1 deletion rs/nns/test_utils/golden_nns_state/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@rules_rust//rust:defs.bzl", "rust_library")
load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test")

package(default_visibility = ["//visibility:public"])

Expand Down Expand Up @@ -40,3 +40,9 @@ rust_library(
version = "0.0.1",
deps = DEPENDENCIES,
)

rust_test(
name = "golden_nns_state_test",
crate = ":golden_nns_state",
deps = DEPENDENCIES,
)
103 changes: 99 additions & 4 deletions rs/nns/test_utils/golden_nns_state/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,11 @@ fn create_routing_table(
subnet_id: SubnetId,
) -> RoutingTable {
let mut routing_table = RoutingTable::new();
let (first_subnet_id, second_subnet_id) = compute_unique_subnet_ids(subnet_id);
if canister_range_start > 0 {
let subnet_id = SubnetId::from(PrincipalId::new_subnet_test_id(0));
add_canister_range_to_routing_table(
&mut routing_table,
subnet_id,
first_subnet_id,
0,
canister_range_start - 1,
);
Expand All @@ -251,17 +251,29 @@ fn create_routing_table(
canister_range_end,
);
if canister_range_end < u64::MAX {
let subnet_id = SubnetId::from(PrincipalId::new_subnet_test_id(u64::MAX));
add_canister_range_to_routing_table(
&mut routing_table,
subnet_id,
second_subnet_id,
canister_range_end + 1,
u64::MAX,
);
}
routing_table
}

fn compute_unique_subnet_ids(subnet_id: SubnetId) -> (SubnetId, SubnetId) {
let mut candidates = vec![];
let mut subnet_id_counter = 0u64;
while candidates.len() < 2 {
let candidate = SubnetId::from(PrincipalId::new_subnet_test_id(subnet_id_counter));
if candidate != subnet_id {
candidates.push(candidate);
}
subnet_id_counter += 1;
}
(candidates[0], candidates[1])
}

fn add_canister_range_to_routing_table(
routing_table: &mut RoutingTable,
subnet_id: SubnetId,
Expand Down Expand Up @@ -355,3 +367,86 @@ fn bazel_test_compatible_temp_dir_or_panic() -> TempDir {
Err(_err) => TempDir::new().unwrap(),
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn should_create_valid_routing_table_with_canister_ids_neither_0_nor_u64_max() {
let routing_table = create_routing_table(
0x1000000,
0x1FFFFFE,
SubnetId::new(
PrincipalId::from_str(
"x33ed-h457x-bsgyx-oqxqf-6pzwv-wkhzr-rm2j3-npodi-purzm-n66cg-gae",
)
.unwrap(),
),
);
assert_routing_table_size(routing_table, 3);
}

#[test]
fn should_create_valid_routing_table_with_canister_ids_starting_at_0() {
let routing_table = create_routing_table(
0x0,
0x1FFFFFE,
SubnetId::new(
PrincipalId::from_str(
"x33ed-h457x-bsgyx-oqxqf-6pzwv-wkhzr-rm2j3-npodi-purzm-n66cg-gae",
)
.unwrap(),
),
);
assert_routing_table_size(routing_table, 2);
}

#[test]
fn should_create_valid_routing_table_with_canister_ids_ending_at_u64_max() {
let routing_table = create_routing_table(
0x1000000,
u64::MAX,
SubnetId::new(
PrincipalId::from_str(
"x33ed-h457x-bsgyx-oqxqf-6pzwv-wkhzr-rm2j3-npodi-purzm-n66cg-gae",
)
.unwrap(),
),
);
assert_routing_table_size(routing_table, 2);
}

#[test]
fn should_create_valid_routing_table_with_canister_ids_starting_at_0_and_ending_at_u64_max() {
let routing_table = create_routing_table(
0x0,
u64::MAX,
SubnetId::new(
PrincipalId::from_str(
"x33ed-h457x-bsgyx-oqxqf-6pzwv-wkhzr-rm2j3-npodi-purzm-n66cg-gae",
)
.unwrap(),
),
);
assert_routing_table_size(routing_table, 1);
}

#[test]
fn should_create_routing_table_with_distinct_subnets_with_conflicting_subnet_id() {
let routing_table = create_routing_table(
0x1000000,
0x1FFFFFE,
SubnetId::from(PrincipalId::new_subnet_test_id(0)),
);
assert_routing_table_size(routing_table, 3);
}

fn assert_routing_table_size(routing_table: RoutingTable, expected_size: usize) {
let mut num_subnets = 0;
for _ in routing_table.iter() {
num_subnets += 1;
}
assert_eq!(num_subnets, expected_size);
}
}

0 comments on commit 1fe4699

Please sign in to comment.