Search in sources :

Example 66 with ManagedChannel

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

the class LoggingChannelProviderTest method forTarget_interceptorCalled.

@Test
public void forTarget_interceptorCalled() {
    ClientInterceptor interceptor = mock(ClientInterceptor.class, delegatesTo(new NoopInterceptor()));
    InternalLoggingChannelInterceptor.Factory factory = mock(InternalLoggingChannelInterceptor.Factory.class);
    when(factory.create()).thenReturn(interceptor);
    LoggingChannelProvider.init(factory);
    ManagedChannelBuilder<?> builder = ManagedChannelBuilder.forTarget("localhost");
    ManagedChannel channel = builder.build();
    CallOptions callOptions = CallOptions.DEFAULT;
    ClientCall<Void, Void> unused = channel.newCall(method, callOptions);
    verify(interceptor).interceptCall(same(method), same(callOptions), ArgumentMatchers.<Channel>any());
    channel.shutdownNow();
    LoggingChannelProvider.finish();
}
Also used : InternalLoggingChannelInterceptor(io.grpc.observability.interceptors.InternalLoggingChannelInterceptor) ClientInterceptor(io.grpc.ClientInterceptor) ManagedChannel(io.grpc.ManagedChannel) CallOptions(io.grpc.CallOptions) Test(org.junit.Test)

Example 67 with ManagedChannel

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

the class OkHttpChannelBuilderTest method authorityIsReadable.

@Test
public void authorityIsReadable() {
    OkHttpChannelBuilder builder = OkHttpChannelBuilder.forAddress("original", 1234);
    ManagedChannel channel = grpcCleanupRule.register(builder.build());
    assertEquals("original:1234", channel.authority());
}
Also used : ManagedChannel(io.grpc.ManagedChannel) Test(org.junit.Test)

Example 68 with ManagedChannel

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

the class OrcaOobUtilTest method setUp.

@Before
@SuppressWarnings("unchecked")
public void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
    for (int i = 0; i < NUM_SUBCHANNELS; i++) {
        orcaServiceImps[i] = new OpenRcaServiceImp();
        cleanupRule.register(InProcessServerBuilder.forName("orca-reporting-test-" + i).addService(orcaServiceImps[i]).directExecutor().build().start());
        ManagedChannel channel = cleanupRule.register(InProcessChannelBuilder.forName("orca-reporting-test-" + i).directExecutor().build());
        channels[i] = channel;
        EquivalentAddressGroup eag = new EquivalentAddressGroup(new FakeSocketAddress("address-" + i));
        List<EquivalentAddressGroup> eagList = Arrays.asList(eag);
        eagLists[i] = eagList;
        mockStateListeners[i] = mock(SubchannelStateListener.class);
    }
    when(backoffPolicyProvider.get()).thenReturn(backoffPolicy1, backoffPolicy2);
    when(backoffPolicy1.nextBackoffNanos()).thenReturn(11L, 21L);
    when(backoffPolicy2.nextBackoffNanos()).thenReturn(12L, 22L);
    orcaHelperWrapper = OrcaOobUtil.newOrcaReportingHelperWrapper(origHelper, mockOrcaListener0, backoffPolicyProvider, fakeClock.getStopwatchSupplier());
    parentHelperWrapper = OrcaOobUtil.newOrcaReportingHelperWrapper(origHelper, mockOrcaListener1, backoffPolicyProvider, fakeClock.getStopwatchSupplier());
    childHelperWrapper = OrcaOobUtil.newOrcaReportingHelperWrapper(parentHelperWrapper.asHelper(), mockOrcaListener2, backoffPolicyProvider, fakeClock.getStopwatchSupplier());
}
Also used : SubchannelStateListener(io.grpc.LoadBalancer.SubchannelStateListener) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) ManagedChannel(io.grpc.ManagedChannel) Before(org.junit.Before)

Example 69 with ManagedChannel

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

the class AbstractBenchmark method startFlowControlledStreamingCalls.

/**
 * Start a continuously executing set of duplex streaming ping-pong calls that will terminate when
 * {@code done.get()} is true. Each completed call will increment the counter by the specified
 * delta which benchmarks can use to measure messages per second or bandwidth.
 */
