From 5b5104c27c3a641c822992d0e23976ef771439e0 Mon Sep 17 00:00:00 2001 From: axaysagathiya Date: Wed, 22 Apr 2026 11:13:04 +0530 Subject: [PATCH 1/3] return different min, max, avg from make_latency --- client/doublezero/src/dzd_latency.rs | 66 ++++++++++++++++++---------- 1 file changed, 42 insertions(+), 24 deletions(-) diff --git a/client/doublezero/src/dzd_latency.rs b/client/doublezero/src/dzd_latency.rs index 00cdc01cc7..f44f68babf 100644 --- a/client/doublezero/src/dzd_latency.rs +++ b/client/doublezero/src/dzd_latency.rs @@ -314,14 +314,20 @@ mod tests { ) } - fn make_latency(pk: &str, latency_ns: i64, reachable: bool) -> LatencyRecord { + fn make_latency( + pk: &str, + min_latency_ns: i64, + avg_latency_ns: i64, + max_latency_ns: i64, + reachable: bool, + ) -> LatencyRecord { LatencyRecord { device_pk: pk.to_string(), device_code: "device".to_string(), device_ip: "0.0.0.0".to_string(), - min_latency_ns: latency_ns, - max_latency_ns: latency_ns, - avg_latency_ns: latency_ns, + min_latency_ns, + avg_latency_ns, + max_latency_ns, reachable, } } @@ -338,9 +344,9 @@ mod tests { devices.insert(pk3, dev3); let latencies = vec![ - make_latency(&pk1.to_string(), 10000000, true), - make_latency(&pk2.to_string(), 20000000, false), - make_latency(&pk3.to_string(), 5000000, true), + make_latency(&pk1.to_string(), 10000000, 11000000, 12000000, true), + make_latency(&pk2.to_string(), 20000000, 21000000, 22000000, false), + make_latency(&pk3.to_string(), 5000000, 5000000, 5000000, true), ]; let mut controller = MockServiceController::new(); @@ -377,8 +383,8 @@ mod tests { devices.insert(pk2, dev2); let latencies = vec![ - make_latency(&pk1.to_string(), 10000000, true), - make_latency(&pk2.to_string(), 11000000, true), + make_latency(&pk1.to_string(), 10000000, 10500000, 11000000, true), + make_latency(&pk2.to_string(), 11000000, 11500000, 12000000, true), ]; let mut controller = MockServiceController::new(); @@ -408,9 +414,9 @@ mod tests { devices.insert(pk3, dev3); let latencies = vec![ - make_latency(&pk1.to_string(), 12000000, true), - make_latency(&pk2.to_string(), 9000000, true), - make_latency(&pk3.to_string(), 15000000, true), + make_latency(&pk1.to_string(), 12000000, 13000000, 14000000, true), + make_latency(&pk2.to_string(), 9000000, 10000000, 11000000, true), + make_latency(&pk3.to_string(), 15000000, 16000000, 17000000, true), ]; let mut controller = MockServiceController::new(); @@ -440,9 +446,9 @@ mod tests { devices.insert(pk3, dev3); let latencies = vec![ - make_latency(&pk1.to_string(), 12000000, false), // unreachable - make_latency(&pk2.to_string(), 9000000, false), // unreachable - make_latency(&pk3.to_string(), 15000000, true), // reachable + make_latency(&pk1.to_string(), 12000000, 13000000, 14000000, false), // unreachable + make_latency(&pk2.to_string(), 9000000, 10000000, 11000000, false), // unreachable + make_latency(&pk3.to_string(), 15000000, 16000000, 17000000, true), // reachable ]; let mut controller = MockServiceController::new(); @@ -470,8 +476,8 @@ mod tests { devices.insert(pk2, dev2); let latencies = vec![ - make_latency(&pk1.to_string(), 9000000, true), - make_latency(&pk2.to_string(), 12000000, true), + make_latency(&pk1.to_string(), 9000000, 10000000, 11000000, true), + make_latency(&pk2.to_string(), 12000000, 13000000, 14000000, true), ]; let mut controller = MockServiceController::new(); @@ -499,8 +505,8 @@ mod tests { devices.insert(pk2, dev2); let latencies = vec![ - make_latency(&pk1.to_string(), 12000000, true), - make_latency(&pk2.to_string(), 9000000, true), + make_latency(&pk1.to_string(), 12000000, 13000000, 14000000, true), + make_latency(&pk2.to_string(), 9000000, 10000000, 11000000, true), ]; let mut controller = MockServiceController::new(); @@ -534,9 +540,9 @@ mod tests { devices.insert(pk3, dev3); let latencies = vec![ - make_latency(&pk1.to_string(), 12000000, true), - make_latency(&pk2.to_string(), 5000000, true), // lowest but will be excluded - make_latency(&pk3.to_string(), 15000000, true), + make_latency(&pk1.to_string(), 12000000, 13000000, 14000000, true), + make_latency(&pk2.to_string(), 5000000, 6000000, 7000000, true), // lowest but will be excluded + make_latency(&pk3.to_string(), 15000000, 16000000, 17000000, true), ]; let mut controller = MockServiceController::new(); @@ -1134,7 +1140,13 @@ mod tests { let mut devices = HashMap::new(); devices.insert(pk1, dev1); - let latencies = vec![make_latency(&pk1.to_string(), 10000000, true)]; + let latencies = vec![make_latency( + &pk1.to_string(), + 10000000, + 11000000, + 12000000, + true, + )]; let call_count = std::sync::Arc::new(std::sync::atomic::AtomicU32::new(0)); let call_count_clone = call_count.clone(); let latencies_clone = latencies.clone(); @@ -1190,7 +1202,13 @@ mod tests { devices.insert(pk1, dev1); // Device exists and is activated, but unreachable - let latencies = vec![make_latency(&pk1.to_string(), 10000000, false)]; + let latencies = vec![make_latency( + &pk1.to_string(), + 10000000, + 11000000, + 12000000, + false, + )]; let mut controller = MockServiceController::new(); controller.expect_latency().returning(move || { From 1650f396a9de5e6a1f07623ad947d9eeef6ef218 Mon Sep 17 00:00:00 2001 From: axaysagathiya Date: Thu, 23 Apr 2026 19:37:43 +0530 Subject: [PATCH 2/3] Make latency selection tests adversarial --- client/doublezero/src/dzd_latency.rs | 46 +++++----------------------- 1 file changed, 7 insertions(+), 39 deletions(-) diff --git a/client/doublezero/src/dzd_latency.rs b/client/doublezero/src/dzd_latency.rs index f44f68babf..6093ccb303 100644 --- a/client/doublezero/src/dzd_latency.rs +++ b/client/doublezero/src/dzd_latency.rs @@ -414,9 +414,9 @@ mod tests { devices.insert(pk3, dev3); let latencies = vec![ - make_latency(&pk1.to_string(), 12000000, 13000000, 14000000, true), - make_latency(&pk2.to_string(), 9000000, 10000000, 11000000, true), - make_latency(&pk3.to_string(), 15000000, 16000000, 17000000, true), + make_latency(&pk1.to_string(), 12_000_000, 5_000_000, 14_000_000, true), + make_latency(&pk2.to_string(), 9_000_000, 20_000_000, 11_000_000, true), + make_latency(&pk3.to_string(), 15_000_000, 15_000_000, 17_000_000, true), ]; let mut controller = MockServiceController::new(); @@ -968,24 +968,8 @@ mod tests { // pk1: low min, high avg. pk2: high min, low avg. // Sorted by min: pk1 first. Sorted by avg: pk2 first. let latencies = vec![ - LatencyRecord { - device_pk: pk1.to_string(), - device_code: "device".to_string(), - device_ip: "0.0.0.0".to_string(), - min_latency_ns: 5_000_000, // lower min → should be first - max_latency_ns: 30_000_000, - avg_latency_ns: 25_000_000, // higher avg - reachable: true, - }, - LatencyRecord { - device_pk: pk2.to_string(), - device_code: "device".to_string(), - device_ip: "0.0.0.0".to_string(), - min_latency_ns: 15_000_000, // higher min - max_latency_ns: 20_000_000, - avg_latency_ns: 8_000_000, // lower avg → would be first if sorted by avg - reachable: true, - }, + make_latency(&pk1.to_string(), 5_000_000, 25_000_000, 30_000_000, true), + make_latency(&pk2.to_string(), 15_000_000, 8_000_000, 20_000_000, true), ]; let mut controller = MockServiceController::new(); @@ -1070,24 +1054,8 @@ mod tests { devices.insert(pk2, dev2); let latencies = vec![ - LatencyRecord { - device_pk: pk1.to_string(), - device_code: "device".to_string(), - device_ip: "0.0.0.0".to_string(), - min_latency_ns: 5_000_000, // lower min → should win - max_latency_ns: 30_000_000, - avg_latency_ns: 25_000_000, // higher avg - reachable: true, - }, - LatencyRecord { - device_pk: pk2.to_string(), - device_code: "device".to_string(), - device_ip: "0.0.0.0".to_string(), - min_latency_ns: 13_000_000, // higher min → current device - max_latency_ns: 20_000_000, - avg_latency_ns: 6_000_000, // lower avg (would anchor tolerance if avg-seeded) - reachable: true, - }, + make_latency(&pk1.to_string(), 5_000_000, 25_000_000, 30_000_000, true), + make_latency(&pk2.to_string(), 13_000_000, 6_000_000, 20_000_000, true), ]; let mut controller = MockServiceController::new(); From ab52ff51ce573547718e97833bc99db0b7263600 Mon Sep 17 00:00:00 2001 From: axaysagathiya Date: Fri, 24 Apr 2026 07:51:43 +0530 Subject: [PATCH 3/3] clean up --- client/doublezero/src/dzd_latency.rs | 34 ++++++++-------------------- 1 file changed, 9 insertions(+), 25 deletions(-) diff --git a/client/doublezero/src/dzd_latency.rs b/client/doublezero/src/dzd_latency.rs index 6093ccb303..fbe4e0a8e3 100644 --- a/client/doublezero/src/dzd_latency.rs +++ b/client/doublezero/src/dzd_latency.rs @@ -414,9 +414,9 @@ mod tests { devices.insert(pk3, dev3); let latencies = vec![ - make_latency(&pk1.to_string(), 12_000_000, 5_000_000, 14_000_000, true), - make_latency(&pk2.to_string(), 9_000_000, 20_000_000, 11_000_000, true), - make_latency(&pk3.to_string(), 15_000_000, 15_000_000, 17_000_000, true), + make_latency(&pk1.to_string(), 12000000, 5000000, 14000000, true), + make_latency(&pk2.to_string(), 9000000, 20000000, 11000000, true), + make_latency(&pk3.to_string(), 15000000, 15000000, 17000000, true), ]; let mut controller = MockServiceController::new(); @@ -968,8 +968,8 @@ mod tests { // pk1: low min, high avg. pk2: high min, low avg. // Sorted by min: pk1 first. Sorted by avg: pk2 first. let latencies = vec![ - make_latency(&pk1.to_string(), 5_000_000, 25_000_000, 30_000_000, true), - make_latency(&pk2.to_string(), 15_000_000, 8_000_000, 20_000_000, true), + make_latency(&pk1.to_string(), 5000000, 25000000, 30000000, true), + make_latency(&pk2.to_string(), 15000000, 8000000, 20000000, true), ]; let mut controller = MockServiceController::new(); @@ -1001,24 +1001,8 @@ mod tests { // pk1: low min, high avg. pk2: high min, low avg. // Should select pk1 (lower min). let latencies = vec![ - LatencyRecord { - device_pk: pk1.to_string(), - device_code: "device".to_string(), - device_ip: "0.0.0.0".to_string(), - min_latency_ns: 5_000_000, // lower min → should win - max_latency_ns: 30_000_000, - avg_latency_ns: 25_000_000, // higher avg - reachable: true, - }, - LatencyRecord { - device_pk: pk2.to_string(), - device_code: "device".to_string(), - device_ip: "0.0.0.0".to_string(), - min_latency_ns: 15_000_000, // higher min → should lose - max_latency_ns: 20_000_000, - avg_latency_ns: 8_000_000, // lower avg → would win if sorted by avg - reachable: true, - }, + make_latency(&pk1.to_string(), 5000000, 25000000, 30000000, true), + make_latency(&pk2.to_string(), 15000000, 20000000, 8000000, true), ]; let mut controller = MockServiceController::new(); @@ -1054,8 +1038,8 @@ mod tests { devices.insert(pk2, dev2); let latencies = vec![ - make_latency(&pk1.to_string(), 5_000_000, 25_000_000, 30_000_000, true), - make_latency(&pk2.to_string(), 13_000_000, 6_000_000, 20_000_000, true), + make_latency(&pk1.to_string(), 5000000, 25000000, 30000000, true), + make_latency(&pk2.to_string(), 13000000, 6000000, 20000000, true), ]; let mut controller = MockServiceController::new();