From e5b9b972b2e61a0bda96f1fadb2fa3b60a4b7690 Mon Sep 17 00:00:00 2001 From: Alan Somers Date: Sun, 2 Jan 2022 14:36:24 -0700 Subject: Fix intermittency in test_timer::alarm_fires The test was disabling the signal handler before disabling the timer. Fix intermittent failures by: * Reversing the cleanup order. * Sleeping for a while before removing the signal handler, since POSIX does not guarantee that timer_delete will clear pending signals. Also, speed up the timer to make the test suite complete faster. --- test/test_timer.rs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) (limited to 'test/test_timer.rs') diff --git a/test/test_timer.rs b/test/test_timer.rs index 31a740b0..d07d9633 100644 --- a/test/test_timer.rs +++ b/test/test_timer.rs @@ -23,6 +23,7 @@ fn alarm_fires() { // Avoid interfering with other signal using tests by taking a mutex shared // among other tests in this crate. let _m = crate::SIGNAL_MTX.lock(); + const TIMER_PERIOD: Duration = Duration::from_millis(100); // // Setup @@ -44,7 +45,7 @@ fn alarm_fires() { si_value: 0, }); let mut timer = Timer::new(clockid, sigevent).expect("failed to create timer"); - let expiration = Expiration::Interval(Duration::from_millis(250).into()); + let expiration = Expiration::Interval(TIMER_PERIOD.into()); let flags = TimerSetTimeFlags::empty(); timer.set(expiration, flags).expect("could not set timer"); @@ -73,7 +74,7 @@ fn alarm_fires() { // is never called something has gone sideways and the test fails. let starttime = Instant::now(); loop { - thread::sleep(Duration::from_millis(500)); + thread::sleep(2 * TIMER_PERIOD); if ALARM_CALLED.load(Ordering::Acquire) { break; } @@ -82,9 +83,16 @@ fn alarm_fires() { } } - // Replace the old signal handler now that we've completed the test. If the - // test fails this process panics, so the fact we might not get here is - // okay. + // Cleanup: + // 1) deregister the OS's timer. + // 2) Wait for a full timer period, since POSIX does not require that + // disabling the timer will clear pending signals, and on NetBSD at least + // it does not. + // 2) Replace the old signal handler now that we've completed the test. If + // the test fails this process panics, so the fact we might not get here + // is okay. + drop(timer); + thread::sleep(TIMER_PERIOD); unsafe { sigaction(SIG, &old_handler).expect("unable to reset signal handler"); } -- cgit v1.2.3