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