Search in sources :

Example 21 with TaskMessage

use of backtype.storm.messaging.TaskMessage in project jstorm by alibaba.

the class NettyUnitTest method test_large_msg.

@Test
public void test_large_msg() {
    System.out.println("!!!!!!!!!!start large message test!!!!!!!!");
    String req_msg = setupLargMsg();
    System.out.println("!!!!Finish batch data, size:" + req_msg.length() + "!!!!");
    IConnection server = null;
    IConnection client = null;
    server = initNettyServer();
    client = context.connect(null, "localhost", port);
    List<TaskMessage> list = new ArrayList<TaskMessage>();
    TaskMessage message = new TaskMessage(task, req_msg.getBytes());
    list.add(message);
    LOG.info("Client send data");
    client.send(message);
    byte[] recv = (byte[]) server.recv(task, 0);
    Assert.assertEquals(req_msg, new String(recv));
    client.close();
    server.close();
    System.out.println("!!!!!!!!!!End larget message test!!!!!!!!");
}
Also used : IConnection(backtype.storm.messaging.IConnection) TaskMessage(backtype.storm.messaging.TaskMessage) Test(org.junit.Test)

Example 22 with TaskMessage

use of backtype.storm.messaging.TaskMessage in project jstorm by alibaba.

the class NettyUnitTest method test_multiple_server.

@Test
public void test_multiple_server() {
    System.out.println("!!!!!!!!Start test_multiple_server !!!!!!!!!!!");
    final String req_msg = setupLargMsg();
    final int clientNum = 3;
    final AtomicLong received = new AtomicLong(clientNum);
    for (int i = 0; i < clientNum; i++) {
        final int realPort = port + i;
        new Thread(new Runnable() {

            @Override
            public void run() {
                IConnection server = null;
                JStormUtils.sleepMs(1000);
                System.out.println("!!server begin start!!!!!");
                server = initNettyServer(realPort);
                byte[] recv = (byte[]) server.recv(task, 0);
                Assert.assertEquals(req_msg, new String(recv));
                received.decrementAndGet();
                System.out.println("!!server received !!!!!" + realPort);
                server.close();
            }
        }).start();
    }
    List<TaskMessage> list = new ArrayList<TaskMessage>();
    TaskMessage message = new TaskMessage(task, req_msg.getBytes());
    list.add(message);
    List<IConnection> clients = new ArrayList<IConnection>();
    for (int i = 0; i < clientNum; i++) {
        final int realPort = port + i;
        IConnection client = context.connect(null, "localhost", realPort);
        clients.add(client);
        client.send(message);
        System.out.println("!!Client has sent data to " + realPort);
    }
    while (received.get() != 0) {
        JStormUtils.sleepMs(1000);
    }
    for (int i = 0; i < clientNum; i++) {
        clients.get(i).close();
    }
    System.out.println("!!!!!!!!!!!!End test_multiple_server!!!!!!!!!!!!!");
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) IConnection(backtype.storm.messaging.IConnection) TaskMessage(backtype.storm.messaging.TaskMessage) Test(org.junit.Test)

Example 23 with TaskMessage

use of backtype.storm.messaging.TaskMessage in project jstorm by alibaba.

the class NettyUnitTest method test_first_client.

