Search in sources :

Example 26 with ConditionCheck

use of com.linkedin.databus2.test.ConditionCheck 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)

Example 27 with ConditionCheck

use of com.linkedin.databus2.test.ConditionCheck in project databus by linkedin.

the class DummyRemoteExceptionHandler method runRegisterV4.

private void runRegisterV4(final String subtestName, final String protocolVersionHeader, final String responseStr, final TestResponseProcessors.TestConnectionStateMessage.State expectedRegisterState) throws IOException, ScnNotFoundException, OffsetNotFoundException //throws JsonGenerationException, JsonMappingException, IOException, ScnNotFoundException, OffsetNotFoundException
{
    final Logger log = Logger.getLogger("TestNettyHttpDatabusRelayConnection." + subtestName);
    //log.setLevel(Level.DEBUG);
    TestingConnectionCallback callback = TestingConnectionCallback.createAndStart(subtestName);
    DummyRemoteExceptionHandler remoteExceptionHandler = new DummyRemoteExceptionHandler();
    final NettyHttpDatabusRelayConnection conn = (NettyHttpDatabusRelayConnection) CONN_FACTORY.createRelayConnection(RELAY_SERVER_INFO, callback, remoteExceptionHandler);
    // verify the version - current DBUS_EVENT_V1
    Assert.assertEquals(MAX_EVENT_VERSION, conn.getMaxEventVersion());
    try {
        // connect to server and send /sources
        TestResponseProcessors.TestConnectionStateMessage msg = new TestResponseProcessors.TestConnectionStateMessage();
        conn.requestSources(msg);
        waitForServerConnection(conn, log);
        // introspect connection to server
        Channel channel = conn._channel;
        final SocketAddress clientAddr = channel.getLocalAddress();
        TestUtil.assertWithBackoff(new ConditionCheck() {

            @Override
            public boolean check() {
                return null != _dummyServer.getChildChannel(clientAddr);
            }
        }, "client connection established", 1000, log);
        Channel serverChannel = _dummyServer.getChildChannel(clientAddr);
        ChannelPipeline serverPipeline = serverChannel.getPipeline();
        SimpleObjectCaptureHandler objCapture = (SimpleObjectCaptureHandler) serverPipeline.get("3");
        // verify server gets the /sources request
        HttpResponse sourcesResp = runHappyPathSources(log, callback, remoteExceptionHandler, clientAddr, objCapture);
        // send /register and check result
        doRegisterV4(log, callback, remoteExceptionHandler, conn, msg, clientAddr, objCapture, sourcesResp, protocolVersionHeader, responseStr, expectedRegisterState);
        callback.clearLastMsg();
        objCapture.clear();
    } finally {
        conn.close();
        callback.shutdown();
    }
}
Also used : ConditionCheck(com.linkedin.databus2.test.ConditionCheck) Channel(org.jboss.netty.channel.Channel) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) Logger(org.apache.log4j.Logger) ChannelPipeline(org.jboss.netty.channel.ChannelPipeline) SimpleObjectCaptureHandler(com.linkedin.databus2.test.container.SimpleObjectCaptureHandler) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress)

Example 28 with ConditionCheck

use of com.linkedin.databus2.test.ConditionCheck in project databus by linkedin.

the class DummyRemoteExceptionHandler method runServerRegisterDisconnectIteration.

private void runServerRegisterDisconnectIteration(final Logger log, TestingConnectionCallback callback, DummyRemoteExceptionHandler remoteExceptionHandler, final NettyHttpDatabusRelayConnection conn) throws JsonGenerationException, JsonMappingException, IOException, ScnNotFoundException, OffsetNotFoundException {
    //connect to server and send /sources
    TestResponseProcessors.TestConnectionStateMessage msg = new TestResponseProcessors.TestConnectionStateMessage();
    conn.requestSources(msg);
    waitForServerConnection(conn, log);
    //introspect connection to server
    Channel channel = conn._channel;
    SocketAddress clientAddr = channel.getLocalAddress();
    final SocketAddress finalClientAddr = clientAddr;
    TestUtil.assertWithBackoff(new ConditionCheck() {

        @Override
        public boolean check() {
            return _dummyServer.getChildChannel(finalClientAddr) != null;
        }
    }, "client connected", 100, log);
    Channel serverChannel = _dummyServer.getChildChannel(clientAddr);
    ChannelPipeline serverPipeline = serverChannel.getPipeline();
    SimpleObjectCaptureHandler objCapture = (SimpleObjectCaptureHandler) serverPipeline.get("3");
    //verify server gets the /source request
    runHappyPathSources(log, callback, remoteExceptionHandler, clientAddr, objCapture);
    callback.clearLastMsg();
    objCapture.clear();
    conn.requestRegister("1", msg);
    //verify server gets the /register request
    HttpRequest msgReq = captureRequest(objCapture);
    Assert.assertTrue(msgReq.getUri().startsWith("/register"));
    serverChannel.close();
    waitForCallback(callback, TestResponseProcessors.TestConnectionStateMessage.State.REGISTER_RESPONSE_ERROR, log);
    Assert.assertNull(remoteExceptionHandler.getLastException());
    Assert.assertEquals(1, callback.getAllMsgs().size());
    callback.clearLastMsg();
    objCapture.clear();
}
Also used : ConditionCheck(com.linkedin.databus2.test.ConditionCheck) HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) SimpleObjectCaptureHandler(com.linkedin.databus2.test.container.SimpleObjectCaptureHandler) Channel(org.jboss.netty.channel.Channel) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) ChannelPipeline(org.jboss.netty.channel.ChannelPipeline)

