use of org.ballerinalang.siddhi.core.event.stream.StreamEvent in project ballerina by ballerina-lang.
the class EventTestCase method testEventCreation.
@Test
public void testEventCreation() {
SiddhiEventFactory siddhiEventFactory = new SiddhiEventFactory(2);
AssertJUnit.assertEquals(2, siddhiEventFactory.newInstance().getData().length);
StreamEventFactory streamEventFactory = new StreamEventFactory(2, 3, 4);
StreamEvent streamEvent = streamEventFactory.newInstance();
AssertJUnit.assertEquals(2, streamEvent.getBeforeWindowData().length);
AssertJUnit.assertEquals(3, streamEvent.getOnAfterWindowData().length);
AssertJUnit.assertEquals(4, streamEvent.getOutputData().length);
}
use of org.ballerinalang.siddhi.core.event.stream.StreamEvent in project ballerina by ballerina-lang.
the class EventTestCase method testStreamEventConverter.
@Test
public void testStreamEventConverter() {
Attribute price = new Attribute("price", Attribute.Type.DOUBLE);
Attribute volume = new Attribute("volume", Attribute.Type.INT);
Attribute symbol = new Attribute("symbol", Attribute.Type.STRING);
MetaStreamEvent metaStreamEvent = new MetaStreamEvent();
metaStreamEvent.addData(volume);
metaStreamEvent.initializeAfterWindowData();
metaStreamEvent.addData(price);
metaStreamEvent.addOutputData(symbol);
// complex attribute
metaStreamEvent.addOutputData(null);
StreamDefinition streamDefinition = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.DOUBLE).attribute("volume", Attribute.Type.INT);
Event event = new Event(System.currentTimeMillis(), new Object[] { "WSO2", 200, 50 });
metaStreamEvent.addInputDefinition(streamDefinition);
StreamEventConverter converter = StreamEventConverterFactory.constructEventConverter(metaStreamEvent);
StreamEventPool eventPool = new StreamEventPool(metaStreamEvent, 5);
StreamEvent borrowedEvent = eventPool.borrowEvent();
converter.convertEvent(event, borrowedEvent);
AssertJUnit.assertTrue(converter instanceof SelectiveStreamEventConverter);
// volume
AssertJUnit.assertEquals(1, borrowedEvent.getBeforeWindowData().length);
// price
AssertJUnit.assertEquals(1, borrowedEvent.getOnAfterWindowData().length);
// symbol and avgPrice
AssertJUnit.assertEquals(2, borrowedEvent.getOutputData().length);
AssertJUnit.assertEquals(50, borrowedEvent.getBeforeWindowData()[0]);
AssertJUnit.assertEquals(200, borrowedEvent.getOnAfterWindowData()[0]);
AssertJUnit.assertEquals("WSO2", borrowedEvent.getOutputData()[0]);
}
use of org.ballerinalang.siddhi.core.event.stream.StreamEvent in project ballerina by ballerina-lang.
the class EventTestCase method testEventPool.
@Test
public void testEventPool() {
StreamEventPool streamEventPool = new StreamEventPool(2, 3, 1, 4);
StreamEvent[] streamEvents = new StreamEvent[5];
for (int i = 0; i < 5; i++) {
streamEvents[i] = streamEventPool.borrowEvent();
}
AssertJUnit.assertEquals(0, streamEventPool.getBufferedEventsSize());
for (int i = 0; i < 5; i++) {
streamEventPool.returnEvents(streamEvents[i]);
}
AssertJUnit.assertEquals(4, streamEventPool.getBufferedEventsSize());
streamEventPool.borrowEvent();
AssertJUnit.assertEquals(3, streamEventPool.getBufferedEventsSize());
}
use of org.ballerinalang.siddhi.core.event.stream.StreamEvent in project ballerina by ballerina-lang.
the class EventTestCase method testSimpleStreamEventConverter.
@Test
public void testSimpleStreamEventConverter() {
Attribute price = new Attribute("price", Attribute.Type.DOUBLE);
Attribute symbol = new Attribute("symbol", Attribute.Type.STRING);
MetaStreamEvent metaStreamEvent = new MetaStreamEvent();
metaStreamEvent.addOutputData(symbol);
metaStreamEvent.addOutputData(price);
StreamDefinition streamDefinition = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.DOUBLE).attribute("volume", Attribute.Type.INT);
Event event = new Event(System.currentTimeMillis(), new Object[] { "WSO2", 200, 50 });
metaStreamEvent.addInputDefinition(streamDefinition);
StreamEventConverter converter = StreamEventConverterFactory.constructEventConverter(metaStreamEvent);
StreamEventPool eventPool = new StreamEventPool(metaStreamEvent, 5);
StreamEvent borrowedEvent = eventPool.borrowEvent();
converter.convertEvent(event, borrowedEvent);
AssertJUnit.assertTrue(converter instanceof SimpleStreamEventConverter);
AssertJUnit.assertNull(borrowedEvent.getBeforeWindowData());
AssertJUnit.assertNull(borrowedEvent.getOnAfterWindowData());
AssertJUnit.assertEquals(2, borrowedEvent.getOutputData().length);
AssertJUnit.assertEquals(200, borrowedEvent.getOutputData()[1]);
AssertJUnit.assertEquals("WSO2", borrowedEvent.getOutputData()[0]);
}
use of org.ballerinalang.siddhi.core.event.stream.StreamEvent in project ballerina by ballerina-lang.
the class JunctionTestCase method oneToOneTest.
@Test
public void oneToOneTest() throws InterruptedException {
log.info("one to one");
StreamDefinition streamA = StreamDefinition.id("streamA").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.INT).annotation(Annotation.annotation("parallel"));
StreamJunction streamJunctionA = new StreamJunction(streamA, executorService, 1024, siddhiAppContext);
StreamJunction.Publisher streamPublisherA = streamJunctionA.constructPublisher();
StreamDefinition streamB = StreamDefinition.id("streamB").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.INT).annotation(Annotation.annotation("parallel"));
StreamJunction streamJunctionB = new StreamJunction(streamB, executorService, 1024, siddhiAppContext);
final StreamJunction.Publisher streamPublisherB = streamJunctionB.constructPublisher();
StreamCallback streamCallbackA = new StreamCallback() {
@Override
public void receive(Event[] streamEvents) {
for (Event streamEvent : streamEvents) {
StreamEvent innerStreamEvent = new StreamEvent(2, 2, 2);
innerStreamEvent.setTimestamp(streamEvent.getTimestamp());
innerStreamEvent.setOutputData(streamEvent.getData());
streamPublisherB.send(innerStreamEvent);
}
}
};
StreamCallback streamCallbackB = new StreamCallback() {
@Override
public void receive(Event[] streamEvents) {
count += streamEvents.length;
eventArrived = true;
for (Event streamEvent : streamEvents) {
AssertJUnit.assertTrue(streamEvent.getData()[0].equals("IBM") || (streamEvent.getData()[0].equals("WSO2")));
}
}
};
streamJunctionA.subscribe(streamCallbackA);
streamJunctionA.startProcessing();
streamJunctionB.subscribe(streamCallbackB);
streamJunctionB.startProcessing();
// Thread.sleep(100);
StreamEvent streamEvent1 = new StreamEvent(2, 2, 2);
streamEvent1.setTimestamp(System.currentTimeMillis());
streamEvent1.setOutputData(new Object[] { "IBM", 12 });
StreamEvent streamEvent2 = new StreamEvent(2, 2, 2);
streamEvent2.setTimestamp(System.currentTimeMillis());
streamEvent2.setOutputData(new Object[] { "WSO2", 112 });
streamPublisherA.send(streamEvent1);
streamPublisherA.send(streamEvent2);
Thread.sleep(100);
AssertJUnit.assertTrue(eventArrived);
AssertJUnit.assertEquals(2, count);
streamJunctionA.stopProcessing();
streamJunctionB.stopProcessing();
}
Aggregations