Actix 4/Tokio, cargo test unexpected --test-thread argument

I'm trying to run tests in an Actix application, some of the tests are just "normal" test, declared with the #[test] macro, and some others are async tests declared with #[tokio::test].

Some of the tests are doing operations with files and directories, creation, deletion etc, so everything must be run on 1 thread.

In previous projects (with actix 3.*)I had no problem running similar tests just specifying in the CLI argument cargo test -- --test-threads 1. But now in this project I'm using actix 4 and things seem to be different:

As soon as some tokio::test tests are declared, the --test-threads argument is causing an error like invalid or unexpected argument --test-threads.
Not only --test-threads but any argument from CLI (I can't name a test to run, nor ignore one, etc...) is failing as soon as a tokio::test is declared somewhere.

So I tried to declare all my tests as tokio tests :

async fn mytest() {}

thinking that it might be some conflict between the regular test runner and the tokio runtime.

My tests are run but they fail because of concurrency issues, the io operations are not synchronized so the tests fail. That's what happens when the test are run on more than 1 thread.

So I tried to specify explicitly that I want only one thread running (despite the Tokio docs says that default is single threaded)

async fn mytest() {}

or even

#[tokio::test(flavor="multi_thread", worker_threads=1)]
async fn mytest() {}

But nothing works, the tests keep randomly failing for the same reasons and I can't get the tests being run synchronously on one thread. And again I never had this problem with previous versions of Actix.

I didn't create an issue about this on Github because I'm not sure about whats going wrong, I might just have misunderstood something ...

Thank you for any help :slight_smile: !

Use actix_test instead of tokio_test

I tried that, and also actix_rt::test same result always...

In Actix v4 is called #[actix_web::test]. It's worth mentioning that it doesn't support any arguments such as flavor or worker_threads.

Yes as I said I also tried actix_web::test without any better result. I cannot pass a argument from the CLI (unexpected argument error again), and as you said, the flavor and worker_threads are not supported so I don't see what I can do... is that expected behavior or should I report an issue ?

Tokio spawns a separate runtime for every thread, and the flavor option only controls how many threads that test's runtime has. Tokio has no control over whether tests run in parallel - that's up to cargo test, and you should be able to configure it normally.

Yes I should be able, and it was the case when I worked with actix 3, but with actix 4 I have unexpected argument error. I can't pass any argument with cargo test, it's like the syntax is changed when using the async runtime or something ..

The cargo test command really should work either way. I don't see how actix would influence it, and I know for a fact that Tokio does not.

Ok I finally got the problem it was just stupid ....
just a test implementation problem, because I use a struct that parses the CLI arguments of the program, and so the argument of cargo test are not recognized... :grimacing: :expressionless:

No problem with actix_web::test then, I just misinterpreted the error message...

Thanks for helping anyway !

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.