Files
hse-2026/homework/07-kv-sharding/tests/src/main.rs
T
2026-11-07 22:19:18 +03:00

255 lines
6.8 KiB
Rust

mod common;
mod python_seed;
mod shared_state;
mod tests;
mod tests_mc;
use indexmap::IndexMap;
use std::io::Write;
use clap::Parser;
use env_logger::Builder;
use log::LevelFilter;
use anysystem::python::PyProcessFactory;
use anysystem::test::{TestResult, TestSuite};
use crate::common::TestConfig;
use crate::tests::*;
use crate::tests_mc::*;
/// Sharded KV Store Homework Tests
#[derive(Parser, Debug)]
#[clap(about, long_about = None)]
struct Args {
/// Path to Python file with solution
#[clap(long = "impl", short = 'i', default_value = "solution/node.py")]
solution_path: String,
/// Test to run (optional)
#[clap(long = "test", short)]
test: Option<String>,
/// Print execution trace
#[clap(long, short)]
debug: bool,
/// Number of nodes used in tests
#[clap(long, short, default_value = "10")]
node_count: u32,
/// Random seed used in tests
#[clap(long, short, default_value = "123")]
seed: u64,
}
fn main() {
let args = Args::parse();
python_seed::configure(args.seed);
if !shared_state::validate_or_report(&args.solution_path, &["StorageNode"]) {
return;
}
if args.debug {
Builder::new()
.filter(Some("anysystem"), LevelFilter::Debug)
.format(|buf, record| writeln!(buf, "{}", record.args()))
.init();
}
let process_factory = PyProcessFactory::new(&args.solution_path, "StorageNode");
let config = TestConfig {
process_factory: &process_factory,
proc_count: args.node_count,
seed: args.seed,
};
let mut single_config = config;
single_config.proc_count = 1;
let mut mc_config = config;
mc_config.proc_count = 3;
let mut tests = TestSuite::new();
tests.add("SINGLE NODE", test_single_node, single_config);
tests.add("INSERTS", test_inserts, config);
tests.add("DELETES", test_deletes, config);
tests.add("MEMORY GROWTH", test_memory_growth, config);
tests.add("ZERO HOP", test_zero_hop, config);
tests.add("MC NORMAL", test_mc_normal, mc_config);
tests.add("NODE ADDED", test_node_added, config);
tests.add(
"NODE ADDED DURING INSERTS",
test_node_added_during_inserts,
config,
);
tests.add(
"NODE ADDED DURING DELETIONS",
test_node_added_during_deletions,
config,
);
tests.add(
"NODE ADDED DURING READS",
test_node_added_during_reads,
config,
);
tests.add("NODE REMOVED", test_node_removed, config);
tests.add(
"NODE REMOVED AFTER CRASH",
test_node_removed_after_crash,
config,
);
tests.add("MC NODE REMOVED", test_mc_node_removed, mc_config);
tests.add("MIGRATION", test_migration, config);
tests.add("SCALE UP DOWN", test_scale_up_down, config);
tests.add("DISTRIBUTION", test_distribution, config);
tests.add(
"DISTRIBUTION NODE ADDED",
test_distribution_node_added,
config,
);
tests.add(
"DISTRIBUTION NODE REMOVED",
test_distribution_node_removed,
config,
);
if let Some(test) = &args.test {
tests.run_test(test);
} else {
let (_, results) = tests.run();
let score = score(results);
println!("\nSCORE: {score}\n");
}
}
const BASIC_TESTS: &[&str] = &[
"SINGLE NODE",
"INSERTS",
"DELETES",
"MEMORY GROWTH",
"ZERO HOP",
"MC NORMAL",
];
const ADDITIONAL_GROUPS: &[(&str, &[&str])] = &[
(
"Node changes",
&[
"NODE ADDED",
"NODE REMOVED",
"NODE REMOVED AFTER CRASH",
"MC NODE REMOVED",
"MIGRATION",
"SCALE UP DOWN",
],
),
(
"Requests during node addition",
&[
"NODE ADDED DURING INSERTS",
"NODE ADDED DURING DELETIONS",
"NODE ADDED DURING READS",
],
),
(
"Distribution",
&[
"DISTRIBUTION",
"DISTRIBUTION NODE ADDED",
"DISTRIBUTION NODE REMOVED",
],
),
];
fn group_passed(name: &str, tests: &[&str], results: &mut IndexMap<String, TestResult>) -> bool {
let mut failed = Vec::new();
for &test in tests {
let result = results
.swap_remove(test)
.unwrap_or_else(|| panic!("missing result for {test}"));
if result.is_err() {
failed.push(test);
}
}
if failed.is_empty() {
println!("{name} tests: passed");
true
} else {
println!("{name} tests: not passed {failed:?}");
false
}
}
fn score(mut results: IndexMap<String, TestResult>) -> u32 {
let basic_passed = group_passed("Basic", BASIC_TESTS, &mut results);
let mut points = if basic_passed { 3 } else { 0 };
for &(name, tests) in ADDITIONAL_GROUPS {
if group_passed(name, tests, &mut results) && basic_passed {
points += 2;
}
}
assert!(results.is_empty(), "unscored tests: {:?}", results.keys());
points
}
#[cfg(test)]
mod scoring_tests {
use super::*;
fn score_with_failures(failed: &[&str]) -> u32 {
let mut results: IndexMap<String, TestResult> = BASIC_TESTS
.iter()
.chain(ADDITIONAL_GROUPS.iter().flat_map(|(_, tests)| tests.iter()))
.map(|&name| (name.to_string(), Ok(true)))
.collect();
for &name in failed {
*results.get_mut(name).unwrap() = Err("failed".to_string());
}
score(results)
}
#[test]
fn every_basic_test_is_required() {
for name in [
"SINGLE NODE",
"INSERTS",
"DELETES",
"MC NORMAL",
"ZERO HOP",
"MEMORY GROWTH",
] {
assert_eq!(score_with_failures(&[name]), 0, "{name}");
}
}
#[test]
fn additional_groups_earn_two_points_each() {
let change = "NODE ADDED";
let read = "NODE ADDED DURING READS";
let balance = "DISTRIBUTION NODE ADDED";
let cases: &[(&[&str], u32)] = &[
(&[], 9),
(&[change], 7),
(&[read], 7),
(&[balance], 7),
(&[change, read], 5),
(&[change, balance], 5),
(&[read, balance], 5),
(&[change, read, balance], 3),
];
for &(failures, expected) in cases {
assert_eq!(score_with_failures(failures), expected, "{failures:?}");
}
}
#[test]
fn concurrent_requests_are_scored_as_one_group() {
let requests = [
"NODE ADDED DURING INSERTS",
"NODE ADDED DURING DELETIONS",
"NODE ADDED DURING READS",
];
for name in requests {
assert_eq!(score_with_failures(&[name]), 7, "{name}");
}
assert_eq!(score_with_failures(&requests), 7);
}
}