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.LinkedQueue;
029: import org.jicarilla.plumbing.SimpleCollector;
030: import org.jicarilla.plumbing.SimpleSource;
031: import org.jicarilla.plumbing.SimpleCollector;
032: import org.jicarilla.plumbing.SimpleSource;
033: import junit.framework.TestCase;
034:
035: import java.util.List;
036:
037: /**
038: * <a href="http://www.junit.org/">JUnit</a> {@link TestCase testcase} for
039: * SimpleCollector.
040: *
041: * @todo this test just deadlocked on me. Figure out what's going on.
042: * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
043: * @version $Id: SimpleCollectorTestCase.java,v 1.1 2003/11/18 16:09:16 lsimons
044: * Exp $
045: */
046: public class SimpleCollectorTestCase extends TestCase {
047: public void testAdd() {
048: Collector c = new Collector();
049:
050: LinkedQueue l = new LinkedQueue();
051: SimpleSource s = new SimpleSource(l);
052:
053: c.addSource(s);
054:
055: assertEquals(c.getSources().get(0), s);
056: assertEquals(c.getSources().size(), 1);
057: assertEquals(c.getSources().size(), c.getSize());
058:
059: LinkedQueue l2 = new LinkedQueue();
060: SimpleSource s2 = new SimpleSource(l2);
061:
062: c.addSource(s2);
063:
064: Throwable t = null;
065: try {
066: c.addSource(null);
067: } catch (AssertionError ae) {
068: t = ae;
069: }
070: assertNotNull(t);
071: }
072:
073: boolean running = true;
074:
075: public void testConcurrentAdd() throws InterruptedException {
076: final Collector c = new Collector();
077:
078: Thread[] threads = new Thread[5];
079: for (int i = 0; i < 5; i++) {
080: threads[i] = new Thread(new Runnable() {
081:
082: public void run() {
083: while (running) {
084: try {
085: Thread.sleep(100);
086: } catch (InterruptedException e1) {
087: break;
088: }
089: LinkedQueue l = new LinkedQueue();
090: SimpleSource s = new SimpleSource(l);
091:
092: c.addSource(s);
093: Thread.yield();
094: }
095: }
096: });
097: }
098: for (int i = 0; i < 5; i++) {
099: threads[i].start();
100: }
101:
102: Thread.sleep(1000);
103: running = false;
104:
105: for (int i = 0; i < 5; i++) {
106: threads[i].interrupt();
107: }
108: }
109:
110: public void testTake() throws InterruptedException {
111: Collector c = new Collector();
112:
113: LinkedQueue l = new Q();
114: SimpleSource s = new SimpleSource(l);
115: c.addSource(s);
116:
117: Object o1 = new Object();
118: Object o2 = new Object();
119: Object o3 = new Object();
120: Object o4 = new Object();
121: l.put(o1);
122: l.put(o2);
123: l.put(o3);
124: l.put(o4);
125:
126: assertEquals(o1, c.take());
127: assertEquals(o2, c.take());
128: assertEquals(o3, c.take());
129: assertEquals(o4, c.take());
130:
131: LinkedQueue l2 = new LinkedQueue();
132: SimpleSource s2 = new SimpleSource(l2);
133: c.addSource(s2);
134:
135: l2.put(o3);
136: assertEquals(o3, c.take());
137:
138: l.put(null);
139: l2.put(o1);
140: assertEquals(o1, c.take());
141:
142: }
143:
144: boolean interrupted = false;
145:
146: public void testBlockOnTake() throws InterruptedException {
147: final Collector c = new Collector();
148:
149: final LinkedQueue l = new LinkedQueue();
150: final SimpleSource s = new SimpleSource(l);
151: c.addSource(s);
152: c.addSource(s);
153: c.addSource(s);
154: c.addSource(s);
155: c.addSource(s);
156: c.addSource(s);
157: c.addSource(s);
158: c.addSource(s);
159: c.addSource(s);
160: c.addSource(s);
161: c.addSource(s);
162: c.addSource(s);
163: c.addSource(s);
164:
165: Thread thread = new Thread(new Runnable() {
166: public void run() {
167: try {
168: c.take(); // should block, then be interrupted
169: } catch (InterruptedException ie) {
170: interrupted = true;
171: }
172: }
173: });
174: thread.start();
175:
176: Thread.sleep(1000);
177: thread.interrupt();
178: Thread.sleep(1000);
179:
180: assertTrue(interrupted);
181: }
182:
183: public static class Q extends LinkedQueue {
184: public void put(Object o) throws InterruptedException {
185: if (o == null) {
186: super .insert(o);
187: } else {
188: super .put(o);
189: }
190: }
191: }
192:
193: public void testPoll() throws InterruptedException {
194: Collector c = new Collector();
195:
196: LinkedQueue l = new LinkedQueue();
197: SimpleSource s = new SimpleSource(l);
198: c.addSource(s);
199:
200: Object o1 = new Object();
201: Object o2 = new Object();
202: Object o3 = new Object();
203: Object o4 = new Object();
204: l.put(o1);
205: l.put(o2);
206: l.put(o3);
207: l.put(o4);
208:
209: assertEquals(o1, c.poll(10));
210: assertEquals(o2, c.poll(20));
211: assertEquals(o3, c.poll(100));
212: assertEquals(o4, c.poll(50));
213:
214: assertNull(c.poll(100));
215: }
216:
217: public static class Collector extends SimpleCollector {
218: public List getSources() {
219: return m_sources;
220: }
221:
222: public int getSize() {
223: return m_sources.size();
224: }
225: }
226: }
|