Java Code Examples for io.siddhi.core.util.EventPrinter#print()

The following examples show how to use io.siddhi.core.util.EventPrinter#print() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PartitionTestCase1.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testPartitionQuery40() throws InterruptedException {
    log.info("Partition test");
    SiddhiManager siddhiManager = new SiddhiManager();

    String siddhiApp = "" +
            "@app:name('PartitionTest') " +
            "@app:statistics('true') " +
            "define stream streamA (symbol string,  price int);" +
            "@info(name = 'partitionB')" +
            "partition with (symbol of streamA) " +
            "begin " +
            "@info(name = 'query1') " +
            "from streamA  " +
            "select symbol, price insert into StockQuote ;  " +
            "end ";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);

    StreamCallback streamCallback = new StreamCallback() {
        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            AssertJUnit.assertTrue("IBM".equals(events[0].getData(0)) || "WSO2".equals(events[0].getData(0)));
            count.addAndGet(events.length);
            eventArrived = true;
        }
    };
    siddhiAppRuntime.addCallback("StockQuote", streamCallback);

    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("streamA");
    siddhiAppRuntime.start();
    inputHandler.send(new Event(System.currentTimeMillis(), new Object[]{"IBM", 700}));
    inputHandler.send(new Event(System.currentTimeMillis(), new Object[]{"WSO2", 60}));
    inputHandler.send(new Event(System.currentTimeMillis(), new Object[]{"WSO2", 60}));
    SiddhiTestHelper.waitForEvents(100, 3, count, 60000);
    AssertJUnit.assertTrue(eventArrived);
    AssertJUnit.assertEquals(3, count.get());
    siddhiAppRuntime.shutdown();
}
 
Example 2
Source File: UpdateTestStoreTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void updateFromTableTest1() throws InterruptedException, SQLException {
    log.info("updateFromTableTest1");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" +
            "define stream StockStream (symbol string, price double, volume long); " +
            "define stream UpdateStockStream (symbol string, price double, volume long); " +
            "@Store(type=\"testStoreContainingInMemoryTable\")\n" +
            "define table StockTable (symbol string, price double, volume long); ";

    String query = "" +
            "@info(name = 'query1')\n" +
            "from StockStream\n" +
            "insert into StockTable;\n" +
            "@info(name = 'query2') " +
            "from UpdateStockStream\n" +
            "select symbol, price, volume\n" +
            "update StockTable\n" +
            "on (StockTable.symbol == symbol);";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    InputHandler stockStream = siddhiAppRuntime.getInputHandler("StockStream");
    InputHandler updateStockStream = siddhiAppRuntime.getInputHandler("UpdateStockStream");
    siddhiAppRuntime.start();

    stockStream.send(new Object[]{"WSO2", 55.6, 100L});
    stockStream.send(new Object[]{"IBM", 75.6, 100L});
    stockStream.send(new Object[]{"WSO2", 57.6, 100L});
    updateStockStream.send(new Object[]{"IBM", 57.6, 100L});
    Thread.sleep(1000);

    Event[] events = siddhiAppRuntime.query("" +
            "from StockTable ");
    EventPrinter.print(events);
    AssertJUnit.assertEquals(3, events.length);
    siddhiAppRuntime.shutdown();
}
 
Example 3
Source File: QueryAPITestCaseForTableWithCache.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test(expectedExceptions = StoreQueryCreationException.class)
public void test4() throws InterruptedException {
    log.info("Test4 table with cache");

    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" +
            "define stream StockStream (symbol string, price float, volume long); " +
            "@Store(type=\"testStoreDummyForCache\", @Cache(size=\"10\"))\n" +
            "define table StockTable (symbol string, price float, volume long); ";
    String query = "" +
            "@info(name = 'query1') " +
            "from StockStream " +
            "insert into StockTable ;";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    try {
        InputHandler stockStream = siddhiAppRuntime.getInputHandler("StockStream");
        siddhiAppRuntime.start();
        stockStream.send(new Object[]{"WSO2", 55.6f, 100L});
        stockStream.send(new Object[]{"IBM", 75.6f, 100L});
        stockStream.send(new Object[]{"WSO2", 57.6f, 100L});
        Thread.sleep(500);

        Event[] events = siddhiAppRuntime.query("" +
                "from StockTabled " +
                "on price > 5 " +
                "select symbol1, sum(volume) as totalVolume " +
                "group by symbol " +
                "having totalVolume >150 ");
        EventPrinter.print(events);
        AssertJUnit.assertEquals(1, events.length);
        AssertJUnit.assertEquals(400L, events[0].getData(1));

    } finally {
        siddhiAppRuntime.shutdown();
    }
}
 
