use of com.twitter.heron.common.network.IncomingPacket in project heron by twitter.
the class ConnectTest method testStart.
/**
* Test connection
*/
@Test
public void testStart() throws Exception {
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.socket().bind(new InetSocketAddress(HOST, serverPort));
SocketChannel socketChannel = null;
try {
runStreamManagerClient();
socketChannel = serverSocketChannel.accept();
configure(socketChannel);
socketChannel.configureBlocking(false);
close(serverSocketChannel);
// Receive request
IncomingPacket incomingPacket = new IncomingPacket();
while (incomingPacket.readFromChannel(socketChannel) != 0) {
// 1ms sleep to mitigate busy looping
SysUtils.sleep(1);
}
// Send back response
// Though we do not use typeName, we need to unpack it first,
// since the order is required
String typeName = incomingPacket.unpackString();
REQID rid = incomingPacket.unpackREQID();
OutgoingPacket outgoingPacket = new OutgoingPacket(rid, UnitTestHelper.getRegisterInstanceResponse());
outgoingPacket.writeToChannel(socketChannel);
for (int i = 0; i < Constants.RETRY_TIMES; i++) {
InstanceControlMsg instanceControlMsg = inControlQueue.poll();
if (instanceControlMsg != null) {
nioLooper.exitLoop();
threadsPool.shutdownNow();
PhysicalPlanHelper physicalPlanHelper = instanceControlMsg.getNewPhysicalPlanHelper();
Assert.assertEquals("test-bolt", physicalPlanHelper.getMyComponent());
Assert.assertEquals(InetAddress.getLocalHost().getHostName(), physicalPlanHelper.getMyHostname());
Assert.assertEquals(0, physicalPlanHelper.getMyInstanceIndex());
Assert.assertEquals(1, physicalPlanHelper.getMyTaskId());
break;
} else {
SysUtils.sleep(Constants.RETRY_INTERVAL_MS);
}
}
} catch (ClosedChannelException ignored) {
} finally {
close(socketChannel);
}
}
use of com.twitter.heron.common.network.IncomingPacket in project heron by twitter.
the class HandleWriteTest method testHandleWrite.
/**
* Test write into network
*/
@Test
public void testHandleWrite() throws Exception {
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.socket().bind(new InetSocketAddress(HOST, serverPort));
SocketChannel socketChannel = null;
try {
runStreamManagerClient();
socketChannel = serverSocketChannel.accept();
configure(socketChannel);
socketChannel.configureBlocking(false);
close(serverSocketChannel);
// Receive request
IncomingPacket incomingPacket = new IncomingPacket();
while (incomingPacket.readFromChannel(socketChannel) != 0) {
// 1ms sleep to mitigate busy looping
SysUtils.sleep(1);
}
// Send back response
// Though we do not use typeName, we need to unpack it first,
// since the order is required
String typeName = incomingPacket.unpackString();
REQID rid = incomingPacket.unpackREQID();
OutgoingPacket outgoingPacket = new OutgoingPacket(rid, UnitTestHelper.getRegisterInstanceResponse());
outgoingPacket.writeToChannel(socketChannel);
for (int i = 0; i < Constants.RETRY_TIMES; i++) {
InstanceControlMsg instanceControlMsg = inControlQueue.poll();
if (instanceControlMsg != null) {
break;
} else {
SysUtils.sleep(Constants.RETRY_INTERVAL_MS);
}
}
for (int i = 0; i < 10; i++) {
// We randomly choose some messages writing to stream mgr
streamManagerClient.sendMessage(UnitTestHelper.getRegisterInstanceResponse());
}
for (int i = 0; i < 10; i++) {
incomingPacket = new IncomingPacket();
while (incomingPacket.readFromChannel(socketChannel) != 0) {
// 1ms sleep to mitigate busy looping
SysUtils.sleep(1);
}
typeName = incomingPacket.unpackString();
rid = incomingPacket.unpackREQID();
StreamManager.RegisterInstanceResponse.Builder builder = StreamManager.RegisterInstanceResponse.newBuilder();
incomingPacket.unpackMessage(builder);
StreamManager.RegisterInstanceResponse response = builder.build();
Assert.assertNotNull(response);
Assert.assertTrue(response.isInitialized());
Assert.assertEquals(Common.StatusCode.OK, response.getStatus().getStatus());
Assert.assertEquals(1, response.getPplan().getStmgrsCount());
Assert.assertEquals(2, response.getPplan().getInstancesCount());
Assert.assertEquals(1, response.getPplan().getTopology().getBoltsCount());
Assert.assertEquals(1, response.getPplan().getTopology().getSpoutsCount());
Assert.assertEquals(TopologyAPI.TopologyState.RUNNING, response.getPplan().getTopology().getState());
}
nioLooper.exitLoop();
} catch (ClosedChannelException ignored) {
} finally {
close(socketChannel);
}
}
use of com.twitter.heron.common.network.IncomingPacket in project heron by twitter.
the class HandleReadTest method testHandleRead.
/**
* Test reading from network
*/
@Test
public void testHandleRead() throws Exception {
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.socket().bind(new InetSocketAddress(HOST, serverPort));
SocketChannel socketChannel = null;
try {
runStreamManagerClient();
socketChannel = serverSocketChannel.accept();
configure(socketChannel);
socketChannel.configureBlocking(false);
close(serverSocketChannel);
// Receive request
IncomingPacket incomingPacket = new IncomingPacket();
while (incomingPacket.readFromChannel(socketChannel) != 0) {
// 1ms sleep to mitigate busy looping
SysUtils.sleep(1);
}
// Send back response
// Though we do not use typeName, we need to unpack it first,
// since the order is required
String typeName = incomingPacket.unpackString();
REQID rid = incomingPacket.unpackREQID();
OutgoingPacket outgoingPacket = new OutgoingPacket(rid, UnitTestHelper.getRegisterInstanceResponse());
outgoingPacket.writeToChannel(socketChannel);
for (int i = 0; i < Constants.RETRY_TIMES; i++) {
InstanceControlMsg instanceControlMsg = inControlQueue.poll();
if (instanceControlMsg != null) {
break;
} else {
SysUtils.sleep(Constants.RETRY_INTERVAL_MS);
}
}
outgoingPacket = new OutgoingPacket(REQID.zeroREQID, constructMockMessage());
outgoingPacket.writeToChannel(socketChannel);
for (int i = 0; i < Constants.RETRY_TIMES; i++) {
if (!inStreamQueue.isEmpty()) {
break;
}
SysUtils.sleep(Constants.RETRY_INTERVAL_MS);
}
nioLooper.exitLoop();
Assert.assertEquals(1, inStreamQueue.size());
HeronTuples.HeronTupleSet msg = inStreamQueue.poll();
HeronTuples.HeronTupleSet heronTupleSet = msg;
Assert.assertTrue(heronTupleSet.hasData());
Assert.assertFalse(heronTupleSet.hasControl());
HeronTuples.HeronDataTupleSet heronDataTupleSet = heronTupleSet.getData();
Assert.assertEquals("test-spout", heronDataTupleSet.getStream().getComponentName());
Assert.assertEquals("default", heronDataTupleSet.getStream().getId());
String res = "";
for (HeronTuples.HeronDataTuple heronDataTuple : heronDataTupleSet.getTuplesList()) {
res += heronDataTuple.getValues(0).toStringUtf8();
Assert.assertEquals(1, heronDataTuple.getRootsCount());
}
Assert.assertEquals("ABABABABAB", res);
} catch (ClosedChannelException ignored) {
} finally {
close(socketChannel);
}
}
Aggregations