Search in sources :

Example 91 with ManagedChannel

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

the class ManagedChannelImplIdlenessTest method updateOobChannelAddresses_newAddressConnects.

@Test
public void updateOobChannelAddresses_newAddressConnects() {
    ClientCall<String, Integer> call = channel.newCall(method, CallOptions.DEFAULT);
    // Create LB
    call.start(mockCallListener, new Metadata());
    ArgumentCaptor<Helper> helperCaptor = ArgumentCaptor.forClass(null);
    verify(mockLoadBalancerProvider).newLoadBalancer(helperCaptor.capture());
    deliverResolutionResult();
    Helper helper = helperCaptor.getValue();
    ManagedChannel oobChannel = helper.createOobChannel(servers.subList(0, 1), "localhost");
    oobChannel.newCall(method, CallOptions.DEFAULT).start(mockCallListener, new Metadata());
    MockClientTransportInfo t0 = newTransports.poll();
    t0.listener.transportReady();
    helper.updateOobChannelAddresses(oobChannel, servers.subList(1, 2));
    oobChannel.newCall(method, CallOptions.DEFAULT).start(mockCallListener, new Metadata());
    MockClientTransportInfo t1 = newTransports.poll();
    t1.listener.transportReady();
    // Drain InternalSubchannel's delayed shutdown on updateAddresses
    timer.forwardTime(ManagedChannelImpl.SUBCHANNEL_SHUTDOWN_DELAY_SECONDS, TimeUnit.SECONDS);
}
Also used : Helper(io.grpc.LoadBalancer.Helper) Metadata(io.grpc.Metadata) ManagedChannel(io.grpc.ManagedChannel) MockClientTransportInfo(io.grpc.internal.TestUtils.MockClientTransportInfo) Test(org.junit.Test)

Example 92 with ManagedChannel

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

the class ManagedChannelImplIdlenessTest method updateOobChannelAddresses_existingAddressDoesNotConnect.

@Test
public void updateOobChannelAddresses_existingAddressDoesNotConnect() {
    ClientCall<String, Integer> call = channel.newCall(method, CallOptions.DEFAULT);
    // Create LB
    call.start(mockCallListener, new Metadata());
    ArgumentCaptor<Helper> helperCaptor = ArgumentCaptor.forClass(null);
    verify(mockLoadBalancerProvider).newLoadBalancer(helperCaptor.capture());
    Helper helper = helperCaptor.getValue();
    deliverResolutionResult();
    ManagedChannel oobChannel = helper.createOobChannel(servers.subList(0, 1), "localhost");
    oobChannel.newCall(method, CallOptions.DEFAULT).start(mockCallListener, new Metadata());
    MockClientTransportInfo t0 = newTransports.poll();
    t0.listener.transportReady();
    List<SocketAddress> changedList = new ArrayList<>(servers.get(0).getAddresses());
    changedList.add(new FakeSocketAddress("aDifferentServer"));
    helper.updateOobChannelAddresses(oobChannel, Collections.singletonList(new EquivalentAddressGroup(changedList)));
    oobChannel.newCall(method, CallOptions.DEFAULT).start(mockCallListener, new Metadata());
    assertNull(newTransports.poll());
}
Also used : Metadata(io.grpc.Metadata) ArrayList(java.util.ArrayList) MockClientTransportInfo(io.grpc.internal.TestUtils.MockClientTransportInfo) Helper(io.grpc.LoadBalancer.Helper) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) ManagedChannel(io.grpc.ManagedChannel) SocketAddress(java.net.SocketAddress) Test(org.junit.Test)

Example 93 with ManagedChannel

use of io.grpc.ManagedChannel in project dubbo by alibaba.

the class JEtcdClientWrapper method newChannel.

/**
 * try get client's shared channel, because all fields is private on jetcd,
 * we must using it by reflect, in the future, jetcd may provider better tools.
 *
 * @param client get channel from current client
 * @return current connection channel
 */
