Search in sources :

Example 11 with Trigger

use of org.wso2.siddhi.core.trigger.Trigger in project siddhi by wso2.

the class TriggerTestCase method testFilterQuery7.

@Test
public void testFilterQuery7() throws InterruptedException {
    log.info("testTrigger7 - OUT 2");
    SiddhiManager siddhiManager = new SiddhiManager();
    String plan = "" + "define stream cseEventStream (symbol string, price float, volume long);" + "define trigger triggerStream at '*/1 * * * * ?' ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(plan);
    siddhiAppRuntime.addCallback("triggerStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            for (Event event : events) {
                long timestamp = event.getTimestamp();
                count++;
                if (count > 1) {
                    float triggerTimeDiff = timestamp / 1000 - lastTimeStamp / 1000;
                    AssertJUnit.assertTrue(1.0f == triggerTimeDiff);
                }
                lastTimeStamp = timestamp;
            }
            eventArrived = true;
        }
    });
    siddhiAppRuntime.start();
    Thread.sleep(1000);
    siddhiAppRuntime.shutdown();
    AssertJUnit.assertEquals(true, eventArrived);
}
Also used : SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) StreamCallback(org.wso2.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 12 with Trigger

use of org.wso2.siddhi.core.trigger.Trigger in project siddhi by wso2.

the class TriggerTestCase method testFilterQuery6.

@Test
public void testFilterQuery6() throws InterruptedException {
    log.info("testTrigger6 - OUT 2");
    SiddhiManager siddhiManager = new SiddhiManager();
    String plan = "" + "define stream cseEventStream (symbol string, price float, volume long);" + "define trigger triggerStream at every 500 milliseconds ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(plan);
    siddhiAppRuntime.addCallback("triggerStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            count += events.length;
            eventArrived = true;
        }
    });
    siddhiAppRuntime.start();
    Thread.sleep(1100);
    AssertJUnit.assertEquals(2, count);
    AssertJUnit.assertEquals(true, eventArrived);
    siddhiAppRuntime.shutdown();
}
Also used : SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) StreamCallback(org.wso2.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 13 with Trigger

use of org.wso2.siddhi.core.trigger.Trigger in project siddhi by wso2.

the class TriggerTestCase method testFilterQuery5.

@Test
public void testFilterQuery5() throws InterruptedException {
    log.info("testTrigger5 - OUT 1");
    SiddhiManager siddhiManager = new SiddhiManager();
    String plan = "" + "define stream cseEventStream (symbol string, price float, volume long);" + "define trigger triggerStream at 'start';";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(plan);
    siddhiAppRuntime.addCallback("triggerStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            count += events.length;
            eventArrived = true;
        }
    });
    siddhiAppRuntime.start();
    Thread.sleep(100);
    AssertJUnit.assertEquals(1, count);
    AssertJUnit.assertEquals(true, eventArrived);
    siddhiAppRuntime.shutdown();
}
Also used : SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) StreamCallback(org.wso2.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 14 with Trigger

use of org.wso2.siddhi.core.trigger.Trigger in project siddhi by wso2.

the class ExternalTimeBatchWindowTestCase method test01DownSampling.

@Test
public void test01DownSampling() throws Exception {
    siddhiManager = new SiddhiManager();
    String stream = "define stream jmxMetric(cpu int, memory int, bytesIn long, bytesOut long, timestamp long);";
    String query = "@info(name = 'downSample') " + "from jmxMetric#window.externalTimeBatch(timestamp, 10 sec) " + "select " + "avg(cpu) as avgCpu, max(cpu) as maxCpu, min(cpu) as minCpu, " + " '|' as s, " + " avg(memory) as avgMem, max(memory) as maxMem, min(memory) as minMem, " + " '|' as s1, " + " avg(bytesIn) as avgBytesIn, max(bytesIn) as maxBytesIn, min(bytesIn) as minBytesIn, " + " '|' as s2, " + " avg(bytesOut) as avgBytesOut, max(bytesOut) as maxBytesOut, min(bytesOut) as minBytesOut, " + " '|' as s3, " + " timestamp as timeWindowEnds, " + " '|' as s4, " + " count(1) as metric_count " + " INSERT INTO tmp;";
    SiddhiManager sm = new SiddhiManager();
    SiddhiAppRuntime plan = sm.createSiddhiAppRuntime(stream + query);
    InputHandler input = plan.getInputHandler("jmxMetric");
    // stream call back doesn't follow the counter
    final AtomicInteger counter = new AtomicInteger();
    {
        // stream callback
        plan.addCallback("jmxMetric", new StreamCallback() {

            @Override
            public void receive(Event[] arg0) {
                counter.addAndGet(arg0.length);
            }
        });
    }
    final AtomicInteger queryWideCounter = new AtomicInteger();
    {
        plan.addCallback("downSample", new QueryCallback() {

            @Override
            public void receive(long timestamp, Event[] inevents, Event[] removevents) {
                int currentCount = queryWideCounter.addAndGet(inevents.length);
                log.info(MessageFormat.format("Round {0} ====", currentCount));
                log.info(" events count " + inevents.length);
                EventPrinter.print(inevents);
            }
        });
    }
    plan.start();
    int round = 4;
    int eventsPerRound = 0;
    long externalTs = System.currentTimeMillis();
    for (int i = 0; i < round; i++) {
        eventsPerRound = sendEvent(input, i, externalTs);
        Thread.sleep(3000);
    }
    // trigger next round
    sendEvent(input, round, externalTs);
    plan.shutdown();
    Thread.sleep(1000);
    AssertJUnit.assertEquals(round * eventsPerRound + eventsPerRound, counter.get());
    AssertJUnit.assertEquals(round, queryWideCounter.get());
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) StreamCallback(org.wso2.siddhi.core.stream.output.StreamCallback) QueryCallback(org.wso2.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)9 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)9 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)9 Event (org.wso2.siddhi.core.event.Event)5 StreamCallback (org.wso2.siddhi.core.stream.output.StreamCallback)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 QueryCallback (org.wso2.siddhi.core.query.output.callback.QueryCallback)4 InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)4 ComplexEventChunk (org.wso2.siddhi.core.event.ComplexEventChunk)2 StreamEvent (org.wso2.siddhi.core.event.stream.StreamEvent)2 Gson (com.google.gson.Gson)1 ArrayList (java.util.ArrayList)1 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)1 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)1 Function (org.wso2.carbon.apimgt.core.models.Function)1 HttpResponse (org.wso2.carbon.apimgt.core.models.HttpResponse)1 ComplexEvent (org.wso2.siddhi.core.event.ComplexEvent)1 StateEvent (org.wso2.siddhi.core.event.state.StateEvent)1 StreamJunction (org.wso2.siddhi.core.stream.StreamJunction)1 CronTrigger (org.wso2.siddhi.core.trigger.CronTrigger)1