Skip to content

Commit

Permalink
task: don't assert task ID in tests for JoinError debug and display…
Browse files Browse the repository at this point in the history
… output
  • Loading branch information
abonander committed Aug 6, 2024
1 parent ab656ea commit ed37000
Showing 1 changed file with 46 additions and 14 deletions.
60 changes: 46 additions & 14 deletions tokio/tests/task_abort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,27 +238,43 @@ fn test_join_error_display() {
.await
.unwrap_err();

assert_eq!(
join_err.to_string(),
"task 1 panicked with message \"Format-args payload: 1234\""
// We can't assert the full output because the task ID can change.
let join_err_str = join_err.to_string();

assert!(
join_err_str.starts_with("task ")
&& join_err_str.ends_with(" panicked with message \"Format-args payload: 1234\""),
"Unexpected join_err_str {:?}",
join_err_str
);

// `&'static str` payload
let join_err = tokio::spawn(async move { panic!("Const payload") })
.await
.unwrap_err();

assert_eq!(
join_err.to_string(),
"task 2 panicked with message \"Const payload\""
let join_err_str = join_err.to_string();

assert!(
join_err_str.starts_with("task ")
&& join_err_str.ends_with(" panicked with message \"Const payload\""),
"Unexpected join_err_str {:?}",
join_err_str
);

// Non-string payload
let join_err = tokio::spawn(async move { std::panic::panic_any(1234i32) })
.await
.unwrap_err();

assert_eq!(join_err.to_string(), "task 3 panicked");
let join_err_str = join_err.to_string();

assert!(
join_err_str.starts_with("task ")
&& join_err_str.ends_with(" panicked"),
"Unexpected join_err_str {:?}",
join_err_str
);
});
}

Expand All @@ -277,26 +293,42 @@ fn test_join_error_debug() {
.await
.unwrap_err();

assert_eq!(
format!("{:?}", join_err),
"JoinError::Panic(Id(1), \"Format-args payload: 1234\", ...)"
// We can't assert the full output because the task ID can change.
let join_err_str = format!("{:?}", join_err);

assert!(
join_err_str.starts_with("JoinError::Panic(Id(")
&& join_err_str.ends_with("), \"Format-args payload: 1234\", ...)"),
"Unexpected join_err_str {:?}",
join_err_str
);

// `&'static str` payload
let join_err = tokio::spawn(async move { panic!("Const payload") })
.await
.unwrap_err();

assert_eq!(
format!("{:?}", join_err),
"JoinError::Panic(Id(2), \"Const payload\", ...)"
let join_err_str = format!("{:?}", join_err);

assert!(
join_err_str.starts_with("JoinError::Panic(Id(")
&& join_err_str.ends_with("), \"Const payload\", ...)"),
"Unexpected join_err_str {:?}",
join_err_str
);

// Non-string payload
let join_err = tokio::spawn(async move { std::panic::panic_any(1234i32) })
.await
.unwrap_err();

assert_eq!(format!("{:?}", join_err), "JoinError::Panic(Id(3), ...)");
let join_err_str = format!("{:?}", join_err);

assert!(
join_err_str.starts_with("JoinError::Panic(Id(")
&& join_err_str.ends_with("), ...)"),
"Unexpected join_err_str {:?}",
join_err_str
);
});
}

0 comments on commit ed37000

Please sign in to comment.