Skip to content

Commit

Permalink
add default timeout environment variable RSTEST_TIMEOUT (#191)
Browse files Browse the repository at this point in the history
* add default timeout environment variable RSTEST_TIMEOUT

* added tests
  • Loading branch information
aviramha committed Mar 24, 2023
1 parent c16601d commit dea3bea
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 3 deletions.
5 changes: 5 additions & 0 deletions README.md
Expand Up @@ -204,6 +204,11 @@ async fn single(#[future] base: u32, #[case] expected: u32, #[future(awt)] #[cas
}
```

### Default timeout

You can set a default timeout for test using the `RSTEST_TIMEOUT` environment variable.
The value is in seconds and is evaluated on test compile time.

### Test `#[timeout()]`

You can define an execution timeout for your tests with `#[timeout(<duration>)]` attribute. Timeouts
Expand Down
7 changes: 6 additions & 1 deletion rstest/tests/resources/rstest/timeout.rs
Expand Up @@ -24,7 +24,7 @@ mod thread {
fn single_fail_value() {
assert_eq!(5, delayed_sum(2, 2, ms(1)));
}

#[rstest]
#[timeout(ms(1000))]
#[should_panic = "user message"]
Expand Down Expand Up @@ -106,6 +106,11 @@ mod thread {
fn compile_with_no_copy_fixture(no_copy: S) {
assert!(true);
}

#[rstest]
fn default_timeout_failure() {
assert_eq!(4, delayed_sum(2, 2, ms(1001)));
}
}

mod async_std_cases {
Expand Down
4 changes: 3 additions & 1 deletion rstest/tests/rstest/mod.rs
Expand Up @@ -917,8 +917,9 @@ fn ignore_underscore_args() {

#[test]
fn timeout() {
let prj = prj("timeout.rs");
let mut prj = prj("timeout.rs");
prj.add_dependency("async-std", r#"{version="*", features=["attributes"]}"#);
prj.set_default_timeout(1);
let output = prj.run_tests().unwrap();

TestResults::new()
Expand All @@ -940,6 +941,7 @@ fn timeout() {
.fail("thread::group_one_timeout_override::case_3_fail_value")
.ok("thread::compile_with_no_copy_arg::case_1")
.ok("thread::compile_with_no_copy_fixture")
.fail("thread::default_timeout_failure")
.ok("async_std_cases::single_pass")
.fail("async_std_cases::single_fail_value")
.ok("async_std_cases::fail_with_user_message")
Expand Down
2 changes: 1 addition & 1 deletion rstest_macros/src/parse/expressions.rs
Expand Up @@ -25,4 +25,4 @@ impl From<Expressions> for Vec<Expr> {
fn from(expressions: Expressions) -> Self {
expressions.0
}
}
}
5 changes: 5 additions & 0 deletions rstest_macros/src/render/mod.rs
Expand Up @@ -206,6 +206,11 @@ fn render_test_call(
timeout: Option<Expr>,
is_async: bool,
) -> TokenStream {
let timeout = timeout.map(|x| quote! {#x}).or_else(|| {
std::env::var("RSTEST_TIMEOUT")
.ok()
.and_then(|to| Some(quote! { std::time::Duration::from_secs( (#to).parse().unwrap()) }))
});
match (timeout, is_async) {
(Some(to_expr), true) => quote! {
use rstest::timeout::*;
Expand Down
12 changes: 12 additions & 0 deletions rstest_test/src/prj.rs
Expand Up @@ -49,6 +49,7 @@ pub struct Project {
channel: Channel,
nocapture: bool,
ws: Arc<std::sync::RwLock<()>>,
default_timeout: Option<u64>,
}

impl Project {
Expand All @@ -61,6 +62,7 @@ impl Project {
channel: Default::default(),
nocapture: false,
ws: Arc::new(std::sync::RwLock::new(())),
default_timeout: Default::default(),
}
.create()
}
Expand All @@ -83,6 +85,7 @@ impl Project {
channel: self.channel.clone(),
nocapture: self.nocapture,
ws: self.ws.clone(),
default_timeout: Default::default(),
}
.create()
}
Expand All @@ -103,6 +106,10 @@ impl Project {
}
let mut cmd = Command::new("cargo");

if let Some(timeout) = self.default_timeout {
cmd.env("RSTEST_TIMEOUT", timeout.to_string());
}

cmd.current_dir(&self.path())
.arg(&self.cargo_channel_arg())
.arg("test");
Expand Down Expand Up @@ -250,4 +257,9 @@ impl Project {
Channel::Custom(name) => format!("+{name}"),
}
}

// in seconds
pub fn set_default_timeout(&mut self, timeout: u64) {
self.default_timeout = Some(timeout);
}
}

0 comments on commit dea3bea

Please sign in to comment.