Search in sources :

Example 1 with IncomingPacket

use of org.apache.heron.common.network.IncomingPacket in project heron by twitter.

the class HandleWriteTest method testHandleWrite.

/**
 * Test write into network
 */
@Test
public void testHandleWrite() throws IOException {
    ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
    serverSocketChannel.socket().bind(new InetSocketAddress(HOST, getServerPort()));
    SocketChannel socketChannel = null;
    try {
        StreamManagerClient streamManagerClient = runStreamManagerClient();
        socketChannel = acceptSocketChannel(serverSocketChannel);
        // Receive request
        REQID rid = readIncomingPacket(socketChannel).unpackREQID();
        OutgoingPacket outgoingPacket = new OutgoingPacket(rid, UnitTestHelper.getRegisterInstanceResponse());
        outgoingPacket.writeToChannel(socketChannel);
        HeronServerTester.await(getInControlQueueOfferLatch());
        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 incomingPacket = readIncomingPacket(socketChannel);
            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());
        }
        getNIOLooper().exitLoop();
    } catch (ClosedChannelException ignored) {
    } finally {
        close(socketChannel);
    }
}
Also used : ServerSocketChannel(java.nio.channels.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) ClosedChannelException(java.nio.channels.ClosedChannelException) IncomingPacket(org.apache.heron.common.network.IncomingPacket) InetSocketAddress(java.net.InetSocketAddress) StreamManager(org.apache.heron.proto.stmgr.StreamManager) REQID(org.apache.heron.common.network.REQID) ServerSocketChannel(java.nio.channels.ServerSocketChannel) OutgoingPacket(org.apache.heron.common.network.OutgoingPacket) Test(org.junit.Test)

Example 2 with IncomingPacket

use of org.apache.heron.common.network.IncomingPacket in project heron by twitter.

the class AbstractNetworkTest method readIncomingPacket.

IncomingPacket readIncomingPacket(SocketChannel socketChannel) throws IOException {
    // Receive request
    IncomingPacket incomingPacket = new IncomingPacket();
    Selector readSelector = Selector.open();
    socketChannel.register(readSelector, SelectionKey.OP_READ);
    readSelector.select(HeronServerTester.RESPONSE_RECEIVED_TIMEOUT.toMillis());
    // and the data read
    if (incomingPacket.readFromChannel(socketChannel) != 0) {
        readSelector.select(HeronServerTester.RESPONSE_RECEIVED_TIMEOUT.toMillis());
        assertEquals(0, incomingPacket.readFromChannel(socketChannel));
    }
    // Though we do not use typeName, we need to unpack it first, since the order is required.
    // doing this as a convenience since none of the callers of this method need this.
    incomingPacket.unpackString();
    return incomingPacket;
}
Also used : IncomingPacket(org.apache.heron.common.network.IncomingPacket) Selector(java.nio.channels.Selector)

Aggregations

IncomingPacket (org.apache.heron.common.network.IncomingPacket)2 InetSocketAddress (java.net.InetSocketAddress)1 ClosedChannelException (java.nio.channels.ClosedChannelException)1 Selector (java.nio.channels.Selector)1 ServerSocketChannel (java.nio.channels.ServerSocketChannel)1 SocketChannel (java.nio.channels.SocketChannel)1 OutgoingPacket (org.apache.heron.common.network.OutgoingPacket)1 REQID (org.apache.heron.common.network.REQID)1 StreamManager (org.apache.heron.proto.stmgr.StreamManager)1 Test (org.junit.Test)1