Skip to content

Commit bf17f13

Browse files
authored
Rollup merge of #142224 - joshtriplett:remove-gratuitous-wait-in-stress-test, r=workingjubilee
Avoid a gratuitous 10s wait in a stress test `stress_recv_timeout_two_threads`, in the mpmc and mpsc testsuites, is a stress test of the `recv_timeout` function. This test processes and ignores timeouts, and just ensures that every sent value gets received. As such, the exact length of the timeouts is not critical, only that the timeout and sleep durations ensure that at least one timeout occurred. The current tests have 100 iterations, half of which sleep for 200ms, causing the test to take 10s. This represents around 2/3rds of the *total* runtime of the `library/std` testsuite, and is the only standard library test that takes more than a second. Reduce this to 50 iterations where half of them sleep for 10ms, causing the test to take 0.25s. Add a check that at least one timeout occurred.
2 parents a9ba52b + 889f7cb commit bf17f13

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

library/std/tests/sync/mpmc.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -462,8 +462,8 @@ fn oneshot_single_thread_recv_timeout() {
462462
#[test]
463463
fn stress_recv_timeout_two_threads() {
464464
let (tx, rx) = channel();
465-
let stress = stress_factor() + 100;
466-
let timeout = Duration::from_millis(100);
465+
let stress = stress_factor() + 50;
466+
let timeout = Duration::from_millis(5);
467467

468468
thread::spawn(move || {
469469
for i in 0..stress {
@@ -475,18 +475,23 @@ fn stress_recv_timeout_two_threads() {
475475
});
476476

477477
let mut recv_count = 0;
478+
let mut got_timeout = false;
478479
loop {
479480
match rx.recv_timeout(timeout) {
480481
Ok(n) => {
481482
assert_eq!(n, 1usize);
482483
recv_count += 1;
483484
}
484-
Err(RecvTimeoutError::Timeout) => continue,
485+
Err(RecvTimeoutError::Timeout) => {
486+
got_timeout = true;
487+
continue;
488+
}
485489
Err(RecvTimeoutError::Disconnected) => break,
486490
}
487491
}
488492

489493
assert_eq!(recv_count, stress);
494+
assert!(got_timeout);
490495
}
491496

492497
#[test]

library/std/tests/sync/mpsc.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -425,8 +425,8 @@ fn oneshot_single_thread_recv_timeout() {
425425
#[test]
426426
fn stress_recv_timeout_two_threads() {
427427
let (tx, rx) = channel();
428-
let stress = stress_factor() + 100;
429-
let timeout = Duration::from_millis(100);
428+
let stress = stress_factor() + 50;
429+
let timeout = Duration::from_millis(5);
430430

431431
thread::spawn(move || {
432432
for i in 0..stress {
@@ -438,18 +438,23 @@ fn stress_recv_timeout_two_threads() {
438438
});
439439

440440
let mut recv_count = 0;
441+
let mut got_timeout = false;
441442
loop {
442443
match rx.recv_timeout(timeout) {
443444
Ok(n) => {
444445
assert_eq!(n, 1usize);
445446
recv_count += 1;
446447
}
447-
Err(RecvTimeoutError::Timeout) => continue,
448+
Err(RecvTimeoutError::Timeout) => {
449+
got_timeout = true;
450+
continue;
451+
}
448452
Err(RecvTimeoutError::Disconnected) => break,
449453
}
450454
}
451455

452456
assert_eq!(recv_count, stress);
457+
assert!(got_timeout);
453458
}
454459

455460
#[test]

0 commit comments

Comments
 (0)