Example 4
Source File: QueryAPITestCaseForTableWithCache.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test(expectedExceptions = StoreQueryCreationException.class)
public void test5() {
    log.info("Test5 table with cache");

    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" +
            "define stream StockStream (symbol string, price float, volume long); " +
            "@Store(type=\"testStoreDummyForCache\", @Cache(size=\"10\"))\n" +
            "define table StockTable (symbol string, price float, volume long); ";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams);
    try {
        siddhiAppRuntime.start();
        Event[] events = siddhiAppRuntime.query("" +
                "from StockTable " +
                "on price > 5 " +
                "select symbol1, sum(volume) as totalVolume " +
                "group by symbol " +
                "having totalVolume >150 ");
        EventPrinter.print(events);
        AssertJUnit.assertEquals(1, events.length);
        AssertJUnit.assertEquals(200L, events[0].getData(1));

    } finally {
        siddhiAppRuntime.shutdown();
    }
}
 
Example 5
Source File: OnDemandQueryWindowTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void test3() throws InterruptedException {
    log.info("Test3 window");

    SiddhiManager siddhiManager = new SiddhiManager();

    String streams = "" +
            "define stream StockStream (symbol string, price float, volume long); " +
            "define window StockWindow (symbol string, price float, volume long) length(3); ";
    String query = "" +
            "@info(name = 'query1') " +
            "from StockStream " +
            "insert into StockWindow ;";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);

    InputHandler stockStream = siddhiAppRuntime.getInputHandler("StockStream");

    siddhiAppRuntime.start();

    stockStream.send(new Object[]{"WSO2", 55.6f, 100L});
    stockStream.send(new Object[]{"IBM", 75.6f, 100L});
    stockStream.send(new Object[]{"WSO2", 57.6f, 100L});
    Thread.sleep(500);

    Event[] events = siddhiAppRuntime.query("" +
            "from StockWindow " +
            "on price > 5 " +
            "select symbol, sum(volume) as totalVolume " +
            "group by symbol " +
            "having totalVolume >150 ");
    EventPrinter.print(events);
    AssertJUnit.assertEquals(1, events.length);
    AssertJUnit.assertEquals(200L, events[0].getData(1));

    events = siddhiAppRuntime.query("" +
            "from StockWindow " +
            "on price > 5 " +
            "select symbol, sum(volume) as totalVolume " +
            "group by symbol  ");
    EventPrinter.print(events);
    AssertJUnit.assertEquals(2, events.length);

    siddhiAppRuntime.shutdown();
}
 
Example 6
Source File: Aggregation1TestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test(dependsOnMethods = {"incrementalStreamProcessorTest9"})
public void incrementalStreamProcessorTest11() throws InterruptedException {
    LOG.info("incrementalStreamProcessorTest11");
    SiddhiManager siddhiManager = new SiddhiManager();

    String stockStream =
            "define stream stockStream (symbol string, price float, lastClosingPrice float, volume long , " +
                    "quantity int, timestamp long);";
    String query =
            "define aggregation stockAggregation " +
                    "from stockStream " +
                    "select symbol, avg(price) as avgPrice, sum(price) as totalPrice, " +
                    "(price * quantity) as lastTradeValue  " +
                    "group by symbol " +
                    "aggregate every sec...hour ;";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(stockStream + query);

    InputHandler stockStreamInputHandler = siddhiAppRuntime.getInputHandler("stockStream");
    siddhiAppRuntime.start();

    stockStreamInputHandler.send(new Object[]{"WSO2", 50f, 60f, 90L, 6, 1496289950000L});
    stockStreamInputHandler.send(new Object[]{"WSO2", 70f, null, 40L, 10, 1496289950000L});
    Thread.sleep(2000);

    stockStreamInputHandler.send(new Object[]{"WSO2", 60f, 44f, 200L, 56, 1496289952000L});
    stockStreamInputHandler.send(new Object[]{"WSO2", 100f, null, 200L, 16, 1496289952000L});
    Thread.sleep(2000);

    stockStreamInputHandler.send(new Object[]{"IBM", 100f, null, 200L, 26, 1496289954000L});
    stockStreamInputHandler.send(new Object[]{"IBM", 100f, null, 200L, 96, 1496289954000L});
    Thread.sleep(5000);

    LocalDate currentDate = LocalDate.now();
    String year = String.valueOf(currentDate.getYear());
    String month = String.valueOf(currentDate.getMonth().getValue());
    if (month.length() == 1) {
        month = "0".concat(month);
    }

    Event[] events = siddhiAppRuntime.query("from stockAggregation " +
            "on symbol == \"IBM\" " +
            "within \"" + year + "-" + month + "-** **:**:** +05:30\" " +
            "per \"seconds\"; ");

    EventPrinter.print(events);
    Assert.assertNotNull(events);
    AssertJUnit.assertEquals(1, events.length);

    Object[] copyEventsWithoutTime = new Object[4];
    System.arraycopy(events[0].getData(), 1, copyEventsWithoutTime, 0, 4);
    AssertJUnit.assertArrayEquals(new Object[]{"IBM", 100.0, 200.0, 9600f}, copyEventsWithoutTime);

    Thread.sleep(100);
    siddhiAppRuntime.shutdown();
}
 