protected CountDownLatch startFlowControlledStreamingCalls(int callsPerChannel, final AtomicLong counter, final AtomicBoolean record, final AtomicBoolean done, final long counterDelta) {
    final CountDownLatch latch = new CountDownLatch(callsPerChannel * channels.length);
    for (final ManagedChannel channel : channels) {
        for (int i = 0; i < callsPerChannel; i++) {
            final ClientCall<ByteBuf, ByteBuf> streamingCall = channel.newCall(flowControlledStreaming, CALL_OPTIONS);
            final AtomicReference<StreamObserver<ByteBuf>> requestObserverRef = new AtomicReference<>();
            final AtomicBoolean ignoreMessages = new AtomicBoolean();
            StreamObserver<ByteBuf> requestObserver = ClientCalls.asyncBidiStreamingCall(streamingCall, new StreamObserver<ByteBuf>() {

                @Override
                public void onNext(ByteBuf value) {
                    StreamObserver<ByteBuf> obs = requestObserverRef.get();
                    if (done.get()) {
                        if (!ignoreMessages.getAndSet(true)) {
                            obs.onCompleted();
                        }
                        return;
                    }
                    if (record.get()) {
                        counter.addAndGet(counterDelta);
                    }
                // request is called automatically because the observer implicitly has auto
                // inbound flow control
                }

                @Override
                public void onError(Throwable t) {
                    logger.log(Level.WARNING, "call error", t);
                    latch.countDown();
                }

                @Override
                public void onCompleted() {
                    latch.countDown();
                }
            });
            requestObserverRef.set(requestObserver);
            // Add some outstanding requests to ensure the server is filling the connection
            streamingCall.request(5);
            requestObserver.onNext(request.slice());
        }
    }
    return latch;
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ManagedChannel(io.grpc.ManagedChannel) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuf(io.netty.buffer.ByteBuf)

Example 70 with ManagedChannel

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

the class AbstractBenchmark method startUnaryCalls.

/**
 * Start a continuously executing set of unary calls that will terminate when
 * {@code done.get()} is true. Each completed call will increment the counter by the specified
 * delta which benchmarks can use to measure QPS or bandwidth.
 */
protected void startUnaryCalls(int callsPerChannel, final AtomicLong counter, final AtomicBoolean done, final long counterDelta) {
    for (final ManagedChannel channel : channels) {
        for (int i = 0; i < callsPerChannel; i++) {
            StreamObserver<ByteBuf> observer = new StreamObserver<ByteBuf>() {

                @Override
                public void onNext(ByteBuf value) {
                    counter.addAndGet(counterDelta);
                }

                @Override
                public void onError(Throwable t) {
                    done.set(true);
                }

                @Override
                public void onCompleted() {
                    if (!done.get()) {
                        ByteBuf slice = request.slice();
                        ClientCalls.asyncUnaryCall(channel.newCall(unaryMethod, CALL_OPTIONS), slice, this);
                    }
                }
            };
            observer.onCompleted();
        }
    }
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) ManagedChannel(io.grpc.ManagedChannel) ByteBuf(io.netty.buffer.ByteBuf)

Aggregations

ManagedChannel (io.grpc.ManagedChannel)164 Test (org.junit.Test)92 CountDownLatch (java.util.concurrent.CountDownLatch)26 ManagedChannel (org.apache.beam.vendor.grpc.v1p43p2.io.grpc.ManagedChannel)26 ArrayList (java.util.ArrayList)20 Metadata (io.grpc.Metadata)18 EquivalentAddressGroup (io.grpc.EquivalentAddressGroup)15 ExecutorService (java.util.concurrent.ExecutorService)13 ByteString (com.google.protobuf.ByteString)12 Status (io.grpc.Status)12 StreamObserver (org.apache.beam.vendor.grpc.v1p43p2.io.grpc.stub.StreamObserver)11 StreamObserver (io.grpc.stub.StreamObserver)10 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)10 AtomicReference (java.util.concurrent.atomic.AtomicReference)10 BeamFnApi (org.apache.beam.model.fnexecution.v1.BeamFnApi)10 CallOptions (io.grpc.CallOptions)9 Subchannel (io.grpc.LoadBalancer.Subchannel)9 SubchannelPicker (io.grpc.LoadBalancer.SubchannelPicker)9 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)9 Endpoints (org.apache.beam.model.pipeline.v1.Endpoints)9