forked from tokio-rs/tokio
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
stream: add track_caller to public APIs
Functions that may panic can be annotated with `#[track_caller]` so that in the event of a panic, the function where the user called the panicking function is shown instead of the file and line within Tokio source. This change adds `#[track_caller]` to all the non-unstable public APIs in tokio-stream (only `chunks_timeout` in `StreamExt`) where the documentation describes how this function may panic due to incorrect input. A test has been included to cover the panic. Refs: tokio-rs#4413
- Loading branch information
Showing
3 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#![warn(rust_2018_idioms)] | ||
#![cfg(feature = "time")] | ||
|
||
use parking_lot::{const_mutex, Mutex}; | ||
use std::error::Error; | ||
use std::panic; | ||
use std::sync::Arc; | ||
use tokio::time::Duration; | ||
use tokio_stream::{self as stream, StreamExt}; | ||
|
||
fn test_panic<Func: FnOnce() + panic::UnwindSafe>(func: Func) -> Option<String> { | ||
static PANIC_MUTEX: Mutex<()> = const_mutex(()); | ||
|
||
{ | ||
let _guard = PANIC_MUTEX.lock(); | ||
let panic_file: Arc<Mutex<Option<String>>> = Arc::new(Mutex::new(None)); | ||
|
||
let prev_hook = panic::take_hook(); | ||
{ | ||
let panic_file = panic_file.clone(); | ||
panic::set_hook(Box::new(move |panic_info| { | ||
let panic_location = panic_info.location().unwrap(); | ||
panic_file | ||
.lock() | ||
.clone_from(&Some(panic_location.file().to_string())); | ||
})); | ||
} | ||
|
||
let result = panic::catch_unwind(func); | ||
// Return to the previously set panic hook (maybe default) so that we get nice error | ||
// messages in the tests. | ||
panic::set_hook(prev_hook); | ||
|
||
if result.is_err() { | ||
panic_file.lock().clone() | ||
} else { | ||
None | ||
} | ||
} | ||
} | ||
|
||
#[test] | ||
fn stream_chunks_timeout_panic_caller() -> Result<(), Box<dyn Error>> { | ||
let panic_location_file = test_panic(|| { | ||
let iter = vec![1, 2, 3].into_iter(); | ||
let stream0 = stream::iter(iter); | ||
|
||
let _chunk_stream = stream0.chunks_timeout(0, Duration::from_secs(2)); | ||
}); | ||
|
||
// The panic location should be in this file | ||
assert_eq!(&panic_location_file.unwrap(), file!()); | ||
|
||
Ok(()) | ||
} |