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

GrpcMock Junit, UNAVAILABLE: No servers found when running tests in --parallel #11

Closed fercam24 closed 2 years ago

fercam24 commented 2 years ago

Hi all, I actually have some problems running my tests with gradle --parallel option. Without it, everything works fine, with it I continuously get

io.grpc.StatusRuntimeException: UNAVAILABLE: No servers found for localhost:62497
        at app//io.grpc.stub.ClientCalls.toStatusRuntimeException(ClientCalls.java:262)
        at app//io.grpc.stub.ClientCalls.getUnchecked(ClientCalls.java:243)
        at app//io.grpc.stub.ClientCalls.blockingUnaryCall(ClientCalls.java:156)

Here is how I set my tests

@ExtendWith({MockitoExtension.class, GrpcMockExtension.class})
public class TicketServiceTest implements WithAssertions {

    private ManagedChannel frontendChannel;
    private ManagedChannel backendChannel;

    @BeforeEach
    public void setUp() {
        frontendChannel = buildManagedChannel();
        backendChannel = buildManagedChannel();

                val frontendStub = FrontendServiceGrpc.newBlockingStub(frontendChannel);
        val backendStub = BackendServiceGrpc.newBlockingStub(backendChannel);
                ...
    }

    @AfterEach
    void cleanup() {
        val channels = List.of(frontendChannel, backendChannel);
        channels.forEach(managedChannel -> Optional.ofNullable(managedChannel).ifPresent(ManagedChannel::shutdownNow));
    }

    private ManagedChannel buildManagedChannel() {
        return ManagedChannelBuilder.forAddress("localhost", GrpcMock.getGlobalPort())
                                    .usePlaintext()
                                    .build();
    }
}

Do you guys have any idea?

Fadelis commented 2 years ago

@fercam24 Most probably you have a similar issue as described in https://github.com/Fadelis/grpcmock/issues/8 . Since the static methods (like getGlobalPort) are based on ThreadLocal, each parallel execution has it's own instance, and only a single one of those is a running server configured via Junit's GrpcMockExtension for a single test class.