use of com.twitter.distributedlog.client.proxy.MockProxyClientBuilder.MockProxyClient in project distributedlog by twitter.
the class TestProxyClientManager method testHandshake.
@Test(timeout = 60000)
public void testHandshake() throws Exception {
final int numHosts = 3;
final int numStreamsPerHost = 3;
final int initialPort = 4000;
MockProxyClientBuilder builder = new MockProxyClientBuilder();
Map<SocketAddress, ServerInfo> serverInfoMap = new HashMap<SocketAddress, ServerInfo>();
for (int i = 0; i < numHosts; i++) {
SocketAddress address = createSocketAddress(initialPort + i);
ServerInfo serverInfo = new ServerInfo();
for (int j = 0; j < numStreamsPerHost; j++) {
serverInfo.putToOwnerships(runtime.getMethodName() + "_stream_" + j, address.toString());
}
Pair<MockProxyClient, MockServerInfoService> mockProxyClient = createMockProxyClient(address, serverInfo);
builder.provideProxyClient(address, mockProxyClient.getLeft());
serverInfoMap.put(address, serverInfo);
}
final Map<SocketAddress, ServerInfo> results = new HashMap<SocketAddress, ServerInfo>();
final CountDownLatch doneLatch = new CountDownLatch(2 * numHosts);
ProxyListener listener = new ProxyListener() {
@Override
public void onHandshakeSuccess(SocketAddress address, ProxyClient client, ServerInfo serverInfo) {
synchronized (results) {
results.put(address, serverInfo);
}
doneLatch.countDown();
}
@Override
public void onHandshakeFailure(SocketAddress address, ProxyClient client, Throwable cause) {
}
};
TestHostProvider rs = new TestHostProvider();
ProxyClientManager clientManager = createProxyClientManager(builder, rs, 0L);
clientManager.registerProxyListener(listener);
assertEquals("There should be no clients in the manager", 0, clientManager.getNumProxies());
for (int i = 0; i < numHosts; i++) {
rs.addHost(createSocketAddress(initialPort + i));
}
// handshake would handshake with 3 hosts again
clientManager.handshake();
doneLatch.await();
assertEquals("Handshake should return server info", numHosts, results.size());
assertTrue("Handshake should get all server infos", Maps.difference(serverInfoMap, results).areEqual());
}
use of com.twitter.distributedlog.client.proxy.MockProxyClientBuilder.MockProxyClient in project distributedlog by twitter.
the class TestProxyClientManager method testCreateClientShouldHandshake.
@Test(timeout = 60000)
public void testCreateClientShouldHandshake() throws Exception {
SocketAddress address = createSocketAddress(3000);
MockProxyClientBuilder builder = new MockProxyClientBuilder();
ServerInfo serverInfo = new ServerInfo();
serverInfo.putToOwnerships(runtime.getMethodName() + "_stream", runtime.getMethodName() + "_owner");
Pair<MockProxyClient, MockServerInfoService> mockProxyClient = createMockProxyClient(address, serverInfo);
builder.provideProxyClient(address, mockProxyClient.getLeft());
final AtomicReference<ServerInfo> resultHolder = new AtomicReference<ServerInfo>(null);
final CountDownLatch doneLatch = new CountDownLatch(1);
ProxyListener listener = new ProxyListener() {
@Override
public void onHandshakeSuccess(SocketAddress address, ProxyClient client, ServerInfo serverInfo) {
resultHolder.set(serverInfo);
doneLatch.countDown();
}
@Override
public void onHandshakeFailure(SocketAddress address, ProxyClient client, Throwable cause) {
}
};
ProxyClientManager clientManager = createProxyClientManager(builder, 0L);
clientManager.registerProxyListener(listener);
assertEquals("There should be no clients in the manager", 0, clientManager.getNumProxies());
clientManager.createClient(address);
assertEquals("Create client should build the proxy client", 1, clientManager.getNumProxies());
// When a client is created, it would handshake with that proxy
doneLatch.await();
assertEquals("Handshake should return server info", serverInfo, resultHolder.get());
}
use of com.twitter.distributedlog.client.proxy.MockProxyClientBuilder.MockProxyClient in project distributedlog by twitter.
the class TestProxyClientManager method testRemoveClient.
@Test(timeout = 60000)
public void testRemoveClient() throws Exception {
SocketAddress address = createSocketAddress(3000);
MockProxyClientBuilder builder = new MockProxyClientBuilder();
MockProxyClient mockProxyClient = createMockProxyClient(address);
builder.provideProxyClient(address, mockProxyClient);
ProxyClientManager clientManager = createProxyClientManager(builder, 0L);
assertEquals("There should be no clients in the manager", 0, clientManager.getNumProxies());
clientManager.createClient(address);
assertEquals("Create client should build the proxy client", 1, clientManager.getNumProxies());
clientManager.removeClient(address);
assertEquals("Remove should remove proxy client", 0, clientManager.getNumProxies());
}
Aggregations