Search in sources :

Example 21 with SimpleResponse

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

the class AbstractInteropTest method serviceAccountCreds.

/**
 * Sends a large unary rpc with service account credentials.
 */
public void serviceAccountCreds(String jsonKey, InputStream credentialsStream, String authScope) throws Exception {
    // cast to ServiceAccountCredentials to double-check the right type of object was created.
    GoogleCredentials credentials = ServiceAccountCredentials.class.cast(GoogleCredentials.fromStream(credentialsStream));
    credentials = credentials.createScoped(Arrays.asList(authScope));
    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);
    assertFalse(response.getUsername().isEmpty());
    assertTrue("Received username: " + response.getUsername(), jsonKey.contains(response.getUsername()));
    assertFalse(response.getOauthScope().isEmpty());
    assertTrue("Received oauth scope: " + response.getOauthScope(), authScope.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) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials) SimpleRequest(io.grpc.testing.integration.Messages.SimpleRequest)

Example 22 with SimpleResponse

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

the class AbstractInteropTest method performOneSoakIteration.

private SoakIterationResult performOneSoakIteration(boolean resetChannel) throws Exception {
    long startNs = System.nanoTime();
    Status status = Status.OK;
    ManagedChannel soakChannel = channel;
    TestServiceGrpc.TestServiceBlockingStub soakStub = blockingStub;
    if (resetChannel) {
        soakChannel = createChannel();
        soakStub = TestServiceGrpc.newBlockingStub(soakChannel);
    }
    try {
        final SimpleRequest request = SimpleRequest.newBuilder().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, soakStub.unaryCall(request));
    } catch (StatusRuntimeException e) {
        status = e.getStatus();
    }
    long elapsedNs = System.nanoTime() - startNs;
    if (resetChannel) {
        soakChannel.shutdownNow();
        soakChannel.awaitTermination(10, TimeUnit.SECONDS);
    }
    return new SoakIterationResult(TimeUnit.NANOSECONDS.toMillis(elapsedNs), status);
}
Also used : Status(io.grpc.Status) EchoStatus(io.grpc.testing.integration.Messages.EchoStatus) SimpleResponse(io.grpc.testing.integration.Messages.SimpleResponse) StatusRuntimeException(io.grpc.StatusRuntimeException) ManagedChannel(io.grpc.ManagedChannel) SimpleRequest(io.grpc.testing.integration.Messages.SimpleRequest)

Example 23 with SimpleResponse

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

the class TestServiceImpl method unaryCall.

/**
 * Immediately responds with a payload of the type and size specified in the request.
 */
@Override
public void unaryCall(SimpleRequest req, StreamObserver<SimpleResponse> responseObserver) {
    ServerCallStreamObserver<SimpleResponse> obs = (ServerCallStreamObserver<SimpleResponse>) responseObserver;
    SimpleResponse.Builder responseBuilder = SimpleResponse.newBuilder();
    try {
        if (req.hasResponseCompressed() && req.getResponseCompressed().getValue()) {
            obs.setCompression("gzip");
        } else {
            obs.setCompression("identity");
        }
    } catch (IllegalArgumentException e) {
        obs.onError(Status.UNIMPLEMENTED.withDescription("compression not supported.").withCause(e).asRuntimeException());
        return;
    }
    if (req.getResponseSize() != 0) {
        // For consistency with the c++ TestServiceImpl, use a random offset for unary calls.
        // TODO(wonderfly): whether or not this is a good approach needs further discussion.
        int offset = random.nextInt(compressableBuffer.size());
        ByteString payload = generatePayload(compressableBuffer, offset, req.getResponseSize());
        responseBuilder.setPayload(Payload.newBuilder().setBody(payload));
    }
    if (req.hasResponseStatus()) {
        obs.onError(Status.fromCodeValue(req.getResponseStatus().getCode()).withDescription(req.getResponseStatus().getMessage()).asRuntimeException());
        return;
    }
    responseObserver.onNext(responseBuilder.build());
    responseObserver.onCompleted();
}
Also used : ServerCallStreamObserver(io.grpc.stub.ServerCallStreamObserver) ByteString(com.google.protobuf.ByteString) SimpleResponse(io.grpc.testing.integration.Messages.SimpleResponse)

Example 24 with SimpleResponse

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

the class GrpclbFallbackTestClient method doRpcAndGetPath.

private GrpclbRouteType doRpcAndGetPath(Deadline deadline) {
    logger.info("doRpcAndGetPath deadline: " + deadline);
    final SimpleRequest request = SimpleRequest.newBuilder().setFillGrpclbRouteType(true).build();
    GrpclbRouteType result = GrpclbRouteType.GRPCLB_ROUTE_TYPE_UNKNOWN;
    try {
        SimpleResponse response = blockingStub.withDeadline(deadline).unaryCall(request);
        result = response.getGrpclbRouteType();
    } catch (StatusRuntimeException ex) {
        logger.warning("doRpcAndGetPath failed. Status: " + ex);
        return GrpclbRouteType.GRPCLB_ROUTE_TYPE_UNKNOWN;
    }
    logger.info("doRpcAndGetPath. GrpclbRouteType result: " + result);
    if (result != GrpclbRouteType.GRPCLB_ROUTE_TYPE_FALLBACK && result != GrpclbRouteType.GRPCLB_ROUTE_TYPE_BACKEND) {
        throw new AssertionError("Received invalid LB route type. This suggests " + "that the server hasn't implemented this test correctly.");
    }
    return result;
}
Also used : GrpclbRouteType(io.grpc.testing.integration.Messages.GrpclbRouteType) SimpleResponse(io.grpc.testing.integration.Messages.SimpleResponse) StatusRuntimeException(io.grpc.StatusRuntimeException) 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