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 org.jicarilla.plumbing.NoopSink;
029: import org.jicarilla.plumbing.SimpleAlternator;
030: import org.jicarilla.plumbing.Sink;
031: import org.jicarilla.plumbing.Sink;
032: import org.jicarilla.plumbing.NoopSink;
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: * SimpleAlternator.
040: *
041: * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
042: * @version $Id: SimpleAlternatorTestCase.java,v 1.1 2003/11/18 16:09:16
043: * lsimons Exp $
044: */
045: public class SimpleAlternatorTestCase extends TestCase {
046: public static class CountingSink implements Sink {
047: public int put = 0;
048: public int offered = 0;
049: public static int sput = 0;
050: public static int soffered = 0;
051:
052: public void put(Object o) throws InterruptedException {
053: put++;
054: sput++;
055: }
056:
057: public boolean offer(Object o, long l)
058: throws InterruptedException {
059: offered++;
060: soffered++;
061: return true;
062: }
063: }
064:
065: public void testConstructor() {
066: new SimpleAlternator();
067:
068: assertEquals(0, new Alternator().getSinks().size());
069: assertEquals(0, new Alternator().getSize());
070: }
071:
072: public void testAdd() {
073: Alternator a = new Alternator();
074:
075: NoopSink s = new NoopSink();
076: a.addSink(s);
077:
078: assertEquals(a.getSinks().get(0), s);
079: assertEquals(a.getSinks().get(Alternator.DEFAULT_PRIORITY - 1),
080: s);
081:
082: NoopSink s2 = new NoopSink();
083: a.addSink(s2, 2);
084: assertEquals(a.getSinks().get(Alternator.DEFAULT_PRIORITY), s2);
085: assertEquals(a.getSinks().get(Alternator.DEFAULT_PRIORITY + 1),
086: s2);
087:
088: assertEquals(a.getSinks().size(),
089: Alternator.DEFAULT_PRIORITY + 2);
090: assertEquals(a.getSinks().size(), a.getSize());
091:
092: Throwable t = null;
093: try {
094: a.addSink(null);
095: } catch (AssertionError ae) {
096: t = ae;
097: }
098: assertNotNull(t);
099:
100: t = null;
101: try {
102: a.addSink(null, 10000);
103: } catch (AssertionError ae) {
104: t = ae;
105: }
106: assertNotNull(t);
107:
108: t = null;
109: try {
110: a.addSink(null, 0);
111: } catch (AssertionError ae) {
112: t = ae;
113: }
114: assertNotNull(t);
115: }
116:
117: boolean running = true;
118:
119: public void testConcurrentAdd() throws InterruptedException {
120: final Alternator a = new Alternator();
121:
122: Thread[] threads = new Thread[5];
123: for (int i = 0; i < 5; i++) {
124: threads[i] = new Thread(new Runnable() {
125:
126: public void run() {
127: while (running) {
128: try {
129: Thread.sleep(100);
130: } catch (InterruptedException e1) {
131: break;
132: }
133: NoopSink s = new NoopSink();
134: a.addSink(s, 3);
135: Thread.yield();
136: }
137: }
138: });
139: }
140: for (int i = 0; i < 5; i++) {
141: threads[i].start();
142: }
143:
144: Thread.sleep(1000);
145: running = false;
146:
147: for (int i = 0; i < 5; i++) {
148: threads[i].interrupt();
149: }
150: }
151:
152: public void testPut() throws InterruptedException {
153: Alternator a = new Alternator();
154:
155: CountingSink s1 = new CountingSink();
156: CountingSink s2 = new CountingSink();
157: CountingSink s3 = new CountingSink();
158:
159: a.addSink(s1);
160: a.addSink(s2);
161: a.addSink(s3);
162:
163: a.put(new Object());
164: a.put(new Object());
165: a.put(new Object());
166: a.put(new Object());
167:
168: assertEquals(4, CountingSink.sput);
169: assertEquals(4, s1.put + s2.put + s3.put);
170: }
171:
172: public void testOffer() throws InterruptedException {
173: Alternator a = new Alternator();
174:
175: CountingSink s1 = new CountingSink();
176: CountingSink s2 = new CountingSink();
177: CountingSink s3 = new CountingSink();
178:
179: a.addSink(s1);
180: a.addSink(s2);
181: a.addSink(s3);
182:
183: a.offer(new Object(), 10);
184: a.offer(new Object(), 10);
185: a.offer(new Object(), 10);
186:
187: assertEquals(3, CountingSink.soffered);
188: assertEquals(3, s1.offered + s2.offered + s3.offered);
189: }
190:
191: public static class Alternator extends SimpleAlternator {
192: public List getSinks() {
193: return m_sinks;
194: }
195:
196: public int getSize() {
197: return m_sinks.size();
198: }
199: }
200:
201: }
|