001: //The contents of this file are subject to the Mozilla Public License Version 1.1
002: //(the "License"); you may not use this file except in compliance with the
003: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
004: //
005: //Software distributed under the License is distributed on an "AS IS" basis,
006: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
007: //for the specific language governing rights and
008: //limitations under the License.
009: //
010: //The Original Code is "The Columba Project"
011: //
012: //The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
013: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
014: //
015: //All Rights Reserved.
016: package org.columba.core.command;
017:
018: import junit.framework.TestCase;
019:
020: import org.columba.api.command.IWorkerStatusController;
021:
022: /**
023: * @author Timo Stich (tstich@users.sourceforge.net)
024: *
025: */
026: public class DefaultProcessorTest extends TestCase {
027: private CommandProcessor processor;
028:
029: int executedID;
030:
031: int finishedID;
032:
033: /**
034: * Constructor for DefaultProcessorTest.
035: *
036: * @param arg0
037: */
038: public DefaultProcessorTest(String arg0) {
039: super (arg0);
040: }
041:
042: public void testAddOp_PriorityOrdering() {
043: processor = new CommandProcessor(false);
044:
045: TestCommand command1 = new TestCommand(this , 1);
046: command1.setPriority(Command.NORMAL_PRIORITY);
047: processor.addOp(command1);
048:
049: TestCommand command2 = new TestCommand(this , 2);
050: command2.setPriority(Command.NORMAL_PRIORITY);
051: processor.addOp(command2);
052:
053: TestCommand command3 = new TestCommand(this , 3);
054: command3.setPriority(Command.REALTIME_PRIORITY);
055: processor.addOp(command3);
056:
057: TestCommand command4 = new TestCommand(this , 4);
058: command4.setPriority(Command.DAEMON_PRIORITY);
059: processor.addOp(command4);
060:
061: TestCommand command5 = new TestCommand(this , 5);
062: command5.setPriority(Command.NORMAL_PRIORITY);
063: processor.addOp(command5);
064:
065: assertEquals(
066: 3,
067: ((TestCommand) ((OperationItem) processor.operationQueue
068: .get(0)).getOperation()).getId());
069: assertEquals(
070: 1,
071: ((TestCommand) ((OperationItem) processor.operationQueue
072: .get(1)).getOperation()).getId());
073: assertEquals(
074: 2,
075: ((TestCommand) ((OperationItem) processor.operationQueue
076: .get(2)).getOperation()).getId());
077: assertEquals(
078: 5,
079: ((TestCommand) ((OperationItem) processor.operationQueue
080: .get(3)).getOperation()).getId());
081: assertEquals(
082: 4,
083: ((TestCommand) ((OperationItem) processor.operationQueue
084: .get(4)).getOperation()).getId());
085: }
086:
087: public void testAddOp_PriorityOrderingWithSynchronized() {
088: processor = new CommandProcessor(false);
089:
090: TestCommand command1 = new TestCommand(this , 1);
091: command1.setPriority(Command.NORMAL_PRIORITY);
092: processor.addOp(command1, Command.FIRST_EXECUTION);
093:
094: TestCommand command2 = new TestCommand(this , 2);
095: command2.setPriority(Command.NORMAL_PRIORITY);
096: command2.setSynchronize(true);
097: processor.addOp(command2, Command.FIRST_EXECUTION);
098:
099: TestCommand command3 = new TestCommand(this , 3);
100: command3.setPriority(Command.REALTIME_PRIORITY);
101: processor.addOp(command3, Command.FIRST_EXECUTION);
102:
103: TestCommand command4 = new TestCommand(this , 4);
104: command4.setPriority(Command.DAEMON_PRIORITY);
105: command4.setSynchronize(true);
106: processor.addOp(command4, Command.FIRST_EXECUTION);
107:
108: TestCommand command5 = new TestCommand(this , 5);
109: command5.setPriority(Command.NORMAL_PRIORITY);
110: processor.addOp(command5, Command.FIRST_EXECUTION);
111:
112: assertEquals(
113: 1,
114: ((TestCommand) ((OperationItem) processor.operationQueue
115: .get(0)).getOperation()).getId());
116: assertEquals(
117: 2,
118: ((TestCommand) ((OperationItem) processor.operationQueue
119: .get(1)).getOperation()).getId());
120: assertEquals(
121: 3,
122: ((TestCommand) ((OperationItem) processor.operationQueue
123: .get(2)).getOperation()).getId());
124: assertEquals(
125: 4,
126: ((TestCommand) ((OperationItem) processor.operationQueue
127: .get(3)).getOperation()).getId());
128: assertEquals(
129: 5,
130: ((TestCommand) ((OperationItem) processor.operationQueue
131: .get(4)).getOperation()).getId());
132: }
133:
134: public void testReserveForRealtime() throws Exception {
135: processor = new CommandProcessor(false);
136:
137: // empty the worker list until one is left
138: while (processor.getWorker(Command.NORMAL_PRIORITY) != null)
139: ;
140:
141: assertEquals(1, processor.worker.size());
142:
143: assertTrue(processor.getWorker(Command.REALTIME_PRIORITY) != null);
144: }
145:
146: public void testRunOne() throws Exception {
147: processor = new CommandProcessor(false);
148:
149: TestCommand command1 = new TestCommand(this , 1);
150: command1.setPriority(Command.NORMAL_PRIORITY);
151: processor.addOp(command1, Command.FIRST_EXECUTION);
152:
153: TestCommand command2 = new TestCommand(this , 2);
154: command2.setPriority(Command.NORMAL_PRIORITY);
155: command2.setSynchronize(true);
156: processor.addOp(command2, Command.FIRST_EXECUTION);
157:
158: processor.startOperation();
159: assertEquals(CommandProcessor.MAX_WORKERS - 1, processor.worker
160: .size());
161:
162: Thread.sleep(1000);
163:
164: assertEquals(1, executedID);
165: assertEquals(CommandProcessor.MAX_WORKERS, processor.worker
166: .size());
167:
168: processor.startOperation();
169: assertEquals(CommandProcessor.MAX_WORKERS - 1, processor.worker
170: .size());
171:
172: Thread.sleep(1000);
173:
174: assertEquals(2, executedID);
175: assertEquals(CommandProcessor.MAX_WORKERS, processor.worker
176: .size());
177: }
178:
179: public void testRunMultiple() throws Exception {
180: processor = new CommandProcessor(false);
181:
182: TestCommand command1 = new TestCommand(this , 1);
183: command1.setPriority(Command.NORMAL_PRIORITY);
184: processor.addOp(command1, Command.FIRST_EXECUTION);
185:
186: TestCommand command2 = new TestCommand(this , 2);
187: command2.setPriority(Command.NORMAL_PRIORITY);
188: command2.setSynchronize(true);
189: processor.addOp(command2, Command.FIRST_EXECUTION);
190:
191: processor.startOperation();
192: processor.startOperation();
193: assertEquals(CommandProcessor.MAX_WORKERS - 2, processor.worker
194: .size());
195:
196: Thread.sleep(1000);
197:
198: assertEquals(2, executedID);
199: assertEquals(CommandProcessor.MAX_WORKERS, processor.worker
200: .size());
201: }
202:
203: public void testRunMax() throws Exception {
204: processor = new CommandProcessor(false);
205:
206: processor.addOp(new TestCommand(this , 1));
207: processor.addOp(new TestCommand(this , 2));
208: processor.addOp(new TestCommand(this , 3));
209: processor.addOp(new TestCommand(this , 4));
210: processor.addOp(new TestCommand(this , 5));
211:
212: processor.startOperation();
213: processor.startOperation();
214: processor.startOperation();
215: processor.startOperation();
216: processor.startOperation();
217: assertEquals(1, processor.worker.size());
218:
219: Thread.sleep(1000);
220:
221: assertEquals(1, processor.operationQueue.size());
222: assertEquals(CommandProcessor.MAX_WORKERS, processor.worker
223: .size());
224:
225: processor.startOperation();
226: assertEquals(CommandProcessor.MAX_WORKERS - 1, processor.worker
227: .size());
228: assertEquals(0, processor.operationQueue.size());
229: }
230:
231: public void testRunRealtime() throws Exception {
232: processor = new CommandProcessor(true);
233:
234: processor.addOp(new TestCommand(this , 1));
235: processor.addOp(new TestCommand(this , 2));
236: processor.addOp(new TestCommand(this , 3));
237: processor.addOp(new TestCommand(this , 4,
238: Command.REALTIME_PRIORITY));
239: processor.addOp(new TestCommand(this , 5));
240: processor.addOp(new TestCommand(this , 1));
241: processor.addOp(new TestCommand(this , 2));
242: processor.addOp(new TestCommand(this , 3));
243: processor.addOp(new TestCommand(this , 4));
244: processor.addOp(new TestCommand(this , 5,
245: Command.REALTIME_PRIORITY));
246: processor.addOp(new TestCommand(this , 1));
247: processor.addOp(new TestCommand(this , 2));
248: processor.addOp(new TestCommand(this , 3));
249: processor.addOp(new TestCommand(this , 4));
250: processor.addOp(new TestCommand(this , 5));
251:
252: Thread.sleep(3000);
253:
254: assertEquals(0, processor.operationQueue.size());
255: assertEquals(CommandProcessor.MAX_WORKERS, processor.worker
256: .size());
257: }
258:
259: }
|