Fadelis / grpcmock

A gRPC Java testing tool to easily mock endpoints of gRPC services for IT or Unit testing
http://grpcmock.org
Apache License 2.0
144 stars 13 forks source link

Allow integration tests to run in parallell - bug? #20

Closed MelkerForssell closed 1 year ago

MelkerForssell commented 1 year ago

Hi, I have a problem with running integration tests in parallel which I solved and I just wanted to notify you about the problem. It seems like the grpcmock is shared between tests. (Atleast in the way I am using it)

I am unable to use the @AutoConfigureGrpcMock since I am using the starter from https://github.com/yidongnan/grpc-spring-boot-starter and need to set the address via @DynamicPropertySource like this:

@DynamicPropertySource
    static void overwriteProperties(DynamicPropertyRegistry registry) {
        registry.add("grpc.client.<client>.address") { "static://localhost:$GrpcMock.globalPort()" }
    }

I start the server like this:

    def setupSpec() {
        grpcMock = GrpcMock.grpcMock().build().start()
        GrpcMock.configureFor(grpcMock)
    }

When I run tests in parallel I will get an error with the port being taken.

I found the following in your code and it turns out that the socket.setReuseAddress(true); in findFreePort is what is causing the issue, so by removing it is resolved.

 private static int findFreePort() {
    try (ServerSocket socket = new ServerSocket(0)) {
      return socket.getLocalPort();
    } catch (IOException e) {
      throw new GrpcMockException("Failed finding a free port", e);
    }
  }
Fadelis commented 1 year ago

Yes, that can be removed. Thanks for reporting. Edit: fixed in 0.8.0 version