Attribute Macro ntest::timeout

source ·
#[timeout]
Expand description

The timeout attribute can be used for tests to let them fail if they exceed a certain execution time. With the #[timeout] attribute a timeout in milliseconds is added to a test.

The function input must be of type int. For example #[timeout(10)] will fail if the test takes longer than 10 milliseconds.

§Examples

This example will not panic

#[test]
#[timeout(100)]
fn no_timeout() {
    let fifty_millis = time::Duration::from_millis(50);
    thread::sleep(fifty_millis);
}

This example will panic and break the infinite loop after 10 milliseconds.

#[test]
#[timeout(10)]
#[should_panic]
fn timeout() {
    loop {};
}

Also works with test functions using a Result:

#[test]
#[timeout(100)]
fn timeout_with_result() -> Result<(), String> {
    let ten_millis = time::Duration::from_millis(10);
    thread::sleep(ten_millis);
    Ok(())
}