Example 7
Source File: UpdateOrInsertTestStoreTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void updateOrInsertTableTest13() throws InterruptedException, SQLException {
    log.info("updateOrInsertTableTest12");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" +
            "define stream StockStream (symbol string, price float, volume long); " +
            "define stream UpdateStockStream (symbol string, price float, volume long); " +
            "@Store(type=\"testStoreContainingInMemoryTable\")\n" +
            "define table StockTable (symbol string, price float, volume long); ";
    String query = "" +
            "@info(name = 'query1') " +
            "from StockStream " +
            "insert into StockTable ;" +
            "" +
            "@info(name = 'query2') " +
            "from UpdateStockStream " +
            "update or insert into StockTable " +
            "   on StockTable.volume == volume ;";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    InputHandler stockStream = siddhiAppRuntime.getInputHandler("StockStream");
    InputHandler updateStockStream = siddhiAppRuntime.getInputHandler("UpdateStockStream");
    siddhiAppRuntime.start();

    stockStream.send(new Object[]{"WSO2", 55.6F, 100L});
    stockStream.send(new Object[]{"IBM", 75.6F, 100L});
    stockStream.send(new Object[]{"WSO2", 57.6F, 100L});
    updateStockStream.send(new Event[]{
                    new Event(System.currentTimeMillis(), new Object[]{"GOOG", 10.6F, 200L}),
                    new Event(System.currentTimeMillis(), new Object[]{"FB", 11.6F, 200L}),
            }
    );
    Thread.sleep(500);

    Event[] events = siddhiAppRuntime.query("" +
            "from StockTable ");
    EventPrinter.print(events);
    AssertJUnit.assertEquals(4, events.length);

    siddhiAppRuntime.shutdown();
}
 
Example 8
Source File: Aggregation2TestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test(dependsOnMethods = {"incrementalStreamProcessorTest5SGTTimeZoneDay"})
public void incrementalStreamProcessorTest5SGTTimeZoneDay2() throws InterruptedException {
    //feb 29 leap
    LOG.info("incrementalStreamProcessorTest5");
    SiddhiManager siddhiManager = new SiddhiManager();
    Map<String, String> systemConfigs = new HashMap<>();
    systemConfigs.put("aggTimeZone", "Asia/Singapore");
    ConfigManager configManager = new InMemoryConfigManager(null, null, systemConfigs);
    siddhiManager.setConfigManager(configManager);

    String stockStream =
            "define stream stockStream (symbol string, price float, timestamp long);";
    String query = " define aggregation stockAggregation " +
            "from stockStream " +
            "select symbol, avg(price) as avgPrice  " +
            "group by symbol " +
            "aggregate by timestamp every day ;";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(stockStream + query);

    InputHandler stockStreamInputHandler = siddhiAppRuntime.getInputHandler("stockStream");
    siddhiAppRuntime.start();

    // Saturday February 29, 2020 00:00:01 (am) in time zone Asia/Singapore (+08)
    stockStreamInputHandler.send(new Object[]{"WSO2", 50f, 1582905601000L});

    // Saturday February 29, 2020 23:59:59 (pm) in time zone Asia/Singapore (+08)
    stockStreamInputHandler.send(new Object[]{"WSO2", 70f, 1582991999000L});

    // Sunday March 01, 2020 00:00:01 (am) in time zone Asia/Singapore (+08)
    stockStreamInputHandler.send(new Object[]{"WSO2", 80f, 1582992001000L});

    Thread.sleep(2000);

    Event[] events = siddhiAppRuntime.query("from stockAggregation " +
            "within \"2020-**-** **:**:**\" " +
            "per \"day\"");
    EventPrinter.print(events);

    Assert.assertNotNull(events, "Queried results cannot be null.");
    AssertJUnit.assertEquals(2, events.length);

    List<Object[]> eventsOutputList = new ArrayList<>();
    for (Event event : events) {
        eventsOutputList.add(event.getData());
    }
    List<Object[]> expected = Arrays.asList(
            new Object[]{1582905600000L, "WSO2", 60.0},
            new Object[]{1582992000000L, "WSO2", 80.0}
    );
    AssertJUnit.assertTrue("In events matched", SiddhiTestHelper.isUnsortedEventsMatch(eventsOutputList, expected));
    siddhiAppRuntime.shutdown();
}
 
Example 9
Source File: DeleteFromTestStoreTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void deleteFromTestStoreTest7() throws InterruptedException, SQLException {
    log.info("deleteFromTestStoreTest7");
    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=\"testStoreContainingInMemoryTable\")\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 ;";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    InputHandler stockStream = siddhiAppRuntime.getInputHandler("StockStream");
    InputHandler deleteStockStream = siddhiAppRuntime.getInputHandler("DeleteStockStream");
    siddhiAppRuntime.start();

    stockStream.send(new Object[]{"WSO2", 55.6F, 100L});
    stockStream.send(new Object[]{"IBM", 75.6F, 100L});
    stockStream.send(new Object[]{"WSO2", 57.6F, 100L});
    deleteStockStream.send(new Object[]{"IBM", 57.6F, 100L});
    Thread.sleep(1000);

    Event[] events = siddhiAppRuntime.query("" +
            "from StockTable ");
    EventPrinter.print(events);
    try {
        AssertJUnit.assertEquals(2, events.length);
    } catch (NullPointerException ignore) {

    }

    siddhiAppRuntime.shutdown();
}
 