private ManagedChannel newChannel(Client client) {
    try {
        Field connectionField = client.getClass().getDeclaredField("connectionManager");
        ReflectUtils.makeAccessible(connectionField);
        Object connection = connectionField.get(client);
        Method channel = connection.getClass().getDeclaredMethod("getChannel");
        ReflectUtils.makeAccessible(channel);
        return (ManagedChannel) channel.invoke(connection);
    } catch (Exception e) {
        throw new RuntimeException("Failed to obtain connection channel from " + url.getBackupAddress(), e);
    }
}
Also used : Field(java.lang.reflect.Field) ManagedChannel(io.grpc.ManagedChannel) Method(java.lang.reflect.Method) TimeoutException(java.util.concurrent.TimeoutException) EtcdException(io.etcd.jetcd.common.exception.EtcdException) ExecutionException(java.util.concurrent.ExecutionException)

Example 94 with ManagedChannel

use of io.grpc.ManagedChannel in project dubbo by alibaba.

the class JEtcdClientTest method getChannel.

private ManagedChannel getChannel(Client client) {
    try {
        // hack, use reflection to get the shared channel.
        Field connectionField = client.getClass().getDeclaredField("connectionManager");
        ReflectUtils.makeAccessible(connectionField);
        Object connection = connectionField.get(client);
        Method channelMethod = connection.getClass().getDeclaredMethod("getChannel");
        ReflectUtils.makeAccessible(channelMethod);
        ManagedChannel channel = (ManagedChannel) channelMethod.invoke(connection);
        return channel;
    } catch (Exception e) {
        return null;
    }
}
Also used : Field(java.lang.reflect.Field) ManagedChannel(io.grpc.ManagedChannel) Method(java.lang.reflect.Method) ClosedClientException(io.etcd.jetcd.common.exception.ClosedClientException)

Example 95 with ManagedChannel

use of io.grpc.ManagedChannel in project dubbo by alibaba.

the class JEtcdClientTest method testWatchWithGrpc.

@Test
public void testWatchWithGrpc() {
    String path = "/dubbo/config/test_watch_with_grpc/configurators";
    String endpoint = "http://127.0.0.1:2379";
    CountDownLatch latch = new CountDownLatch(1);
    try (Client client = Client.builder().endpoints(endpoint).build()) {
        ManagedChannel channel = getChannel(client);
        StreamObserver<WatchRequest> observer = WatchGrpc.newStub(channel).watch(new StreamObserver<WatchResponse>() {

            @Override
            public void onNext(WatchResponse response) {
                for (Event event : response.getEventsList()) {
                    Assertions.assertEquals("PUT", event.getType().toString());
                    Assertions.assertEquals(path, event.getKv().getKey().toString(UTF_8));
                    Assertions.assertEquals("Hello", event.getKv().getValue().toString(UTF_8));
                    latch.countDown();
                }
            }

            @Override
            public void onError(Throwable throwable) {
            }

            @Override
            public void onCompleted() {
            }
        });
        WatchCreateRequest.Builder builder = WatchCreateRequest.newBuilder().setKey(ByteString.copyFrom(path, UTF_8));
        observer.onNext(WatchRequest.newBuilder().setCreateRequest(builder).build());
        // try to modify the key
        client.getKVClient().put(ByteSequence.from(path, UTF_8), ByteSequence.from("Hello", UTF_8));
        latch.await(5, TimeUnit.SECONDS);
    } catch (Exception e) {
        Assertions.fail(e.getMessage());
    }
}
Also used : WatchRequest(io.etcd.jetcd.api.WatchRequest) ByteString(com.google.protobuf.ByteString) CountDownLatch(java.util.concurrent.CountDownLatch) WatchResponse(io.etcd.jetcd.api.WatchResponse) ClosedClientException(io.etcd.jetcd.common.exception.ClosedClientException) WatchCreateRequest(io.etcd.jetcd.api.WatchCreateRequest) ManagedChannel(io.grpc.ManagedChannel) WatchEvent(io.etcd.jetcd.watch.WatchEvent) Event(io.etcd.jetcd.api.Event) Client(io.etcd.jetcd.Client) Test(org.junit.jupiter.api.Test)

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