Example 29 with ConditionCheck

use of com.linkedin.databus2.test.ConditionCheck in project databus by linkedin.

the class TestNettyHttpDatabusBootstrapConnection method sendStartScnHappyPath.

private void sendStartScnHappyPath(final NettyHttpDatabusBootstrapConnection conn, Checkpoint cp, DummyDatabusBootstrapConnectionStateMessage bstCallback, String sourceNames, long startScn, Logger log) throws IOException, JsonGenerationException, JsonMappingException {
    //send startSCN()
    conn.requestStartScn(cp, bstCallback, sourceNames);
    TestUtil.assertWithBackoff(new ConditionCheck() {

        @Override
        public boolean check() {
            return null != conn._channel && conn._channel.isConnected();
        }
    }, "wait for client to connect", 1000, log);
    //introspect connection to server
    Channel channel = conn._channel;
    SocketAddress clientAddr = channel.getLocalAddress();
    Channel serverChannel = _dummyServer.getChildChannel(clientAddr);
    ChannelPipeline serverPipeline = serverChannel.getPipeline();
    SimpleObjectCaptureHandler objCapture = (SimpleObjectCaptureHandler) serverPipeline.get("3");
    Assert.assertTrue(objCapture.waitForMessage(1000, 0));
    Object msgObj = objCapture.getMessages().get(0);
    Assert.assertTrue(msgObj instanceof HttpRequest);
    //verify we got a /startSCN call
    HttpRequest msgReq = (HttpRequest) msgObj;
    Assert.assertTrue(msgReq.getUri().startsWith("/startSCN"));
    //send back some response
    HttpResponse sourcesResp = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
    sourcesResp.setHeader(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
    sourcesResp.setHeader(HttpHeaders.Names.TRANSFER_ENCODING, HttpHeaders.Values.CHUNKED);
    ObjectMapper objMapper = new ObjectMapper();
    HttpChunk body = new DefaultHttpChunk(ChannelBuffers.wrappedBuffer(objMapper.writeValueAsBytes(String.valueOf(startScn))));
    NettyTestUtils.sendServerResponses(_dummyServer, clientAddr, sourcesResp, body);
}
Also used : ConditionCheck(com.linkedin.databus2.test.ConditionCheck) HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) DefaultHttpChunk(org.jboss.netty.handler.codec.http.DefaultHttpChunk) ChunkedBodyReadableByteChannel(com.linkedin.databus.client.ChunkedBodyReadableByteChannel) Channel(org.jboss.netty.channel.Channel) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) ChannelPipeline(org.jboss.netty.channel.ChannelPipeline) SimpleObjectCaptureHandler(com.linkedin.databus2.test.container.SimpleObjectCaptureHandler) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) DefaultHttpChunk(org.jboss.netty.handler.codec.http.DefaultHttpChunk) HttpChunk(org.jboss.netty.handler.codec.http.HttpChunk)

Example 30 with ConditionCheck

use of com.linkedin.databus2.test.ConditionCheck in project databus by linkedin.

the class TestGoldenGateEventProducer method testGGParserStats.

/**
   * test collection of parser stats, especially lag between parsed and added files
   * @throws Exception
   */
