use of com.twitter.thrift.ServiceInstance in project commons by twitter.
the class DynamicPoolTest method mySetUp.
@Before
public void mySetUp() throws Exception {
control = createControl();
@SuppressWarnings("unchecked") Function<InetSocketAddress, ObjectPool<Connection<TTransport, InetSocketAddress>>> poolFactory = control.createMock(Function.class);
this.poolFactory = poolFactory;
LoadBalancerImpl<InetSocketAddress> lb = LoadBalancerImpl.create(new RandomStrategy<InetSocketAddress>());
poolRebuilds = new LinkedBlockingQueue<Pair<Set<ObjectPool<Connection<TTransport, InetSocketAddress>>>, Map<InetSocketAddress, ObjectPool<Connection<TTransport, InetSocketAddress>>>>>();
serverSet = new ServerSetImpl(createZkClient(), ZooDefs.Ids.OPEN_ACL_UNSAFE, "/test-service");
Closure<Collection<InetSocketAddress>> onBackendsChosen = Closures.noop();
Amount<Long, Time> restoreInterval = Amount.of(1L, Time.MINUTES);
connectionPool = new DynamicPool<ServiceInstance, TTransport, InetSocketAddress>(serverSet, poolFactory, lb, onBackendsChosen, restoreInterval, Util.GET_ADDRESS, Util.IS_ALIVE) {
@Override
void poolRebuilt(Set<ObjectPool<Connection<TTransport, InetSocketAddress>>> deadPools, Map<InetSocketAddress, ObjectPool<Connection<TTransport, InetSocketAddress>>> livePools) {
super.poolRebuilt(deadPools, livePools);
poolRebuilds.offer(Pair.of(deadPools, livePools));
}
};
}
use of com.twitter.thrift.ServiceInstance in project commons by twitter.
the class ThriftFactoryTest method testCreateEmpty.
@Test(expected = TResourceExhaustedException.class)
public void testCreateEmpty() throws Exception {
@SuppressWarnings("unchecked") DynamicHostSet<ServiceInstance> emptyHostSet = control.createMock(DynamicHostSet.class);
final Thrift<GoodService.Iface> thrift = ThriftFactory.create(GoodService.Iface.class).withMaxConnectionsPerEndpoint(1).build(emptyHostSet);
addTearDown(new TearDown() {
@Override
public void tearDown() {
thrift.close();
}
});
GoodService.Iface client = thrift.create();
// This should throw a TResourceExhaustedException
client.doWork();
}
use of com.twitter.thrift.ServiceInstance in project commons by twitter.
the class CompoundServerSetTest method testMonitorFailure.
@Test(expected = MonitorException.class)
public void testMonitorFailure() throws Exception {
serverSet1.watch(EasyMock.<HostChangeMonitor<ServiceInstance>>anyObject());
expectLastCall().andThrow(new MonitorException("Monitor exception", null));
control.replay();
compoundServerSet.watch(compoundMonitor);
}
use of com.twitter.thrift.ServiceInstance in project commons by twitter.
the class ServerSetImplTest method testJsonCodecRoundtrip.
@Test
public void testJsonCodecRoundtrip() throws Exception {
Codec<ServiceInstance> codec = ServerSetImpl.createJsonCodec();
ServiceInstance instance1 = new ServiceInstance(new Endpoint("foo", 1000), ImmutableMap.of("http", new Endpoint("foo", 8080)), Status.ALIVE).setShard(0);
byte[] data = ServerSets.serializeServiceInstance(instance1, codec);
assertTrue(ServerSets.deserializeServiceInstance(data, codec).getServiceEndpoint().isSetPort());
assertTrue(ServerSets.deserializeServiceInstance(data, codec).isSetShard());
ServiceInstance instance2 = new ServiceInstance(new Endpoint("foo", 1000), ImmutableMap.of("http-admin1", new Endpoint("foo", 8080)), Status.ALIVE);
data = ServerSets.serializeServiceInstance(instance2, codec);
assertTrue(ServerSets.deserializeServiceInstance(data, codec).getServiceEndpoint().isSetPort());
assertFalse(ServerSets.deserializeServiceInstance(data, codec).isSetShard());
ServiceInstance instance3 = new ServiceInstance(new Endpoint("foo", 1000), ImmutableMap.<String, Endpoint>of(), Status.ALIVE);
data = ServerSets.serializeServiceInstance(instance3, codec);
assertTrue(ServerSets.deserializeServiceInstance(data, codec).getServiceEndpoint().isSetPort());
assertFalse(ServerSets.deserializeServiceInstance(data, codec).isSetShard());
}
use of com.twitter.thrift.ServiceInstance in project distributedlog by twitter.
the class TestInetNameResolution method testInetNameResolution.
@Test(timeout = 10000)
public void testInetNameResolution() throws Exception {
String nameStr = "inet!127.0.0.1:3181";
final CountDownLatch resolved = new CountDownLatch(1);
final AtomicBoolean validationFailed = new AtomicBoolean(false);
NameServerSet serverSet = new NameServerSet(nameStr);
serverSet.watch(new DynamicHostSet.HostChangeMonitor<ServiceInstance>() {
@Override
public void onChange(ImmutableSet<ServiceInstance> hostSet) {
if (hostSet.size() > 1) {
logger.error("HostSet has more elements than expected {}", hostSet);
validationFailed.set(true);
resolved.countDown();
} else if (hostSet.size() == 1) {
ServiceInstance serviceInstance = hostSet.iterator().next();
Endpoint endpoint = serviceInstance.getAdditionalEndpoints().get("thrift");
InetSocketAddress address = new InetSocketAddress(endpoint.getHost(), endpoint.getPort());
if (endpoint.getPort() != 3181) {
logger.error("Port does not match the expected port {}", endpoint.getPort());
validationFailed.set(true);
} else if (!address.getAddress().getHostAddress().equals("127.0.0.1")) {
logger.error("Host address does not match the expected address {}", address.getAddress().getHostAddress());
validationFailed.set(true);
}
resolved.countDown();
}
}
});
resolved.await();
Assert.assertEquals(false, validationFailed.get());
}
Aggregations