001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU General
007: * Public License Version 2 only ("GPL") or the Common Development and Distribution
008: * License("CDDL") (collectively, the "License"). You may not use this file except in
009: * compliance with the License. You can obtain a copy of the License at
010: * http://www.netbeans.org/cddl-gplv2.html or nbbuild/licenses/CDDL-GPL-2-CP. See the
011: * License for the specific language governing permissions and limitations under the
012: * License. When distributing the software, include this License Header Notice in
013: * each file and include the License file at nbbuild/licenses/CDDL-GPL-2-CP. Sun
014: * designates this particular file as subject to the "Classpath" exception as
015: * provided by Sun in the GPL Version 2 section of the License file that
016: * accompanied this code. If applicable, add the following below the License Header,
017: * with the fields enclosed by brackets [] replaced by your own identifying
018: * information: "Portions Copyrighted [year] [name of copyright owner]"
019: *
020: * Contributor(s):
021: *
022: * The Original Software is NetBeans. The Initial Developer of the Original Software
023: * is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun Microsystems, Inc. All
024: * Rights Reserved.
025: *
026: * If you wish your version of this file to be governed by only the CDDL or only the
027: * GPL Version 2, indicate your decision by adding "[Contributor] elects to include
028: * this software in this distribution under the [CDDL or GPL Version 2] license." If
029: * you do not indicate a single choice of license, a recipient has the option to
030: * distribute your version of this file under either the CDDL, the GPL Version 2 or
031: * to extend the choice of license to its licensees as provided above. However, if
032: * you add GPL Version 2 code and therefore, elected the GPL Version 2 license, then
033: * the option applies only if the new code is made subject to such option by the
034: * copyright holder.
035: */
036:
037: package org.dispatcher;
038:
039: import org.MyTestCase;
040: import org.netbeans.installer.downloader.dispatcher.Process;
041: import org.netbeans.installer.downloader.dispatcher.ProcessDispatcher;
042: import org.netbeans.installer.downloader.dispatcher.impl.RoundRobinDispatcher;
043:
044: /**
045: *
046: * @author Danila_Dugurov
047: */
048:
049: /**
050: * be aware of that if time quantum or sleep time change - failes may occur.
051: * it's all ok becouse asynchronious dispatcher wroking.
052: */
053: public class DispatcherTest extends MyTestCase {
054:
055: public void testRunStop() {
056: final ProcessDispatcher dispatcher = new RoundRobinDispatcher(
057: 500, 1);
058: assertFalse(dispatcher.isActive());
059: dispatcher.start();
060: assertTrue(dispatcher.isActive());
061: dispatcher.stop();
062: assertFalse(dispatcher.isActive());
063:
064: assertFalse(dispatcher.isActive());
065: dispatcher.start();
066: assertTrue(dispatcher.isActive());
067: dispatcher.stop();
068: assertFalse(dispatcher.isActive());
069: }
070:
071: public void testSingleProcessAddAndTerminate() {
072: final ProcessDispatcher dispatcher = new RoundRobinDispatcher(
073: 50, 1);
074: final DummyProcess dummy = new DummyProcess();
075: dispatcher.schedule(dummy);
076: shortSleep();
077: assertEquals(0, dispatcher.activeCount());
078: assertEquals(1, dispatcher.waitingCount());
079: assertFalse(dummy.isProcessed());
080: dispatcher.start();
081: shortSleep();
082: assertTrue(dummy.isProcessed());
083: assertEquals(1, dispatcher.activeCount());
084: assertEquals(0, dispatcher.waitingCount());
085: dummy.terminate();
086: shortSleep();
087: assertFalse(dummy.isProcessed());
088: assertEquals(0, dispatcher.activeCount());
089: assertEquals(0, dispatcher.waitingCount());
090: dispatcher.stop();
091: }
092:
093: public void testSingleProcessAddAndDispatcherTerminate() {
094: final ProcessDispatcher dispatcher = new RoundRobinDispatcher(
095: 50, 1);
096: final DummyProcess dummy = new DummyProcess();
097: dispatcher.schedule(dummy);
098: dispatcher.start();
099: shortSleep();
100: assertTrue(dummy.isProcessed());
101: assertEquals(1, dispatcher.activeCount());
102: assertEquals(0, dispatcher.waitingCount());
103: dispatcher.stop();
104: assertFalse(dummy.isProcessed());
105: assertEquals(0, dispatcher.activeCount());
106: assertEquals(0, dispatcher.waitingCount());
107: }
108:
109: public void testReuseThreadWorker() {
110: final ProcessDispatcher dispatcher = new RoundRobinDispatcher(
111: 50, 1);
112: final DummyProcess dummy = new DummyProcess();
113: final DummyProcess dummy2 = new DummyProcess();
114: dispatcher.schedule(dummy);
115: dispatcher.start();
116: shortSleep();
117: dummy.terminate();
118: shortSleep();
119: assertEquals(0, dispatcher.activeCount());
120: assertEquals(0, dispatcher.waitingCount());
121: assertFalse(dummy.isProcessed());
122: Thread worker = dummy.getWorker();
123: assertTrue(worker.isAlive());
124: assertTrue(dispatcher.isActive());
125: assertTrue(dispatcher.schedule(dummy2));
126: assertEquals(1, dispatcher.waitingCount()
127: + dispatcher.activeCount());
128: shortSleep();
129: assertEquals(1, dispatcher.activeCount());
130: assertTrue(dummy2.isProcessed());
131: assertEquals(worker, dummy2.getWorker());//key line in this test
132: dispatcher.stop();
133: assertFalse(dummy2.isProcessed());
134: }
135:
136: public void testTwoProcessWhenPoolOnlyOne() {
137: final ProcessDispatcher dispatcher = new RoundRobinDispatcher(
138: 50, 1);
139: final DummyProcess dummy = new DummyProcess();
140: final DummyProcess dummy2 = new DummyProcess();
141: dispatcher.start();
142: dispatcher.schedule(dummy);
143: dispatcher.schedule(dummy2);
144: shortSleep();
145: assertTrue(dummy.isProcessed());
146: assertFalse(dummy2.isProcessed());
147: assertEquals(1, dispatcher.activeCount());
148: assertEquals(1, dispatcher.waitingCount());
149: final Thread worker = dummy.getWorker();
150: dummy.terminate();
151: shortSleep();
152: assertFalse(dummy.isProcessed());
153: assertTrue(dummy2.isProcessed());
154: assertEquals(1, dispatcher.activeCount());
155: assertEquals(0, dispatcher.waitingCount());
156: assertEquals(worker, dummy2.getWorker());
157: dispatcher.stop();
158: assertEquals(0, dispatcher.activeCount());
159: assertEquals(0, dispatcher.waitingCount());
160: }
161:
162: public void testTwicetheSameProcess() {
163: final ProcessDispatcher dispatcher = new RoundRobinDispatcher(
164: 50, 1);
165: final DummyProcess dummy = new DummyProcess();
166: dispatcher.schedule(dummy);
167: dispatcher.start();
168: shortSleep();
169: assertTrue(dummy.isProcessed());
170: dummy.terminate();
171: shortSleep();
172: assertTrue(dispatcher.schedule(dummy));
173: assertEquals(1, dispatcher.activeCount()
174: + dispatcher.waitingCount());
175: dispatcher.stop();
176: assertEquals(0, dispatcher.activeCount());
177: assertEquals(0, dispatcher.waitingCount());
178: assertTrue(dispatcher.schedule(dummy));
179: }
180:
181: public void testTerminateByDispatcher() {
182: final ProcessDispatcher dispatcher = new RoundRobinDispatcher(
183: 50, 1);
184: final DummyProcess goodDummy = new DummyProcess();
185: final DummyProcess badDummy = new DummyProcess() {
186: public void run() {
187: while (true) {
188: try {
189: Thread.sleep(1000);
190: } catch (InterruptedException ex) {
191: //no reaction - I'm very very bad dummy
192: }
193: }
194: }
195:
196: public void terminate() {
197: }
198: };
199: dispatcher.schedule(goodDummy);
200: dispatcher.start();
201: shortSleep();
202: assertTrue(goodDummy.isProcessed());
203: final Thread worker = goodDummy.getWorker();
204: dispatcher.terminate(goodDummy);
205: dispatcher.schedule(badDummy);
206: shortSleep();
207: assertTrue(badDummy.isProcessed());
208: assertEquals(worker, badDummy.getWorker());
209: dispatcher.terminate(badDummy);
210: assertEquals(0, dispatcher.activeCount());
211: assertEquals(0, dispatcher.waitingCount());
212: dispatcher.schedule(goodDummy);
213: assertEquals(1, dispatcher.activeCount()
214: + dispatcher.waitingCount());
215: shortSleep();
216: assertEquals(1, dispatcher.activeCount());
217: assertEquals(0, dispatcher.waitingCount());
218: assertTrue(goodDummy.isProcessed());
219: assertNotSame(worker, goodDummy.getWorker());
220: dispatcher.stop();
221: }
222:
223: public void testWorkability() {
224: final ProcessDispatcher dispatcher = new RoundRobinDispatcher(
225: 50, 10);
226: final DummyProcess[] dummies = new DummyProcess[15];
227: for (int i = 0; i < 15; i++) {
228: dummies[i] = new DummyProcess(i + 1);
229: }
230: for (int i = 0; i < 5; i++) {
231: dispatcher.schedule(dummies[i]);
232: }
233: assertEquals(5, dispatcher.waitingCount());
234: dispatcher.start();
235: shortSleep();
236: assertEquals(5, dispatcher.activeCount());
237: for (int i = 5; i < 10; i++) {
238: dispatcher.schedule(dummies[i]);
239: shortSleep();
240: assertEquals(i + 1, dispatcher.activeCount());
241: assertEquals(0, dispatcher.waitingCount());
242: }
243: longSleep();
244: for (int i = 0; i < 10; i++) {
245: assertTrue(dummies[i].isProcessed());
246: }
247: dispatcher.schedule(dummies[11]);
248: dispatcher.schedule(dummies[10]);
249: assertEquals(10, dispatcher.activeCount());
250: assertEquals(2, dispatcher.waitingCount());
251: dummies[5].terminate();
252: //dispatcher.terminate(dummies[5]);
253: longSleep();
254: longSleep();
255: assertEquals(10, dispatcher.activeCount());
256: assertEquals(1, dispatcher.waitingCount());
257: dispatcher.schedule(dummies[12]);
258: dispatcher.schedule(dummies[13]);
259: dispatcher.schedule(dummies[14]);
260:
261: dispatcher.terminate(dummies[0]);
262: dispatcher.terminate(dummies[1]);
263: dispatcher.terminate(dummies[2]);
264: dispatcher.terminate(dummies[3]);
265: // dummies[0].terminate();
266: // dummies[1].terminate();
267: // dummies[2].terminate();
268: // dummies[3].terminate();
269: longSleep();
270: assertEquals(10, dispatcher.activeCount());
271: assertEquals(0, dispatcher.waitingCount());
272: for (int i = 6; i < 15; i++) {
273: assertTrue(dummies[i].isProcessed());
274: }
275: dispatcher.stop();
276: assertEquals(0, dispatcher.activeCount());
277: assertEquals(0, dispatcher.waitingCount());
278: for (int i = 0; i < 15; i++) {
279: assertFalse(dummies[i].isProcessed());
280: }
281: dispatcher.stop();
282: }
283:
284: private void longSleep() {
285: try {
286: Thread.sleep(500);
287: } catch (InterruptedException ex) {//skip
288: }
289: }
290:
291: private void shortSleep() {
292: try {
293: Thread.sleep(100);
294: } catch (InterruptedException ex) {//skip
295: }
296: }
297: }
|