Example 10
Source File: PurgingTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void incrementalPurgingTest2() throws InterruptedException {
    LOG.info("incrementalPurgingTest2");
    SiddhiManager siddhiManager = new SiddhiManager();

    String stockStream = " define stream stockStream (symbol string, price float, lastClosingPrice float," +
            " volume long ,quantity int);";
    String query = "  @purge(enable='true',interval='5 sec',@retentionPeriod(sec='120 sec',min='all',hours='all'" +
            "                ,days='all',months='all',years='all'))  " +
            "define aggregation stockAggregation " +
            "from stockStream " +
            "select symbol, avg(price) as avgPrice, sum(price) as totalPrice, (price * quantity) " +
            "as lastTradeValue  " +
            "group by symbol " +
            "aggregate every sec...years ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(stockStream + query);
    InputHandler stockStreamInputHandler = siddhiAppRuntime.getInputHandler("stockStream");
    siddhiAppRuntime.start();

    stockStreamInputHandler.send(new Object[]{"WSO2", 50f, 60f, 90L, 6});
    Thread.sleep(1000);
    stockStreamInputHandler.send(new Object[]{"WSO2", 70f, null, 40L, 10});
    Thread.sleep(1000);
    stockStreamInputHandler.send(new Object[]{"WSO2", 60f, 44f, 200L, 56});
    Thread.sleep(1000);
    stockStreamInputHandler.send(new Object[]{"WSO2", 100f, null, 200L, 16});
    Thread.sleep(1000);
    stockStreamInputHandler.send(new Object[]{"IBM", 100f, null, 200L, 26});
    Thread.sleep(1000);
    stockStreamInputHandler.send(new Object[]{"IBM", 100f, null, 200L, 96});
    Thread.sleep(1000);

    Event[] events = siddhiAppRuntime.query("from stockAggregation " +
            "within \"" + Calendar.getInstance().get(Calendar.YEAR) + "-**-** **:**:**\" " +
            "per \"seconds\"");
    EventPrinter.print(events);

    AssertJUnit.assertNotNull("Initial queried events cannot be null", events);
    AssertJUnit.assertEquals("Number of events before purging", 6, events.length);
    Thread.sleep(120100);

    events = siddhiAppRuntime.query("from stockAggregation " +
            "within \"" + Calendar.getInstance().get(Calendar.YEAR) + "-**-** **:**:**\" " +
            "per \"seconds\"");
    EventPrinter.print(events);
    AssertJUnit.assertNull("No events expected after purging", events);

    siddhiAppRuntime.shutdown();
}
 
