001: //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/test/junit/org/deegree/framework/concurrent/ExecutorTest.java $
002: /*---------------- FILE HEADER ------------------------------------------
003:
004: This file is part of deegree.
005: Copyright (C) 2001-2008 by:
006: EXSE, Department of Geography, University of Bonn
007: http://www.giub.uni-bonn.de/deegree/
008: lat/lon GmbH
009: http://www.lat-lon.de
010:
011: This library is free software; you can redistribute it and/or
012: modify it under the terms of the GNU Lesser General Public
013: License as published by the Free Software Foundation; either
014: version 2.1 of the License, or (at your option) any later version.
015:
016: This library is distributed in the hope that it will be useful,
017: but WITHOUT ANY WARRANTY; without even the implied warranty of
018: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: Lesser General Public License for more details.
020:
021: You should have received a copy of the GNU Lesser General Public
022: License along with this library; if not, write to the Free Software
023: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024:
025: Contact:
026:
027: Andreas Poth
028: lat/lon GmbH
029: Aennchenstr. 19
030: 53177 Bonn
031: Germany
032: E-Mail: poth@lat-lon.de
033:
034: Prof. Dr. Klaus Greve
035: Department of Geography
036: University of Bonn
037: Meckenheimer Allee 166
038: 53115 Bonn
039: Germany
040: E-Mail: greve@giub.uni-bonn.de
041:
042: ---------------------------------------------------------------------------*/
043: package org.deegree.framework.concurrent;
044:
045: import java.util.ArrayList;
046: import java.util.List;
047: import java.util.concurrent.Callable;
048: import java.util.concurrent.CancellationException;
049:
050: import junit.framework.TestCase;
051:
052: /**
053: *
054: *
055: *
056: * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
057: * @author last edited by: $Author: apoth $
058: *
059: * @version August 2nd 2006
060: */
061: public class ExecutorTest extends TestCase implements
062: ExecutionFinishedListener, Callable<Object> {
063:
064: public void testSynchronousCallsWithTimeout()
065: throws InterruptedException {
066: int numberOfThreads = 100;
067: long threadTimeout = 1000;
068: System.out.println("Testing synchrous call of "
069: + numberOfThreads + " threads with a timeout of "
070: + threadTimeout + " miliseconds");
071:
072: ArrayList<Callable<Object>> mylist = new ArrayList<Callable<Object>>();
073: for (int i = 0; i < numberOfThreads; ++i) {
074: mylist.add(this );
075: }
076:
077: List<ExecutionFinishedEvent<Object>> results = Executor
078: .getInstance().performSynchronously(mylist,
079: threadTimeout);
080:
081: for (ExecutionFinishedEvent obj : results) {
082: try {
083: System.out.println(obj.getResult());
084: } catch (CancellationException e) {
085: // TODO Auto-generated catch block
086: e.printStackTrace();
087: } catch (Throwable e) {
088: // TODO Auto-generated catch block
089: e.printStackTrace();
090: }
091: }
092: }
093:
094: public void testSynchronousCallsWithoutTimeout()
095: throws InterruptedException {
096: int numberOfThreads = 100;
097: System.out.println("Testing synchrous call of "
098: + numberOfThreads + " threads.");
099:
100: ArrayList<Callable<Object>> mylist = new ArrayList<Callable<Object>>();
101: for (int i = 0; i < numberOfThreads; ++i) {
102: mylist.add(this );
103: }
104:
105: List<ExecutionFinishedEvent<Object>> results = Executor
106: .getInstance().performSynchronously(mylist);
107:
108: for (ExecutionFinishedEvent obj : results) {
109: try {
110: System.out.println(obj.getResult());
111: } catch (CancellationException e) {
112: // TODO Auto-generated catch block
113: e.printStackTrace();
114: } catch (Throwable e) {
115: // TODO Auto-generated catch block
116: e.printStackTrace();
117: }
118: }
119:
120: }
121:
122: public void testSynchronousCallWithTimeout() throws Throwable {
123: long threadTimeout = 1000;
124: System.out
125: .println("Testing synchrous call with one Thread and a timeout of "
126: + threadTimeout + " milliseconds.");
127:
128: Executor.getInstance()
129: .performSynchronously(this , threadTimeout);
130: }
131:
132: public void testAsynchronousWithListeners() throws Exception {
133: System.out
134: .println("Testing asynchrous call with one Thread and failed and finished listener.");
135: // Callable, ExecutionFinishedListener, ExecutionFailedListener
136: Executor.getInstance().performAsynchronously(this , this );
137: // The Junit Testsuit does a System.exit() therefor we must wait for the
138: // Tasks to be executes.
139: Thread.sleep(2000);
140: }
141:
142: /**
143: * Is called if the execution of the tasks (in form of a Callable Object) finished.
144: *
145: * @param finishedEvent
146: */
147: public void executionFinished(ExecutionFinishedEvent finishedEvent) {
148: System.out.println("in Listener executionFinished");
149: Object resultValue;
150: try {
151: resultValue = finishedEvent.getResult();
152: assertNull(resultValue);
153: } catch (Throwable t) {
154: System.out.println("in Listener executionFailed: " + t);
155: }
156: }
157:
158: /**
159: * Test "Task" to be called
160: *
161: * @return a String
162: */
163: public Object call() throws Exception {
164: System.out.println("Callthread->" + Thread.currentThread());
165: Thread.sleep(950);
166:
167: return "The answer to a call";
168: }
169: }
|