/* * ************************************************************************************* * Copyright (C) 2008 EsperTech, Inc. All rights reserved. * * http://esper.codehaus.org * * http://www.espertech.com * * ---------------------------------------------------------------------------------- * * The software in this package is published under the terms of the GPL license * * a copy of which has been included with this distribution in the license.txt file. * * ************************************************************************************* */ package com.espertech.esper.multithread; import junit.framework.TestCase; import com.espertech.esper.client.EPServiceProvider; import com.espertech.esper.client.Configuration; import com.espertech.esper.client.EPServiceProviderManager; import com.espertech.esper.client.time.TimerControlEvent; import com.espertech.esper.support.client.SupportConfigFactory; import com.espertech.esper.support.bean.SupportBean; import com.espertech.esper.support.bean.SupportMarketDataBean; import java.util.concurrent.*; /** * Test for multithread-safety of named windows and fire-and-forget queries. */ public class TestMTStmtNamedWindowFAF extends TestCase { private EPServiceProvider engine; public void setUp() { Configuration configuration = SupportConfigFactory.getConfiguration(); engine = EPServiceProviderManager.getDefaultProvider(configuration); engine.initialize(); engine.getEPAdministrator().createEPL( "create window MyWindow.win:keepall() as select theString, longPrimitive from " + SupportBean.class.getName()); engine.getEPAdministrator().createEPL( "insert into MyWindow(theString, longPrimitive) " + " select symbol, volume \n" + " from " + SupportMarketDataBean.class.getName()); } public void testThreading() throws Exception { tryIterate(2, 500); } private void tryIterate(int numThreads, int numRepeats) throws Exception { ExecutorService threadPool = Executors.newFixedThreadPool(numThreads); Future<Boolean> future[] = new Future[numThreads]; for (int i = 0; i < numThreads; i++) { Callable callable = new StmtNamedWindowQueryCallable(Integer.toString(i), engine, numRepeats); future[i] = threadPool.submit(callable); } threadPool.shutdown(); threadPool.awaitTermination(10, TimeUnit.SECONDS); Thread.sleep(100); for (int i = 0; i < numThreads; i++) { assertTrue(future[i].get(10, TimeUnit.SECONDS)); } } }