use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.StatusRuntimeException in project grpc-java by grpc.
the class CascadingTest method startChainingServer.
/**
* Create a chain of client to server calls which can be cancelled top down.
*
* @return a Future that completes when call chain is created
*/
private Future<?> startChainingServer(final int depthThreshold) throws IOException {
final AtomicInteger serversReady = new AtomicInteger();
final SettableFuture<Void> chainReady = SettableFuture.create();
class ChainingService extends TestServiceGrpc.TestServiceImplBase {
@Override
public void unaryCall(final SimpleRequest request, final StreamObserver<SimpleResponse> responseObserver) {
((ServerCallStreamObserver) responseObserver).setOnCancelHandler(new Runnable() {
@Override
public void run() {
receivedCancellations.countDown();
}
});
if (serversReady.incrementAndGet() == depthThreshold) {
// Stop recursion
chainReady.set(null);
return;
}
Context.currentContextExecutor(otherWork).execute(new Runnable() {
@Override
public void run() {
try {
blockingStub.unaryCall(request);
} catch (StatusRuntimeException e) {
Status status = e.getStatus();
if (status.getCode() == Status.Code.CANCELLED) {
observedCancellations.countDown();
} else {
responseObserver.onError(e);
}
}
}
});
}
}
server = InProcessServerBuilder.forName("channel").executor(otherWork).addService(new ChainingService()).build().start();
return chainReady;
}
use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.StatusRuntimeException in project grpc-java by grpc.
the class UtilServerInterceptorsTest method statusRuntimeExceptionTransmitter.
@Test
public void statusRuntimeExceptionTransmitter() {
final Status expectedStatus = Status.UNAVAILABLE;
final Metadata expectedMetadata = new Metadata();
FakeServerCall<Void, Void> call = new FakeServerCall<>(expectedStatus, expectedMetadata);
final StatusRuntimeException exception = new StatusRuntimeException(expectedStatus, expectedMetadata);
listener = new VoidCallListener() {
@Override
public void onMessage(Void message) {
throw exception;
}
@Override
public void onHalfClose() {
throw exception;
}
@Override
public void onCancel() {
throw exception;
}
@Override
public void onComplete() {
throw exception;
}
@Override
public void onReady() {
throw exception;
}
};
ServerServiceDefinition intercepted = ServerInterceptors.intercept(serviceDefinition, Arrays.asList(TransmitStatusRuntimeExceptionInterceptor.instance()));
// The interceptor should have handled the error by directly closing the ServerCall
// and the exception should not propagate to the method's caller
getSoleMethod(intercepted).getServerCallHandler().startCall(call, headers).onMessage(null);
getSoleMethod(intercepted).getServerCallHandler().startCall(call, headers).onCancel();
getSoleMethod(intercepted).getServerCallHandler().startCall(call, headers).onComplete();
getSoleMethod(intercepted).getServerCallHandler().startCall(call, headers).onHalfClose();
getSoleMethod(intercepted).getServerCallHandler().startCall(call, headers).onReady();
assertEquals(5, call.numCloses);
}
use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.StatusRuntimeException in project grpc-java by grpc.
the class UtilServerInterceptorsTest method statusRuntimeExceptionTransmitterIgnoresClosedCalls.
@Test
public void statusRuntimeExceptionTransmitterIgnoresClosedCalls() {
final Status expectedStatus = Status.UNAVAILABLE;
final Status unexpectedStatus = Status.CANCELLED;
final Metadata expectedMetadata = new Metadata();
FakeServerCall<Void, Void> call = new FakeServerCall<>(expectedStatus, expectedMetadata);
final StatusRuntimeException exception = new StatusRuntimeException(expectedStatus, expectedMetadata);
listener = new VoidCallListener() {
@Override
public void onMessage(Void message) {
throw exception;
}
@Override
public void onHalfClose() {
throw exception;
}
};
ServerServiceDefinition intercepted = ServerInterceptors.intercept(serviceDefinition, Arrays.asList(TransmitStatusRuntimeExceptionInterceptor.instance()));
ServerCall.Listener<Void> callDoubleSreListener = getSoleMethod(intercepted).getServerCallHandler().startCall(call, headers);
// the only close with our exception
callDoubleSreListener.onMessage(null);
// should not trigger a close
callDoubleSreListener.onHalfClose();
// this listener closes the call when it is initialized with startCall
listener = new VoidCallListener() {
@Override
public void onCall(ServerCall<Void, Void> call, Metadata headers) {
call.close(unexpectedStatus, headers);
}
@Override
public void onHalfClose() {
throw exception;
}
};
ServerCall.Listener<Void> callClosedListener = getSoleMethod(intercepted).getServerCallHandler().startCall(call, headers);
// call is already closed, does not match exception
// should not trigger a close
callClosedListener.onHalfClose();
assertEquals(1, call.numCloses);
}
use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.StatusRuntimeException in project grpc-java by grpc.
the class TlsTest method clientRejectsUntrustedServerCert.
/**
* Tests that a client configured using GrpcSslContexts refuses to talk to a server that has an
* an untrusted certificate.
*/
@Test
public void clientRejectsUntrustedServerCert() throws Exception {
// Create & start a server.
File serverCertFile = TestUtils.loadCert("badserver.pem");
File serverPrivateKeyFile = TestUtils.loadCert("badserver.key");
X509Certificate[] serverTrustedCaCerts = { TestUtils.loadX509Cert("ca.pem") };
server = serverBuilder(0, serverCertFile, serverPrivateKeyFile, serverTrustedCaCerts).addService(new SimpleServiceImpl()).build().start();
// Create a client.
File clientCertChainFile = TestUtils.loadCert("client.pem");
File clientPrivateKeyFile = TestUtils.loadCert("client.key");
X509Certificate[] clientTrustedCaCerts = { TestUtils.loadX509Cert("ca.pem") };
channel = clientChannel(server.getPort(), clientContextBuilder.keyManager(clientCertChainFile, clientPrivateKeyFile).trustManager(clientTrustedCaCerts).build());
SimpleServiceGrpc.SimpleServiceBlockingStub client = SimpleServiceGrpc.newBlockingStub(channel);
// Check that the TLS handshake fails.
try {
client.unaryRpc(SimpleRequest.getDefaultInstance());
fail("TLS handshake should have failed, but didn't; received RPC response");
} catch (StatusRuntimeException e) {
// GRPC reports this situation by throwing a StatusRuntimeException that wraps either a
// javax.net.ssl.SSLHandshakeException or a java.nio.channels.ClosedChannelException.
// Thus, reliably detecting the underlying cause is not feasible.
// TODO(carl-mastrangelo): eventually replace this with a hamcrest matcher.
assertEquals(Throwables.getStackTraceAsString(e), Status.Code.UNAVAILABLE, e.getStatus().getCode());
}
// We really want to see TRANSIENT_FAILURE here, but if the test runs slowly the 1s backoff
// may be exceeded by the time the failure happens (since it counts from the start of the
// attempt). Even so, CONNECTING is a strong indicator that the handshake failed; otherwise we'd
assertThat(channel.getState(false)).isAnyOf(ConnectivityState.TRANSIENT_FAILURE, ConnectivityState.CONNECTING);
}
use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.StatusRuntimeException in project grpc-java by grpc.
the class XdsSdsClientServerTest method tlsServer_plaintextClient_expectException.
@Test
public void tlsServer_plaintextClient_expectException() throws Exception {
DownstreamTlsContext downstreamTlsContext = setBootstrapInfoAndBuildDownstreamTlsContext(null, null, null, null, false, false);
buildServerWithTlsContext(downstreamTlsContext);
SimpleServiceGrpc.SimpleServiceBlockingStub blockingStub = getBlockingStub(/* upstreamTlsContext= */
null, /* overrideAuthority= */
null);
try {
unaryRpc("buddy", blockingStub);
fail("exception expected");
} catch (StatusRuntimeException sre) {
assertThat(sre.getStatus().getCode()).isEqualTo(Status.UNAVAILABLE.getCode());
assertThat(sre.getStatus().getDescription()).contains("Network closed");
}
}
Aggregations