use of com.linkedin.databus2.test.container.SimpleTestServerConnection 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 com.linkedin.databus2.test.container.SimpleTestServerConnection 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 com.linkedin.databus2.test.container.SimpleTestServerConnection 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");
}
use of com.linkedin.databus2.test.container.SimpleTestServerConnection in project databus by linkedin.
the class TestDatabusV2ClusterRegistrationImpl method setUpClass.
@BeforeClass
public void setUpClass() throws InvalidConfigException {
// setup logging
TestUtil.setupLogging(true, null, Level.INFO);
InternalLoggerFactory.setDefaultFactory(new Log4JLoggerFactory());
// initialize relays
for (int relayN = 0; relayN < RELAY_PORT.length; ++relayN) {
_dummyServer[relayN] = new SimpleTestServerConnection(new DbusEventV2Factory().getByteOrder(), SimpleTestServerConnection.ServerType.NIO);
_dummyServer[relayN].setPipelineFactory(new ChannelPipelineFactory() {
@Override
public ChannelPipeline getPipeline() throws Exception {
return Channels.pipeline(new LoggingHandler(InternalLogLevel.DEBUG), new HttpServerCodec(), new LoggingHandler(InternalLogLevel.DEBUG), new SimpleObjectCaptureHandler());
}
});
_dummyServer[relayN].start(RELAY_PORT[relayN]);
}
// create standard client config
DatabusHttpClientImpl.Config clientCfgBuilder = new DatabusHttpClientImpl.Config();
clientCfgBuilder.getContainer().setHttpPort(CLIENT_PORT);
clientCfgBuilder.getContainer().getJmx().setRmiEnabled(false);
clientCfgBuilder.getContainer().setReadTimeoutMs(10000000);
clientCfgBuilder.getConnectionDefaults().getPullerRetries().setInitSleep(10);
clientCfgBuilder.getRuntime().getBootstrap().setEnabled(false);
clientCfgBuilder.getCheckpointPersistence().setClearBeforeUse(true);
for (int i = 0; i < RELAY_PORT.length; ++i) {
clientCfgBuilder.getRuntime().getRelay(Integer.toString(i)).setHost("localhost");
clientCfgBuilder.getRuntime().getRelay(Integer.toString(i)).setPort(RELAY_PORT[i]);
clientCfgBuilder.getRuntime().getRelay(Integer.toString(i)).setSources(SOURCE1_NAME);
}
_stdClientCfgBuilder = clientCfgBuilder;
_stdClientCfg = clientCfgBuilder.build();
// create standard relay buffer config
DbusEventBuffer.Config bufCfgBuilder = new DbusEventBuffer.Config();
bufCfgBuilder.setAllocationPolicy(AllocationPolicy.HEAP_MEMORY.toString());
bufCfgBuilder.setMaxSize(100000);
bufCfgBuilder.setScnIndexSize(128);
bufCfgBuilder.setAverageEventSize(1);
_bufCfg = bufCfgBuilder.build();
}
use of com.linkedin.databus2.test.container.SimpleTestServerConnection in project databus by linkedin.
the class TestNettyHttpDatabusBootstrapConnection method setUpClass.
@BeforeClass
public void setUpClass() throws InvalidConfigException {
TestUtil.setupLoggingWithTimestampedFile(true, "/tmp/TestNettyHttpDatabusBootstrapConnection_", ".log", Level.INFO);
InternalLoggerFactory.setDefaultFactory(new Log4JLoggerFactory());
_dummyServer = new SimpleTestServerConnection(new DbusEventV2Factory().getByteOrder(), SimpleTestServerConnection.ServerType.NIO);
_dummyServer.setPipelineFactory(new ChannelPipelineFactory() {
@Override
public ChannelPipeline getPipeline() throws Exception {
return Channels.pipeline(new LoggingHandler(InternalLogLevel.DEBUG), new HttpServerCodec(), new LoggingHandler(InternalLogLevel.DEBUG), new SimpleObjectCaptureHandler());
}
});
_dummyServer.start(SERVER_ADDRESS_ID);
DatabusHttpClientImpl.Config clientCfgBuilder = new DatabusHttpClientImpl.Config();
clientCfgBuilder.getContainer().setReadTimeoutMs(DEFAULT_READ_TIMEOUT_MS);
clientCfgBuilder.getContainer().setWriteTimeoutMs(DEFAULT_WRITE_TIMEOUT_MS);
CONN_FACTORY = new NettyHttpConnectionFactory(BOSS_POOL, IO_POOL, null, NETWORK_TIMER, clientCfgBuilder.getContainer().getWriteTimeoutMs(), clientCfgBuilder.getContainer().getReadTimeoutMs(), clientCfgBuilder.getContainer().getBstReadTimeoutMs(), // protocolVersion
4, MAX_EVENT_VERSION, TEST_CHANNELS_GROUP);
}
Aggregations