Search in sources :

Example 86 with StatusRuntimeException

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;
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) ServerCallStreamObserver(io.grpc.stub.ServerCallStreamObserver) Status(io.grpc.Status) ServerCallStreamObserver(io.grpc.stub.ServerCallStreamObserver) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StatusRuntimeException(io.grpc.StatusRuntimeException) SimpleRequest(io.grpc.testing.integration.Messages.SimpleRequest)

Example 87 with StatusRuntimeException

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);
}
Also used : Status(io.grpc.Status) ServerServiceDefinition(io.grpc.ServerServiceDefinition) Metadata(io.grpc.Metadata) StatusRuntimeException(io.grpc.StatusRuntimeException) Test(org.junit.Test)

Example 88 with StatusRuntimeException

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);
}
Also used : Status(io.grpc.Status) ServerCall(io.grpc.ServerCall) NoopServerCall(io.grpc.internal.NoopServerCall) ServerServiceDefinition(io.grpc.ServerServiceDefinition) Metadata(io.grpc.Metadata) StatusRuntimeException(io.grpc.StatusRuntimeException) Test(org.junit.Test)

Example 89 with StatusRuntimeException

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);
}
Also used : StatusRuntimeException(io.grpc.StatusRuntimeException) SimpleServiceGrpc(io.grpc.testing.protobuf.SimpleServiceGrpc) File(java.io.File) X509Certificate(java.security.cert.X509Certificate) Test(org.junit.Test)

Example 90 with StatusRuntimeException

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");
    }
}
Also used : DownstreamTlsContext(io.grpc.xds.EnvoyServerProtoData.DownstreamTlsContext) StatusRuntimeException(io.grpc.StatusRuntimeException) SimpleServiceGrpc(io.grpc.testing.protobuf.SimpleServiceGrpc) Test(org.junit.Test)

Aggregations

StatusRuntimeException (io.grpc.StatusRuntimeException)240 Test (org.junit.Test)164 ApiException (com.google.api.gax.grpc.ApiException)74 Status (io.grpc.Status)25 StreamObserver (io.grpc.stub.StreamObserver)20 ByteString (com.google.protobuf.ByteString)18 ArrayList (java.util.ArrayList)18 Metadata (io.grpc.Metadata)14 SimpleServiceGrpc (io.grpc.testing.protobuf.SimpleServiceGrpc)13 ExecutionException (java.util.concurrent.ExecutionException)12 JanusGraphGrpcServerBaseTest (org.janusgraph.graphdb.grpc.JanusGraphGrpcServerBaseTest)12 Test (org.junit.jupiter.api.Test)12 SubscriptionName (com.google.pubsub.v1.SubscriptionName)9 ManagedChannel (io.grpc.ManagedChannel)9 StatusRuntimeException (org.apache.beam.vendor.grpc.v1p43p2.io.grpc.StatusRuntimeException)8 BStruct (org.ballerinalang.model.values.BStruct)8 SimpleRequest (io.grpc.testing.integration.Messages.SimpleRequest)7 ChannelCredentials (io.grpc.ChannelCredentials)6 ServerCredentials (io.grpc.ServerCredentials)6 TlsChannelCredentials (io.grpc.TlsChannelCredentials)6