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);
}
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());
}
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);
}
}
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;
}
}
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());
}
}
Aggregations