Search in sources :

Example 1 with ExceptionListenerTestHandler

use of com.linkedin.databus2.test.container.ExceptionListenerTestHandler in project databus by linkedin.

the class SimpleClientPipelineFactory method testServerSimpleResponseTimeout.

@Test
public /**
 * Simulates the follow communication with no request timeout
 *
 * <table>
 * <th><td>client</td><td>server</td></th>
 * <tr><td>send "hello"</td><td></td></tr>
 * <tr><td></td><td>start read timeout</td></tr>
 * <tr><td>generate read timeout</td><td></td></tr>
 * <tr><td></td><td>induce timeout</td></tr>
 * <tr><td>disconnect from server</td><td></td></tr>
 * <tr><td></td><td>send "hi there"</td></tr>
 * <tr><td></td><td>detect it has been disconnected</td></tr>
 * </table>
 */
void testServerSimpleResponseTimeout() {
    SimpleTestServerConnection srvConn = new SimpleTestServerConnection(_eventFactory.getByteOrder());
    srvConn.setPipelineFactory(new SimpleServerPipelineFactory());
    boolean serverStarted = srvConn.startSynchronously(4, CONNECT_TIMEOUT_MS);
    Assert.assertTrue(serverStarted, "server started");
    SimpleTestClientConnection clientConn = new SimpleTestClientConnection(_eventFactory.getByteOrder());
    clientConn.setPipelineFactory(new SimpleClientPipelineFactory());
    boolean clientConnected = clientConn.startSynchronously(4, CONNECT_TIMEOUT_MS);
    Assert.assertTrue(clientConnected, "client connected");
    // hook in to key places in the server pipeline
    ChannelPipeline lastSrvConnPipeline = srvConn.getLastConnChannel().getPipeline();
    ExtendedReadTimeoutHandler srvTimeoutHandler = (ExtendedReadTimeoutHandler) lastSrvConnPipeline.get(ExtendedReadTimeoutHandler.class.getSimpleName());
    SimpleTestMessageReader srvMsgReader = (SimpleTestMessageReader) lastSrvConnPipeline.get(SimpleTestMessageReader.class.getSimpleName());
    ExceptionListenerTestHandler srvExceptionListener = (ExceptionListenerTestHandler) lastSrvConnPipeline.get(ExceptionListenerTestHandler.class.getSimpleName());
    // hook in to key places in the client pipeline
    ChannelPipeline clientPipeline = clientConn.getChannel().getPipeline();
    ExtendedReadTimeoutHandler clientTimeoutHandler = (ExtendedReadTimeoutHandler) clientPipeline.get(ExtendedReadTimeoutHandler.class.getSimpleName());
    ExceptionListenerTestHandler clientExceptionListener = (ExceptionListenerTestHandler) clientPipeline.get(ExceptionListenerTestHandler.class.getSimpleName());
    // send a request
    ChannelBuffer msg = ChannelBuffers.wrappedBuffer("hello".getBytes(Charset.defaultCharset()));
    clientConn.getChannel().write(msg);
    // wait for the request to propagate
    try {
        Thread.sleep(10);
    } catch (InterruptedException ie) {
    }
    ;
    Assert.assertNull(srvExceptionListener.getLastException(), "no server read timeout");
    Assert.assertNull(clientExceptionListener.getLastException(), "no client read timeout");
    Assert.assertEquals(srvMsgReader.getMsg(), "hello", "message read");
    // start server read timeout
    srvTimeoutHandler.start(lastSrvConnPipeline.getContext(srvTimeoutHandler));
    ChannelBuffer msg2 = ChannelBuffers.wrappedBuffer("eom".getBytes(Charset.defaultCharset()));
    clientConn.getChannel().write(msg2);
    // start the client timeout handler
    clientTimeoutHandler.start(clientPipeline.getContext(clientTimeoutHandler));
    Assert.assertNull(srvExceptionListener.getLastException(), "no server read timeout");
    Assert.assertNull(clientExceptionListener.getLastException(), "no client read timeout");
    Assert.assertEquals(srvMsgReader.getMsg(), "eom", "message read");
    // stop server read timeout
    srvTimeoutHandler.stop();
    ChannelBuffer resp = ChannelBuffers.wrappedBuffer("hi there".getBytes(Charset.defaultCharset()));
    // Induce timeout
    try {
        Thread.sleep(500);
    } catch (InterruptedException ie) {
    }
    ;
    lastSrvConnPipeline.getChannel().write(resp);
    // wait for the response to propagate
    try {
        Thread.sleep(10);
    } catch (InterruptedException ie) {
    }
    ;
    Assert.assertTrue(srvExceptionListener.getLastException() instanceof ClosedChannelException, "no server read timeout but client has disconnected");
    Assert.assertTrue(clientExceptionListener.getLastException() instanceof ReadTimeoutException, "client read timeout");
    Assert.assertTrue(!lastSrvConnPipeline.getChannel().isConnected(), "client has disconnected");
    Assert.assertTrue(!clientPipeline.getChannel().isConnected(), "closed connection to server");
    // stop client read timeout
    clientTimeoutHandler.stop();
    clientConn.stop();
    srvConn.stop();
}
Also used : SimpleTestClientConnection(com.linkedin.databus2.test.container.SimpleTestClientConnection) ClosedChannelException(java.nio.channels.ClosedChannelException) SimpleTestMessageReader(com.linkedin.databus2.test.container.SimpleTestMessageReader) ReadTimeoutException(org.jboss.netty.handler.timeout.ReadTimeoutException) SimpleTestServerConnection(com.linkedin.databus2.test.container.SimpleTestServerConnection) ExceptionListenerTestHandler(com.linkedin.databus2.test.container.ExceptionListenerTestHandler) ChannelPipeline(org.jboss.netty.channel.ChannelPipeline) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) Test(org.testng.annotations.Test)

