This commit is contained in:
Mateus Azis 2025-07-15 18:10:55 -07:00
parent 33f8227022
commit fca403a658
1 changed files with 27 additions and 26 deletions

View File

@ -27,8 +27,8 @@ import io.grpc.stub.ClientCalls;
import io.grpc.stub.ServerCalls; import io.grpc.stub.ServerCalls;
import io.grpc.testing.GrpcCleanupRule; import io.grpc.testing.GrpcCleanupRule;
import io.grpc.testing.TestMethodDescriptors; import io.grpc.testing.TestMethodDescriptors;
import java.io.IOException;
import java.time.Duration; import java.time.Duration;
import java.util.concurrent.atomic.AtomicBoolean;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
@ -43,6 +43,9 @@ import org.mockito.junit.MockitoRule;
@RunWith(JUnit4.class) @RunWith(JUnit4.class)
public final class PendingAuthListenerTest { public final class PendingAuthListenerTest {
private static final MethodDescriptor<Void, Void> TEST_METHOD =
TestMethodDescriptors.voidMethod();
@Rule public final MockitoRule mocks = MockitoJUnit.rule(); @Rule public final MockitoRule mocks = MockitoJUnit.rule();
@Rule public final GrpcCleanupRule grpcCleanupRule = new GrpcCleanupRule(); @Rule public final GrpcCleanupRule grpcCleanupRule = new GrpcCleanupRule();
@ -105,16 +108,32 @@ public final class PendingAuthListenerTest {
@Test @Test
public void whenStartCallFails_closesTheCallWithInternalStatus() throws Exception { public void whenStartCallFails_closesTheCallWithInternalStatus() throws Exception {
// Arrange // Arrange
String name = TestMethodDescriptors.SERVICE_NAME;
AtomicBoolean closed = new AtomicBoolean(false);
MethodDescriptor<Void, Void> method = TestMethodDescriptors.voidMethod();
ServerCallHandler<Void, Void> callHandler = ServerCallHandler<Void, Void> callHandler =
ServerCalls.asyncUnaryCall( ServerCalls.asyncUnaryCall(
(req, respObserver) -> { (req, respObserver) -> {
throw new IllegalStateException("ooops"); throw new IllegalStateException("ooops");
}); });
ManagedChannel channel = startServer(callHandler);
// Act
StatusRuntimeException ex =
assertThrows(
StatusRuntimeException.class,
() ->
ClientCalls.blockingUnaryCall(
channel,
TEST_METHOD,
CallOptions.DEFAULT.withDeadlineAfter(Duration.ofSeconds(5)),
/* request= */ null));
// Assert
assertThat(ex.getStatus().getCode()).isEqualTo(Status.Code.INTERNAL);
}
private ManagedChannel startServer(ServerCallHandler<Void, Void> callHandler) throws IOException {
String name = TestMethodDescriptors.SERVICE_NAME;
ServerServiceDefinition serviceDef = ServerServiceDefinition serviceDef =
ServerServiceDefinition.builder(name).addMethod(method, callHandler).build(); ServerServiceDefinition.builder(name).addMethod(TEST_METHOD, callHandler).build();
Server server = Server server =
InProcessServerBuilder.forName(name) InProcessServerBuilder.forName(name)
.addService(serviceDef) .addService(serviceDef)
@ -150,35 +169,17 @@ public final class PendingAuthListenerTest {
public void start(Listener<RespT> responseListener, Metadata headers) { public void start(Listener<RespT> responseListener, Metadata headers) {
ClientCall.Listener<RespT> wrappedListener = ClientCall.Listener<RespT> wrappedListener =
new ForwardingClientCallListener.SimpleForwardingClientCallListener< new ForwardingClientCallListener.SimpleForwardingClientCallListener<
RespT>(responseListener) { RespT>(responseListener) {};
@Override
public void onClose(Status status, Metadata trailers) {
super.onClose(status, trailers);
closed.set(true);
}
};
super.start(wrappedListener, headers); super.start(wrappedListener, headers);
} }
}; };
} }
}) })
.build(); .build();
grpcCleanupRule.register(server); grpcCleanupRule.register(server);
grpcCleanupRule.register(channel); grpcCleanupRule.register(channel);
// Act return channel;
StatusRuntimeException ex =
assertThrows(
StatusRuntimeException.class,
() ->
ClientCalls.blockingUnaryCall(
channel,
method,
CallOptions.DEFAULT.withDeadlineAfter(Duration.ofSeconds(5)),
/* request= */ null));
// Assert
assertThat(ex.getStatus().getCode()).isEqualTo(Status.Code.INTERNAL);
assertThat(closed.get()).isTrue();
} }
} }