use of org.jboss.netty.handler.timeout.ReadTimeoutException in project databus by linkedin.
the class TestHttpResponseProcessor method testReadTimeoutChunking.
@Test
public void testReadTimeoutChunking() throws DatabusException {
final Logger log = Logger.getLogger("GenericHttpResponseHandler.testReadTimeoutChunking");
log.info("start");
final GenericHttpResponseHandler responseHandler = new GenericHttpResponseHandler(KeepAliveType.KEEP_ALIVE);
responseHandler.getLog().setLevel(_logLevel);
TestHttpResponseProcessor respProcessor = new TestHttpResponseProcessor(log);
TestConnectListener connectListener = new TestConnectListener(log);
TestSendRequestListener requestListener = new TestSendRequestListener(log);
TestCloseListener closeListener = new TestCloseListener(log);
responseHandler.setConnectionListener(connectListener);
Channel channel = createClientBootstrap(responseHandler);
SocketAddress clientAddr = channel.getLocalAddress();
try {
setListeners(responseHandler, respProcessor, requestListener, closeListener);
channel.write(new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/test"));
//It seems that there is a race condition between the writeFuture succeeding
//and the writeComplete message getting to the handler. Make sure that the
//writeComplete has got to the handler before we do anything else with
//the channel.
final GenericHttpResponseHandler handler = getResponseHandler(channel);
TestUtil.assertWithBackoff(new ConditionCheck() {
@Override
public boolean check() {
return handler._messageState.hasSentRequest();
}
}, "request sent", 1000, log);
HttpResponse resp = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
resp.setHeader(HttpHeaders.Names.TRANSFER_ENCODING, HttpHeaders.Values.CHUNKED);
sendServerResponse(clientAddr, resp, 1000);
HttpChunk chunk1 = new DefaultHttpChunk(ChannelBuffers.wrappedBuffer("chunk1".getBytes(Charset.defaultCharset())));
sendServerResponse(clientAddr, chunk1, 1000);
final List<String> callbacks = respProcessor.getCallbacks();
TestUtil.assertWithBackoff(new ConditionCheck() {
@Override
public boolean check() {
return 2 == callbacks.size();
}
}, "waiting for response processed", 1000, null);
Assert.assertEquals(callbacks.get(0), "startResponse");
Assert.assertEquals(callbacks.get(1), "addChunk");
Channels.fireExceptionCaught(channel, new ReadTimeoutException());
channel.close();
final List<String> closeCallbacks = closeListener.getCallbacks();
TestUtil.assertWithBackoff(new ConditionCheck() {
@Override
public boolean check() {
return 3 == callbacks.size() && 1 == closeCallbacks.size();
}
}, "waiting for response processed", 1000, null);
final List<String> connectCallbacks = connectListener.getCallbacks();
final List<String> requestCallbacks = requestListener.getCallbacks();
Assert.assertEquals(callbacks.get(0), "startResponse");
Assert.assertEquals(callbacks.get(1), "addChunk");
Assert.assertTrue(callbacks.get(2).startsWith("channelException"));
//Assert.assertEquals(callbacks.get(3), "channelClosed"); // no more channelClosed after channel Exception
//make sure that no new callbacks have showed up
stateSanityCheck(connectCallbacks, requestCallbacks, callbacks, closeCallbacks);
Assert.assertEquals(callbacks.size(), 3);
} finally {
channel.close();
log.info("end");
}
}
use of org.jboss.netty.handler.timeout.ReadTimeoutException in project databus by linkedin.
the class TestHttpResponseProcessor method testReadTimeoutNoChunking.
@Test
public void testReadTimeoutNoChunking() throws InterruptedException, DatabusException {
final Logger log = Logger.getLogger("GenericHttpResponseHandler.testReadTimeoutNoChunking");
final GenericHttpResponseHandler responseHandler = new GenericHttpResponseHandler(KeepAliveType.KEEP_ALIVE);
responseHandler.getLog().setLevel(_logLevel);
log.info("start");
log.setLevel(_logLevel);
TestHttpResponseProcessor respProcessor = new TestHttpResponseProcessor(log);
TestConnectListener connectListener = new TestConnectListener(log);
TestSendRequestListener requestListener = new TestSendRequestListener(log);
TestCloseListener closeListener = new TestCloseListener(log);
responseHandler.setConnectionListener(connectListener);
Channel channel = createClientBootstrap(responseHandler);
try {
setListeners(responseHandler, respProcessor, requestListener, closeListener);
ChannelFuture writeFuture = channel.write(new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/test"));
Assert.assertTrue(writeFuture.await(1000));
//It seems that there is a race condition between the writeFuture succeeding
//and the writeComplete message getting to the handler. Make sure that the
//writeComplete has got to the handler before we do anything else with
//the channel.
final GenericHttpResponseHandler handler = getResponseHandler(channel);
TestUtil.assertWithBackoff(new ConditionCheck() {
@Override
public boolean check() {
return handler._messageState.hasSentRequest();
}
}, "request sent", 1000, log);
Channels.fireExceptionCaught(channel, new ReadTimeoutException());
channel.close();
final List<String> callbacks = respProcessor.getCallbacks();
final List<String> closeCallbacks = closeListener.getCallbacks();
TestUtil.assertWithBackoff(new ConditionCheck() {
@Override
public boolean check() {
log.debug("# callbacks:" + callbacks + ";expecting 1");
return 1 == callbacks.size() && 1 == closeCallbacks.size();
}
}, "waiting for response processed", 5000, null);
final List<String> connectCallbacks = connectListener.getCallbacks();
final List<String> requestCallbacks = requestListener.getCallbacks();
stateSanityCheck(connectCallbacks, requestCallbacks, callbacks, closeCallbacks);
Assert.assertTrue(callbacks.get(0).startsWith("channelException"));
//Assert.assertEquals(callbacks.get(1), "channelClosed"); // we don't get channelClosed after exception anymore
//make sure that no new callbacks have showed up
Assert.assertEquals(callbacks.size(), 1);
} finally {
channel.close();
log.info("end");
}
}
use of org.jboss.netty.handler.timeout.ReadTimeoutException 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();
}
use of org.jboss.netty.handler.timeout.ReadTimeoutException 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();
}
use of org.jboss.netty.handler.timeout.ReadTimeoutException in project druid by druid-io.
the class DirectDruidClientTest method testRun.
@Test
public void testRun() throws Exception {
HttpClient httpClient = EasyMock.createMock(HttpClient.class);
final URL url = new URL("http://foo/druid/v2/");
SettableFuture<InputStream> futureResult = SettableFuture.create();
Capture<Request> capturedRequest = EasyMock.newCapture();
EasyMock.expect(httpClient.go(EasyMock.capture(capturedRequest), EasyMock.<HttpResponseHandler>anyObject())).andReturn(futureResult).times(1);
SettableFuture futureException = SettableFuture.create();
EasyMock.expect(httpClient.go(EasyMock.capture(capturedRequest), EasyMock.<HttpResponseHandler>anyObject())).andReturn(futureException).times(1);
EasyMock.expect(httpClient.go(EasyMock.capture(capturedRequest), EasyMock.<HttpResponseHandler>anyObject())).andReturn(SettableFuture.create()).atLeastOnce();
EasyMock.replay(httpClient);
final ServerSelector serverSelector = new ServerSelector(new DataSegment("test", new Interval("2013-01-01/2013-01-02"), new DateTime("2013-01-01").toString(), Maps.<String, Object>newHashMap(), Lists.<String>newArrayList(), Lists.<String>newArrayList(), NoneShardSpec.instance(), 0, 0L), new HighestPriorityTierSelectorStrategy(new ConnectionCountServerSelectorStrategy()));
DirectDruidClient client1 = new DirectDruidClient(new ReflectionQueryToolChestWarehouse(), QueryRunnerTestHelper.NOOP_QUERYWATCHER, new DefaultObjectMapper(), httpClient, "foo", new NoopServiceEmitter());
DirectDruidClient client2 = new DirectDruidClient(new ReflectionQueryToolChestWarehouse(), QueryRunnerTestHelper.NOOP_QUERYWATCHER, new DefaultObjectMapper(), httpClient, "foo2", new NoopServiceEmitter());
QueryableDruidServer queryableDruidServer1 = new QueryableDruidServer(new DruidServer("test1", "localhost", 0, "historical", DruidServer.DEFAULT_TIER, 0), client1);
serverSelector.addServerAndUpdateSegment(queryableDruidServer1, serverSelector.getSegment());
QueryableDruidServer queryableDruidServer2 = new QueryableDruidServer(new DruidServer("test1", "localhost", 0, "historical", DruidServer.DEFAULT_TIER, 0), client2);
serverSelector.addServerAndUpdateSegment(queryableDruidServer2, serverSelector.getSegment());
TimeBoundaryQuery query = Druids.newTimeBoundaryQueryBuilder().dataSource("test").build();
HashMap<String, List> context = Maps.newHashMap();
Sequence s1 = client1.run(query, context);
Assert.assertTrue(capturedRequest.hasCaptured());
Assert.assertEquals(url, capturedRequest.getValue().getUrl());
Assert.assertEquals(HttpMethod.POST, capturedRequest.getValue().getMethod());
Assert.assertEquals(1, client1.getNumOpenConnections());
// simulate read timeout
Sequence s2 = client1.run(query, context);
Assert.assertEquals(2, client1.getNumOpenConnections());
futureException.setException(new ReadTimeoutException());
Assert.assertEquals(1, client1.getNumOpenConnections());
// subsequent connections should work
Sequence s3 = client1.run(query, context);
Sequence s4 = client1.run(query, context);
Sequence s5 = client1.run(query, context);
Assert.assertTrue(client1.getNumOpenConnections() == 4);
// produce result for first connection
futureResult.set(new ByteArrayInputStream("[{\"timestamp\":\"2014-01-01T01:02:03Z\", \"result\": 42.0}]".getBytes()));
List<Result> results = Sequences.toList(s1, Lists.<Result>newArrayList());
Assert.assertEquals(1, results.size());
Assert.assertEquals(new DateTime("2014-01-01T01:02:03Z"), results.get(0).getTimestamp());
Assert.assertEquals(3, client1.getNumOpenConnections());
client2.run(query, context);
client2.run(query, context);
Assert.assertTrue(client2.getNumOpenConnections() == 2);
Assert.assertTrue(serverSelector.pick() == queryableDruidServer2);
EasyMock.verify(httpClient);
}
Aggregations