@Test
public void testGGParserStats() throws Exception {
    short[] sourceIds = new short[] { 505, 506 };
    String[] sourceNames = new String[] { "source1", "source2" };
    // setup trail Files directory
    File ggTrailDir = FileUtils.createTempDir("testGGParserStats");
    // configure physical source
    String uri = "gg://" + ggTrailDir.getAbsolutePath() + ":x3";
    PhysicalSourceStaticConfig pssc = buildSimplePssc(sourceIds, sourceNames, uri);
    LOG.info("Uri=" + uri);
    // create schema
    Schema s = Schema.parse(sourceAvroSchema);
    VersionedSchema vs = new VersionedSchema(new VersionedSchemaId("source1", (short) 3), s, null);
    // mock for schema registry
    SchemaRegistryService srs = EasyMock.createMock(SchemaRegistryService.class);
    EasyMock.expect(srs.fetchLatestVersionedSchemaBySourceName("source1")).andReturn(vs).anyTimes();
    EasyMock.expect(srs.fetchLatestVersionedSchemaBySourceName("source2")).andReturn(vs).anyTimes();
    EasyMock.expect(srs.fetchLatestVersionedSchemaBySourceName(null)).andReturn(vs);
    // mock for MaxSCNReadWriter
    MaxSCNReaderWriter mscn = EasyMock.createMock(MaxSCNReaderWriter.class);
    EasyMock.expect(mscn.getMaxScn()).andReturn((long) -2).atLeastOnce();
    mscn.saveMaxScn(EasyMock.anyLong());
    EasyMock.expectLastCall().anyTimes();
    EasyMock.replay(mscn);
    EasyMock.replay(srs);
    int totalTransWritten = 0;
    int totalFilesWritten = 0;
    // buffer
    DbusEventBufferAppendable mb = createBufMult(pssc);
    // start GG producer
    GoldenGateEventProducer gg = new GoldenGateEventProducer(pssc, srs, mb, null, mscn);
    // create first 2 files
    addToTrailFile(new File(ggTrailDir.getAbsolutePath() + "/x301"), 100, 4);
    addToTrailFile(new File(ggTrailDir.getAbsolutePath() + "/x302"), 200, 4);
    totalTransWritten = 8;
    totalFilesWritten = 2;
    // get hold of parser stats object
    final GGParserStatistics ggParserStats = gg.getParserStats();
    // all should be 0
    Assert.assertEquals(0, ggParserStats.getNumFilesParsed());
    Assert.assertEquals(0, ggParserStats.getNumFilesAdded());
    Assert.assertEquals(0, ggParserStats.getFilesLag());
    Assert.assertEquals(0, ggParserStats.getTimeLag());
    Assert.assertEquals(0, ggParserStats.getBytesLag());
    try {
        LOG.info("starting event producer");
        // -2 here does nothing. actual setting happens thru the mock of
        gg.start(-2);
        // MaxSCNReadWriter
        // let it parse first files
        TestUtil.assertWithBackoff(new ConditionCheck() {

            @Override
            public boolean check() {
                return ggParserStats.getNumFilesParsed() == 2 && (8 * _transactionPatternSize == ggParserStats.getNumBytesTotalParsed());
            }
        }, "First two files parsed", 2000, LOG);
        // stats in the interim
        Assert.assertEquals(2, ggParserStats.getNumFilesParsed());
        Assert.assertEquals(2, ggParserStats.getNumFilesAdded());
        Assert.assertEquals(0, ggParserStats.getFilesLag());
        Assert.assertEquals(0, ggParserStats.getTimeLag());
        Assert.assertEquals(0, ggParserStats.getBytesLag());
        Assert.assertEquals(totalTransWritten * _transactionPatternSize, ggParserStats.getNumBytesTotalParsed());
        gg.pause();
        // the file will get parsed but not processed
        addToTrailFile(new File(ggTrailDir.getAbsolutePath() + "/x303"), 300, 4);
        totalTransWritten += 4;
        totalFilesWritten++;
        // to get more then a ms lag time
        TestUtil.sleep(2000);
        addToTrailFile(new File(ggTrailDir.getAbsolutePath() + "/x304"), 400, 4);
        totalTransWritten += 4;
        totalFilesWritten++;
        // to guarantee we picked up stats update (stats are updated
        TestUtil.sleep(6000);
        // every 5 seconds)
        // now we should be 2 files behind. parser thread gets paused AFTER it start
        // processing the file
        // so the actuall value will be 1 file behind
        // 303(already started being parsed), only 304 is behind
        int lagFiles = 1;
        // 1 file, 4 transactions each
        long lagBytes = 1 * 4 * _transactionPatternSize;
        /*
       * Assert.assertEquals(totalFilesWritten-1, ggParserStats.getNumFilesParsed());
       * Assert.assertEquals(totalFilesWritten, ggParserStats.getNumFilesAdded());
       * Assert.assertEquals(lagFiles, ggParserStats.getFilesLag()); // because 303 got
       * parsed
       *
       * // we added 4 files and parsed 3 , so the diff should be 1 file size (4
       * trasactions in 1 file) Assert.assertEquals(lagBytes,
       * ggParserStats.getBytesLag()); Assert.assertTrue(ggParserStats.getTimeLag()>0);
       */
        gg.unpause();
        TestUtil.sleep(5000);
        // now we should catchup
        Assert.assertEquals(4, ggParserStats.getNumFilesParsed());
        Assert.assertEquals(4, ggParserStats.getNumFilesAdded());
        Assert.assertEquals(0, ggParserStats.getFilesLag());
        Assert.assertEquals(0, ggParserStats.getTimeLag());
        Assert.assertEquals(0, ggParserStats.getBytesLag());
        // append to a file
        LOG.info("pausing again");
        gg.pause();
        addToTrailFile(new File(ggTrailDir.getAbsolutePath() + "/x304"), 410, 4);
        totalTransWritten += 4;
        TestUtil.sleep(1000);
        addToTrailFile(new File(ggTrailDir.getAbsolutePath() + "/x304"), 420, 4);
        totalTransWritten += 4;
        TestUtil.sleep(2000);
        gg.unpause();
        TestUtil.sleep(5500);
        // should be still up
        Assert.assertEquals(4, ggParserStats.getNumFilesParsed());
        Assert.assertEquals(4, ggParserStats.getNumFilesAdded());
        Assert.assertEquals(0, ggParserStats.getFilesLag());
        Assert.assertEquals(0, ggParserStats.getTimeLag());
        Assert.assertEquals(0, ggParserStats.getBytesLag());
        // assert the stats
        int totalFilesSize = totalTransWritten * _transactionPatternSize;
        Assert.assertEquals((totalFilesSize / totalFilesWritten), ggParserStats.getAvgFileSize());
        Assert.assertEquals(true, ggParserStats.getAvgParseTransactionTimeNs() > 0);
        Assert.assertEquals("part1", ggParserStats.getPhysicalSourceName());
        Assert.assertEquals(totalFilesSize / totalTransWritten, ggParserStats.getAvgTransactionSize());
        Assert.assertEquals(423, ggParserStats.getMaxScn());
        // 2
        Assert.assertEquals(totalTransWritten * 2, ggParserStats.getNumTotalEvents());
        // events
        // per
        // transaction
        Assert.assertEquals(totalTransWritten, ggParserStats.getNumTransactionsTotal());
        Assert.assertEquals(totalTransWritten, ggParserStats.getNumTransactionsWithEvents());
        Assert.assertEquals(0, ggParserStats.getNumTransactionsWithoutEvents());
        Assert.assertEquals(true, ggParserStats.getTimeSinceLastAccessMs() > 0);
        Assert.assertEquals(totalTransWritten * _transactionPatternSize, ggParserStats.getNumBytesTotalParsed());
        Assert.assertEquals("NumSCNRegressions", 0, ggParserStats.getNumSCNRegressions());
        Assert.assertEquals("LastSCNRegressed", -1, ggParserStats.getLastRegressedScn());
    } finally {
        gg.shutdown();
    }
    return;
}
Also used : ConditionCheck(com.linkedin.databus2.test.ConditionCheck) PhysicalSourceStaticConfig(com.linkedin.databus2.relay.config.PhysicalSourceStaticConfig) MaxSCNReaderWriter(com.linkedin.databus2.core.seq.MaxSCNReaderWriter) GGParserStatistics(com.linkedin.databus.monitoring.mbean.GGParserStatistics) VersionedSchemaId(com.linkedin.databus2.schemas.VersionedSchemaId) Schema(org.apache.avro.Schema) VersionedSchema(com.linkedin.databus2.schemas.VersionedSchema) SchemaRegistryService(com.linkedin.databus2.schemas.SchemaRegistryService) DbusEventBufferAppendable(com.linkedin.databus.core.DbusEventBufferAppendable) VersionedSchema(com.linkedin.databus2.schemas.VersionedSchema) File(java.io.File) Test(org.testng.annotations.Test)

