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.cldc.isolate.IsolateStartupException;
031: import com.sun.midp.i3test.TestCase;
032: import java.io.IOException;
033: import java.io.InterruptedIOException;
034:
035: /**
036: * Tests proper link state after an isolate is killed.
037: */
038: public class TestKill extends TestCase {
039:
040: Isolate us;
041: Isolate them;
042: Link to;
043: Link from;
044:
045: void setUp() throws IOException, IsolateStartupException {
046: us = Isolate.currentIsolate();
047: them = new Isolate("com.sun.midp.links.Echo", null);
048: them.start();
049: to = Link.newLink(us, them);
050: from = Link.newLink(them, us);
051: LinkPortal.setLinks(them, new Link[] { to, from });
052: }
053:
054: void tearDown() {
055: us = them = null;
056: to = from = null;
057: }
058:
059: void checkClosedLinks() throws IOException {
060: boolean thrown = false;
061:
062: try {
063: to.send(LinkMessage.newStringMessage("Hello, world!"));
064: } catch (ClosedLinkException cle) {
065: thrown = true;
066: }
067: assertTrue("send should throw CLE", thrown);
068:
069: thrown = false;
070: try {
071: LinkMessage msg = from.receive();
072: } catch (ClosedLinkException cle) {
073: thrown = true;
074: }
075: assertTrue("receive should throw CLE", thrown);
076: }
077:
078: void testKillReceive() throws IOException {
079: Receiver receiver = new Receiver(from);
080: assertFalse("receiver blocked", receiver.done);
081:
082: them.exit(0);
083: them.waitForExit();
084: receiver.await();
085:
086: assertTrue("receiver got IIOE",
087: receiver.exception instanceof InterruptedIOException);
088: checkClosedLinks();
089: }
090:
091: void testKillSend() throws IOException {
092: to.send(LinkMessage.newStringMessage("one"));
093: Sender sender = new Sender(to, LinkMessage
094: .newStringMessage("two"));
095: assertFalse("sender blocked", sender.done);
096:
097: them.exit(0);
098: them.waitForExit();
099: sender.await();
100:
101: assertTrue("sender got IIOE",
102: sender.exception instanceof InterruptedIOException);
103: checkClosedLinks();
104: }
105:
106: /**
107: * Tests have the sender strand a message in a link.
108: */
109: void testStrandMessage() throws IOException {
110: byte[] data = new byte[20];
111: to.send(LinkMessage.newDataMessage(data));
112:
113: // wait until echo is blocked sending reply back to us
114: Utils.sleep(100);
115:
116: them.exit(0);
117: them.waitForExit();
118: checkClosedLinks();
119: }
120:
121: /**
122: * Runs all tests.
123: */
124: public void runTests() throws IOException, IsolateStartupException {
125:
126: declare("testKillReceive");
127: setUp();
128: testKillReceive();
129: tearDown();
130:
131: declare("testKillSend");
132: setUp();
133: testKillSend();
134: tearDown();
135:
136: declare("testStrandMessage");
137: setUp();
138: testStrandMessage();
139: tearDown();
140: }
141: }
|