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:
033: /**
034: * Tests simple exchange of a message containing a Link and also tests for
035: * proper cleanup. Covers simple cases only, with one receiver thread and one
036: * sender thread. Covers receive-then-send as well as send-then-receive.
037: */
038: public class TestLinkTransfer extends TestCase {
039:
040: /**
041: * Tests the transfer of a message containing a link, where the receiver
042: * thread blocks first.
043: */
044: void testReceiveLink() throws IOException {
045: Isolate i = Isolate.currentIsolate();
046: Link link = Link.newLink(i, i);
047: Link sentlink = Link.newLink(i, i);
048: Receiver receiver = new Receiver(link);
049:
050: link.send(LinkMessage.newLinkMessage(sentlink));
051: receiver.await();
052:
053: assertTrue("receiver should be done", receiver.done);
054: assertNull("receiver should have no exceptions",
055: receiver.exception);
056: assertNotNull("receiver should have received a message",
057: receiver.msg);
058: assertTrue("received message should contain link", receiver.msg
059: .containsLink());
060:
061: Link recvlink = receiver.msg.extractLink();
062: assertTrue("links must not be identical", recvlink != sentlink);
063: assertTrue("links must be equal", sentlink.equals(recvlink));
064: assertEquals("refcount must be 2", 2, Utils
065: .getRefCount(sentlink));
066:
067: /* clean out the free list */
068:
069: Utils.forceGC();
070: Utils.getFreedRendezvousPoints();
071:
072: /* free one reference */
073:
074: int[] freed;
075: int rp;
076:
077: receiver.msg = null;
078: recvlink = null;
079: Utils.forceGC();
080: freed = Utils.getFreedRendezvousPoints();
081: assertEquals("length must be zero", 0, freed.length);
082: assertEquals("refcount must be 1", 1, Utils
083: .getRefCount(sentlink));
084:
085: /* free the second reference */
086:
087: rp = sentlink.hashCode();
088: sentlink = null;
089: Utils.forceGC();
090: freed = Utils.getFreedRendezvousPoints();
091: assertEquals("length must be one", 1, freed.length);
092: if (freed.length == 1) {
093: assertEquals("freed rp must match", rp, freed[0]);
094: }
095: }
096:
097: /**
098: * Tests the transfer of a message containing a link, where the sender
099: * thread blocks first.
100: */
101: void testSendLink() throws IOException {
102: Isolate i = Isolate.currentIsolate();
103: Link link = Link.newLink(i, i);
104: Link sentlink = Link.newLink(i, i);
105: Sender sender = new Sender(link, LinkMessage
106: .newLinkMessage(sentlink));
107:
108: LinkMessage msg = link.receive();
109: sender.await();
110:
111: assertTrue("sender should be done", sender.done);
112: assertNull("sender should have no exceptions", sender.exception);
113: assertTrue("received message should contain link", msg
114: .containsLink());
115:
116: Link recvlink = msg.extractLink();
117: assertTrue("links must not be identical", recvlink != sentlink);
118: assertTrue("links must be equal", sentlink.equals(recvlink));
119: assertEquals("refcount must be 2", 2, Utils
120: .getRefCount(sentlink));
121:
122: /* clean out the free list */
123:
124: Utils.forceGC();
125: Utils.getFreedRendezvousPoints();
126:
127: /* free one reference */
128:
129: int[] freed;
130: int rp;
131:
132: sender.msg = null;
133: sentlink = null;
134: Utils.forceGC();
135: freed = Utils.getFreedRendezvousPoints();
136: assertEquals("length must be zero", 0, freed.length);
137: assertEquals("refcount must be 1", 1, Utils
138: .getRefCount(recvlink));
139:
140: /* free the second reference */
141:
142: rp = recvlink.hashCode();
143: recvlink = null;
144: msg = null;
145: Utils.forceGC();
146: freed = Utils.getFreedRendezvousPoints();
147: assertEquals("length must be one", 1, freed.length);
148: if (freed.length == 1) {
149: assertEquals("freed rp must match", rp, freed[0]);
150: }
151: }
152:
153: /**
154: * Runs all tests.
155: */
156: public void runTests() throws IOException {
157: declare("testReceiveLink");
158: testReceiveLink();
159:
160: declare("testSendLink");
161: testSendLink();
162: }
163:
164: }
|