Example 11
Source File: CacheFIFOTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test(description = "cacheFIFOTestCase2") // using query api and 2 primary keys
public void cacheFIFOTestCase2() throws InterruptedException, SQLException {
    final TestAppenderToValidateLogsForCachingTests appender = new TestAppenderToValidateLogsForCachingTests();
    final Logger logger = Logger.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=\"2\", cache.policy=\"FIFO\"))\n" +
            "@PrimaryKey(\'symbol\', \'price\') " +
            "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, 1L});
    deleteStockStream.send(new Object[]{"IBM", 75.6f, 2L});
    stockStream.send(new Object[]{"CISCO", 75.6f, 3L});
    Thread.sleep(10);
    stockStream.send(new Object[]{"APPLE", 75.6f, 4L});
    Thread.sleep(1000);

    Event[] events = siddhiAppRuntime.query("" +
            "from StockTable " +
            "on symbol == \"WSO2\" AND price == 55.6f ");
    EventPrinter.print(events);
    AssertJUnit.assertEquals(1, events.length);

    events = siddhiAppRuntime.query("" +
            "from StockTable " +
            "on symbol == \"CISCO\" AND price == 75.6f ");
    EventPrinter.print(events);
    AssertJUnit.assertEquals(1, events.length);

    final List<LoggingEvent> log = appender.getLog();
    List<String> logMessages = new ArrayList<>();
    for (LoggingEvent logEvent : log) {
        String message = String.valueOf(logEvent.getMessage());
        if (message.contains(":")) {
            message = message.split(": ")[1];
        }
        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."), 2);
    Assert.assertEquals(logMessages.contains("cache constraints satisfied. Checking cache"), true);
    Assert.assertEquals(Collections.frequency(logMessages, "cache constraints satisfied. Checking cache"), 2);
    Assert.assertEquals(logMessages.contains("cache hit. Sending results from cache"), false);
    Assert.assertEquals(logMessages.contains("cache miss. Loading from store"), true);
    Assert.assertEquals(Collections.frequency(logMessages, "cache miss. Loading from store"), 2);
    Assert.assertEquals(logMessages.contains("store also miss. sending null"), false);
    Assert.assertEquals(logMessages.contains("sending results from cache after loading from store"), true);
    Assert.assertEquals(Collections.frequency(logMessages, "sending results from cache after loading from store"),
            2);
    Assert.assertEquals(logMessages.contains("sending results from store"), false);

    siddhiAppRuntime.shutdown();
}
 
Example 12
Source File: CacheFIFOTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test(description = "cacheFIFOTestCase0") // using query api
public void cacheFIFOTestCase0() throws InterruptedException, SQLException {
    final TestAppenderToValidateLogsForCachingTests appender = new TestAppenderToValidateLogsForCachingTests();
    final Logger logger = Logger.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=\"2\", cache.policy=\"FIFO\"))\n" +
            "@PrimaryKey('symbol') " +
            "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, 1L});
    deleteStockStream.send(new Object[]{"IBM", 75.6f, 2L});
    stockStream.send(new Object[]{"CISCO", 75.6f, 3L});
    Thread.sleep(10);
    stockStream.send(new Object[]{"APPLE", 75.6f, 4L});
    Thread.sleep(1000);

    Event[] events = siddhiAppRuntime.query("" +
            "from StockTable " +
            "on symbol == \"WSO2\" ");
    EventPrinter.print(events);
    AssertJUnit.assertEquals(1, events.length);

    events = siddhiAppRuntime.query("" +
            "from StockTable " +
            "on symbol == \"CISCO\" ");
    EventPrinter.print(events);
    AssertJUnit.assertEquals(1, events.length);

    final List<LoggingEvent> log = appender.getLog();
    List<String> logMessages = new ArrayList<>();
    for (LoggingEvent logEvent : log) {
        String message = String.valueOf(logEvent.getMessage());
        if (message.contains(":")) {
            message = message.split(": ")[1];
        }
        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."), 2);
    Assert.assertEquals(logMessages.contains("cache constraints satisfied. Checking cache"), true);
    Assert.assertEquals(Collections.frequency(logMessages, "cache constraints satisfied. Checking cache"), 2);
    Assert.assertEquals(logMessages.contains("cache hit. Sending results from cache"), false);
    Assert.assertEquals(logMessages.contains("cache miss. Loading from store"), true);
    Assert.assertEquals(Collections.frequency(logMessages, "cache miss. Loading from store"), 2);
    Assert.assertEquals(logMessages.contains("store also miss. sending null"), false);
    Assert.assertEquals(logMessages.contains("sending results from cache after loading from store"), true);
    Assert.assertEquals(Collections.frequency(logMessages, "sending results from cache after loading from store"),
            2);
    Assert.assertEquals(logMessages.contains("sending results from store"), false);

    siddhiAppRuntime.shutdown();
}
 
Example 13
Source File: PersistenceTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test(dependsOnMethods = "persistenceTest1")
public void persistenceTest2() throws InterruptedException {
    log.info("persistence test 2 - pattern count query");

    PersistenceStore persistenceStore = new InMemoryPersistenceStore();

    SiddhiManager siddhiManager = new SiddhiManager();
    siddhiManager.setPersistenceStore(persistenceStore);

    String siddhiApp = "" +
            "@app:name('Test') " +
            "" +
            "define stream Stream1 (symbol string, price float, volume int); " +
            "define stream Stream2 (symbol string, price float, volume int); " +
            "" +
            "@info(name = 'query1') " +
            "from e1=Stream1[price>20] <2:5> -> e2=Stream2[price>20] " +
            "select e1[0].price as price1_0, e1[1].price as price1_1, e1[2].price as price1_2, " +
            "   e1[3].price as price1_3, e2.price as price2 " +
            "insert into OutputStream ;";


    QueryCallback queryCallback = new QueryCallback() {
        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            eventArrived = true;
            for (Event inEvent : inEvents) {
                count++;
                AssertJUnit.assertArrayEquals(new Object[]{25.6f, 47.6f, null, null, 45.7f}, inEvent.getData());
            }
        }
    };

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("query1", queryCallback);

    InputHandler stream1 = siddhiAppRuntime.getInputHandler("Stream1");
    InputHandler stream2;
    siddhiAppRuntime.start();

    stream1.send(new Object[]{"WSO2", 25.6f, 100});
    Thread.sleep(100);
    stream1.send(new Object[]{"GOOG", 47.6f, 100});
    Thread.sleep(100);
    stream1.send(new Object[]{"GOOG", 13.7f, 100});
    Thread.sleep(100);

    AssertJUnit.assertEquals("Number of success events", 0, count);
    AssertJUnit.assertEquals("Event arrived", false, eventArrived);

    //persisting
    Thread.sleep(500);
    siddhiAppRuntime.persist();

    //restarting siddhi app
    Thread.sleep(500);
    siddhiAppRuntime.shutdown();
    siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("query1", queryCallback);
    stream1 = siddhiAppRuntime.getInputHandler("Stream1");
    stream2 = siddhiAppRuntime.getInputHandler("Stream2");
    siddhiAppRuntime.start();

    //loading
    try {
        siddhiAppRuntime.restoreLastRevision();
    } catch (CannotRestoreSiddhiAppStateException e) {
        Assert.fail("Restoring of Siddhi app " + siddhiAppRuntime.getName() + " failed");
    }

    stream2.send(new Object[]{"IBM", 45.7f, 100});
    Thread.sleep(500);
    stream1.send(new Object[]{"GOOG", 47.8f, 100});
    Thread.sleep(500);
    stream2.send(new Object[]{"IBM", 55.7f, 100});
    Thread.sleep(500);

    //shutdown siddhi app
    Thread.sleep(500);
    siddhiAppRuntime.shutdown();

    AssertJUnit.assertEquals("Number of success events", 1, count);
    AssertJUnit.assertEquals("Event arrived", true, eventArrived);

}
 
Example 14
Source File: Aggregation1TestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test(dependsOnMethods = {"incrementalStreamProcessorTest34"})
public void incrementalStreamProcessorTest35() throws InterruptedException {
    LOG.info("incrementalStreamProcessorTest35");
    SiddhiManager siddhiManager = new SiddhiManager();

    String stockStream =
            "define stream stockStream (symbol string, price float, lastClosingPrice float, volume long , " +
                    "quantity int, timestamp long);";
    String query = " define aggregation stockAggregation " +
            "from stockStream " +
            "select symbol, avg(price) as avgPrice, sum(price) as totalPrice, (price * quantity) " +
            "as lastTradeValue  " +
            "group by symbol " +
            "aggregate by timestamp every sec...hour ;";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(stockStream + query);

    InputHandler stockStreamInputHandler = siddhiAppRuntime.getInputHandler("stockStream");
    siddhiAppRuntime.start();

    stockStreamInputHandler.send(new Object[]{"WSO2", 50f, 60f, 90L, 6, 1496289950000L});
    stockStreamInputHandler.send(new Object[]{"WSO2", 70f, null, 40L, 10, 1496289950000L});

    stockStreamInputHandler.send(new Object[]{"WSO2", 60f, 44f, 200L, 56, 1496289952000L});
    stockStreamInputHandler.send(new Object[]{"WSO2", 100f, null, 200L, 16, 1496289952000L});

    stockStreamInputHandler.send(new Object[]{"IBM", 100f, null, 200L, 26, 1496289954000L});
    stockStreamInputHandler.send(new Object[]{"IBM", 100f, null, 200L, 96, 1496289954000L});

    stockStreamInputHandler.send(new Object[]{"CISCO", 100f, null, 200L, 26, 1513578087000L});
    stockStreamInputHandler.send(new Object[]{"CISCO", 100f, null, 200L, 96, 1513578087000L});

    Thread.sleep(2000);

    Event[] events = siddhiAppRuntime.query("from stockAggregation " +
            "within \"2017-12-18 11:51:27 +05:30\" " +
            "per \"seconds\" " +
            "select * " +
            "order by AGG_TIMESTAMP ;");
    EventPrinter.print(events);
    Assert.assertNotNull(events);
    AssertJUnit.assertEquals(1, events.length);
    AssertJUnit.assertArrayEquals(new Object[]{1513578087000L, "CISCO", 100.0, 200.0, 9600f}, events[0].getData());

    Thread.sleep(100);
    siddhiAppRuntime.shutdown();
}
 
Example 15
Source File: QueryAPITestCaseForTestStore.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void test11() throws InterruptedException {
    log.info("Test10 table with cache");

    SiddhiManager siddhiManager = new SiddhiManager();

    String streams = "" +
            "define stream StockStream (symbol string, price float, volume long); " +
            "@Store(type=\"testStoreContainingInMemoryTable\")\n" +
            "define table StockTable (symbol string, price float, volume long); ";
    String query = "" +
            "@info(name = 'query1') " +
            "from StockStream " +
            "insert into StockTable ;";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);

    InputHandler stockStream = siddhiAppRuntime.getInputHandler("StockStream");

    siddhiAppRuntime.start();

    stockStream.send(new Object[]{"WSO2", 55.6f, 100L});
    stockStream.send(new Object[]{"IBM", 75.6f, 100L});
    stockStream.send(new Object[]{"WSO2", 57.6f, 100L});
    Thread.sleep(500);

    String storeQuery = "" +
            "from StockTable " +
            "on price > 56 " +
            "select symbol, price, sum(volume) as totalVolume " +
            "group by symbol, price ";
    Event[] events = siddhiAppRuntime.query(storeQuery);
    EventPrinter.print(events);
    AssertJUnit.assertEquals(2, events.length);
    AssertJUnit.assertEquals(100L, events[0].getData()[2]);
    AssertJUnit.assertEquals(100L, events[1].getData()[2]);

    events = siddhiAppRuntime.query(storeQuery);
    EventPrinter.print(events);
    AssertJUnit.assertEquals(2, events.length);
    AssertJUnit.assertEquals(100L, events[0].getData()[2]);
    AssertJUnit.assertEquals(100L, events[1].getData()[2]);
}
 