@Test
public void test_first_client() {
    System.out.println("!!!!!!!!Start test_first_client !!!!!!!!!!!");
    final String req_msg = setupLargMsg();
    final IContext context = TransportFactory.makeContext(storm_conf);
    new Thread(new Runnable() {

        @Override
        public void run() {
            lock.lock();
            IConnection client = context.connect(null, "localhost", port);
            List<TaskMessage> list = new ArrayList<TaskMessage>();
            TaskMessage message = new TaskMessage(task, req_msg.getBytes());
            list.add(message);
            client.send(message);
            System.out.println("!!Client has sent data");
            JStormUtils.sleepMs(1000);
            try {
                clientClose.await();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            client.close();
            contextClose.signal();
            lock.unlock();
        }
    }).start();
    IConnection server = null;
    JStormUtils.sleepMs(1000);
    System.out.println("!!server begin start!!!!!");
    server = initNettyServer();
    JStormUtils.sleepMs(5000);
    System.out.println("Begin to receive message");
    byte[] recv = (byte[]) server.recv(task, 1);
    Assert.assertEquals(req_msg, new String(recv));
    System.out.println("Finished to receive message");
    lock.lock();
    try {
        clientClose.signal();
        server.close();
        try {
            contextClose.await();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        context.term();
    } finally {
        lock.unlock();
    }
    System.out.println("!!!!!!!!!!!!End test_first_client!!!!!!!!!!!!!");
}
Also used : IContext(backtype.storm.messaging.IContext) IConnection(backtype.storm.messaging.IConnection) TaskMessage(backtype.storm.messaging.TaskMessage) Test(org.junit.Test)

Example 24 with TaskMessage

use of backtype.storm.messaging.TaskMessage in project jstorm by alibaba.

the class NettyUnitTest method test_pending_read_server.

@Test
public void test_pending_read_server() {
    System.out.println("!!!!!!!! Start test_pending_read_server !!!!!!!!!!!");
    final String req_msg = "Aloha is the most Hawaiian word.";
    final IConnection server;
    final IConnection client;
    server = initNettyServer();
    client = context.connect(null, "localhost", port);
    JStormUtils.sleepMs(1000);
    List<TaskMessage> list = new ArrayList<TaskMessage>();
    TaskMessage message = new TaskMessage(task, req_msg.getBytes());
    list.add(message);
    NettyServer nServer = (NettyServer) server;
    StormChannelGroup channleGroup = nServer.getChannelGroup();
    String remoteAddress = channleGroup.getAllRemoteAddress().iterator().next();
    System.out.println("!!!!!!!!!!!!!!!!!! All remoteAddress: " + channleGroup.getAllRemoteAddress() + " !!!!!!!!!!!!!!!!!!");
    channleGroup.suspendChannel(remoteAddress);
    System.out.println("!!!!!!!!!!!!!!!!!! Suspend netty channel=" + remoteAddress + " !!!!!!!!!!!!!!!!!!");
    client.send(message);
    final List<String> recvMsg = new ArrayList<String>();
    Thread thread = new Thread(new Runnable() {

        @Override
        public void run() {
            System.out.println("!!!!!!!!!!!!!!!!!! Start to receive msg !!!!!!!!!!!!!!!!!");
            byte[] recv = (byte[]) server.recv(task, 0);
            Assert.assertEquals(req_msg, new String(recv));
            recvMsg.add(new String(recv));
            System.out.println("!!!!!!!!!!!!!!!!!! Finish to receive msg !!!!!!!!!!!!!!!!!!");
        }
    });
    thread.start();
    JStormUtils.sleepMs(1000);
    Assert.assertEquals(true, recvMsg.size() == 0);
    System.out.println("!!!!!!!!!!!!!!!!!! Resume channel=" + remoteAddress + " !!!!!!!!!!!!!!!!!!");
    channleGroup.resumeChannel(remoteAddress);
    JStormUtils.sleepMs(1000);
    try {
        thread.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    System.out.println("!!!!!!!!!!!!!!!!!! Recv Msg=" + recvMsg + " !!!!!!!!!!!!!!!!!!");
    Assert.assertEquals(true, recvMsg.size() == 1);
    Assert.assertEquals(req_msg, recvMsg.get(0));
    server.close();
    client.close();
    System.out.println("!!!!!!!!!!!! End test_pending_read_server !!!!!!!!!!!!!");
}
Also used : IConnection(backtype.storm.messaging.IConnection) TaskMessage(backtype.storm.messaging.TaskMessage) Test(org.junit.Test)

Example 25 with TaskMessage

use of backtype.storm.messaging.TaskMessage in project jstorm by alibaba.

the class NettyUnitTest method test_multiple_client.

/**
     * Due to there is only one client to one server in one jvm It can't do this test
     * 
     * @throws InterruptedException
     */
public void test_multiple_client() {
    System.out.println("!!!!!!!!Start test_multiple_client !!!!!!!!!!!");
    final String req_msg = setupLargMsg();
    final int clientNum = 3;
    final AtomicLong received = new AtomicLong(clientNum);
    for (int i = 0; i < clientNum; i++) {
        new Thread(new Runnable() {

            @Override
            public void run() {
                IConnection client = context.connect(null, "localhost", port);
                List<TaskMessage> list = new ArrayList<TaskMessage>();
                TaskMessage message = new TaskMessage(task, req_msg.getBytes());
                list.add(message);
                client.send(message);
                System.out.println("!!Client has sent data");
                while (received.get() != 0) {
                    JStormUtils.sleepMs(1000);
                }
                client.close();
            }
        }).start();
    }
    IConnection server = null;
    JStormUtils.sleepMs(1000);
    System.out.println("!!server begin start!!!!!");
    server = initNettyServer();
    for (int i = 0; i < clientNum; i++) {
        byte[] recv = (byte[]) server.recv(task, 0);
        Assert.assertEquals(req_msg, new String(recv));
        received.decrementAndGet();
    }
    server.close();
    System.out.println("!!!!!!!!!!!!End test_multiple_client!!!!!!!!!!!!!");
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) IConnection(backtype.storm.messaging.IConnection) TaskMessage(backtype.storm.messaging.TaskMessage)

Aggregations

TaskMessage (backtype.storm.messaging.TaskMessage)30 IConnection (backtype.storm.messaging.IConnection)15 Test (org.junit.Test)12 IContext (backtype.storm.messaging.IContext)7 ControlMessage (backtype.storm.messaging.ControlMessage)4 ByteBuffer (java.nio.ByteBuffer)3 ITupleExt (backtype.storm.tuple.ITupleExt)2 KryoException (com.esotericsoftware.kryo.KryoException)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)2 ChannelBufferOutputStream (org.jboss.netty.buffer.ChannelBufferOutputStream)2 DisruptorQueue (backtype.storm.utils.DisruptorQueue)1 AsmHistogram (com.alibaba.jstorm.common.metric.AsmHistogram)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 Condition (java.util.concurrent.locks.Condition)1 Channel (org.jboss.netty.channel.Channel)1