Search in sources :

Example 16 with Color

use of com.google.type.Color in project gax-java by googleapis.

the class GrpcClientCallsTest method testAffinity.

@Test
public void testAffinity() throws IOException {
    MethodDescriptor<Color, Money> descriptor = FakeServiceGrpc.METHOD_RECOGNIZE;
    @SuppressWarnings("unchecked") ClientCall<Color, Money> clientCall0 = Mockito.mock(ClientCall.class);
    @SuppressWarnings("unchecked") ClientCall<Color, Money> clientCall1 = Mockito.mock(ClientCall.class);
    ManagedChannel channel0 = Mockito.mock(ManagedChannel.class);
    ManagedChannel channel1 = Mockito.mock(ManagedChannel.class);
    Mockito.when(channel0.newCall(Mockito.eq(descriptor), Mockito.<CallOptions>any())).thenReturn(clientCall0);
    Mockito.when(channel1.newCall(Mockito.eq(descriptor), Mockito.<CallOptions>any())).thenReturn(clientCall1);
    Channel pool = ChannelPool.create(ChannelPoolSettings.staticallySized(2), new FakeChannelFactory(Arrays.asList(channel0, channel1)));
    GrpcCallContext context = GrpcCallContext.createDefault().withChannel(pool);
    ClientCall<Color, Money> gotCallA = GrpcClientCalls.newCall(descriptor, context.withChannelAffinity(0));
    ClientCall<Color, Money> gotCallB = GrpcClientCalls.newCall(descriptor, context.withChannelAffinity(0));
    ClientCall<Color, Money> gotCallC = GrpcClientCalls.newCall(descriptor, context.withChannelAffinity(1));
    verify(channel0, Mockito.times(2)).newCall(Mockito.eq(descriptor), Mockito.any());
    verify(channel1, Mockito.times(1)).newCall(Mockito.eq(descriptor), Mockito.any());
}
Also used : Money(com.google.type.Money) Color(com.google.type.Color) ManagedChannel(io.grpc.ManagedChannel) Channel(io.grpc.Channel) ManagedChannel(io.grpc.ManagedChannel) FakeChannelFactory(com.google.api.gax.grpc.testing.FakeChannelFactory) Test(org.junit.Test)

Example 17 with Color

use of com.google.type.Color in project gax-java by googleapis.

the class GrpcClientCallsTest method testExtraHeaders.

@Test
public void testExtraHeaders() {
    Metadata emptyHeaders = new Metadata();
    final Map<String, List<String>> extraHeaders = new HashMap<>();
    extraHeaders.put("header-key-1", ImmutableList.<String>of("header-value-11", "header-value-12"));
    extraHeaders.put("header-key-2", ImmutableList.<String>of("header-value-21"));
    MethodDescriptor<Color, Money> descriptor = FakeServiceGrpc.METHOD_RECOGNIZE;
    @SuppressWarnings("unchecked") ClientCall<Color, Money> mockClientCall = Mockito.mock(ClientCall.class);
    @SuppressWarnings("unchecked") ClientCall.Listener<Money> mockListener = Mockito.mock(ClientCall.Listener.class);
    Channel mockChannel = Mockito.mock(ManagedChannel.class);
    Mockito.doAnswer(invocation -> {
        Metadata clientCallHeaders = (Metadata) invocation.getArguments()[1];
        Metadata.Key<String> key1 = Metadata.Key.of("header-key-1", Metadata.ASCII_STRING_MARSHALLER);
        Metadata.Key<String> key2 = Metadata.Key.of("header-key-2", Metadata.ASCII_STRING_MARSHALLER);
        assertThat(clientCallHeaders.getAll(key1)).containsExactly("header-value-11", "header-value-12");
        assertThat(clientCallHeaders.getAll(key2)).containsExactly("header-value-21");
        return null;
    }).when(mockClientCall).start(Mockito.<ClientCall.Listener<Money>>any(), Mockito.<Metadata>any());
    Mockito.when(mockChannel.newCall(Mockito.eq(descriptor), Mockito.<CallOptions>any())).thenReturn(mockClientCall);
    GrpcCallContext context = GrpcCallContext.createDefault().withChannel(mockChannel).withExtraHeaders(extraHeaders);
    GrpcClientCalls.newCall(descriptor, context).start(mockListener, emptyHeaders);
}
Also used : HashMap(java.util.HashMap) Color(com.google.type.Color) ManagedChannel(io.grpc.ManagedChannel) Channel(io.grpc.Channel) Metadata(io.grpc.Metadata) Money(com.google.type.Money) ClientCall(io.grpc.ClientCall) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Test(org.junit.Test)

Example 18 with Color

use of com.google.type.Color in project gax-java by googleapis.

the class GrpcResponseMetadataTest method testResponseMetadataUnaryCall.

