feat: add comprehensive formatting rules and type inference guidelines

This commit is contained in:
overtrue
2025-05-28 16:04:38 +08:00
parent f05a0b8e83
commit 8747a91df8
28 changed files with 792 additions and 383 deletions
+3 -1
View File
@@ -258,7 +258,9 @@ mod tests {
fn test_multiple_error_types() {
let errors = vec![
Error::new(io::Error::new(io::ErrorKind::NotFound, "Not found")),
Error::new(CustomTestError { message: "Custom".to_string() }),
Error::new(CustomTestError {
message: "Custom".to_string(),
}),
Error::new(AnotherTestError),
Error::msg("String error"),
];
+33 -26
View File
@@ -247,11 +247,7 @@ mod tests {
#[test]
fn test_acc_elem_avg_zero_total() {
let elem = AccElem {
total: 0,
size: 0,
n: 5,
};
let elem = AccElem { total: 0, size: 0, n: 5 };
let avg = elem.avg();
assert_eq!(avg, Duration::from_secs(0));
@@ -464,7 +460,8 @@ mod tests {
let mut latency = LastMinuteLatency::default();
// Test that indices wrap around correctly
for sec in 0..120 { // Test for 2 minutes
for sec in 0..120 {
// Test for 2 minutes
let acc_elem = AccElem {
total: sec,
size: 0,
@@ -482,7 +479,14 @@ mod tests {
let mut latency = LastMinuteLatency::default();
// Add data at time 1000
latency.add_all(1000, &AccElem { total: 10, size: 0, n: 1 });
latency.add_all(
1000,
&AccElem {
total: 10,
size: 0,
n: 1,
},
);
// Forward to time 1030 (30 seconds later)
latency.forward_to(1030);
@@ -637,9 +641,21 @@ mod tests {
latency.last_sec = current_time;
// Add data to multiple slots
latency.totals[0] = AccElem { total: 10, size: 100, n: 1 };
latency.totals[1] = AccElem { total: 20, size: 200, n: 2 };
latency.totals[59] = AccElem { total: 30, size: 300, n: 3 };
latency.totals[0] = AccElem {
total: 10,
size: 100,
n: 1,
};
latency.totals[1] = AccElem {
total: 20,
size: 200,
n: 2,
};
latency.totals[59] = AccElem {
total: 30,
size: 300,
n: 3,
};
let total = latency.get_total();
@@ -653,29 +669,20 @@ mod tests {
// Test that window index calculation works correctly
let _latency = LastMinuteLatency::default();
let acc_elem = AccElem {
total: 1,
size: 1,
n: 1,
};
let acc_elem = AccElem { total: 1, size: 1, n: 1 };
// Test various timestamps
let test_cases = [
(0, 0),
(1, 1),
(59, 59),
(60, 0),
(61, 1),
(119, 59),
(120, 0),
];
let test_cases = [(0, 0), (1, 1), (59, 59), (60, 0), (61, 1), (119, 59), (120, 0)];
for (timestamp, expected_idx) in test_cases {
let mut test_latency = LastMinuteLatency::default();
test_latency.add_all(timestamp, &acc_elem);
assert_eq!(test_latency.totals[expected_idx].n, 1,
"Failed for timestamp {} (expected index {})", timestamp, expected_idx);
assert_eq!(
test_latency.totals[expected_idx].n, 1,
"Failed for timestamp {} (expected index {})",
timestamp, expected_idx
);
}
}
+52 -26
View File
@@ -374,9 +374,9 @@ fn check_quorum_locked(locks: &[String], quorum: usize) -> bool {
#[cfg(test)]
mod tests {
use super::*;
use crate::local_locker::LocalLocker;
use async_trait::async_trait;
use common::error::{Error, Result};
use crate::local_locker::LocalLocker;
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
@@ -776,11 +776,7 @@ mod tests {
#[tokio::test]
async fn test_drw_mutex_multiple_resources() {
let names = vec![
"resource1".to_string(),
"resource2".to_string(),
"resource3".to_string(),
];
let names = vec!["resource1".to_string(), "resource2".to_string(), "resource3".to_string()];
let lockers = create_mock_lockers(1);
let mut mutex = DRWMutex::new("owner1".to_string(), names.clone(), lockers);
@@ -884,8 +880,8 @@ mod tests {
// Case 1: Even number of lockers
let locks = vec!["uid1".to_string(), "uid2".to_string(), "uid3".to_string(), "uid4".to_string()];
let tolerance = 2; // locks.len() / 2 = 4 / 2 = 2
// locks.len() - tolerance = 4 - 2 = 2, which equals tolerance
// So the special case applies: un_locks_failed >= tolerance
// locks.len() - tolerance = 4 - 2 = 2, which equals tolerance
// So the special case applies: un_locks_failed >= tolerance
// All 4 failed unlocks
assert!(check_failed_unlocks(&locks, tolerance)); // 4 >= 2 = true
@@ -901,8 +897,8 @@ mod tests {
// Case 2: Odd number of lockers
let locks = vec!["uid1".to_string(), "uid2".to_string(), "uid3".to_string()];
let tolerance = 1; // locks.len() / 2 = 3 / 2 = 1
// locks.len() - tolerance = 3 - 1 = 2, which does NOT equal tolerance (1)
// So the normal case applies: un_locks_failed > tolerance
// locks.len() - tolerance = 3 - 1 = 2, which does NOT equal tolerance (1)
// So the normal case applies: un_locks_failed > tolerance
// 3 failed unlocks
assert!(check_failed_unlocks(&locks, tolerance)); // 3 > 1 = true
@@ -946,11 +942,36 @@ mod tests {
}
let test_cases = vec![
QuorumTest { locker_count: 1, expected_tolerance: 0, expected_write_quorum: 1, expected_read_quorum: 1 },
QuorumTest { locker_count: 2, expected_tolerance: 1, expected_write_quorum: 2, expected_read_quorum: 1 },
QuorumTest { locker_count: 3, expected_tolerance: 1, expected_write_quorum: 2, expected_read_quorum: 2 },
QuorumTest { locker_count: 4, expected_tolerance: 2, expected_write_quorum: 3, expected_read_quorum: 2 },
QuorumTest { locker_count: 5, expected_tolerance: 2, expected_write_quorum: 3, expected_read_quorum: 3 },
QuorumTest {
locker_count: 1,
expected_tolerance: 0,
expected_write_quorum: 1,
expected_read_quorum: 1,
},
QuorumTest {
locker_count: 2,
expected_tolerance: 1,
expected_write_quorum: 2,
expected_read_quorum: 1,
},
QuorumTest {
locker_count: 3,
expected_tolerance: 1,
expected_write_quorum: 2,
expected_read_quorum: 2,
},
QuorumTest {
locker_count: 4,
expected_tolerance: 2,
expected_write_quorum: 3,
expected_read_quorum: 2,
},
QuorumTest {
locker_count: 5,
expected_tolerance: 2,
expected_write_quorum: 3,
expected_read_quorum: 3,
},
];
for test_case in test_cases {
@@ -963,12 +984,21 @@ mod tests {
write_quorum += 1;
}
assert_eq!(tolerance, test_case.expected_tolerance,
"Tolerance mismatch for {} lockers", test_case.locker_count);
assert_eq!(write_quorum, test_case.expected_write_quorum,
"Write quorum mismatch for {} lockers", test_case.locker_count);
assert_eq!(read_quorum, test_case.expected_read_quorum,
"Read quorum mismatch for {} lockers", test_case.locker_count);
assert_eq!(
tolerance, test_case.expected_tolerance,
"Tolerance mismatch for {} lockers",
test_case.locker_count
);
assert_eq!(
write_quorum, test_case.expected_write_quorum,
"Write quorum mismatch for {} lockers",
test_case.locker_count
);
assert_eq!(
read_quorum, test_case.expected_read_quorum,
"Read quorum mismatch for {} lockers",
test_case.locker_count
);
}
}
@@ -998,11 +1028,7 @@ mod tests {
#[test]
fn test_drw_mutex_new_with_unsorted_names() {
let names = vec![
"zebra".to_string(),
"alpha".to_string(),
"beta".to_string(),
];
let names = vec!["zebra".to_string(), "alpha".to_string(), "beta".to_string()];
let lockers = create_mock_lockers(1);
let mutex = DRWMutex::new("owner1".to_string(), names, lockers);