Search in sources :

Example 1 with WriteTimeoutException

use of org.jboss.netty.handler.timeout.WriteTimeoutException in project databus by linkedin.

the class MockServerChannelHandler method writeComplete.

// =>=>=> UPstream from server to client
// <=<=<= DNxtream from client to server
@Override
public void writeComplete(ChannelHandlerContext ctx, WriteCompletionEvent e) throws Exception {
    Thread t = null;
    if (LOG.isDebugEnabled())
        LOG.debug("=>=>=>=WRite complete start" + e);
    if (_disableWriteComplete) {
        LOG.info("Injecting exceptions into writeComplete");
        _future.setFailure(new WriteTimeoutException("Mocked WriteTimeout"));
        _future.setFailure(new ClosedChannelException());
        // 16s sleep
        TestUtil.sleep(16000);
    } else if (_delayWriteComplete) {
        LOG.info("Injecting delay into writeComplete");
        t = startExceptionThread(ctx);
        LOG.info("waiting for timeout");
        TestUtil.sleep(10);
    }
    super.writeComplete(ctx, e);
    LOG.debug("=>=>=> Write complete end");
    if (t != null)
        // interrupt to close the channel
        t.interrupt();
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) WriteTimeoutException(org.jboss.netty.handler.timeout.WriteTimeoutException)

Example 2 with WriteTimeoutException

use of org.jboss.netty.handler.timeout.WriteTimeoutException 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)

Aggregations

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