Search in sources :

Example 1 with ServiceInstance

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

the class ServerSetImplTest method testUnwatchOnException.

@Test
public void testUnwatchOnException() throws Exception {
    IMocksControl control = createControl();
    ZooKeeperClient zkClient = control.createMock(ZooKeeperClient.class);
    Watcher onExpirationWatcher = control.createMock(Watcher.class);
    expect(zkClient.registerExpirationHandler(anyObject(Command.class))).andReturn(onExpirationWatcher);
    expect(zkClient.get()).andThrow(new InterruptedException());
    expect(zkClient.unregister(onExpirationWatcher)).andReturn(true);
    control.replay();
    Group group = new Group(zkClient, ZooDefs.Ids.OPEN_ACL_UNSAFE, "/blabla");
    ServerSetImpl serverset = new ServerSetImpl(zkClient, group);
    try {
        serverset.watch(new DynamicHostSet.HostChangeMonitor<ServiceInstance>() {

            @Override
            public void onChange(ImmutableSet<ServiceInstance> hostSet) {
            }
        });
        fail("Expected MonitorException");
    } catch (DynamicHostSet.MonitorException e) {
    // expected
    }
    control.verify();
}
Also used : Group(com.twitter.common.zookeeper.Group) Watcher(org.apache.zookeeper.Watcher) ServiceInstance(com.twitter.thrift.ServiceInstance) DynamicHostSet(com.twitter.common.net.pool.DynamicHostSet) IMocksControl(org.easymock.IMocksControl) ZooKeeperClient(com.twitter.common.zookeeper.ZooKeeperClient) Command(com.twitter.common.base.Command) Override(java.lang.Override) BaseZooKeeperTest(com.twitter.common.zookeeper.testing.BaseZooKeeperTest) Test(org.junit.Test)

Example 2 with ServiceInstance

use of com.twitter.thrift.ServiceInstance 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 3 with ServiceInstance

use of com.twitter.thrift.ServiceInstance 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 4 with ServiceInstance

use of com.twitter.thrift.ServiceInstance 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 5 with ServiceInstance

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

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