Search in sources :

Example 6 with SimpleRequest

use of io.grpc.testing.integration.Messages.SimpleRequest 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 7 with SimpleRequest

use of io.grpc.testing.integration.Messages.SimpleRequest in project grpc-java by grpc.

the class CascadingTest method testDeadlinePropagation.

@Test
public void testDeadlinePropagation() throws Exception {
    final AtomicInteger recursionDepthRemaining = new AtomicInteger(3);
    final SettableFuture<Deadline> finalDeadline = SettableFuture.create();
    class DeadlineSaver extends TestServiceGrpc.TestServiceImplBase {

        @Override
        public void unaryCall(final SimpleRequest request, final StreamObserver<SimpleResponse> responseObserver) {
            Context.currentContextExecutor(otherWork).execute(new Runnable() {

                @Override
                public void run() {
                    try {
                        if (recursionDepthRemaining.decrementAndGet() == 0) {
                            finalDeadline.set(Context.current().getDeadline());
                            responseObserver.onNext(SimpleResponse.getDefaultInstance());
                        } else {
                            responseObserver.onNext(blockingStub.unaryCall(request));
                        }
                        responseObserver.onCompleted();
                    } catch (Exception ex) {
                        responseObserver.onError(ex);
                    }
                }
            });
        }
    }
    server = InProcessServerBuilder.forName("channel").executor(otherWork).addService(new DeadlineSaver()).build().start();
    Deadline initialDeadline = Deadline.after(1, TimeUnit.MINUTES);
    blockingStub.withDeadline(initialDeadline).unaryCall(SimpleRequest.getDefaultInstance());
    assertNotSame(initialDeadline, finalDeadline);
    // Since deadline is re-calculated at each hop, some variance is acceptable and expected.
    assertAbout(deadline()).that(finalDeadline.get()).isWithin(1, TimeUnit.SECONDS).of(initialDeadline);
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) ServerCallStreamObserver(io.grpc.stub.ServerCallStreamObserver) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Deadline(io.grpc.Deadline) SimpleRequest(io.grpc.testing.integration.Messages.SimpleRequest) IOException(java.io.IOException) StatusRuntimeException(io.grpc.StatusRuntimeException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 8 with SimpleRequest

use of io.grpc.testing.integration.Messages.SimpleRequest in project grpc-java by grpc.

the class TransportCompressionTest method compresses.

@Test
public void compresses() {
    expectFzip = true;
    final SimpleRequest request = SimpleRequest.newBuilder().setResponseSize(314159).setResponseCompression(CompressionType.GZIP).setResponseType(PayloadType.COMPRESSABLE).setPayload(Payload.newBuilder().setBody(ByteString.copyFrom(new byte[271828]))).build();
    final SimpleResponse goldenResponse = SimpleResponse.newBuilder().setPayload(Payload.newBuilder().setType(PayloadType.COMPRESSABLE).setBody(ByteString.copyFrom(new byte[314159]))).build();
    assertEquals(goldenResponse, blockingStub.unaryCall(request));
    // Assert that compression took place
    assertTrue(FZIPPER.anyRead);
    assertTrue(FZIPPER.anyWritten);
}
Also used : SimpleResponse(io.grpc.testing.integration.Messages.SimpleResponse) SimpleRequest(io.grpc.testing.integration.Messages.SimpleRequest) Test(org.junit.Test)

Example 9 with SimpleRequest

use of io.grpc.testing.integration.Messages.SimpleRequest in project grpc-java by grpc.

the class StubConfigTest method testStubCallOptionsPopulatedToNewCall.

@Test
public void testStubCallOptionsPopulatedToNewCall() {
    TestServiceGrpc.TestServiceStub stub = TestServiceGrpc.newStub(channel);
    CallOptions options1 = stub.getCallOptions();
    SimpleRequest request = SimpleRequest.getDefaultInstance();
    stub.unaryCall(request, responseObserver);
    verify(channel).newCall(same(TestServiceGrpc.METHOD_UNARY_CALL), same(options1));
    stub = stub.withDeadlineAfter(2, NANOSECONDS);
    CallOptions options2 = stub.getCallOptions();
    assertNotSame(options1, options2);
    stub.unaryCall(request, responseObserver);
    verify(channel).newCall(same(TestServiceGrpc.METHOD_UNARY_CALL), same(options2));
}
Also used : CallOptions(io.grpc.CallOptions) SimpleRequest(io.grpc.testing.integration.Messages.SimpleRequest) TestServiceGrpc(io.grpc.testing.integration.TestServiceGrpc) Test(org.junit.Test)

Example 10 with SimpleRequest

use of io.grpc.testing.integration.Messages.SimpleRequest in project grpc-java by grpc.

the class AbstractInteropTest method veryLargeResponse.

@Test(timeout = 30000)
public void veryLargeResponse() throws Exception {
    assumeEnoughMemory();
    final SimpleRequest request = SimpleRequest.newBuilder().setResponseSize(unaryPayloadLength()).setResponseType(PayloadType.COMPRESSABLE).build();
    final SimpleResponse goldenResponse = SimpleResponse.newBuilder().setPayload(Payload.newBuilder().setType(PayloadType.COMPRESSABLE).setBody(ByteString.copyFrom(new byte[unaryPayloadLength()]))).build();
    assertEquals(goldenResponse, blockingStub.unaryCall(request));
}
Also used : SimpleResponse(io.grpc.testing.integration.Messages.SimpleResponse) SimpleRequest(io.grpc.testing.integration.Messages.SimpleRequest) Test(org.junit.Test)

Aggregations

SimpleRequest (io.grpc.testing.integration.Messages.SimpleRequest)13 SimpleResponse (io.grpc.testing.integration.Messages.SimpleResponse)9 Test (org.junit.Test)8 ByteString (com.google.protobuf.ByteString)3 StatusRuntimeException (io.grpc.StatusRuntimeException)3 GoogleCredentials (com.google.auth.oauth2.GoogleCredentials)2 ServerCallStreamObserver (io.grpc.stub.ServerCallStreamObserver)2 StreamObserver (io.grpc.stub.StreamObserver)2 StreamingOutputCallRequest (io.grpc.testing.integration.Messages.StreamingOutputCallRequest)2 StreamingOutputCallResponse (io.grpc.testing.integration.Messages.StreamingOutputCallResponse)2 IOException (java.io.IOException)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 AccessToken (com.google.auth.oauth2.AccessToken)1 ComputeEngineCredentials (com.google.auth.oauth2.ComputeEngineCredentials)1 OAuth2Credentials (com.google.auth.oauth2.OAuth2Credentials)1 ServiceAccountCredentials (com.google.auth.oauth2.ServiceAccountCredentials)1 CallOptions (io.grpc.CallOptions)1 Deadline (io.grpc.Deadline)1 Metadata (io.grpc.Metadata)1 Status (io.grpc.Status)1