Search in sources :

Example 6 with StatusException

use of io.grpc.StatusException in project grpc-java by grpc.

the class OkHttpClientTransportTest method ping_failsIfTransportFails.

@Test
public void ping_failsIfTransportFails() throws Exception {
    initTransport();
    PingCallbackImpl callback = new PingCallbackImpl();
    clientTransport.ping(callback, MoreExecutors.directExecutor());
    assertEquals(0, callback.invocationCount);
    clientTransport.onException(new IOException());
    // ping failed on error
    assertEquals(1, callback.invocationCount);
    assertTrue(callback.failureCause instanceof StatusException);
    assertEquals(Status.Code.UNAVAILABLE, ((StatusException) callback.failureCause).getStatus().getCode());
    // now that handler is in terminal state, all future pings fail immediately
    callback = new PingCallbackImpl();
    clientTransport.ping(callback, MoreExecutors.directExecutor());
    assertEquals(1, callback.invocationCount);
    assertTrue(callback.failureCause instanceof StatusException);
    assertEquals(Status.Code.UNAVAILABLE, ((StatusException) callback.failureCause).getStatus().getCode());
    shutdownAndVerify();
}
Also used : StatusException(io.grpc.StatusException) IOException(java.io.IOException) Test(org.junit.Test)

Example 7 with StatusException

use of io.grpc.StatusException in project grpc-java by grpc.

the class HealthServiceImpl method check.

@Override
public void check(HealthCheckRequest request, StreamObserver<HealthCheckResponse> responseObserver) {
    ServingStatus status = getStatus(request.getService());
    if (status == null) {
        responseObserver.onError(new StatusException(Status.NOT_FOUND));
    } else {
        HealthCheckResponse response = HealthCheckResponse.newBuilder().setStatus(status).build();
        responseObserver.onNext(response);
        responseObserver.onCompleted();
    }
}
Also used : StatusException(io.grpc.StatusException) ServingStatus(io.grpc.health.v1.HealthCheckResponse.ServingStatus) HealthCheckResponse(io.grpc.health.v1.HealthCheckResponse)

Example 8 with StatusException

use of io.grpc.StatusException in project grpc-java by grpc.

the class NettyClientTransportTest method maxHeaderListSizeShouldBeEnforcedOnClient.

@Test
public void maxHeaderListSizeShouldBeEnforcedOnClient() throws Exception {
    startServer();
    NettyClientTransport transport = newTransport(newNegotiator(), DEFAULT_MAX_MESSAGE_SIZE, 1, null, true);
    callMeMaybe(transport.start(clientTransportListener));
    try {
        // Send a single RPC and wait for the response.
        new Rpc(transport, new Metadata()).halfClose().waitForResponse();
        fail("The stream should have been failed due to client received header exceeds header list" + " size limit!");
    } catch (Exception e) {
        Throwable rootCause = getRootCause(e);
        Status status = ((StatusException) rootCause).getStatus();
        assertEquals(Status.Code.INTERNAL, status.getCode());
        assertEquals("HTTP/2 error code: PROTOCOL_ERROR\nReceived Rst Stream", status.getDescription());
    }
}
Also used : Status(io.grpc.Status) Metadata(io.grpc.Metadata) TimeoutException(java.util.concurrent.TimeoutException) StatusException(io.grpc.StatusException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 9 with StatusException

use of io.grpc.StatusException in project google-cloud-java by GoogleCloudPlatform.

the class PublisherImplTest method testPublishFailureRetries_nonRetryableFailsImmediately.

@Test(expected = ExecutionException.class)
public void testPublishFailureRetries_nonRetryableFailsImmediately() throws Exception {
    Publisher publisher = getTestPublisherBuilder().setExecutorProvider(SINGLE_THREAD_EXECUTOR).setRetrySettings(Publisher.Builder.DEFAULT_RETRY_SETTINGS.toBuilder().setTotalTimeout(Duration.ofSeconds(10)).build()).setBatchingSettings(Publisher.Builder.DEFAULT_BATCHING_SETTINGS.toBuilder().setElementCountThreshold(1L).setDelayThreshold(Duration.ofSeconds(5)).build()).build();
    testPublisherServiceImpl.addPublishError(new StatusException(Status.INVALID_ARGUMENT));
    ApiFuture<String> publishFuture1 = sendTestMessage(publisher, "A");
    try {
        publishFuture1.get();
    } finally {
        assertTrue(testPublisherServiceImpl.getCapturedRequests().size() >= 1);
        publisher.shutdown();
    }
}
Also used : StatusException(io.grpc.StatusException) ByteString(com.google.protobuf.ByteString) Test(org.junit.Test)

Example 10 with StatusException

use of io.grpc.StatusException in project google-cloud-java by GoogleCloudPlatform.

the class SubscriberTest method testFailedChannel_fatalError_subscriberFails.

@Test(expected = IllegalStateException.class)
public void testFailedChannel_fatalError_subscriberFails() throws Exception {
    if (!isStreamingTest) {
        // This test is not applicable to polling.
        throw new IllegalStateException("To fullfil test expectation");
    }
    Subscriber subscriber = startSubscriber(getTestSubscriberBuilder(testReceiver).setExecutorProvider(InstantiatingExecutorProvider.newBuilder().setExecutorThreadCount(10).build()));
    // Fatal error
    fakeSubscriberServiceImpl.sendError(new StatusException(Status.INVALID_ARGUMENT));
    try {
        subscriber.awaitTerminated();
    } finally {
        // The subscriber must finish with an state error because its FAILED status.
        assertEquals(Subscriber.State.FAILED, subscriber.state());
        assertEquals(Status.INVALID_ARGUMENT, ((StatusRuntimeException) subscriber.failureCause()).getStatus());
    }
}
Also used : StatusException(io.grpc.StatusException) Test(org.junit.Test)

Aggregations

StatusException (io.grpc.StatusException)13 Test (org.junit.Test)9 Metadata (io.grpc.Metadata)3 HealthCheckResponse (io.grpc.health.v1.HealthCheckResponse)3 IOException (java.io.IOException)3 HealthCheckRequest (io.grpc.health.v1.HealthCheckRequest)2 ByteString (com.google.protobuf.ByteString)1 Status (io.grpc.Status)1 StatusRuntimeException (io.grpc.StatusRuntimeException)1 ServingStatus (io.grpc.health.v1.HealthCheckResponse.ServingStatus)1 KeepAliveManager (io.grpc.internal.KeepAliveManager)1 FrameWriter (io.grpc.okhttp.internal.framed.FrameWriter)1 Http2 (io.grpc.okhttp.internal.framed.Http2)1 Settings (io.grpc.okhttp.internal.framed.Settings)1 Variant (io.grpc.okhttp.internal.framed.Variant)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelFutureListener (io.netty.channel.ChannelFutureListener)1 ChannelPromise (io.netty.channel.ChannelPromise)1 Http2Exception (io.netty.handler.codec.http2.Http2Exception)1 Http2Headers (io.netty.handler.codec.http2.Http2Headers)1