Using lazy statics in #[tokio::test] #1510
-
I’m using a static object that is initialized at runtime to manage a shared connection pool. The code looks like the following. (this is more of a pseudocode; a minimal-ish working example using bb8 pool is https://github.com/naskya/connection-pool-test-example/blob/main/src/lib.rs) Expand the code blockstatic CONNECTION_POOL: OnceCell<Pool> = OnceCell::const_new();
/// initializes the connection pool.
async fn init_pool() {
CONNECTION_POOL.get_or_init(|| async { ... }).await;
}
/// acquires a connection from the pool.
async fn get_connection() -> Connection {
if !CONNECTION_POOL.initilized() {
init_pool().await;
}
CONNECTION_POOL.get().unwrap(). ... // get a connection out of the pool
}
/// does something using a connection.
async fn do_something() {
let conn = get_connection().await;
conn.some_operation();
} Since
Example output of
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Thanks for the question! Yes. Nextest guarantees in perpetuity that the default mode will always be process-per-test. (I strongly believe it is the right default.) If we add new ways of executing tests in the future, they'll always be behind an option. |
Beta Was this translation helpful? Give feedback.
Thanks for the question!
Yes. Nextest guarantees in perpetuity that the default mode will always be process-per-test. (I strongly believe it is the right default.) If we add new ways of executing tests in the future, they'll always be behind an option.