|
1 | | -//! The functions in this module should be used to execute futures, serving as a facade to the |
2 | | -//! underlying executor implementation which currently is tokio. This serves a few purposes: |
3 | | -//! - Avoid depending directly on tokio APIs, making upgrades or a potential switch easier. |
4 | | -//! - Reflect our chosen default semantics of aborting on task panic, offering `*_allow_panic` |
5 | | -//! functions to opt out of that. |
6 | | -//! - Reflect that historically we've used blocking futures due to making DB calls directly within |
7 | | -//! futures. This point should go away once https://github.com/graphprotocol/graph-node/issues/905 |
8 | | -//! is resolved. Then the blocking flavors should no longer accept futures but closures. |
| 1 | +//! Helpers for dealing with certain aspects of tokio. |
9 | 2 | //! |
10 | | -//! These should not be called from within executors other than tokio, particularly the blocking |
11 | | -//! functions will panic in that case. We should generally avoid mixing executors whenever possible. |
| 3 | +//! This module sets up a runtime on which all tests should run, as well as |
| 4 | +//! providing some functions for spawning tasks with our desired semantics. |
| 5 | +//! |
| 6 | +//! The functions in this module should be used to execute futures, serving |
| 7 | +//! as a facade to the underlying executor implementation which currently is |
| 8 | +//! tokio. This serves a few purposes: |
| 9 | +//! - Avoid depending directly on tokio APIs, making upgrades or a potential |
| 10 | +//! switch easier. |
| 11 | +//! - Reflect our chosen default semantics of aborting on task panic, |
| 12 | +//! offering `*_allow_panic` functions to opt out of that. |
| 13 | +//! - Reflect that historically we've used blocking futures due to making DB |
| 14 | +//! calls directly within futures. This point should go away once |
| 15 | +//! https://github.com/graphprotocol/graph-node/issues/905 is resolved. |
| 16 | +//! Then the blocking flavors should no longer accept futures but |
| 17 | +//! closures. |
| 18 | +//! |
| 19 | +//! These should not be called from within executors other than tokio, |
| 20 | +//! particularly the blocking functions will panic in that case. We should |
| 21 | +//! generally avoid mixing executors whenever possible. |
12 | 22 |
|
13 | 23 | use futures03::future::{FutureExt, TryFutureExt}; |
14 | 24 | use std::future::Future as Future03; |
15 | 25 | use std::panic::AssertUnwindSafe; |
16 | 26 | use tokio::task::JoinHandle; |
17 | 27 |
|
| 28 | +#[cfg(debug_assertions)] |
| 29 | +use tokio::runtime::{Builder, Runtime}; |
| 30 | + |
| 31 | +#[cfg(debug_assertions)] |
| 32 | +lazy_static::lazy_static! { |
| 33 | + /// The one true runtime for all tests. Tests should use the |
| 34 | + /// `graph::test` macro to make sure they are using this runtime, the |
| 35 | + /// same way they would use `#[tokio::test]`. |
| 36 | + /// |
| 37 | + /// We need to make sure we use a single runtime because if there are |
| 38 | + /// multiple runtimes involved, the task that diesel_async spawns to |
| 39 | + /// drive database connections (see `drive_connection` in the |
| 40 | + /// `diesel_async` crate) may end up on a different runtime than the one |
| 41 | + /// the test is using, leading to that task getting dropped, and the |
| 42 | + /// test using a connection receiving a `Connection closed` error. |
| 43 | + pub static ref TEST_RUNTIME: Runtime = |
| 44 | + Builder::new_multi_thread().enable_all().build().unwrap(); |
| 45 | +} |
| 46 | + |
18 | 47 | fn abort_on_panic<T: Send + 'static>( |
19 | 48 | f: impl Future03<Output = T> + Send + 'static, |
20 | 49 | ) -> impl Future03<Output = T> { |
|
0 commit comments