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

TestMultiple

public class TestMultiple extends com.sun.midp.i3test.TestCase
Tests multiple receiving or sending threads blocked on the same link.

Fields Summary
public static final int
NUM_THREADS
The number of senders or receivers.
public static final long
TIMEOUT
Amount of time to wait for threads to complete after sending or receiving a message.
Random
rand
Object
lock
Hashtable
doneset
Hashtable
messages
Hashtable
throwables
Constructors Summary
Methods Summary
public voidcompleted(java.lang.Thread thr, LinkMessage msg, java.lang.Throwable exc)
Common code for completion callbacks for LoggingReceiver and LoggingSender. Adds the thread to the done set; if non-null, adds msg to the map of received messages; if non-null, adds any throwables caught to the map of caught throwables.

 // Map<Thread, Throwable>

                                                  
            
        synchronized (lock) {
            assertFalse("shouldn't be done already",
                doneset.containsKey(thr));
            doneset.put(thr, thr);

            if (msg != null) {
                messages.put(thr, msg);
            }

            if (exc != null) {
                throwables.put(thr, exc);
            }
        }
    
public voidrunTests()
Runs all tests.

        declare("testReceivers");
        testReceivers();

        declare("testSenders");
        testSenders();
    
voidtestReceivers()
Tests multiple receivers blocked on the same link. Creates NUM_THREADS threads to receive messages. Then, sends messages one by one, and ensures that each time exactly one thread is unblocked and has received the message just sent. Finally, after sending enough messages, ensures that all threads have been accounted for.

        Isolate is = Isolate.currentIsolate();
        Link link = Link.newLink(is, is);
        Receiver ra[] = new LoggingReceiver[NUM_THREADS];

        doneset = new Hashtable(NUM_THREADS);
        messages = new Hashtable(NUM_THREADS);
        throwables = new Hashtable(NUM_THREADS);

        for (int i = 0; i < NUM_THREADS; i++) {
            ra[i] = new LoggingReceiver(link);
        }
        Utils.sleep(2*TIMEOUT);

        for (int i = 0; i < NUM_THREADS; i++) {
            String s = Integer.toString(rand.nextInt());
            
            messages.clear();
            throwables.clear();
            link.send(LinkMessage.newStringMessage(s));
            Utils.sleep(TIMEOUT);

            assertEquals("iteration " + i, i + 1, doneset.size());

            assertEquals("one message", 1, messages.size());
            for (Enumeration e = messages.elements() ;
                    e.hasMoreElements() ; ) {
                LinkMessage msg = (LinkMessage)e.nextElement();
                assertTrue("message should contain a string",
                    msg.containsString());
                String rs = msg.extractString();
                assertEquals("strings should be equal", s, rs);
            }

            assertEquals("zero throwables", 0, throwables.size());
            for (Enumeration e = throwables.elements() ;
                    e.hasMoreElements() ; ) {
                System.out.println("### " + e.nextElement());
            }
        }

        for (int i = 0; i < NUM_THREADS; i++) {
            assertTrue("should be done", doneset.containsKey(ra[i]));
            doneset.remove(ra[i]);
        }
    
voidtestSenders()
Tests multiple senders blocked on the same link. Creates NUM_THREADS threads to send messages. Then, receives messages one by one, and ensures that each time exactly one thread is unblocked and had sent the message just received. Finally, after receiving enough messages, ensures that all threads have been accounted for.

        Isolate is = Isolate.currentIsolate();
        Link link = Link.newLink(is, is);
        LoggingSender sa[] = new LoggingSender[NUM_THREADS];

        for (int i = 0; i < NUM_THREADS; i++) {
            String s = Integer.toString(rand.nextInt());
            sa[i] = new LoggingSender(link, LinkMessage.newStringMessage(s));
        }
        Utils.sleep(2*TIMEOUT);

        doneset = new Hashtable(NUM_THREADS);
        throwables = new Hashtable(NUM_THREADS);

        for (int i = 0; i < NUM_THREADS; i++) {
            LinkMessage rm;
            String rs;

            messages.clear();
            throwables.clear();

            rm = link.receive();
            Utils.sleep(TIMEOUT);

            assertEquals("iteration " + i, i + 1, doneset.size());

            assertTrue("message should contain a string",
                rm.containsString());
            rs = rm.extractString();

            assertEquals("one message", 1, messages.size());
            for (Enumeration e = messages.elements() ;
                    e.hasMoreElements() ; ) {
                LinkMessage msg = (LinkMessage)e.nextElement();
                String ss = msg.extractString();
                assertEquals("strings should be equal", ss, rs);
            }

            assertEquals("zero throwables", 0, throwables.size());
            for (Enumeration e = throwables.elements() ;
                    e.hasMoreElements() ; ) {
                System.out.println("### " + e.nextElement());
            }
        }

        for (int i = 0; i < NUM_THREADS; i++) {
            assertTrue("should be done", doneset.containsKey(sa[i]));
            doneset.remove(sa[i]);
        }