Search in sources :

Example 1 with Endpoint

use of com.twitter.thrift.Endpoint in project commons by twitter.

the class ServerSetImplTest method testJsonCodecCompatibility.

@Test
public void testJsonCodecCompatibility() throws IOException {
    ServiceInstance instance = new ServiceInstance(new Endpoint("foo", 1000), ImmutableMap.of("http", new Endpoint("foo", 8080)), Status.ALIVE).setShard(42);
    ByteArrayOutputStream legacy = new ByteArrayOutputStream();
    JsonCodec.create(ServiceInstance.class, new GsonBuilder().setExclusionStrategies(JsonCodec.getThriftExclusionStrategy()).create()).serialize(instance, legacy);
    ByteArrayOutputStream results = new ByteArrayOutputStream();
    ServerSetImpl.createJsonCodec().serialize(instance, results);
    assertEquals(legacy.toString(), results.toString());
    results = new ByteArrayOutputStream();
    ServerSetImpl.createJsonCodec().serialize(instance, results);
    assertEquals("{\"serviceEndpoint\":{\"host\":\"foo\",\"port\":1000}," + "\"additionalEndpoints\":{\"http\":{\"host\":\"foo\",\"port\":8080}}," + "\"status\":\"ALIVE\"," + "\"shard\":42}", results.toString());
}
Also used : Endpoint(com.twitter.thrift.Endpoint) GsonBuilder(com.google.gson.GsonBuilder) ServiceInstance(com.twitter.thrift.ServiceInstance) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BaseZooKeeperTest(com.twitter.common.zookeeper.testing.BaseZooKeeperTest) Test(org.junit.Test)

Example 2 with Endpoint

use of com.twitter.thrift.Endpoint in project commons by twitter.

the class ServerSetImplTest method testOrdering.

@Test
public void testOrdering() throws Exception {
    ServerSetImpl client = createServerSet();
    client.watch(serverSetMonitor);
    assertChangeFiredEmpty();
    Map<String, InetSocketAddress> server1Ports = makePortMap("http-admin1", 8080);
    Map<String, InetSocketAddress> server2Ports = makePortMap("http-admin2", 8081);
    Map<String, InetSocketAddress> server3Ports = makePortMap("http-admin3", 8082);
    ServerSetImpl server1 = createServerSet();
    ServerSetImpl server2 = createServerSet();
    ServerSetImpl server3 = createServerSet();
    ServiceInstance instance1 = new ServiceInstance(new Endpoint("foo", 1000), ImmutableMap.of("http-admin1", new Endpoint("foo", 8080)), Status.ALIVE).setShard(0);
    ServiceInstance instance2 = new ServiceInstance(new Endpoint("foo", 1001), ImmutableMap.of("http-admin2", new Endpoint("foo", 8081)), Status.ALIVE).setShard(1);
    ServiceInstance instance3 = new ServiceInstance(new Endpoint("foo", 1002), ImmutableMap.of("http-admin3", new Endpoint("foo", 8082)), Status.ALIVE).setShard(2);
    server1.join(InetSocketAddress.createUnresolved("foo", 1000), server1Ports, 0);
    assertEquals(ImmutableList.of(instance1), ImmutableList.copyOf(serverSetBuffer.take()));
    EndpointStatus status2 = server2.join(InetSocketAddress.createUnresolved("foo", 1001), server2Ports, 1);
    assertEquals(ImmutableList.of(instance1, instance2), ImmutableList.copyOf(serverSetBuffer.take()));
    server3.join(InetSocketAddress.createUnresolved("foo", 1002), server3Ports, 2);
    assertEquals(ImmutableList.of(instance1, instance2, instance3), ImmutableList.copyOf(serverSetBuffer.take()));
    status2.leave();
    assertEquals(ImmutableList.of(instance1, instance3), ImmutableList.copyOf(serverSetBuffer.take()));
}
Also used : EndpointStatus(com.twitter.common.zookeeper.ServerSet.EndpointStatus) Endpoint(com.twitter.thrift.Endpoint) InetSocketAddress(java.net.InetSocketAddress) ServiceInstance(com.twitter.thrift.ServiceInstance) BaseZooKeeperTest(com.twitter.common.zookeeper.testing.BaseZooKeeperTest) Test(org.junit.Test)

Example 3 with Endpoint

use of com.twitter.thrift.Endpoint in project commons by twitter.

the class ServerSetImplTest method testLifecycle.

@Test
public void testLifecycle() throws Exception {
    ServerSetImpl client = createServerSet();
    client.watch(serverSetMonitor);
    assertChangeFiredEmpty();
    ServerSetImpl server = createServerSet();
    EndpointStatus status = server.join(InetSocketAddress.createUnresolved("foo", 1234), makePortMap("http-admin", 8080), 0);
    ServiceInstance serviceInstance = new ServiceInstance(new Endpoint("foo", 1234), ImmutableMap.of("http-admin", new Endpoint("foo", 8080)), Status.ALIVE).setShard(0);
    assertChangeFired(serviceInstance);
    status.leave();
    assertChangeFiredEmpty();
    assertTrue(serverSetBuffer.isEmpty());
}
Also used : EndpointStatus(com.twitter.common.zookeeper.ServerSet.EndpointStatus) Endpoint(com.twitter.thrift.Endpoint) ServiceInstance(com.twitter.thrift.ServiceInstance) BaseZooKeeperTest(com.twitter.common.zookeeper.testing.BaseZooKeeperTest) Test(org.junit.Test)

Example 4 with Endpoint

use of com.twitter.thrift.Endpoint in project commons by twitter.

the class ServerSetsTest method testSimpleSerialization.

@Test
public void testSimpleSerialization() throws Exception {
    InetSocketAddress endpoint = new InetSocketAddress(12345);
    Map<String, Endpoint> additionalEndpoints = ImmutableMap.of();
    Status status = Status.ALIVE;
    Codec<ServiceInstance> codec = ServerSetImpl.createDefaultCodec();
    byte[] data = ServerSets.serializeServiceInstance(endpoint, additionalEndpoints, status, codec);
    ServiceInstance instance = ServerSets.deserializeServiceInstance(data, codec);
    assertEquals(endpoint.getPort(), instance.getServiceEndpoint().getPort());
    assertEquals(additionalEndpoints, instance.getAdditionalEndpoints());
    assertEquals(Status.ALIVE, instance.getStatus());
}
Also used : Status(com.twitter.thrift.Status) Endpoint(com.twitter.thrift.Endpoint) InetSocketAddress(java.net.InetSocketAddress) ServiceInstance(com.twitter.thrift.ServiceInstance) Test(org.junit.Test)

Example 5 with Endpoint

use of com.twitter.thrift.Endpoint 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

Endpoint (com.twitter.thrift.Endpoint)8 ServiceInstance (com.twitter.thrift.ServiceInstance)8 Test (org.junit.Test)6 BaseZooKeeperTest (com.twitter.common.zookeeper.testing.BaseZooKeeperTest)4 InetSocketAddress (java.net.InetSocketAddress)4 EndpointStatus (com.twitter.common.zookeeper.ServerSet.EndpointStatus)2 GsonBuilder (com.google.gson.GsonBuilder)1 DynamicHostSet (com.twitter.common.net.pool.DynamicHostSet)1 Status (com.twitter.thrift.Status)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 HashMap (java.util.HashMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1