@Test
public void testResponseMetadataUnaryCall() {
    GrpcCallSettings<Color, Money> grpcCallSettings = GrpcCallSettings.create(FakeServiceGrpc.METHOD_RECOGNIZE);
    UnaryCallSettings<Color, Money> callSettings = UnaryCallSettings.<Color, Money>newUnaryCallSettingsBuilder().build();
    UnaryCallable<Color, Money> callable = GrpcCallableFactory.createUnaryCallable(grpcCallSettings, callSettings, clientContext);
    Assert.assertNull(requestHeaders);
    GrpcResponseMetadata responseMetadata = new GrpcResponseMetadata();
    callable.call(Color.getDefaultInstance(), responseMetadata.createContextWithHandlers());
    Assert.assertNotNull(requestHeaders);
    Metadata metadata = responseMetadata.getMetadata();
    Metadata trailingMetadata = responseMetadata.getTrailingMetadata();
    Assert.assertEquals(metadata.get(Key.of(HEADER_KEY, Metadata.ASCII_STRING_MARSHALLER)), HEADER_VALUE);
    Assert.assertEquals(trailingMetadata.get(Key.of(TRAILER_KEY, Metadata.ASCII_STRING_MARSHALLER)), TRAILER_VALUE);
}
Also used : Money(com.google.type.Money) Color(com.google.type.Color) Metadata(io.grpc.Metadata) Test(org.junit.Test)

Example 19 with Color

use of com.google.type.Color in project gax-java by googleapis.

the class GrpcResponseMetadataTest method setUp.

@Before
public void setUp() throws Exception {
    String serverName = "fakeservice";
    FakeServiceImplBase serviceImpl = Mockito.mock(FakeServiceImplBase.class);
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) {
            Color color = invocation.getArgument(0);
            StreamObserver<Money> observer = invocation.getArgument(1);
            observer.onNext(Money.newBuilder().setCurrencyCode("USD").setUnits((long) (color.getRed() * 255)).build());
            observer.onCompleted();
            return null;
        }
    }).when(serviceImpl).recognize(Mockito.<Color>any(), Mockito.<StreamObserver<Money>>any());
    requestHeaders = null;
    inprocessServer = new InProcessServer<>(serviceImpl, serverName, new ServerInterceptor() {

        @Override
        public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
            requestHeaders = headers;
            return next.startCall(new SimpleForwardingServerCall<ReqT, RespT>(call) {

                @Override
                public void sendHeaders(Metadata responseHeaders) {
                    responseHeaders.put(Key.of(HEADER_KEY, Metadata.ASCII_STRING_MARSHALLER), HEADER_VALUE);
                    super.sendHeaders(responseHeaders);
                }

                @Override
                public void close(Status status, Metadata trailers) {
                    trailers.put(Key.of(TRAILER_KEY, Metadata.ASCII_STRING_MARSHALLER), TRAILER_VALUE);
                    super.close(status, trailers);
                }
            }, headers);
        }
    });
    inprocessServer.start();
    channel = InProcessChannelBuilder.forName(serverName).directExecutor().usePlaintext().intercept(new GrpcMetadataHandlerInterceptor()).build();
    clientContext = ClientContext.newBuilder().setTransportChannel(GrpcTransportChannel.create(channel)).setDefaultCallContext(GrpcCallContext.of(channel, CallOptions.DEFAULT)).build();
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) Status(io.grpc.Status) ServerCallHandler(io.grpc.ServerCallHandler) Color(com.google.type.Color) Metadata(io.grpc.Metadata) Money(com.google.type.Money) SimpleForwardingServerCall(io.grpc.ForwardingServerCall.SimpleForwardingServerCall) ServerCall(io.grpc.ServerCall) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ServerInterceptor(io.grpc.ServerInterceptor) FakeServiceImplBase(com.google.api.gax.grpc.testing.FakeServiceGrpc.FakeServiceImplBase) Before(org.junit.Before)

Aggregations

Color (com.google.type.Color)19 Money (com.google.type.Money)19 Test (org.junit.Test)15 ManagedChannel (io.grpc.ManagedChannel)8 Metadata (io.grpc.Metadata)6 CallOptions (io.grpc.CallOptions)5 Channel (io.grpc.Channel)5 ClientCall (io.grpc.ClientCall)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 Deadline (io.grpc.Deadline)3 Before (org.junit.Before)3 Duration (org.threeten.bp.Duration)3 FakeChannelFactory (com.google.api.gax.grpc.testing.FakeChannelFactory)2 Operation (com.google.longrunning.Operation)2 StatusRuntimeException (io.grpc.StatusRuntimeException)2 FakeApiClock (com.google.api.gax.core.FakeApiClock)1 FakeServiceImplBase (com.google.api.gax.grpc.testing.FakeServiceGrpc.FakeServiceImplBase)1 FakeServiceImpl (com.google.api.gax.grpc.testing.FakeServiceImpl)1 OperationSnapshot (com.google.api.gax.longrunning.OperationSnapshot)1 TransportChannel (com.google.api.gax.rpc.TransportChannel)1