Example 16
Source File: DeleteFromTableWithCacheTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void deleteFromTableWithCacheTest7() throws InterruptedException, SQLException {
    log.info("deleteFromTableWithCacheTest7");
    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=\"testStoreDummyForCache\", @Cache(size=\"10\"))\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 ;";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    InputHandler stockStream = siddhiAppRuntime.getInputHandler("StockStream");
    InputHandler deleteStockStream = siddhiAppRuntime.getInputHandler("DeleteStockStream");
    siddhiAppRuntime.start();

    stockStream.send(new Object[]{"WSO2", 55.6F, 100L});
    stockStream.send(new Object[]{"IBM", 75.6F, 100L});
    stockStream.send(new Object[]{"WSO2", 57.6F, 100L});
    deleteStockStream.send(new Object[]{"IBM", 57.6F, 100L});
    Thread.sleep(1000);

    Event[] events = siddhiAppRuntime.query("" +
            "from StockTable ");
    EventPrinter.print(events);
    try {
        AssertJUnit.assertEquals(2, events.length);
    } catch (NullPointerException ignore) {

    }

    siddhiAppRuntime.shutdown();
}
 
Example 17
Source File: Aggregation1TestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test(dependsOnMethods = {"incrementalStreamProcessorTest4"})
public void incrementalStreamProcessorTest5() throws InterruptedException {
    LOG.info("incrementalStreamProcessorTest5");
    SiddhiManager siddhiManager = new SiddhiManager();

    String stockStream =
            "define stream stockStream (symbol string, price float, lastClosingPrice float, volume long , " +
                    "quantity int, timestamp long);";
    String query = " define aggregation stockAggregation " +
            "from stockStream " +
            "select symbol, avg(price) as avgPrice, sum(price) as totalPrice, (price * quantity) " +
            "as lastTradeValue  " +
            "group by symbol " +
            "aggregate by timestamp every sec...hour ;";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(stockStream + query);

    InputHandler stockStreamInputHandler = siddhiAppRuntime.getInputHandler("stockStream");
    siddhiAppRuntime.start();

    stockStreamInputHandler.send(new Object[]{"WSO2", 50f, 60f, 90L, 6, 1496289950000L});
    stockStreamInputHandler.send(new Object[]{"WSO2", 70f, null, 40L, 10, 1496289950000L});

    stockStreamInputHandler.send(new Object[]{"WSO2", 60f, 44f, 200L, 56, 1496289952000L});
    stockStreamInputHandler.send(new Object[]{"WSO2", 100f, null, 200L, 16, 1496289952500L});

    stockStreamInputHandler.send(new Object[]{"IBM", 100f, null, 200L, 26, 1496289954000L});
    stockStreamInputHandler.send(new Object[]{"IBM", 100f, null, 200L, 96, 1496289954500L});

    Thread.sleep(2000);

    Event[] events = siddhiAppRuntime.query("from stockAggregation " +
            "within \"2017-06-** **:**:**\" " +
            "per \"seconds\"");
    EventPrinter.print(events);

    Assert.assertNotNull(events);
    AssertJUnit.assertEquals(3, events.length);

    List<Object[]> eventsOutputList = new ArrayList<>();
    for (Event event : events) {
        eventsOutputList.add(event.getData());
    }
    List<Object[]> expected = Arrays.asList(
            new Object[]{1496289952000L, "WSO2", 80.0, 160.0, 1600f},
            new Object[]{1496289950000L, "WSO2", 60.0, 120.0, 700f},
            new Object[]{1496289954000L, "IBM", 100.0, 200.0, 9600f}
    );
    AssertJUnit.assertTrue("In events matched", SiddhiTestHelper.isUnsortedEventsMatch(eventsOutputList, expected));

    siddhiAppRuntime.shutdown();
}
 