Example 2 with ExceptionListenerTestHandler

use of com.linkedin.databus2.test.container.ExceptionListenerTestHandler in project databus by linkedin.

the class SimpleClientPipelineFactory method getPipeline.

@Override
public ChannelPipeline getPipeline() throws Exception {
    ChannelPipeline newPipeline = Channels.pipeline();
    newPipeline.addLast(ExtendedReadTimeoutHandler.class.getSimpleName(), new ExtendedReadTimeoutHandler("test", null, 100, true));
    newPipeline.addLast(ExceptionListenerTestHandler.class.getSimpleName(), new ExceptionListenerTestHandler());
    newPipeline.addLast(SimpleTestMessageReader.class.getSimpleName(), new SimpleTestMessageReader());
    return newPipeline;
}
Also used : SimpleTestMessageReader(com.linkedin.databus2.test.container.SimpleTestMessageReader) ExceptionListenerTestHandler(com.linkedin.databus2.test.container.ExceptionListenerTestHandler) ChannelPipeline(org.jboss.netty.channel.ChannelPipeline)

Example 3 with ExceptionListenerTestHandler

use of com.linkedin.databus2.test.container.ExceptionListenerTestHandler in project databus by linkedin.

the class SimpleClientPipelineFactory method testServerSimpleRequestTimeout.

@Test
public /**
 * Simulates the follow communication with no request timeout
 *
 * <table>
 * <th><td>client</td><td>server</td></th>
 * <tr><td>send "hello"</td><td></td></tr>
 * <tr><td></td><td>start read timeout</td></tr>
 * <tr><td>induce timeout</td><td></td></tr>
 * <tr><td>generate read timeout</td><td></td></tr>
 * <tr><td></td><td>disconnect client</td></tr>
 * <tr><td>send "eom"</td><td></td></tr>
 * <tr><td>detect it has been disconnected</td><td></td></tr>
 * </table>
 */
