Search in sources :

Example 6 with ServiceInstance

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));
        }
    };
}
Also used : InetSocketAddress(java.net.InetSocketAddress) ServiceInstance(com.twitter.thrift.ServiceInstance) Time(com.twitter.common.quantity.Time) ServerSetImpl(com.twitter.common.zookeeper.ServerSetImpl) Collection(java.util.Collection) TTransport(org.apache.thrift.transport.TTransport) Pair(com.twitter.common.collections.Pair) Before(org.junit.Before)

Example 7 with ServiceInstance

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();
}
Also used : TearDown(com.google.common.testing.TearDown) AsyncIface(com.twitter.common.thrift.ThriftFactoryTest.GoodService.AsyncIface) ServiceInstance(com.twitter.thrift.ServiceInstance) Test(org.junit.Test)

Example 8 with ServiceInstance

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);
}
Also used : ServiceInstance(com.twitter.thrift.ServiceInstance) MonitorException(com.twitter.common.net.pool.DynamicHostSet.MonitorException) EasyMockTest(com.twitter.common.testing.easymock.EasyMockTest) Test(org.junit.Test)

Example 9 with ServiceInstance

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());
}
Also used : Endpoint(com.twitter.thrift.Endpoint) ServiceInstance(com.twitter.thrift.ServiceInstance) BaseZooKeeperTest(com.twitter.common.zookeeper.testing.BaseZooKeeperTest) Test(org.junit.Test)

Example 10 with ServiceInstance

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());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Endpoint(com.twitter.thrift.Endpoint) InetSocketAddress(java.net.InetSocketAddress) ServiceInstance(com.twitter.thrift.ServiceInstance) CountDownLatch(java.util.concurrent.CountDownLatch) DynamicHostSet(com.twitter.common.net.pool.DynamicHostSet) Test(org.junit.Test)

Aggregations

ServiceInstance (com.twitter.thrift.ServiceInstance)14 Test (org.junit.Test)10 Endpoint (com.twitter.thrift.Endpoint)8 BaseZooKeeperTest (com.twitter.common.zookeeper.testing.BaseZooKeeperTest)5 InetSocketAddress (java.net.InetSocketAddress)5 EndpointStatus (com.twitter.common.zookeeper.ServerSet.EndpointStatus)3 DynamicHostSet (com.twitter.common.net.pool.DynamicHostSet)2 EasyMockTest (com.twitter.common.testing.easymock.EasyMockTest)2 Status (com.twitter.thrift.Status)2 TearDown (com.google.common.testing.TearDown)1 GsonBuilder (com.google.gson.GsonBuilder)1 Command (com.twitter.common.base.Command)1 Pair (com.twitter.common.collections.Pair)1 MonitorException (com.twitter.common.net.pool.DynamicHostSet.MonitorException)1 Time (com.twitter.common.quantity.Time)1 AsyncIface (com.twitter.common.thrift.ThriftFactoryTest.GoodService.AsyncIface)1 Group (com.twitter.common.zookeeper.Group)1 ServerSetImpl (com.twitter.common.zookeeper.ServerSetImpl)1 ZooKeeperClient (com.twitter.common.zookeeper.ZooKeeperClient)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1