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