001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package com.sun.midp.links;
028:
029: import com.sun.cldc.isolate.Isolate;
030: import com.sun.midp.i3test.TestCase;
031: import java.io.IOException;
032: import java.io.InterruptedIOException;
033: import java.util.Enumeration;
034: import java.util.Hashtable;
035: import java.util.Random;
036:
037: /**
038: * Tests multiple receiving or sending threads blocked on the same link.
039: */
040: public class TestMultiple extends TestCase {
041:
042: /**
043: * The number of senders or receivers.
044: */
045: public static final int NUM_THREADS = 10;
046:
047: /**
048: * Amount of time to wait for threads to complete after sending or
049: * receiving a message.
050: */
051: public static final long TIMEOUT = 100L;
052:
053: Random rand = new Random();
054: Object lock = new Object();
055:
056: Hashtable doneset; // Set<Thread>
057: Hashtable messages; // Map<Thread, LinkMessage>
058: Hashtable throwables; // Map<Thread, Throwable>
059:
060: /**
061: * Common code for completion callbacks for LoggingReceiver and
062: * LoggingSender. Adds the thread to the done set; if non-null, adds msg
063: * to the map of received messages; if non-null, adds any throwables
064: * caught to the map of caught throwables.
065: */
066: public void completed(Thread thr, LinkMessage msg, Throwable exc) {
067: synchronized (lock) {
068: assertFalse("shouldn't be done already", doneset
069: .containsKey(thr));
070: doneset.put(thr, thr);
071:
072: if (msg != null) {
073: messages.put(thr, msg);
074: }
075:
076: if (exc != null) {
077: throwables.put(thr, exc);
078: }
079: }
080: }
081:
082: /**
083: * Adds a completion callback to a Receiver thread.
084: */
085: class LoggingReceiver extends Receiver {
086: public LoggingReceiver(Link newlink) {
087: super (newlink);
088: }
089:
090: public void completed(LinkMessage msg, Throwable exc) {
091: TestMultiple.this .completed(this , msg, exc);
092: }
093: }
094:
095: /**
096: * Adds a completion callback to a Sender thread.
097: */
098: class LoggingSender extends Sender {
099: public LoggingSender(Link newlink, LinkMessage lm) {
100: super (newlink, lm);
101: }
102:
103: public void completed(LinkMessage msg, Throwable exc) {
104: TestMultiple.this .completed(this , msg, exc);
105: }
106: }
107:
108: /**
109: * Tests multiple receivers blocked on the same link. Creates NUM_THREADS
110: * threads to receive messages. Then, sends messages one by one, and
111: * ensures that each time exactly one thread is unblocked and has received
112: * the message just sent. Finally, after sending enough messages, ensures
113: * that all threads have been accounted for.
114: */
115: void testReceivers() throws IOException {
116: Isolate is = Isolate.currentIsolate();
117: Link link = Link.newLink(is, is);
118: Receiver ra[] = new LoggingReceiver[NUM_THREADS];
119:
120: doneset = new Hashtable(NUM_THREADS);
121: messages = new Hashtable(NUM_THREADS);
122: throwables = new Hashtable(NUM_THREADS);
123:
124: for (int i = 0; i < NUM_THREADS; i++) {
125: ra[i] = new LoggingReceiver(link);
126: }
127: Utils.sleep(2 * TIMEOUT);
128:
129: for (int i = 0; i < NUM_THREADS; i++) {
130: String s = Integer.toString(rand.nextInt());
131:
132: messages.clear();
133: throwables.clear();
134: link.send(LinkMessage.newStringMessage(s));
135: Utils.sleep(TIMEOUT);
136:
137: assertEquals("iteration " + i, i + 1, doneset.size());
138:
139: assertEquals("one message", 1, messages.size());
140: for (Enumeration e = messages.elements(); e
141: .hasMoreElements();) {
142: LinkMessage msg = (LinkMessage) e.nextElement();
143: assertTrue("message should contain a string", msg
144: .containsString());
145: String rs = msg.extractString();
146: assertEquals("strings should be equal", s, rs);
147: }
148:
149: assertEquals("zero throwables", 0, throwables.size());
150: for (Enumeration e = throwables.elements(); e
151: .hasMoreElements();) {
152: System.out.println("### " + e.nextElement());
153: }
154: }
155:
156: for (int i = 0; i < NUM_THREADS; i++) {
157: assertTrue("should be done", doneset.containsKey(ra[i]));
158: doneset.remove(ra[i]);
159: }
160: }
161:
162: /**
163: * Tests multiple senders blocked on the same link. Creates NUM_THREADS
164: * threads to send messages. Then, receives messages one by one, and
165: * ensures that each time exactly one thread is unblocked and had sent the
166: * message just received. Finally, after receiving enough messages,
167: * ensures that all threads have been accounted for.
168: */
169: void testSenders() throws IOException {
170: Isolate is = Isolate.currentIsolate();
171: Link link = Link.newLink(is, is);
172: LoggingSender sa[] = new LoggingSender[NUM_THREADS];
173:
174: for (int i = 0; i < NUM_THREADS; i++) {
175: String s = Integer.toString(rand.nextInt());
176: sa[i] = new LoggingSender(link, LinkMessage
177: .newStringMessage(s));
178: }
179: Utils.sleep(2 * TIMEOUT);
180:
181: doneset = new Hashtable(NUM_THREADS);
182: throwables = new Hashtable(NUM_THREADS);
183:
184: for (int i = 0; i < NUM_THREADS; i++) {
185: LinkMessage rm;
186: String rs;
187:
188: messages.clear();
189: throwables.clear();
190:
191: rm = link.receive();
192: Utils.sleep(TIMEOUT);
193:
194: assertEquals("iteration " + i, i + 1, doneset.size());
195:
196: assertTrue("message should contain a string", rm
197: .containsString());
198: rs = rm.extractString();
199:
200: assertEquals("one message", 1, messages.size());
201: for (Enumeration e = messages.elements(); e
202: .hasMoreElements();) {
203: LinkMessage msg = (LinkMessage) e.nextElement();
204: String ss = msg.extractString();
205: assertEquals("strings should be equal", ss, rs);
206: }
207:
208: assertEquals("zero throwables", 0, throwables.size());
209: for (Enumeration e = throwables.elements(); e
210: .hasMoreElements();) {
211: System.out.println("### " + e.nextElement());
212: }
213: }
214:
215: for (int i = 0; i < NUM_THREADS; i++) {
216: assertTrue("should be done", doneset.containsKey(sa[i]));
217: doneset.remove(sa[i]);
218: }
219: }
220:
221: /**
222: * Runs all tests.
223: */
224: public void runTests() throws IOException {
225: declare("testReceivers");
226: testReceivers();
227:
228: declare("testSenders");
229: testSenders();
230: }
231: }
|