Skip to content

Commit 046d8eb

Browse files
authored
test: Allow to omit the timeout for assert_recv_with_timeout (matrix-org#5814)
Add some documentation to it while we're at it as well.
1 parent 896f411 commit 046d8eb

File tree

1 file changed

+35
-0
lines changed
  • crates/matrix-sdk/src/test_utils

1 file changed

+35
-0
lines changed

crates/matrix-sdk/src/test_utils/mod.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,43 @@ macro_rules! assert_next_with_timeout {
136136

137137
/// Asserts the next item in a `Receiver` can be loaded in the given timeout in
138138
/// milliseconds.
139+
///
140+
/// This macro waits for the next item from a `Receiver` or, if no
141+
/// item is received within the specified timeout, the macro panics.
142+
///
143+
/// # Parameters
144+
///
145+
/// - `$receiver`: The receiver to poll for the next item.
146+
/// - `$timeout_ms` (optional): The timeout in milliseconds to wait for the next
147+
/// item. Defaults to 500ms if not provided.
148+
///
149+
/// # Example
150+
///
151+
/// ```rust
152+
/// use matrix_sdk::assert_recv_with_timeout;
153+
/// use tokio::sync::mpsc;
154+
///
155+
/// # async {
156+
/// let (tx, mut rx) = mpsc::channel(10);
157+
/// tx.send(1);
158+
///
159+
/// let next_item = assert_recv_with_timeout!(rx, 1000); // Waits up to 1000ms
160+
/// assert_eq!(next_item, 1);
161+
///
162+
/// let (tx, mut rx) = mpsc::channel(10);
163+
/// tx.send(2);
164+
///
165+
/// // The timeout can be omitted, in which case it defaults to 500 ms.
166+
/// let next_item = assert_recv_with_timeout!(rx); // Waits up to 500ms
167+
/// assert_eq!(next_item, 2);
168+
/// # };
169+
/// ```
139170
#[macro_export]
140171
macro_rules! assert_recv_with_timeout {
172+
($receiver:expr) => {
173+
$crate::assert_recv_with_timeout!($receiver, 500)
174+
};
175+
141176
($receiver:expr, $timeout_ms:expr) => {{
142177
tokio::time::timeout(std::time::Duration::from_millis($timeout_ms), $receiver.recv())
143178
.await

0 commit comments

Comments
 (0)