/*
* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.wso2.siddhi.core.transport;
import org.apache.log4j.Logger;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.wso2.siddhi.core.ExecutionPlanRuntime;
import org.wso2.siddhi.core.SiddhiManager;
import org.wso2.siddhi.core.stream.input.InputHandler;
import org.wso2.siddhi.core.util.transport.InMemoryBroker;
import java.util.concurrent.atomic.AtomicInteger;
public class MultiClientDistributedSinkTestCase {
private static final Logger log = Logger.getLogger(MultiClientDistributedSinkTestCase.class);
private AtomicInteger topic1Count = new AtomicInteger(0);
private AtomicInteger topic2Count = new AtomicInteger(0);
@Before
public void init() {
topic1Count.set(0);
topic2Count.set(0);
}
@Test
public void multiClientRoundRobin() throws InterruptedException {
log.info("Test inMemorySink And EventMapping With SiddhiQL Dynamic Params");
InMemoryBroker.Subscriber subscriptionWSO2 = new InMemoryBroker.Subscriber() {
@Override
public void onMessage(Object msg) {
topic1Count.incrementAndGet();
}
@Override
public String getTopic() {
return "topic1";
}
};
InMemoryBroker.Subscriber subscriptionIBM = new InMemoryBroker.Subscriber() {
@Override
public void onMessage(Object msg) {
topic2Count.incrementAndGet();
}
@Override
public String getTopic() {
return "topic2";
}
};
//subscribe to "inMemory" broker per topic
InMemoryBroker.subscribe(subscriptionWSO2);
InMemoryBroker.subscribe(subscriptionIBM);
String streams = "" +
"@Plan:name('TestExecutionPlan')" +
"define stream FooStream (symbol string, price float, volume long); " +
"@sink(type='testInMemory', @map(type='passThrough'), " +
" @distribution(strategy='roundRobin', " +
" @destination(topic = 'topic1'), " +
" @destination(topic = 'topic2'))) " +
"define stream BarStream (symbol string, price float, volume long); ";
String query = "" +
"from FooStream " +
"select * " +
"insert into BarStream; ";
SiddhiManager siddhiManager = new SiddhiManager();
ExecutionPlanRuntime executionPlanRuntime = siddhiManager.createExecutionPlanRuntime(streams + query);
InputHandler stockStream = executionPlanRuntime.getInputHandler("FooStream");
executionPlanRuntime.start();
stockStream.send(new Object[]{"WSO2", 55.6f, 100L});
stockStream.send(new Object[]{"WSO2", 75.6f, 100L});
stockStream.send(new Object[]{"WSO2", 57.6f, 100L});
stockStream.send(new Object[]{"WSO2", 57.6f, 100L});
stockStream.send(new Object[]{"WSO2", 57.6f, 100L});
Thread.sleep(100);
//assert event count
Assert.assertEquals("Number of WSO2 events", 3, topic1Count.get());
Assert.assertEquals("Number of IBM events", 2, topic2Count.get());
executionPlanRuntime.shutdown();
//unsubscribe from "inMemory" broker per topic
InMemoryBroker.unsubscribe(subscriptionWSO2);
InMemoryBroker.unsubscribe(subscriptionIBM);
}
@Test
public void singleClientPartitioned() throws InterruptedException {
log.info("Test inMemorySink And EventMapping With SiddhiQL Dynamic Params");
InMemoryBroker.Subscriber subscriptionWSO2 = new InMemoryBroker.Subscriber() {
@Override
public void onMessage(Object msg) {
topic1Count.incrementAndGet();
}
@Override
public String getTopic() {
return "topic1";
}
};
InMemoryBroker.Subscriber subscriptionIBM = new InMemoryBroker.Subscriber() {
@Override
public void onMessage(Object msg) {
topic2Count.incrementAndGet();
}
@Override
public String getTopic() {
return "topic2";
}
};
//subscribe to "inMemory" broker per topic
InMemoryBroker.subscribe(subscriptionWSO2);
InMemoryBroker.subscribe(subscriptionIBM);
String streams = "" +
"@Plan:name('TestExecutionPlan')" +
"define stream FooStream (symbol string, price float, volume long); " +
"@sink(type='testInMemory', @map(type='passThrough'), " +
" @distribution(strategy='partitioned', partitionKey='symbol'," +
" @destination(topic = 'topic1'), " +
" @destination(topic = 'topic2'))) " +
"define stream BarStream (symbol string, price float, volume long); ";
String query = "" +
"from FooStream " +
"select * " +
"insert into BarStream; ";
SiddhiManager siddhiManager = new SiddhiManager();
ExecutionPlanRuntime executionPlanRuntime = siddhiManager.createExecutionPlanRuntime(streams + query);
InputHandler stockStream = executionPlanRuntime.getInputHandler("FooStream");
executionPlanRuntime.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});
stockStream.send(new Object[]{"IBM", 57.6f, 100L});
stockStream.send(new Object[]{"WSO2", 57.6f, 100L});
stockStream.send(new Object[]{"WSO2", 57.6f, 100L});
Thread.sleep(100);
//assert event count
Assert.assertEquals("Number of topic 1 events", 2, topic1Count.get());
Assert.assertEquals("Number of topic 2 events", 4, topic2Count.get());
executionPlanRuntime.shutdown();
//unsubscribe from "inMemory" broker per topic
InMemoryBroker.unsubscribe(subscriptionWSO2);
InMemoryBroker.unsubscribe(subscriptionIBM);
}
@Test
public void singleClientBroadcast() throws InterruptedException {
log.info("Test inMemorySink And EventMapping With SiddhiQL Dynamic Params");
InMemoryBroker.Subscriber subscriptionWSO2 = new InMemoryBroker.Subscriber() {
@Override
public void onMessage(Object msg) {
topic1Count.incrementAndGet();
}
@Override
public String getTopic() {
return "topic1";
}
};
InMemoryBroker.Subscriber subscriptionIBM = new InMemoryBroker.Subscriber() {
@Override
public void onMessage(Object msg) {
topic2Count.incrementAndGet();
}
@Override
public String getTopic() {
return "topic2";
}
};
//subscribe to "inMemory" broker per topic
InMemoryBroker.subscribe(subscriptionWSO2);
InMemoryBroker.subscribe(subscriptionIBM);
String streams = "" +
"@Plan:name('TestExecutionPlan')" +
"define stream FooStream (symbol string, price float, volume long); " +
"@sink(type='testInMemory', @map(type='passThrough'), " +
" @distribution(strategy='broadcast'," +
" @destination(topic = 'topic1'), " +
" @destination(topic = 'topic2'))) " +
"define stream BarStream (symbol string, price float, volume long); ";
String query = "" +
"from FooStream " +
"select * " +
"insert into BarStream; ";
SiddhiManager siddhiManager = new SiddhiManager();
ExecutionPlanRuntime executionPlanRuntime = siddhiManager.createExecutionPlanRuntime(streams + query);
InputHandler stockStream = executionPlanRuntime.getInputHandler("FooStream");
executionPlanRuntime.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});
stockStream.send(new Object[]{"IBM", 57.6f, 100L});
stockStream.send(new Object[]{"WSO2", 57.6f, 100L});
stockStream.send(new Object[]{"WSO2", 57.6f, 100L});
Thread.sleep(100);
//assert event count
Assert.assertEquals("Number of topic 1 events", 6, topic1Count.get());
Assert.assertEquals("Number of topic 2 events", 6, topic2Count.get());
executionPlanRuntime.shutdown();
//unsubscribe from "inMemory" broker per topic
InMemoryBroker.unsubscribe(subscriptionWSO2);
InMemoryBroker.unsubscribe(subscriptionIBM);
}
}