Search in sources :

Example 6 with SimpleResponse

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

the class AbstractInteropTest method computeEngineCreds.

/**
 * Sends a large unary rpc with compute engine credentials.
 */
public void computeEngineCreds(String serviceAccount, String oauthScope) throws Exception {
    ComputeEngineCredentials credentials = ComputeEngineCredentials.create();
    TestServiceGrpc.TestServiceBlockingStub stub = blockingStub.withCallCredentials(MoreCallCredentials.from(credentials));
    final SimpleRequest request = SimpleRequest.newBuilder().setFillUsername(true).setFillOauthScope(true).setResponseSize(314159).setPayload(Payload.newBuilder().setBody(ByteString.copyFrom(new byte[271828]))).build();
    final SimpleResponse response = stub.unaryCall(request);
    assertEquals(serviceAccount, response.getUsername());
    assertFalse(response.getOauthScope().isEmpty());
    assertTrue("Received oauth scope: " + response.getOauthScope(), oauthScope.contains(response.getOauthScope()));
    final SimpleResponse goldenResponse = SimpleResponse.newBuilder().setOauthScope(response.getOauthScope()).setUsername(response.getUsername()).setPayload(Payload.newBuilder().setBody(ByteString.copyFrom(new byte[314159]))).build();
    assertResponse(goldenResponse, response);
}
Also used : SimpleResponse(io.grpc.testing.integration.Messages.SimpleResponse) ComputeEngineCredentials(com.google.auth.oauth2.ComputeEngineCredentials) SimpleRequest(io.grpc.testing.integration.Messages.SimpleRequest)

Example 7 with SimpleResponse

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

the class AbstractInteropTest method customMetadata.

@Test
public void customMetadata() throws Exception {
    final int responseSize = 314159;
    final int requestSize = 271828;
    final SimpleRequest request = SimpleRequest.newBuilder().setResponseSize(responseSize).setPayload(Payload.newBuilder().setBody(ByteString.copyFrom(new byte[requestSize]))).build();
    final StreamingOutputCallRequest streamingRequest = StreamingOutputCallRequest.newBuilder().addResponseParameters(ResponseParameters.newBuilder().setSize(responseSize)).setPayload(Payload.newBuilder().setBody(ByteString.copyFrom(new byte[requestSize]))).build();
    final SimpleResponse goldenResponse = SimpleResponse.newBuilder().setPayload(Payload.newBuilder().setBody(ByteString.copyFrom(new byte[responseSize]))).build();
    final StreamingOutputCallResponse goldenStreamingResponse = StreamingOutputCallResponse.newBuilder().setPayload(Payload.newBuilder().setBody(ByteString.copyFrom(new byte[responseSize]))).build();
    final byte[] trailingBytes = { (byte) 0xa, (byte) 0xb, (byte) 0xa, (byte) 0xb, (byte) 0xa, (byte) 0xb };
    // Test UnaryCall
    Metadata metadata = new Metadata();
    metadata.put(Util.ECHO_INITIAL_METADATA_KEY, "test_initial_metadata_value");
    metadata.put(Util.ECHO_TRAILING_METADATA_KEY, trailingBytes);
    AtomicReference<Metadata> headersCapture = new AtomicReference<>();
    AtomicReference<Metadata> trailersCapture = new AtomicReference<>();
    TestServiceGrpc.TestServiceBlockingStub blockingStub = this.blockingStub.withInterceptors(MetadataUtils.newAttachHeadersInterceptor(metadata), MetadataUtils.newCaptureMetadataInterceptor(headersCapture, trailersCapture));
    SimpleResponse response = blockingStub.unaryCall(request);
    assertResponse(goldenResponse, response);
    assertEquals("test_initial_metadata_value", headersCapture.get().get(Util.ECHO_INITIAL_METADATA_KEY));
    assertTrue(Arrays.equals(trailingBytes, trailersCapture.get().get(Util.ECHO_TRAILING_METADATA_KEY)));
    assertStatsTrace("grpc.testing.TestService/UnaryCall", Status.Code.OK, Collections.singleton(request), Collections.singleton(goldenResponse));
    // Test FullDuplexCall
    metadata = new Metadata();
    metadata.put(Util.ECHO_INITIAL_METADATA_KEY, "test_initial_metadata_value");
    metadata.put(Util.ECHO_TRAILING_METADATA_KEY, trailingBytes);
    headersCapture = new AtomicReference<>();
    trailersCapture = new AtomicReference<>();
    TestServiceGrpc.TestServiceStub stub = asyncStub.withInterceptors(MetadataUtils.newAttachHeadersInterceptor(metadata), MetadataUtils.newCaptureMetadataInterceptor(headersCapture, trailersCapture));
    StreamRecorder<Messages.StreamingOutputCallResponse> recorder = StreamRecorder.create();
    StreamObserver<Messages.StreamingOutputCallRequest> requestStream = stub.fullDuplexCall(recorder);
    requestStream.onNext(streamingRequest);
    requestStream.onCompleted();
    recorder.awaitCompletion();
    assertSuccess(recorder);
    assertResponse(goldenStreamingResponse, recorder.firstValue().get());
    assertEquals("test_initial_metadata_value", headersCapture.get().get(Util.ECHO_INITIAL_METADATA_KEY));
    assertTrue(Arrays.equals(trailingBytes, trailersCapture.get().get(Util.ECHO_TRAILING_METADATA_KEY)));
    assertStatsTrace("grpc.testing.TestService/FullDuplexCall", Status.Code.OK, Collections.singleton(streamingRequest), Collections.singleton(goldenStreamingResponse));
}
Also used : Metadata(io.grpc.Metadata) AtomicReference(java.util.concurrent.atomic.AtomicReference) SimpleRequest(io.grpc.testing.integration.Messages.SimpleRequest) SimpleResponse(io.grpc.testing.integration.Messages.SimpleResponse) StreamingOutputCallRequest(io.grpc.testing.integration.Messages.StreamingOutputCallRequest) StreamingOutputCallResponse(io.grpc.testing.integration.Messages.StreamingOutputCallResponse) Test(org.junit.Test)