void testServerSimpleRequestTimeout() {
    SimpleTestServerConnection srvConn = new SimpleTestServerConnection(_eventFactory.getByteOrder());
    srvConn.setPipelineFactory(new SimpleServerPipelineFactory());
    boolean serverStarted = srvConn.startSynchronously(3, CONNECT_TIMEOUT_MS);
    Assert.assertTrue(serverStarted, "server started");
    SimpleTestClientConnection clientConn = new SimpleTestClientConnection(_eventFactory.getByteOrder());
    clientConn.setPipelineFactory(new SimpleClientPipelineFactory());
    boolean clientConnected = clientConn.startSynchronously(3, CONNECT_TIMEOUT_MS);
    Assert.assertTrue(clientConnected, "client connected");
    // hook in to key places in the server pipeline
    ChannelPipeline lastSrvConnPipeline = srvConn.getLastConnChannel().getPipeline();
    ExtendedReadTimeoutHandler srvTimeoutHandler = (ExtendedReadTimeoutHandler) lastSrvConnPipeline.get(ExtendedReadTimeoutHandler.class.getSimpleName());
    SimpleTestMessageReader srvMsgReader = (SimpleTestMessageReader) lastSrvConnPipeline.get(SimpleTestMessageReader.class.getSimpleName());
    ExceptionListenerTestHandler srvExceptionListener = (ExceptionListenerTestHandler) lastSrvConnPipeline.get(ExceptionListenerTestHandler.class.getSimpleName());
    // hook in to key places in the client pipeline
    ChannelPipeline clientPipeline = clientConn.getChannel().getPipeline();
    ExtendedReadTimeoutHandler clientTimeoutHandler = (ExtendedReadTimeoutHandler) clientPipeline.get(ExtendedReadTimeoutHandler.class.getSimpleName());
    ExceptionListenerTestHandler clientExceptionListener = (ExceptionListenerTestHandler) clientPipeline.get(ExceptionListenerTestHandler.class.getSimpleName());
    // send a request
    ChannelBuffer msg = ChannelBuffers.wrappedBuffer("hello".getBytes(Charset.defaultCharset()));
    ChannelFuture writeFuture = clientConn.getChannel().write(msg);
    // wait for the request to propagate
    try {
        writeFuture.await(10);
    } catch (InterruptedException ie) {
    }
    ;
    Assert.assertTrue(writeFuture.isDone(), "write completed");
    Assert.assertTrue(writeFuture.isSuccess(), "write successful");
    Assert.assertNull(srvExceptionListener.getLastException(), "no server read timeout");
    Assert.assertNull(clientExceptionListener.getLastException(), "no client read timeout");
    Assert.assertEquals(srvMsgReader.getMsg(), "hello", "message read");
    // start server read timeout
    srvTimeoutHandler.start(lastSrvConnPipeline.getContext(srvTimeoutHandler));
    // Timeout
    try {
        Thread.sleep(300);
    } catch (InterruptedException ie) {
    }
    ;
    ChannelBuffer msg2 = ChannelBuffers.wrappedBuffer("eom".getBytes(Charset.defaultCharset()));
    writeFuture = clientConn.getChannel().write(msg2);
    // wait for the respomse to propagate
    try {
        writeFuture.await(10);
    } catch (InterruptedException ie) {
    }
    ;
    // start the client timeout handler
    clientTimeoutHandler.start(clientPipeline.getContext(clientTimeoutHandler));
    Assert.assertTrue(srvExceptionListener.getLastException() instanceof ReadTimeoutException, "server read timeout");
    Assert.assertTrue(clientExceptionListener.getLastException() instanceof ClosedChannelException, "failed write");
    Assert.assertTrue(!lastSrvConnPipeline.getChannel().isConnected(), "client has been disconnected");
    Assert.assertTrue(!clientPipeline.getChannel().isConnected(), "disconnected from server");
    // stop server read timeout
    srvTimeoutHandler.stop();
    // stop client read timeout
    clientTimeoutHandler.stop();
    clientConn.stop();
    srvConn.stop();
}
Also used : SimpleTestClientConnection(com.linkedin.databus2.test.container.SimpleTestClientConnection) ChannelFuture(org.jboss.netty.channel.ChannelFuture) ClosedChannelException(java.nio.channels.ClosedChannelException) SimpleTestMessageReader(com.linkedin.databus2.test.container.SimpleTestMessageReader) ReadTimeoutException(org.jboss.netty.handler.timeout.ReadTimeoutException) SimpleTestServerConnection(com.linkedin.databus2.test.container.SimpleTestServerConnection) ExceptionListenerTestHandler(com.linkedin.databus2.test.container.ExceptionListenerTestHandler) ChannelPipeline(org.jboss.netty.channel.ChannelPipeline) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) Test(org.testng.annotations.Test)

Example 4 with ExceptionListenerTestHandler

