spring-projects / spring-boot

Spring Boot
https://spring.io/projects/spring-boot
Apache License 2.0
74.51k stars 40.54k forks source link

DirtiesContext used with Webflux, a random port and multiple contexts causes multiple contexts to misbehave #38199

Closed Dodomix closed 2 months ago

Dodomix commented 10 months ago

Bug description

I'm having an issue while running tests using Webflux. Tests are set up to run on a random port and access a controller using HttpClient (I've tested and the same issue happens with WebTestClient). If there are multiple application contexts used for the tests, then @DirtiesContext annotation causes contexts not being cleared to misbehave (to stop allowing connections).

Bug reproduce

Here is the simplest dummy app I've managed to build which replicates the issue: https://github.com/Dodomix/multiple-test-contexts-bug. If you run Test1 -> Test2 -> Test3, Test3 will fail. Each test by itself passes.

Expected behavior

All tests pass.

System info

Java: Temurin 17.0.9

philwebb commented 10 months ago

I've tracked this down to this call being made to ReactorResourceFactory.

It seems that reactor.netty.http.HttpResources has a shared static so I think that when Test2 stops the context it also stops the shared resources after which Test3 will fail.

@bclozel @rstoyanchev Is this a Framework bug or something we need to configure in Spring Boot. Should we be setting useGlobalResources to false?

Orteke2464 commented 4 months ago

Hello. I've the same problem using spring boot 3.2.4, and spring framework libraries version 6.1.5.

For my test, execution steps same with topic.

I had receiving this problem after migration to spring boot 3, in branch with spring boot 2 problem isn't reproduced

bclozel commented 4 months ago

@philwebb I think setting useGlobalResources is the best solution here. This should only affect runtime performance for memory allocation, this shouldn't be noticeable in tests.

philwebb commented 3 months ago

We're going to investigate using a post processor in the tests to change the flag.