Example 8 with SimpleResponse

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

the class AbstractInteropTest method jwtTokenCreds.

/**
 * Test JWT-based auth.
 */
public void jwtTokenCreds(InputStream serviceAccountJson) throws Exception {
    final SimpleRequest request = SimpleRequest.newBuilder().setResponseSize(314159).setPayload(Payload.newBuilder().setBody(ByteString.copyFrom(new byte[271828]))).setFillUsername(true).build();
    ServiceAccountCredentials credentials = (ServiceAccountCredentials) GoogleCredentials.fromStream(serviceAccountJson);
    TestServiceGrpc.TestServiceBlockingStub stub = blockingStub.withCallCredentials(MoreCallCredentials.from(credentials));
    SimpleResponse response = stub.unaryCall(request);
    assertEquals(credentials.getClientEmail(), response.getUsername());
    assertEquals(314159, response.getPayload().getBody().size());
}
Also used : SimpleResponse(io.grpc.testing.integration.Messages.SimpleResponse) ServiceAccountCredentials(com.google.auth.oauth2.ServiceAccountCredentials) SimpleRequest(io.grpc.testing.integration.Messages.SimpleRequest)

Example 9 with SimpleResponse

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

the class AbstractInteropTest method serverCompressedUnary.

/**
 * Tests if the server can send a compressed unary response. Ideally we would assert that the
 * responses have the requested compression, but this is not supported by the API. Given a
 * compliant server, this test will exercise the code path for receiving a compressed response but
 * cannot itself verify that the response was compressed.
 */
@Test
public void serverCompressedUnary() throws Exception {
    assumeEnoughMemory();
    final SimpleRequest responseShouldBeCompressed = SimpleRequest.newBuilder().setResponseCompressed(BoolValue.newBuilder().setValue(true)).setResponseSize(314159).setPayload(Payload.newBuilder().setBody(ByteString.copyFrom(new byte[271828]))).build();
    final SimpleRequest responseShouldBeUncompressed = SimpleRequest.newBuilder().setResponseCompressed(BoolValue.newBuilder().setValue(false)).setResponseSize(314159).setPayload(Payload.newBuilder().setBody(ByteString.copyFrom(new byte[271828]))).build();
    final SimpleResponse goldenResponse = SimpleResponse.newBuilder().setPayload(Payload.newBuilder().setBody(ByteString.copyFrom(new byte[314159]))).build();
    assertResponse(goldenResponse, blockingStub.unaryCall(responseShouldBeCompressed));
    assertStatsTrace("grpc.testing.TestService/UnaryCall", Status.Code.OK, Collections.singleton(responseShouldBeCompressed), Collections.singleton(goldenResponse));
    assertResponse(goldenResponse, blockingStub.unaryCall(responseShouldBeUncompressed));
    assertStatsTrace("grpc.testing.TestService/UnaryCall", Status.Code.OK, Collections.singleton(responseShouldBeUncompressed), Collections.singleton(goldenResponse));
}
Also used : SimpleResponse(io.grpc.testing.integration.Messages.SimpleResponse) SimpleRequest(io.grpc.testing.integration.Messages.SimpleRequest) Test(org.junit.Test)

Example 10 with SimpleResponse

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

the class AbstractInteropTest method computeEngineChannelCredentials.

/**
 * Sends an unary rpc with ComputeEngineChannelBuilder.
 */
public void computeEngineChannelCredentials(String defaultServiceAccount, TestServiceGrpc.TestServiceBlockingStub computeEngineStub) throws Exception {
    final SimpleRequest request = SimpleRequest.newBuilder().setFillUsername(true).setResponseSize(314159).setPayload(Payload.newBuilder().setBody(ByteString.copyFrom(new byte[271828]))).build();
    final SimpleResponse response = computeEngineStub.unaryCall(request);
    assertEquals(defaultServiceAccount, response.getUsername());
    final SimpleResponse goldenResponse = SimpleResponse.newBuilder().setUsername(defaultServiceAccount).setPayload(Payload.newBuilder().setBody(ByteString.copyFrom(new byte[314159]))).build();
    assertResponse(goldenResponse, response);
}
Also used : SimpleResponse(io.grpc.testing.integration.Messages.SimpleResponse) SimpleRequest(io.grpc.testing.integration.Messages.SimpleRequest)

Aggregations

SimpleResponse (io.grpc.testing.integration.Messages.SimpleResponse)24 SimpleRequest (io.grpc.testing.integration.Messages.SimpleRequest)21 Test (org.junit.Test)8 StatusRuntimeException (io.grpc.StatusRuntimeException)4 ByteString (com.google.protobuf.ByteString)3 ManagedChannel (io.grpc.ManagedChannel)3 Metadata (io.grpc.Metadata)3 Status (io.grpc.Status)3 GoogleCredentials (com.google.auth.oauth2.GoogleCredentials)2 Channel (io.grpc.Channel)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)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 ClientCall (io.grpc.ClientCall)1 ClientInterceptor (io.grpc.ClientInterceptor)1 Context (io.grpc.Context)1