Example 18
Source File: QueryAPITestCaseForTableWithCache.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void test1() throws InterruptedException {
    log.info("Test1 table with cache");

    SiddhiManager siddhiManager = new SiddhiManager();

    String streams = "" +
            "define stream StockStream (symbol string, price float, volume long); " +
            "@Store(type=\"testStoreDummyForCache\", @Cache(size=\"10\"))\n" +
            "define table StockTable (symbol string, price float, volume long); ";
    String query = "" +
            "@info(name = 'query1') " +
            "from StockStream " +
            "insert into StockTable ;";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);

    InputHandler stockStream = siddhiAppRuntime.getInputHandler("StockStream");

    siddhiAppRuntime.start();

    stockStream.send(new Object[]{"WSO2", 55.6f, 100L});
    stockStream.send(new Object[]{"IBM", 75.6f, 100L});
    stockStream.send(new Object[]{"WSO3", 57.6f, 100L});
    Thread.sleep(500);

    Event[] events = siddhiAppRuntime.query("" +
            "from StockTable ");
    EventPrinter.print(events);
    AssertJUnit.assertEquals(3, events.length);

    events = siddhiAppRuntime.query("" +
            "from StockTable " +
            "on price > 75 ");
    EventPrinter.print(events);
    AssertJUnit.assertEquals(1, events.length);

    events = siddhiAppRuntime.query("" +
            "from StockTable " +
            "on price > volume*3/4  ");
    EventPrinter.print(events);
    AssertJUnit.assertEquals(1, events.length);

    siddhiAppRuntime.shutdown();

}
 
