001: /* ====================================================================
002: The Jicarilla Software License
003:
004: Copyright (c) 2003 Leo Simons.
005: All rights reserved.
006:
007: Permission is hereby granted, free of charge, to any person obtaining
008: a copy of this software and associated documentation files (the
009: "Software"), to deal in the Software without restriction, including
010: without limitation the rights to use, copy, modify, merge, publish,
011: distribute, sublicense, and/or sell copies of the Software, and to
012: permit persons to whom the Software is furnished to do so, subject to
013: the following conditions:
014:
015: The above copyright notice and this permission notice shall be
016: included in all copies or substantial portions of the Software.
017:
018: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
019: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
020: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
021: IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
022: CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
023: TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
024: SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
025: ==================================================================== */
026: package org.jicarilla.plumbing.test;
027:
028: import EDU.oswego.cs.dl.util.concurrent.Channel;
029: import EDU.oswego.cs.dl.util.concurrent.LinkedQueue;
030: import org.jicarilla.plumbing.PostProcessor;
031: import org.jicarilla.plumbing.PreProcessor;
032: import org.jicarilla.plumbing.SimpleSink;
033: import org.jicarilla.plumbing.SimpleStage;
034: import org.jicarilla.plumbing.Sink;
035: import org.jicarilla.plumbing.PostProcessor;
036: import org.jicarilla.plumbing.PreProcessor;
037: import org.jicarilla.plumbing.SimpleSink;
038: import org.jicarilla.plumbing.SimpleStage;
039: import junit.framework.TestCase;
040:
041: import java.util.List;
042:
043: /**
044: * <a href="http://www.junit.org/">JUnit</a> {@link TestCase testcase} for
045: * PreProcessor.
046: *
047: * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
048: * @version $Id: PreProcessorTestCase.java,v 1.1 2003/11/17 21:11:10 lsimons
049: * Exp $
050: */
051: public class PreProcessorTestCase extends TestCase {
052: public final static class Processor extends PreProcessor {
053: public int processed = 0;
054:
055: public Processor(Channel channel, Sink errorHandler) {
056: super (channel, errorHandler);
057: }
058:
059: public List getStages() {
060: return m_stages;
061: }
062:
063: public void process(Object o) throws InterruptedException {
064: super .process(o);
065: processed++;
066: }
067: }
068:
069: LinkedQueue c = new LinkedQueue();
070: LinkedQueue l = new LinkedQueue();
071: SimpleSink e = new SimpleSink(l);
072:
073: public void testConstructor() {
074: new PostProcessor(c, e);
075: }
076:
077: public void testAdd() {
078: Processor p = new Processor(c, e);
079: SimpleStage s = new SimpleStage(new LinkedQueue());
080: p.addStage(s);
081:
082: assertEquals(s, p.getStages().get(0));
083:
084: SimpleStage s1 = new SimpleStage(new LinkedQueue());
085: p.addStage(s1);
086: SimpleStage s2 = new SimpleStage(new LinkedQueue());
087: p.addStage(s2);
088: SimpleStage s3 = new SimpleStage(new LinkedQueue());
089: p.addStage(s3);
090: SimpleStage s4 = new SimpleStage(new LinkedQueue());
091: p.addStage(s4);
092:
093: assertEquals(s1, p.getStages().get(1));
094: assertEquals(s2, p.getStages().get(2));
095: assertEquals(s3, p.getStages().get(3));
096: assertEquals(s4, p.getStages().get(4));
097:
098: Throwable t = null;
099: try {
100: p.addStage(null);
101: } catch (AssertionError ae) {
102: t = ae;
103: }
104: assertNotNull(t);
105: }
106:
107: boolean running = true;
108:
109: public void testConcurrentAdd() throws InterruptedException {
110: final Processor p = new Processor(c, e);
111:
112: Thread[] threads = new Thread[5];
113: for (int i = 0; i < 5; i++) {
114: threads[i] = new Thread(new Runnable() {
115:
116: public void run() {
117: while (running) {
118: try {
119: Thread.sleep(100);
120: } catch (InterruptedException e1) {
121: break;
122: }
123: SimpleStage s = new SimpleStage(
124: new LinkedQueue());
125: p.addStage(s);
126: Thread.yield();
127: }
128: }
129: });
130: }
131: for (int i = 0; i < 5; i++) {
132: threads[i].start();
133: }
134:
135: Thread.sleep(1000);
136: running = false;
137:
138: for (int i = 0; i < 5; i++) {
139: threads[i].interrupt();
140: }
141: }
142:
143: public void testPut() throws InterruptedException {
144: final Processor p = new Processor(c, e);
145:
146: p.put(new Object());
147: p.put(new Object());
148: p.put(new Object());
149: p.put(new Object());
150:
151: assertEquals(4, p.processed);
152: }
153:
154: public void testOffer() throws InterruptedException {
155: final Processor p = new Processor(c, e);
156:
157: p.offer(new Object(), 100);
158: p.offer(new Object(), 100);
159: p.offer(new Object(), 100);
160: p.offer(new Object(), 100);
161:
162: assertEquals(4, p.processed);
163: }
164:
165: public final static class CountingStage extends SimpleStage {
166: public int put = 0;
167: public int taken = 0;
168:
169: public static int sput = 0;
170: public static int staken = 0;
171:
172: public CountingStage() {
173: super (new LinkedQueue());
174: }
175:
176: public void put(Object o) throws InterruptedException {
177: super .put(o);
178: put++;
179: sput++;
180: }
181:
182: public Object take() throws InterruptedException {
183: taken++;
184: staken++;
185: return super .take();
186: }
187: }
188:
189: public void testProcess() throws InterruptedException {
190: final Processor p = new Processor(c, e);
191:
192: CountingStage s1 = new CountingStage();
193: p.addStage(s1);
194: CountingStage s2 = new CountingStage();
195: p.addStage(s2);
196: CountingStage s3 = new CountingStage();
197: p.addStage(s3);
198: CountingStage s4 = new CountingStage();
199: p.addStage(s4);
200:
201: p.process(new Object());
202: p.process(new Object());
203: p.process(new Object());
204: p.process(new Object());
205:
206: assertEquals(4, s1.put);
207: assertEquals(4, s1.taken);
208: assertEquals(4, s2.put);
209: assertEquals(4, s2.taken);
210: assertEquals(4, s3.put);
211: assertEquals(4, s3.taken);
212: assertEquals(4, s4.put);
213: assertEquals(4, s4.taken);
214: assertEquals(4 * 4, CountingStage.sput);
215: assertEquals(4 * 4, CountingStage.staken);
216: }
217:
218: }
|