#Run sync code before tokio::test's

14 messages · Page 1 of 1 (latest)

summer bear
#

I've encountered this issue, I put a piece of code in a LazyLock to fetch a token that will be used for all tests, but that needs to run in a sync context, it panics if it's run inside async.
My quick and dirty solution was putting an init test not async to force the initialization, but I feel like this is bad, there has to be a better way.

drifting bluff
drifting bluff
#

uhhhh

#

ok that just wraps a tokio runtime to run it blocking

summer bear
#

Unless there's some way to run async before all tests and share it

drifting bluff
#

tests are also not necessarily ran sequentially

summer bear
drifting bluff
#

and, well, use reqwest async mode

summer bear
#

Oh nice

#

Thanks

olive dawn
#

also note that #[tokio::test] doesn't do anything especially magic for tests; it is just #[tokio::main] + #[test]

#

and it defaults to current-thread runtime to be cheaper