How to reuse expensive non-Send test fixture

In the system I'm working on, there is a non-Send datastructure that is expensive to initialize. The actual application code is single threaded, but I'd like to reuse an initialized datastructure across unit tests.
I looked at storing it in a LazyInit which would require it to be thread safe. Wrapping it in a Mutex would still require it to be Send.
If there was some way to say that 'these tests share non-Send data, so they need to be run on the same thread with this data passed to each of them' that should fit the bill, but there may be other, better solutions.
Any thoughts and ideas are highly appreciated!

If you can have multiple instances of the data structure alive at the same time, using a thread local is probably the simplest answer

You won't have to do the initialization on EVERY test, just once for every thread.

Thank you for the idea. Is there any guarantee for how Cargo uses a thread pool when running tests? I looked at this with rstest a while back, and it looked like more threads were created than the test parallelism, but I may misremember.

Unfortunately it looks like each test spawns a new thread to me, so you may still need to limit the test threads to 1 to avoid recreating the data structure on every test. It might be worth experimenting to see which is faster in practice though.

For future reference - if someone with a similar problem comes across this thread: I will likely solve this by having a single 'real' test method, and a mini-framework of registered functions that get passed the fixture.
Not an ideal solution - somewhat counterintuitive, and one panicking test will skip all subsequent ones - but this looks like the best trade-off so far.

Another possibility is to create a thread which the !Send thing never leaves, and send messages to that thread containing closures which do the actual test work.

The diplomatic_bag crate implements this strategy in a general fashion. I don't care for some of its design, such as using one thread for everything, but that shouldn't matter here; but also, this pattern wouldn't be hard to implement from scratch for your particular purpose, since you have only the one test fixture that never needs to be dropped, not many different !Send values to work with.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.