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 TestDatabusV2RegistrationImpl 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 DummyRemoteExceptionHandler method setUpClass.
@BeforeClass
public void setUpClass() throws InvalidConfigException {
TestUtil.setupLoggingWithTimestampedFile(true, "/tmp/TestNettyHttpDatabusRelayConnection_", ".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);
DbusEventBuffer.Config bufCfgBuilder = new DbusEventBuffer.Config();
bufCfgBuilder.setAllocationPolicy(AllocationPolicy.HEAP_MEMORY.toString());
bufCfgBuilder.setMaxSize(100000);
bufCfgBuilder.setScnIndexSize(128);
bufCfgBuilder.setAverageEventSize(1);
_bufCfg = bufCfgBuilder.build();
initSchemaObjectsLists();
_fullV4ResponseMap = new HashMap<String, List<Object>>();
_fullV4ResponseMap.put(RegisterResponseEntry.SOURCE_SCHEMAS_KEY, _sourceObjectsList);
_fullV4ResponseMap.put(RegisterResponseEntry.KEY_SCHEMAS_KEY, _keyObjectsList);
_fullV4ResponseMap.put(RegisterResponseMetadataEntry.METADATA_SCHEMAS_KEY, _metadataObjectsList);
}
use of com.linkedin.databus2.test.container.SimpleTestServerConnection in project databus by linkedin.
the class SimpleClientPipelineFactory method testServerSimpleRequestResponse.
@Test
public /**
* Simulates the follow communication with no induced timeouts
*
* <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>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 there"<td></td></tr>
* <tr><td></td>send "eom"<td></td></tr>
* <tr><td>stop read timeout</td><td></td></tr>
* </table>
*/
void testServerSimpleRequestResponse() {
SimpleTestServerConnection srvConn = new SimpleTestServerConnection(_eventFactory.getByteOrder());
srvConn.setPipelineFactory(new SimpleServerPipelineFactory());
boolean serverStarted = srvConn.startSynchronously(1, CONNECT_TIMEOUT_MS);
Assert.assertTrue(serverStarted, "server started");
SimpleTestClientConnection clientConn = new SimpleTestClientConnection(_eventFactory.getByteOrder());
clientConn.setPipelineFactory(new SimpleClientPipelineFactory());
boolean clientConnected = clientConn.startSynchronously(1, 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());
//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()));
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");
Assert.assertNull(clientExceptionListener.getLastException(), "no client read timeout");
Assert.assertEquals(clientMsgReader.getMsg(), "hi there", "response read");
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();
}
use of com.linkedin.databus2.test.container.SimpleTestServerConnection 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