|
| 1 | +/* |
| 2 | + * Copyright (c) 2019, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. |
| 3 | + * |
| 4 | + * WSO2 Inc. licenses this file to you under the Apache License, |
| 5 | + * Version 2.0 (the "License"); you may not use this file except |
| 6 | + * in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, |
| 12 | + * software distributed under the License is distributed on an |
| 13 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | + * KIND, either express or implied. See the License for the |
| 15 | + * specific language governing permissions and limitations |
| 16 | + * under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package io.siddhi.core.managment; |
| 20 | + |
| 21 | +import io.siddhi.core.SiddhiAppRuntime; |
| 22 | +import io.siddhi.core.SiddhiManager; |
| 23 | +import io.siddhi.core.event.Event; |
| 24 | +import io.siddhi.core.stream.input.InputHandler; |
| 25 | +import io.siddhi.core.stream.output.StreamCallback; |
| 26 | +import io.siddhi.core.util.EventPrinter; |
| 27 | +import org.apache.log4j.Logger; |
| 28 | +import org.testng.Assert; |
| 29 | +import org.testng.AssertJUnit; |
| 30 | +import org.testng.annotations.BeforeMethod; |
| 31 | +import org.testng.annotations.Test; |
| 32 | + |
| 33 | +import java.util.concurrent.atomic.AtomicInteger; |
| 34 | + |
| 35 | +public class StartStopTestCase { |
| 36 | + private static final Logger log = Logger.getLogger(StartStopTestCase.class); |
| 37 | + private AtomicInteger count; |
| 38 | + private boolean eventArrived; |
| 39 | + |
| 40 | + @BeforeMethod |
| 41 | + public void init() { |
| 42 | + count = new AtomicInteger(); |
| 43 | + eventArrived = false; |
| 44 | + } |
| 45 | + |
| 46 | + @Test(expectedExceptions = InterruptedException.class) |
| 47 | + public void startStopTest1() throws InterruptedException { |
| 48 | + log.info("startStop test 1"); |
| 49 | + |
| 50 | + SiddhiManager siddhiManager = new SiddhiManager(); |
| 51 | + |
| 52 | + String siddhiApp = "" + |
| 53 | + "define stream cseEventStream (symbol string, price float, volume int);" + |
| 54 | + "define stream cseEventStream2 (symbol string, price float, volume int);" + |
| 55 | + "" + |
| 56 | + "@info(name = 'query1') " + |
| 57 | + "from cseEventStream " + |
| 58 | + "select 1 as eventFrom " + |
| 59 | + "insert into outputStream ;" + |
| 60 | + "" + |
| 61 | + "@info(name = 'query2') " + |
| 62 | + "from cseEventStream2 " + |
| 63 | + "select 2 as eventFrom " + |
| 64 | + "insert into outputStream ;"; |
| 65 | + |
| 66 | + SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp); |
| 67 | + siddhiAppRuntime.addCallback("outputStream", new StreamCallback() { |
| 68 | + |
| 69 | + @Override |
| 70 | + public void receive(Event[] events) { |
| 71 | + EventPrinter.print(events); |
| 72 | + } |
| 73 | + |
| 74 | + }); |
| 75 | + |
| 76 | + InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream2"); |
| 77 | + siddhiAppRuntime.start(); |
| 78 | + siddhiAppRuntime.shutdown(); |
| 79 | + siddhiAppRuntime.start(); |
| 80 | + inputHandler.send(new Object[]{"WSO2", 55.6f, 100}); |
| 81 | + siddhiAppRuntime.shutdown(); |
| 82 | + } |
| 83 | + |
| 84 | + @Test() |
| 85 | + public void startStopTest2() throws InterruptedException { |
| 86 | + log.info("startStop test 2"); |
| 87 | + |
| 88 | + SiddhiManager siddhiManager = new SiddhiManager(); |
| 89 | + |
| 90 | + String siddhiApp = "" + |
| 91 | + "define stream cseEventStream (symbol string, price float, volume int);" + |
| 92 | + "define stream cseEventStream2 (symbol string, price float, volume int);" + |
| 93 | + "" + |
| 94 | + "@info(name = 'query1') " + |
| 95 | + "from cseEventStream " + |
| 96 | + "select 1 as eventFrom " + |
| 97 | + "insert into outputStream ;" + |
| 98 | + "" + |
| 99 | + "@info(name = 'query2') " + |
| 100 | + "from cseEventStream2 " + |
| 101 | + "select 2 as eventFrom " + |
| 102 | + "insert into outputStream ;"; |
| 103 | + |
| 104 | + SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp); |
| 105 | + siddhiAppRuntime.addCallback("outputStream", new StreamCallback() { |
| 106 | + |
| 107 | + @Override |
| 108 | + public void receive(Event[] events) { |
| 109 | + EventPrinter.print(events); |
| 110 | + Assert.assertEquals(events[0].getData(0), 1); |
| 111 | + eventArrived = true; |
| 112 | + count.incrementAndGet(); |
| 113 | + } |
| 114 | + |
| 115 | + }); |
| 116 | + |
| 117 | + InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream2"); |
| 118 | + inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream"); |
| 119 | + siddhiAppRuntime.start(); |
| 120 | + siddhiAppRuntime.shutdown(); |
| 121 | + siddhiAppRuntime.start(); |
| 122 | + inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream"); |
| 123 | + inputHandler.send(new Object[]{"WSO2", 55.6f, 100}); |
| 124 | + siddhiAppRuntime.shutdown(); |
| 125 | + AssertJUnit.assertTrue(eventArrived); |
| 126 | + AssertJUnit.assertEquals(1, count.get()); |
| 127 | + } |
| 128 | + |
| 129 | +} |
0 commit comments