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();
}
Aggregations