use of com.linkedin.databus2.test.container.ExceptionListenerTestHandler in project databus by linkedin.

the class SimpleClientPipelineFactoryWithSleep method testClientSimpleRequestResponse.

@Test
public void testClientSimpleRequestResponse() {
    DbusEventFactory eventFactory = new DbusEventV1Factory();
    SimpleTestServerConnection srvConn = new SimpleTestServerConnection(eventFactory.getByteOrder());
    srvConn.setPipelineFactory(new SimpleServerPipelineFactory());
    boolean serverStarted = srvConn.startSynchronously(101, CONNECT_TIMEOUT_MS);
    Assert.assertTrue(serverStarted, "server started");
    final SimpleTestClientConnection clientConn = new SimpleTestClientConnection(eventFactory.getByteOrder());
    clientConn.setPipelineFactory(new SimpleClientPipelineFactoryWithSleep(200));
    boolean clientConnected = clientConn.startSynchronously(101, CONNECT_TIMEOUT_MS);
    Assert.assertTrue(clientConnected, "client connected");
    // hook in to key places in the server pipeline
    ChannelPipeline lastSrvConnPipeline = srvConn.getLastConnChannel().getPipeline();
    SimpleTestMessageReader srvMsgReader = (SimpleTestMessageReader) lastSrvConnPipeline.get(SimpleTestMessageReader.class.getSimpleName());
    ExceptionListenerTestHandler srvExceptionListener = (ExceptionListenerTestHandler) lastSrvConnPipeline.get(ExceptionListenerTestHandler.class.getSimpleName());
    // hook in to key places in the client pipeline
    ChannelPipeline clientPipeline = clientConn.getChannel().getPipeline();
    final ExceptionListenerTestHandler clientExceptionListener = (ExceptionListenerTestHandler) clientPipeline.get(ExceptionListenerTestHandler.class.getSimpleName());
    // System.err.println("Current thread: " + Thread.currentThread());
    // send a request in a separate thread because the client will intentionally block to simulate
    // a timeout
    final ChannelBuffer msg = ChannelBuffers.wrappedBuffer("hello".getBytes(Charset.defaultCharset()));
    Thread sendThread1 = new Thread(new Runnable() {

        @Override
        public void run() {
            // System.err.println(Thread.currentThread().toString() + ": sending message");
            clientConn.getChannel().write(msg);
        }
    }, "send msg thread");
    sendThread1.start();
    // System.err.println(Thread.currentThread().toString() + ": waiting for 10");
    try {
        Thread.sleep(50);
    } catch (InterruptedException ie) {
    }
    ;
    // System.err.println(Thread.currentThread().toString() + ": done Waiting for 10");
    // make sure the server has not received the message
    Assert.assertNull(srvExceptionListener.getLastException(), "no server errors");
    Assert.assertNull(clientExceptionListener.getLastException(), "no errors yet");
    Assert.assertTrue(!"hello".equals(srvMsgReader.getMsg()), "message not read yet");
    // wait for the write timeout
    try {
        Thread.sleep(300);
    } catch (InterruptedException ie) {
    }
    ;
    // System.err.println("Done Waiting for 300");
    // the client should have timed out and closed the connection
    TestUtil.assertWithBackoff(new ConditionCheck() {

        @Override
        public boolean check() {
            return null != clientExceptionListener.getLastException();
        }
    }, "client error", 1000, null);
    Assert.assertTrue(null != clientExceptionListener.getLastException(), "client error");
    Assert.assertTrue(clientExceptionListener.getLastException() instanceof ClosedChannelException || clientExceptionListener.getLastException() instanceof WriteTimeoutException, "client error test");
    Assert.assertTrue(!lastSrvConnPipeline.getChannel().isConnected(), "client has disconnected");
    Assert.assertTrue(!clientPipeline.getChannel().isConnected(), "closed connection to server");
}
Also used : SimpleTestClientConnection(com.linkedin.databus2.test.container.SimpleTestClientConnection) ConditionCheck(com.linkedin.databus2.test.ConditionCheck) ClosedChannelException(java.nio.channels.ClosedChannelException) SimpleTestMessageReader(com.linkedin.databus2.test.container.SimpleTestMessageReader) SimpleTestServerConnection(com.linkedin.databus2.test.container.SimpleTestServerConnection) ExceptionListenerTestHandler(com.linkedin.databus2.test.container.ExceptionListenerTestHandler) ChannelPipeline(org.jboss.netty.channel.ChannelPipeline) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) WriteTimeoutException(org.jboss.netty.handler.timeout.WriteTimeoutException) DbusEventV1Factory(com.linkedin.databus.core.DbusEventV1Factory) DbusEventFactory(com.linkedin.databus.core.DbusEventFactory) Test(org.testng.annotations.Test)

