task-local static for tests #3169
Replies: 2 comments 5 replies
-
Tokio does have support for task locals https://docs.rs/tokio/0.3.4/tokio/task/struct.LocalKey.html, which you can use to scope these connections. However, I'll note that |
Beta Was this translation helpful? Give feedback.
-
It seems to me that the best solution would be to simply not store it in a global variable. Then you can create a separate one in each test. I don't think there's any way to avoid multiple tests from using the same global. There are task-locals, sure, but those don't transfer across calls to As for |
Beta Was this translation helpful? Give feedback.
-
Hi ^^
context
For some server I am building (which uses tokio) we are using a global variable for the database connection:
problem
This generally works very well, but now we are adding some tests (using
#[tokio::test]
) and hit a problem there:All of our tests shall run against a fresh, separate database to avoid side-effects. But since they all share the same static variable this currently doesn't work!
solution ideas
worker_threads = 1
, but this is not desired.Is there something like that? Or do you seem some other (better?) solution or workaround?
Merci beaucoup <3
Beta Was this translation helpful? Give feedback.
All reactions