grpc-spring icon indicating copy to clipboard operation
grpc-spring copied to clipboard

`@DirtiesContext` is required on every test

Open Bwvolleyball opened this issue 3 years ago • 6 comments

The context

The need for @DirtiesContext on all unit tests using a grpc service created by this starter greatly slows down tests, and doesn't feel like a proper solution as applications scale.

The bug

What's the problem? What's not working? What do you expect to happen.

all @SpringBootTest classes also require @DirtiesContext in order for tests across the application to function propertly.

Is there a stacktrace or a hint in the logs? (very important) Screenshots work as well, but don't screenshot your logs.

Steps to Reproduce

Steps to reproduce the behavior:

  1. Create an application using gRPC services.
  2. Write 2 or more @SpringBootTest classes
  3. Run your project's tests. These test classes will compete and your tests will fail.

Workaround, add @DirtiesContext to each test, but, this greatly slows down execution time of your application tests.

The application's environment

Which versions do you use?

  • Spring (boot):
  • grpc-java:
  • grpc-spring-boot-starter:
  • java: version + architecture (64bit?)
  • Other relevant libraries...

These versions don't really matter - it's a core issue with this library that is seemingly well documented, but there has to be a better way?

Additional context

  • Did it ever work before?

No

  • Do you have a demo?

No, but this issue is seemingly well documented with this project, but @DirtiesContext feels like a workaround instead of a solution.

Bwvolleyball avatar Oct 05 '22 04:10 Bwvolleyball

Do you have a suggestion on how to fix this?

ST-DDT avatar Oct 05 '22 06:10 ST-DDT

Spring Boot has to deal with this with normal HTTP servers (netty, tomcat, etc.) so it should be possible to copy what we do there. One thing we do for sure is start the server on a random port to avoid the clashes that seem to be the main problem for people using gRPC. It might mean that we need a new grpc-spring-boot-test jar to carry the necessary test utilities.

Is it perhaps a bit of a smell that none of the example apps in this project have tests? Some more dogfooding would probably be healthy.

dsyer avatar Dec 12 '22 08:12 dsyer

Contributions are welcome.

ST-DDT avatar Dec 12 '22 09:12 ST-DDT

Adding a new jar file is quite a big change. Probably the project maintainers should give a clear signal that this is a welcome addition. Adding tests to all examples would be a big change as well. Maybe we should start with one test so we can agree on what works?

dsyer avatar Dec 12 '22 09:12 dsyer

I played around with this a bit. I still think starting on a random port would be useful, but I found this nice library: https://github.com/asarkar/grpc-test. You just have to declare each test class with @ExtendWith(GrpcCleanupExtension.class). Neat.

However, I also haven't found a way to see a failure that only @DirtiesContext fixes. Is there a sample somewhere that someone can point me to? @Bwvolleyball do you have one?

dsyer avatar Dec 12 '22 17:12 dsyer

UPDATE: I can reproduce the problem - you need 2 tests with different application contexts (so add a nested @Configuration or extra configuration in @SpringBootTest). Definitely should be solved using a random port (but see #826 for wider context).

dsyer avatar Jan 20 '23 06:01 dsyer