Skip to content

Commit

Permalink
test: Make the timeout parameter in the assert_next_with_timeout macr…
Browse files Browse the repository at this point in the history
…o optional
  • Loading branch information
poljar committed Jan 31, 2025
1 parent 3d8114c commit 4dba051
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions crates/matrix-sdk/src/test_utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,38 @@ pub async fn logged_in_client_with_server() -> (Client, wiremock::MockServer) {
(client, server)
}

/// Asserts the next item in a `Stream` or `Subscriber` can be loaded in the
/// given timeout in the given timeout in milliseconds.
/// Asserts that the next item in a `Stream` is received within a given timeout.
///
/// This macro waits for the next item from an asynchronous `Stream` or, if no
/// item is received within the specified timeout, the macro panics.
///
/// # Parameters
///
/// - `$stream`: The `Stream` or `Subscriber` to poll for the next item.
/// - `$timeout_ms` (optional): The timeout in milliseconds to wait for the next
/// item. Defaults to 500ms if not provided.
///
/// # Example
///
/// ```rust
/// use futures_util::{stream, StreamExt};
/// use matrix_sdk::assert_next_with_timeout;
///
/// # async {
/// let mut stream = stream::iter(vec![1, 2, 3]);
/// let next_item = assert_next_with_timeout!(stream, 1000); // Waits up to 1000ms
/// assert_eq!(next_item, 1);
///
/// // The timeout can be omitted, in which case it defaults to 100 ms.
/// let next_item = assert_next_with_timeout!(stream); // Waits up to 100ms
/// assert_eq!(next_item, 2);
/// # };
/// ```
#[macro_export]
macro_rules! assert_next_with_timeout {
($stream:expr) => {
$crate::assert_next_with_timeout!($stream, 500)
};
($stream:expr, $timeout_ms:expr) => {{
// Needed for subscribers, as they won't use the StreamExt features
#[allow(unused_imports)]
Expand Down

0 comments on commit 4dba051

Please sign in to comment.