Example 5 with ExceptionListenerTestHandler

use of com.linkedin.databus2.test.container.ExceptionListenerTestHandler in project databus by linkedin.

the class SimpleClientPipelineFactory method testServerComplexRequestResponse.

@Test
public /**
 * Simulates the follow communication with no induced timeouts
 *
 * <table>
 * <th><td>client</td><td>server</td></th>
 * <tr><td>send "hello1"</td><td></td></tr>
 * <tr><td></td><td>start read timeout</td></tr>
 * <tr><td>send "hello2"</td><td></td></tr>
 * <tr><td>:</td><td></td></tr>
 * <tr><td>:</td><td></td></tr>
 * <tr><td>send "hello50"</td><td></td></tr>
 * <tr><td>send "eom"</td><td></td></tr>
 * <tr><td>start read timeout</td><td></td></tr>
 * <tr><td></td><td>stop read timeout</td></tr>
 * <tr><td></td>send "hi there1"<td></td></tr>
 * <tr><td></td>send ":"<td></td></tr>
 * <tr><td></td>send "hi there100"<td></td></tr>
 * <tr><td></td>send "eom"<td></td></tr>
 * <tr><td>stop read timeout</td><td></td></tr>
 * </table>
 */
void testServerComplexRequestResponse() {
    SimpleTestServerConnection srvConn = new SimpleTestServerConnection(_eventFactory.getByteOrder());
    srvConn.setPipelineFactory(new SimpleServerPipelineFactory());
    boolean serverStarted = srvConn.startSynchronously(2, CONNECT_TIMEOUT_MS);
    Assert.assertTrue(serverStarted, "server started");
    SimpleTestClientConnection clientConn = new SimpleTestClientConnection(_eventFactory.getByteOrder());
    clientConn.setPipelineFactory(new SimpleClientPipelineFactory());
    boolean clientConnected = clientConn.startSynchronously(2, CONNECT_TIMEOUT_MS);
    Assert.assertTrue(clientConnected, "client connected");
    // hook in to key places in the server pipeline
    ChannelPipeline lastSrvConnPipeline = srvConn.getLastConnChannel().getPipeline();
    ExtendedReadTimeoutHandler srvTimeoutHandler = (ExtendedReadTimeoutHandler) lastSrvConnPipeline.get(ExtendedReadTimeoutHandler.class.getSimpleName());
    SimpleTestMessageReader srvMsgReader = (SimpleTestMessageReader) lastSrvConnPipeline.get(SimpleTestMessageReader.class.getSimpleName());
    ExceptionListenerTestHandler srvExceptionListener = (ExceptionListenerTestHandler) lastSrvConnPipeline.get(ExceptionListenerTestHandler.class.getSimpleName());
    // hook in to key places in the client pipeline
    ChannelPipeline clientPipeline = clientConn.getChannel().getPipeline();
    ExtendedReadTimeoutHandler clientTimeoutHandler = (ExtendedReadTimeoutHandler) clientPipeline.get(ExtendedReadTimeoutHandler.class.getSimpleName());
    SimpleTestMessageReader clientMsgReader = (SimpleTestMessageReader) clientPipeline.get(SimpleTestMessageReader.class.getSimpleName());
    ExceptionListenerTestHandler clientExceptionListener = (ExceptionListenerTestHandler) clientPipeline.get(ExceptionListenerTestHandler.class.getSimpleName());
    for (int i = 0; i < 50; ++i) {
        // send a request
        ChannelBuffer msg = ChannelBuffers.wrappedBuffer(("hello" + i).getBytes(Charset.defaultCharset()));
        clientConn.getChannel().write(msg);
        // wait for the request to propagate
        try {
            Thread.sleep(10);
        } catch (InterruptedException ie) {
        }
        ;
        Assert.assertNull(srvExceptionListener.getLastException(), "no server read timeout: " + i);
        Assert.assertNull(clientExceptionListener.getLastException(), "no client read timeout: " + i);
        Assert.assertEquals(srvMsgReader.getMsg(), "hello" + i, "message read: " + i);
        // start server read timeout after the first message
        if (0 == i)
            srvTimeoutHandler.start(lastSrvConnPipeline.getContext(srvTimeoutHandler));
    }
    ChannelBuffer msg2 = ChannelBuffers.wrappedBuffer("eom".getBytes(Charset.defaultCharset()));
    clientConn.getChannel().write(msg2);
    // start the client timeout handler
    clientTimeoutHandler.start(clientPipeline.getContext(clientTimeoutHandler));
    Assert.assertNull(srvExceptionListener.getLastException(), "no server read timeout");
    Assert.assertNull(clientExceptionListener.getLastException(), "no client read timeout");
    Assert.assertEquals(srvMsgReader.getMsg(), "eom", "message read");
    // stop server read timeout
    srvTimeoutHandler.stop();
    for (int i = 0; i < 100; ++i) {
        String responseString = "hi there " + i;
        ChannelBuffer resp = ChannelBuffers.wrappedBuffer(responseString.getBytes(Charset.defaultCharset()));
        lastSrvConnPipeline.getChannel().write(resp);
        // wait for the response to propagate
        try {
            Thread.sleep(10);
        } catch (InterruptedException ie) {
        }
        ;
        Assert.assertNull(srvExceptionListener.getLastException(), "no server read timeout: " + i);
        Assert.assertNull(clientExceptionListener.getLastException(), "no client read timeout: " + i);
        Assert.assertEquals(clientMsgReader.getMsg(), responseString, "response read: " + i);
    }
    ChannelBuffer resp2 = ChannelBuffers.wrappedBuffer("eom".getBytes(Charset.defaultCharset()));
    lastSrvConnPipeline.getChannel().write(resp2);
    // wait for the response to propagate
    try {
        Thread.sleep(10);
    } catch (InterruptedException ie) {
    }
    ;
    Assert.assertNull(srvExceptionListener.getLastException(), "no server read timeout");
    Assert.assertNull(clientExceptionListener.getLastException(), "no client read timeout");
    Assert.assertEquals(clientMsgReader.getMsg(), "eom", "response read");
    // stop client read timeout
    clientTimeoutHandler.stop();
    clientConn.stop();
    srvConn.stop();
}
Also used : SimpleTestClientConnection(com.linkedin.databus2.test.container.SimpleTestClientConnection) SimpleTestMessageReader(com.linkedin.databus2.test.container.SimpleTestMessageReader) SimpleTestServerConnection(com.linkedin.databus2.test.container.SimpleTestServerConnection) ExceptionListenerTestHandler(com.linkedin.databus2.test.container.ExceptionListenerTestHandler) ChannelPipeline(org.jboss.netty.channel.ChannelPipeline) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) Test(org.testng.annotations.Test)

Aggregations

ExceptionListenerTestHandler (com.linkedin.databus2.test.container.ExceptionListenerTestHandler)6 SimpleTestMessageReader (com.linkedin.databus2.test.container.SimpleTestMessageReader)6 ChannelPipeline (org.jboss.netty.channel.ChannelPipeline)6 SimpleTestClientConnection (com.linkedin.databus2.test.container.SimpleTestClientConnection)5 SimpleTestServerConnection (com.linkedin.databus2.test.container.SimpleTestServerConnection)5 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)5 Test (org.testng.annotations.Test)5 ClosedChannelException (java.nio.channels.ClosedChannelException)3 ReadTimeoutException (org.jboss.netty.handler.timeout.ReadTimeoutException)2 DbusEventFactory (com.linkedin.databus.core.DbusEventFactory)1 DbusEventV1Factory (com.linkedin.databus.core.DbusEventV1Factory)1 ConditionCheck (com.linkedin.databus2.test.ConditionCheck)1 ChannelFuture (org.jboss.netty.channel.ChannelFuture)1 WriteTimeoutException (org.jboss.netty.handler.timeout.WriteTimeoutException)1