-
Notifications
You must be signed in to change notification settings - Fork 640
/
Copy pathtasks.rs
40 lines (36 loc) · 1.12 KB
/
tasks.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
use sentry::Hub;
use tokio::task::JoinHandle;
/// Runs the provided closure on a thread where blocking is acceptable.
///
/// This is using [tokio::task::spawn_blocking] internally, but automatically
/// runs the callback function in the context of the current Sentry [Hub].
pub fn spawn_blocking<F, R>(f: F) -> JoinHandle<R>
where
F: FnOnce() -> R + Send + 'static,
R: Send + 'static,
{
let current_span = tracing::Span::current();
let hub = Hub::current();
tokio::task::spawn_blocking(move || current_span.in_scope(|| Hub::run(hub, f)))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::util::errors::BoxedAppError;
/// Test that [spawn_blocking] works with [anyhow].
#[tokio::test]
async fn test_spawn_blocking_anyhow() {
spawn_blocking(|| Ok::<_, anyhow::Error>(()))
.await
.unwrap()
.unwrap()
}
/// Test that [spawn_blocking] works with [BoxedAppError].
#[tokio::test]
async fn test_spawn_blocking_apperror() {
spawn_blocking(|| Ok::<_, BoxedAppError>(()))
.await
.unwrap()
.unwrap()
}
}