FileDocCategorySizeDatePackage
TestLinkTransfer.javaAPI DocphoneME MR2 API (J2ME)5262Wed May 02 18:00:02 BST 2007com.sun.midp.links

TestLinkTransfer

public class TestLinkTransfer extends com.sun.midp.i3test.TestCase
Tests simple exchange of a message containing a Link and also tests for proper cleanup. Covers simple cases only, with one receiver thread and one sender thread. Covers receive-then-send as well as send-then-receive.

Fields Summary
Constructors Summary
Methods Summary
public voidrunTests()
Runs all tests.

        declare("testReceiveLink");
        testReceiveLink();

        declare("testSendLink");
        testSendLink();
    
voidtestReceiveLink()
Tests the transfer of a message containing a link, where the receiver thread blocks first.

        Isolate i = Isolate.currentIsolate();
        Link link = Link.newLink(i, i);
        Link sentlink = Link.newLink(i, i);
        Receiver receiver = new Receiver(link);

        link.send(LinkMessage.newLinkMessage(sentlink));
        receiver.await();

        assertTrue("receiver should be done", receiver.done);
        assertNull("receiver should have no exceptions", receiver.exception);
        assertNotNull("receiver should have received a message", receiver.msg);
        assertTrue("received message should contain link",
            receiver.msg.containsLink());

        Link recvlink = receiver.msg.extractLink();
        assertTrue("links must not be identical", recvlink != sentlink);
        assertTrue("links must be equal", sentlink.equals(recvlink));
        assertEquals("refcount must be 2", 2, Utils.getRefCount(sentlink));

        /* clean out the free list */

        Utils.forceGC();
        Utils.getFreedRendezvousPoints();

        /* free one reference */

        int[] freed;
        int rp;

        receiver.msg = null;
        recvlink = null;
        Utils.forceGC();
        freed = Utils.getFreedRendezvousPoints();
        assertEquals("length must be zero", 0, freed.length);
        assertEquals("refcount must be 1", 1, Utils.getRefCount(sentlink));

        /* free the second reference */

        rp = sentlink.hashCode();
        sentlink = null;
        Utils.forceGC();
        freed = Utils.getFreedRendezvousPoints();
        assertEquals("length must be one", 1, freed.length);
        if (freed.length == 1) {
            assertEquals("freed rp must match", rp, freed[0]);
        }
    
voidtestSendLink()
Tests the transfer of a message containing a link, where the sender thread blocks first.

        Isolate i = Isolate.currentIsolate();
        Link link = Link.newLink(i, i);
        Link sentlink = Link.newLink(i, i);
        Sender sender = new Sender(link,
            LinkMessage.newLinkMessage(sentlink));

        LinkMessage msg = link.receive();
        sender.await();

        assertTrue("sender should be done", sender.done);
        assertNull("sender should have no exceptions", sender.exception);
        assertTrue("received message should contain link",
            msg.containsLink());

        Link recvlink = msg.extractLink();
        assertTrue("links must not be identical", recvlink != sentlink);
        assertTrue("links must be equal", sentlink.equals(recvlink));
        assertEquals("refcount must be 2", 2, Utils.getRefCount(sentlink));

        /* clean out the free list */

        Utils.forceGC();
        Utils.getFreedRendezvousPoints();

        /* free one reference */

        int[] freed;
        int rp;

        sender.msg = null;
        sentlink = null;
        Utils.forceGC();
        freed = Utils.getFreedRendezvousPoints();
        assertEquals("length must be zero", 0, freed.length);
        assertEquals("refcount must be 1", 1, Utils.getRefCount(recvlink));

        /* free the second reference */

        rp = recvlink.hashCode();
        recvlink = null;
        msg = null;
        Utils.forceGC();
        freed = Utils.getFreedRendezvousPoints();
        assertEquals("length must be one", 1, freed.length);
        if (freed.length == 1) {
            assertEquals("freed rp must match", rp, freed[0]);
        }