Aggregations

ConditionCheck (com.linkedin.databus2.test.ConditionCheck)47 Test (org.testng.annotations.Test)40 Logger (org.apache.log4j.Logger)36 Channel (org.jboss.netty.channel.Channel)22 InetSocketAddress (java.net.InetSocketAddress)21 Checkpoint (com.linkedin.databus.core.Checkpoint)20 DefaultHttpResponse (org.jboss.netty.handler.codec.http.DefaultHttpResponse)20 HttpResponse (org.jboss.netty.handler.codec.http.HttpResponse)20 SocketAddress (java.net.SocketAddress)19 RegisterResponseEntry (com.linkedin.databus2.core.container.request.RegisterResponseEntry)15 DefaultHttpChunk (org.jboss.netty.handler.codec.http.DefaultHttpChunk)15 HttpChunk (org.jboss.netty.handler.codec.http.HttpChunk)15 DbusEventBuffer (com.linkedin.databus.core.DbusEventBuffer)12 ArrayList (java.util.ArrayList)11 HashMap (java.util.HashMap)11 ChannelPipeline (org.jboss.netty.channel.ChannelPipeline)11 DefaultHttpRequest (org.jboss.netty.handler.codec.http.DefaultHttpRequest)11 SimpleObjectCaptureHandler (com.linkedin.databus2.test.container.SimpleObjectCaptureHandler)10 List (java.util.List)10 IdNamePair (com.linkedin.databus.core.util.IdNamePair)9