Search in sources :

Example 16 with TestAppenderToValidateLogsForCachingTests

use of io.siddhi.core.query.table.util.TestAppenderToValidateLogsForCachingTests in project siddhi by wso2.

the class CacheMissTestCase method cacheMissTestCase0.

@Test(description = "cacheMissTestCase0")
public void cacheMissTestCase0() throws InterruptedException, SQLException {
    final TestAppenderToValidateLogsForCachingTests appender = new TestAppenderToValidateLogsForCachingTests("TestAppenderToValidateLogsForCachingTests", null);
    final Logger logger = (Logger) LogManager.getRootLogger();
    logger.setLevel(Level.DEBUG);
    logger.addAppender(appender);
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream StockStream (symbol string, price float, volume long); " + "define stream DeleteStockStream (symbol string, price float, volume long); " + "@Store(type=\"testStoreForCacheMiss\", @Cache(size=\"1\", cache.policy=\"FIFO\"))\n" + "define table StockTable (symbol string, price float, volume long); ";
    String query = "" + "@info(name = 'query1') " + "from StockStream " + "insert into StockTable ;" + "" + "@info(name = 'query2') " + "from DeleteStockStream " + "delete StockTable " + "   on StockTable.symbol == symbol AND StockTable.price == price AND StockTable.volume == volume;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    InputHandler stockStream = siddhiAppRuntime.getInputHandler("StockStream");
    InputHandler deleteStockStream = siddhiAppRuntime.getInputHandler("DeleteStockStream");
    siddhiAppRuntime.start();
    deleteStockStream.send(new Object[] { "WSO2", 55.6f, 100L });
    deleteStockStream.send(new Object[] { "IBM", 75.6f, 100L });
    Thread.sleep(1000);
    Event[] events = siddhiAppRuntime.query("" + "from StockTable ");
    EventPrinter.print(events);
    try {
        AssertJUnit.assertEquals(2, events.length);
    } catch (NullPointerException ignore) {
    }
    final List<String> loggedEvents = ((TestAppenderToValidateLogsForCachingTests) logger.getAppenders().get("TestAppenderToValidateLogsForCachingTests")).getLog();
    List<String> logMessages = new ArrayList<>();
    for (String logEvent : loggedEvents) {
        String message = String.valueOf(logEvent);
        if (message.contains(":")) {
            message = message.split(":")[1].trim();
        }
        logMessages.add(message);
    }
    Assert.assertEquals(logMessages.contains("store table size is smaller than max cache. Sending results from cache"), false);
    Assert.assertEquals(logMessages.contains("store table size is bigger than cache."), true);
    Assert.assertEquals(Collections.frequency(logMessages, "store table size is bigger than cache."), 1);
    Assert.assertEquals(logMessages.contains("cache constraints satisfied. Checking cache"), false);
    Assert.assertEquals(logMessages.contains("cache hit. Sending results from cache"), false);
    Assert.assertEquals(logMessages.contains("cache miss. Loading from store"), false);
    Assert.assertEquals(logMessages.contains("store also miss. sending null"), false);
    Assert.assertEquals(logMessages.contains("sending results from cache after loading from store"), false);
    Assert.assertEquals(logMessages.contains("sending results from store"), true);
    Assert.assertEquals(Collections.frequency(logMessages, "sending results from store"), 1);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) TestAppenderToValidateLogsForCachingTests(io.siddhi.core.query.table.util.TestAppenderToValidateLogsForCachingTests) ArrayList(java.util.ArrayList) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) Logger(org.apache.logging.log4j.core.Logger) SiddhiManager(io.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Example 17 with TestAppenderToValidateLogsForCachingTests

use of io.siddhi.core.query.table.util.TestAppenderToValidateLogsForCachingTests in project siddhi by wso2.

the class CachePreLoadingTestCase method cachePreLoadingTestCase0.

@Test
public void cachePreLoadingTestCase0() throws InterruptedException, SQLException {
    final TestAppenderToValidateLogsForCachingTests appender = new TestAppenderToValidateLogsForCachingTests("TestAppenderToValidateLogsForCachingTests", null);
    final Logger logger = (Logger) LogManager.getRootLogger();
    logger.setLevel(Level.DEBUG);
    logger.addAppender(appender);
    // Configure siddhi to insert events data to table only from specific fields of the stream.
    log.info("insertIntoTableWithCacheTest1");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream StockStream (symbol string, price float, volume long); " + "@Store(type=\"testStoreForCachePreLoading\", @Cache(size=\"10\"))\n" + "define table StockTable (symbol string, price float, volume long); ";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams);
    siddhiAppRuntime.start();
    Event[] events = siddhiAppRuntime.query("" + "from StockTable ");
    EventPrinter.print(events);
    AssertJUnit.assertEquals(2, events.length);
    AssertJUnit.assertEquals(2, events.length);
    final List<String> loggedEvents = ((TestAppenderToValidateLogsForCachingTests) logger.getAppenders().get("TestAppenderToValidateLogsForCachingTests")).getLog();
    List<String> logMessages = new ArrayList<>();
    for (String logEvent : loggedEvents) {
        String message = String.valueOf(logEvent);
        if (message.contains(":")) {
            message = message.split(":")[1].trim();
        }
        logMessages.add(message);
    }
    Assert.assertEquals(logMessages.contains("store table size is smaller than max cache. Sending results from cache"), true);
    Assert.assertEquals(Collections.frequency(logMessages, "store table size is smaller than max cache. Sending results from cache"), 1);
    Assert.assertEquals(logMessages.contains("store table size is bigger than cache."), false);
    Assert.assertEquals(logMessages.contains("cache constraints satisfied. Checking cache"), false);
    Assert.assertEquals(logMessages.contains("cache hit. Sending results from cache"), false);
    Assert.assertEquals(logMessages.contains("cache miss. Loading from store"), false);
    Assert.assertEquals(logMessages.contains("store also miss. sending null"), false);
    Assert.assertEquals(logMessages.contains("sending results from cache after loading from store"), false);
    Assert.assertEquals(logMessages.contains("sending results from store"), false);
    logger.removeAppender(appender);
    siddhiAppRuntime.shutdown();
}
Also used : TestAppenderToValidateLogsForCachingTests(io.siddhi.core.query.table.util.TestAppenderToValidateLogsForCachingTests) ArrayList(java.util.ArrayList) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) Logger(org.apache.logging.log4j.core.Logger) SiddhiManager(io.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Example 18 with TestAppenderToValidateLogsForCachingTests

use of io.siddhi.core.query.table.util.TestAppenderToValidateLogsForCachingTests in project siddhi by wso2.

the class CacheCornerCasesTest method testTableJoinQuery2.

@Test(dependsOnMethods = "testTableJoinQuery1")
public void testTableJoinQuery2() throws InterruptedException {
    log.info("testTableJoinQuery2 - OUT 2");
    LoggerContext context = LoggerContext.getContext(false);
    Configuration config = context.getConfiguration();
    TestAppenderToValidateLogsForCachingTests appender = config.getAppender("TestAppenderToValidateLogsForCachingTests");
    final Logger logger = (Logger) LogManager.getRootLogger();
    logger.setLevel(Level.DEBUG);
    logger.addAppender(appender);
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream StockStream (symbol string, price float, volume long); " + "define stream CheckStockStream (symbol string); " + "@Store(type=\"testStoreCacheEnabledOptimisation\", @Cache(size=\"2\"))\n" + "@PrimaryKey('symbol') " + "define table StockTable (symbol string, price float, volume long); ";
    String query = "" + "@info(name = 'query2') " + "from CheckStockStream join StockTable as myTable " + "on CheckStockStream.symbol == myTable.symbol " + "select CheckStockStream.symbol as checkSymbol, myTable.symbol as symbol, " + "myTable.volume as volume  " + "insert into OutputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    siddhiAppRuntime.addCallback("query2", new QueryCallback() {

        @Override
        public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            if (inEvents != null) {
                for (Event event : inEvents) {
                    inEventCount++;
                    switch(inEventCount) {
                        case 1:
                        case 3:
                            Assert.assertEquals(event.getData(), new Object[] { "WSO3", "WSO3", 3L });
                            break;
                        case 2:
                            Assert.assertEquals(event.getData(), new Object[] { "WSO2", "WSO2", 2L });
                            break;
                        default:
                            Assert.assertSame(inEventCount, 2);
                    }
                }
                eventArrived = true;
            }
            if (removeEvents != null) {
                removeEventCount = removeEventCount + removeEvents.length;
            }
            eventArrived = true;
        }
    });
    InputHandler checkStockStream = siddhiAppRuntime.getInputHandler("CheckStockStream");
    siddhiAppRuntime.start();
    // Three records are hard coded for lookup wso1, wso2 and wso3
    checkStockStream.send(new Object[] { "WSO3" });
    Thread.sleep(1000);
    checkStockStream.send(new Object[] { "WSO2" });
    Thread.sleep(1000);
    checkStockStream.send(new Object[] { "WSO3" });
    Thread.sleep(1000);
    Assert.assertTrue(eventArrived, "Event arrived");
    Assert.assertEquals(inEventCount, 3, "Number of success events");
    Assert.assertEquals(removeEventCount, 0, "Number of remove events");
    final List<String> loggedEvents = ((TestAppenderToValidateLogsForCachingTests) logger.getAppenders().get("TestAppenderToValidateLogsForCachingTests")).getLog();
    List<String> logMessages = new ArrayList<>();
    for (String logEvent : loggedEvents) {
        String message = String.valueOf(logEvent);
        if (message.contains(":")) {
            message = message.split(":")[1].trim();
        }
        logMessages.add(message);
    }
    Assert.assertTrue(logMessages.contains("store table size is bigger than cache."));
    Assert.assertTrue(logMessages.contains("sending results from cache after loading from store"));
    Assert.assertEquals(Collections.frequency(logMessages, "cache hit. Sending results from cache"), 2);
    logger.removeAppender(appender);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) Configuration(org.apache.logging.log4j.core.config.Configuration) TestAppenderToValidateLogsForCachingTests(io.siddhi.core.query.table.util.TestAppenderToValidateLogsForCachingTests) ArrayList(java.util.ArrayList) Logger(org.apache.logging.log4j.core.Logger) LoggerContext(org.apache.logging.log4j.core.LoggerContext) QueryCallback(io.siddhi.core.query.output.callback.QueryCallback) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) SiddhiManager(io.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Example 19 with TestAppenderToValidateLogsForCachingTests

use of io.siddhi.core.query.table.util.TestAppenderToValidateLogsForCachingTests in project siddhi by wso2.

the class CacheExpiryAndReloadTestCase method expiryAndReloadTest2.

// with primary key => IndexOperator
@Test
public void expiryAndReloadTest2() throws InterruptedException, SQLException {
    log.info("expiryAndReloadTest2");
    TestAppenderToValidateLogsForCachingTests appender = new TestAppenderToValidateLogsForCachingTests("TestAppenderToValidateLogsForCachingTests", null);
    final Logger logger = (Logger) LogManager.getRootLogger();
    logger.setLevel(Level.DEBUG);
    logger.addAppender(appender);
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream StockStream (symbol string, price float, volume long); " + "define stream DeleteStream (symbol string); " + "@Store(type=\"testStoreContainingInMemoryTable\", @Cache(size=\"10\", retention.period=\"1 sec\", " + "purge.interval=\"1 sec\"))\n" + "@PrimaryKey('symbol') " + "define table StockTable (symbol string, price float, volume long); ";
    String query1 = "" + "@info(name = 'query1') " + "from StockStream\n" + "select symbol, price, volume\n" + "insert into StockTable ;" + "@info(name = 'query2') " + "from DeleteStream " + "delete StockTable " + "on StockTable.symbol == symbol";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query1);
    InputHandler stockStream = siddhiAppRuntime.getInputHandler("StockStream");
    InputHandler deleteStream = siddhiAppRuntime.getInputHandler("DeleteStream");
    siddhiAppRuntime.start();
    Event[] events;
    stockStream.send(new Object[] { "WSO2", 55.6f, 1L });
    Thread.sleep(100);
    events = siddhiAppRuntime.query("" + "from StockTable ");
    EventPrinter.print(events);
    AssertJUnit.assertEquals(1, events.length);
    Thread.sleep(2000);
    events = siddhiAppRuntime.query("" + "from StockTable ");
    EventPrinter.print(events);
    AssertJUnit.assertEquals(1, events.length);
    stockStream.send(new Object[] { "WSO4", 55.6f, 2L });
    stockStream.send(new Object[] { "WSO1", 55.6f, 3L });
    Thread.sleep(100);
    events = siddhiAppRuntime.query("" + "from StockTable ");
    EventPrinter.print(events);
    AssertJUnit.assertEquals(3, events.length);
    Thread.sleep(2000);
    events = siddhiAppRuntime.query("" + "from StockTable ");
    EventPrinter.print(events);
    AssertJUnit.assertEquals(3, events.length);
    stockStream.send(new Object[] { "IBM", 75.6f, 4L });
    stockStream.send(new Object[] { "WS2", 55.6f, 5L });
    Thread.sleep(2000);
    stockStream.send(new Object[] { "WSOr", 55.6f, 6L });
    Thread.sleep(100);
    events = siddhiAppRuntime.query("" + "from StockTable ");
    EventPrinter.print(events);
    AssertJUnit.assertEquals(6, events.length);
    final List<String> loggedEvents = ((TestAppenderToValidateLogsForCachingTests) logger.getAppenders().get("TestAppenderToValidateLogsForCachingTests")).getLog();
    List<String> logMessages = new ArrayList<>();
    for (String logEvent : loggedEvents) {
        String message = String.valueOf(logEvent);
        if (message.contains(":")) {
            message = message.split(":")[1].trim();
        }
        logMessages.add(message);
    }
    Assert.assertEquals(logMessages.contains("store table size is smaller than max cache. Sending results from cache"), true);
    Assert.assertEquals(Collections.frequency(logMessages, "store table size is smaller than max cache. Sending results from cache"), 5);
    Assert.assertEquals(logMessages.contains("store table size is bigger than cache."), false);
    Assert.assertEquals(logMessages.contains("cache constraints satisfied. Checking cache"), false);
    Assert.assertEquals(logMessages.contains("cache hit. Sending results from cache"), false);
    Assert.assertEquals(logMessages.contains("cache miss. Loading from store"), false);
    Assert.assertEquals(logMessages.contains("store also miss. sending null"), false);
    Assert.assertEquals(logMessages.contains("sending results from cache after loading from store"), false);
    Assert.assertEquals(logMessages.contains("sending results from store"), false);
    logger.removeAppender(appender);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) TestAppenderToValidateLogsForCachingTests(io.siddhi.core.query.table.util.TestAppenderToValidateLogsForCachingTests) ArrayList(java.util.ArrayList) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) Logger(org.apache.logging.log4j.core.Logger) SiddhiManager(io.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Example 20 with TestAppenderToValidateLogsForCachingTests

use of io.siddhi.core.query.table.util.TestAppenderToValidateLogsForCachingTests in project siddhi by wso2.

the class CacheExpiryAndReloadTestCase method expiryAndReloadTest1.

@Test
public void expiryAndReloadTest1() throws InterruptedException, SQLException {
    log.info("expiryAndReloadTest1");
    TestAppenderToValidateLogsForCachingTests appender = new TestAppenderToValidateLogsForCachingTests("TestAppenderToValidateLogsForCachingTests", null);
    final Logger logger = (Logger) LogManager.getRootLogger();
    logger.setLevel(Level.DEBUG);
    logger.addAppender(appender);
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream StockStream (symbol string, price float, volume long); " + "define stream DeleteStream (symbol string); " + "@Store(type=\"testStoreContainingInMemoryTable\", @Cache(size=\"10\", retention.period=\"1 sec\", " + "purge.interval=\"1 sec\"))\n" + "define table StockTable (symbol string, price float, volume long); ";
    String query1 = "" + "@info(name = 'query1') " + "from StockStream\n" + "select symbol, price, volume\n" + "insert into StockTable ;" + "@info(name = 'query2') " + "from DeleteStream " + "delete StockTable " + "on StockTable.symbol == symbol";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query1);
    InputHandler stockStream = siddhiAppRuntime.getInputHandler("StockStream");
    InputHandler deleteStream = siddhiAppRuntime.getInputHandler("DeleteStream");
    siddhiAppRuntime.start();
    Event[] events;
    stockStream.send(new Object[] { "WSO2", 55.6f, 1L });
    Thread.sleep(100);
    events = siddhiAppRuntime.query("" + "from StockTable ");
    EventPrinter.print(events);
    AssertJUnit.assertEquals(1, events.length);
    Thread.sleep(2000);
    events = siddhiAppRuntime.query("" + "from StockTable ");
    EventPrinter.print(events);
    AssertJUnit.assertEquals(1, events.length);
    stockStream.send(new Object[] { "WSO4", 55.6f, 2L });
    stockStream.send(new Object[] { "WSO1", 55.6f, 3L });
    Thread.sleep(100);
    events = siddhiAppRuntime.query("" + "from StockTable ");
    EventPrinter.print(events);
    AssertJUnit.assertEquals(3, events.length);
    Thread.sleep(2000);
    events = siddhiAppRuntime.query("" + "from StockTable ");
    EventPrinter.print(events);
    AssertJUnit.assertEquals(3, events.length);
    stockStream.send(new Object[] { "IBM", 75.6f, 4L });
    stockStream.send(new Object[] { "WS2", 55.6f, 5L });
    Thread.sleep(2000);
    stockStream.send(new Object[] { "WSOr", 55.6f, 6L });
    Thread.sleep(100);
    events = siddhiAppRuntime.query("" + "from StockTable ");
    EventPrinter.print(events);
    AssertJUnit.assertEquals(6, events.length);
    final List<String> loggedEvents = ((TestAppenderToValidateLogsForCachingTests) logger.getAppenders().get("TestAppenderToValidateLogsForCachingTests")).getLog();
    List<String> logMessages = new ArrayList<>();
    for (String logEvent : loggedEvents) {
        String message = String.valueOf(logEvent);
        if (message.contains(":")) {
            message = message.split(":")[1].trim();
        }
        logMessages.add(message);
    }
    Assert.assertEquals(logMessages.contains("store table size is smaller than max cache. Sending results from cache"), true);
    Assert.assertEquals(Collections.frequency(logMessages, "store table size is smaller than max cache. Sending results from cache"), 5);
    Assert.assertEquals(logMessages.contains("store table size is bigger than cache."), false);
    Assert.assertEquals(logMessages.contains("cache constraints satisfied. Checking cache"), false);
    Assert.assertEquals(logMessages.contains("cache hit. Sending results from cache"), false);
    Assert.assertEquals(logMessages.contains("cache miss. Loading from store"), false);
    Assert.assertEquals(logMessages.contains("store also miss. sending null"), false);
    Assert.assertEquals(logMessages.contains("sending results from cache after loading from store"), false);
    Assert.assertEquals(logMessages.contains("sending results from store"), false);
    logger.removeAppender(appender);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) TestAppenderToValidateLogsForCachingTests(io.siddhi.core.query.table.util.TestAppenderToValidateLogsForCachingTests) ArrayList(java.util.ArrayList) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) Logger(org.apache.logging.log4j.core.Logger) SiddhiManager(io.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Aggregations

SiddhiAppRuntime (io.siddhi.core.SiddhiAppRuntime)34 SiddhiManager (io.siddhi.core.SiddhiManager)34 Event (io.siddhi.core.event.Event)34 TestAppenderToValidateLogsForCachingTests (io.siddhi.core.query.table.util.TestAppenderToValidateLogsForCachingTests)34 Logger (org.apache.logging.log4j.core.Logger)34 Test (org.testng.annotations.Test)34 InputHandler (io.siddhi.core.stream.input.InputHandler)33 ArrayList (java.util.ArrayList)28 QueryCallback (io.siddhi.core.query.output.callback.QueryCallback)21 LoggerContext (org.apache.logging.log4j.core.LoggerContext)2 Configuration (org.apache.logging.log4j.core.config.Configuration)2