Example 19
Source File: Aggregation1TestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test(dependsOnMethods = {"incrementalStreamProcessorTest32"})
public void incrementalStreamProcessorTest33() throws InterruptedException {
    LOG.info("incrementalStreamProcessorTest33");
    SiddhiManager siddhiManager = new SiddhiManager();

    String stockStream =
            "define stream stockStream (symbol string, price float, lastClosingPrice float, volume long , " +
                    "quantity int, timestamp long);";
    String query = " define aggregation stockAggregation " +
            "from stockStream " +
            "select symbol, avg(price) as avgPrice, sum(price) as totalPrice, (price * quantity) " +
            "as lastTradeValue  " +
            "group by symbol " +
            "aggregate by timestamp every sec...hour ;";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(stockStream + query);

    InputHandler stockStreamInputHandler = siddhiAppRuntime.getInputHandler("stockStream");
    siddhiAppRuntime.start();

    stockStreamInputHandler.send(new Object[]{"WSO2", 50f, 60f, 90L, 6, 1496289950000L});
    stockStreamInputHandler.send(new Object[]{"WSO2", 70f, null, 40L, 10, 1496289950000L});

    stockStreamInputHandler.send(new Object[]{"WSO2", 60f, 44f, 200L, 56, 1496289952000L});
    stockStreamInputHandler.send(new Object[]{"WSO2", 100f, null, 200L, 16, 1496289952000L});

    stockStreamInputHandler.send(new Object[]{"IBM", 100f, null, 200L, 26, 1496289954000L});
    stockStreamInputHandler.send(new Object[]{"IBM", 100f, null, 200L, 96, 1496289954000L});

    stockStreamInputHandler.send(new Object[]{"CISCO", 100f, null, 200L, 26, 1513578087000L});
    stockStreamInputHandler.send(new Object[]{"CISCO", 100f, null, 200L, 96, 1513578087000L});

    Thread.sleep(2000);

    Event[] events = siddhiAppRuntime.query("from stockAggregation " +
            "within \"2017-12-18 06:**:**\" " +
            "per \"seconds\" " +
            "select * " +
            "order by AGG_TIMESTAMP ;");
    EventPrinter.print(events);

    Assert.assertNotNull(events);
    AssertJUnit.assertEquals(1, events.length);
    AssertJUnit.assertArrayEquals(new Object[]{1513578087000L, "CISCO", 100.0, 200.0, 9600f}, events[0].getData());

    Thread.sleep(100);
    siddhiAppRuntime.shutdown();
}
 
Example 20
Source File: LogTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void persistenceTest1() throws InterruptedException {
    log.info("persistence test 1 - window query");

    PersistenceStore persistenceStore = new InMemoryPersistenceStore();

    SiddhiManager siddhiManager = new SiddhiManager();
    siddhiManager.setPersistenceStore(persistenceStore);

    String siddhiApp = "" +
            "@app:name('Test') " +
            "" +
            "define stream StockStream ( symbol string, price float, volume int );" +
            "" +
            "@info(name = 'query1')" +
            "from StockStream" +
            "   #log()" +
            "   #log('test message')" +
            "   #log(false)" +
            "   #log(true)" +
            "   #log('test message',false)" +
            "   #log('test message',true)" +
            "   #log('error','test message')" +
            "   #log('error','test message',false)" +
            "   #log('warn','test message',true)" +
            "select * " +
            "insert into OutStream ";

    QueryCallback queryCallback = new QueryCallback() {
        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            eventArrived = true;
        }
    };

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("query1", queryCallback);

    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("StockStream");
    siddhiAppRuntime.start();

    inputHandler.send(new Event[]{
            new Event(System.currentTimeMillis(), new Object[]{"IBM", 75.6f, 100}),
            new Event(System.currentTimeMillis(), new Object[]{"GOOG", 70.6f, 100})});
    Thread.sleep(100);

    siddhiAppRuntime.shutdown();
    AssertJUnit.assertEquals(true, eventArrived);
}