Skip to content

Commit e5f1efa

Browse files
authored
Fix flaky tests (#511)
* chore: use a proper tempdir like the other tests * chore: use `BTreeSet` to ensure `Role`s are in order * chore: make `naive_mul` a little less naive so tests run at tolerable speed * chore: use multiple threads to speed up dkg test * chore:formatting * chore: use multi threaded tokio test for tests involving multiple parties * chore: gate heavy tests in threshold-experimental under `slow_tests` * Attempt to fix another set of flaky test * Revert "Attempt to fix another set of flaky test" This reverts commit c61a37a.
1 parent 0d43add commit e5f1efa

6 files changed

Lines changed: 33 additions & 26 deletions

File tree

core/service/src/client/tests/threshold/custodian_backup_tests.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ async fn auto_update_backup(amount_custodians: usize, threshold: u32) {
7373
))
7474
.unwrap();
7575

76-
let test_path = None;
76+
let temp_dir = tempfile::tempdir().unwrap();
77+
let test_path = Some(temp_dir.path());
7778
let dkg_param: WrappedDKGParams = FheParameter::Test.into();
7879
tokio::time::sleep(tokio::time::Duration::from_millis(TIME_TO_SLEEP_MS)).await;
7980
let (kms_servers, kms_clients, mut internal_client) = threshold_handles_custodian_backup(

core/threshold-experimental/src/algebra/ntt.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -226,27 +226,27 @@ mod tests {
226226

227227
fn naive_mul<T>(a: &[T], b: &[T], n: usize) -> Vec<T>
228228
where
229-
T: Zero + Copy,
229+
T: Zero + Copy + Send + Sync,
230230
T: Add<Output = T>,
231231
T: Mul<Output = T>,
232232
T: Sub<Output = T>,
233233
{
234-
let mut c = Vec::with_capacity(n);
235-
for _ in 0..n {
236-
c.push(T::ZERO);
237-
}
234+
use rayon::prelude::*;
238235

239-
for i in 0..n {
240-
for j in 0..n {
241-
let w = c[(i + j) % n];
242-
if (i + j) < n {
243-
c[i + j] = w + a[i] * b[j];
244-
} else {
245-
c[(i + j) % n] = w - a[i] * b[j]
236+
(0..n)
237+
.into_par_iter()
238+
.map(|k| {
239+
let mut pos = T::ZERO;
240+
let mut neg = T::ZERO;
241+
for j in 0..=k {
242+
pos = pos + a[k - j] * b[j];
246243
}
247-
}
248-
}
249-
c
244+
for j in (k + 1)..n {
245+
neg = neg + a[k + n - j] * b[j];
246+
}
247+
pos - neg
248+
})
249+
.collect()
250250
}
251251

252252
#[test]

core/threshold-experimental/src/bgv/dkg.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ mod tests {
250250
assert_eq!(plaintext, plaintext_vec);
251251
}
252252

253-
#[tokio::test]
253+
#[tokio::test(flavor = "multi_thread")]
254254
async fn test_dkg_dummy_preproc() {
255255
let parties = 5;
256256
let threshold = 1;
@@ -286,7 +286,7 @@ mod tests {
286286
test_dkg(&mut results, PLAINTEXT_MODULUS.get().0);
287287
}
288288

289-
#[tokio::test]
289+
#[tokio::test(flavor = "multi_thread")]
290290
async fn test_dkg_with_offline() {
291291
let parties = 5;
292292
let threshold = 1;

core/threshold-experimental/src/bgv/endpoints.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use algebra::sharing::share::Share;
1313
use hashing::serialize_hash_element;
1414
use itertools::Itertools;
1515
use rand::SeedableRng;
16-
use std::collections::HashMap;
16+
use std::collections::{HashMap, HashSet};
1717
use std::sync::Arc;
1818
use threshold_execution::runtime::sessions::{
1919
base_session::BaseSession,
@@ -64,8 +64,13 @@ pub fn threshold_decrypt(
6464
let net = Arc::clone(&runtime.user_nets[role.one_based() - 1]);
6565
let threshold = runtime.threshold;
6666

67-
let session_params =
68-
SessionParameters::new(threshold, session_id, role, runtime.roles.clone()).unwrap();
67+
let session_params = SessionParameters::new(
68+
threshold,
69+
session_id,
70+
role,
71+
runtime.roles.iter().copied().collect::<HashSet<_>>(),
72+
)
73+
.unwrap();
6974
let base_session = BaseSession::new(session_params, net, AesRng::from_entropy()).unwrap();
7075

7176
let sk_shares = private_keys[&role]

core/threshold-experimental/src/bgv/runtime.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::collections::{HashMap, HashSet};
1+
use std::collections::{BTreeSet, HashMap, HashSet};
22
use std::sync::Arc;
33
use tokio::time::Duration;
44

@@ -9,7 +9,7 @@ use threshold_types::role::Role;
99
pub struct BGVTestRuntime {
1010
pub threshold: u8,
1111
pub user_nets: Vec<Arc<LocalNetworking<Role>>>,
12-
pub roles: HashSet<Role>,
12+
pub roles: BTreeSet<Role>,
1313
}
1414

1515
impl BGVTestRuntime {
@@ -20,7 +20,8 @@ impl BGVTestRuntime {
2020
delayed_map: Option<HashMap<Role, Duration>>,
2121
) -> Self {
2222
let net_producer = LocalNetworkingProducer::from_roles(&roles);
23-
let user_nets: Vec<Arc<LocalNetworking<Role>>> = roles
23+
let sorted_roles: BTreeSet<Role> = roles.into_iter().collect();
24+
let user_nets: Vec<Arc<LocalNetworking<Role>>> = sorted_roles
2425
.iter()
2526
.map(|role| {
2627
let delay = if let Some(delayed_map) = &delayed_map {
@@ -36,7 +37,7 @@ impl BGVTestRuntime {
3637
BGVTestRuntime {
3738
threshold,
3839
user_nets,
39-
roles,
40+
roles: sorted_roles,
4041
}
4142
}
4243
}

core/threshold-experimental/src/gen_bits_odd.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ mod tests {
159159

160160
use super::*;
161161

162-
#[tokio::test]
162+
#[tokio::test(flavor = "multi_thread")]
163163
async fn test_bitgen() {
164164
let parties = 4;
165165
let threshold = 1;

0 commit comments

Comments
 (0)