FileDocCategorySizeDatePackage
InvocStoreStress.javaAPI DocphoneME MR2 API (J2ME)9773Wed May 02 18:00:44 BST 2007com.sun.midp.content

InvocStoreStress

public class InvocStoreStress extends Object implements Runnable
A test thread to pound on the InvocationStore.

Fields Summary
Thread
thread
The thread that is running this Stress case.
int
appID
The application ID consumed by this thread.
String
classname
The classname consumed by this thread.
int
nappIDs
The number of applicationIDs to target.
int
nclassnames
The number of content handlers per applicationID.
boolean
stopping
Flag to stop this thread.
int
maxInvokes
The maximum number of iterations.
int
numInvokes
Number of invocations queued by this test.
int
numResponses
Number of responses to invocations.
com.sun.midp.i3test.TestCase
testcase
The TestCase to handle the assertions.
Random
random
The random number generator.
int[]
scorecard
Counters for received responses.
int
numTerminations
Number of terminations pending to be received; zero = done.
AppProxy
appl
The application to stress.
Constructors Summary
InvocStoreStress(int appNdx, int handlerNdx, int nappids, int nclassnames, com.sun.midp.i3test.TestCase testcase, AppProxy appl)
Construct a new test thread with parameters.

param
appNdx index of this application's id
param
handlerNdx index of this application's classname
param
nappids number of applications ids to generate
param
nclassnames number of classnames to generate
param
testcase to use for asserts


                                                   
           
                  
    this.appID = getAppID(appNdx);
    this.classname = getClassname(handlerNdx);
        this.nappIDs = nappids;
        this.nclassnames = nclassnames;
        this.testcase = testcase;
    this.appl = appl;
        random = new Random(47);  // To get a consistent start for tests
    
Methods Summary
voiddoInvoker()
Generate new requests as needed and tally the response. The final invoke is the termination request/response.

        InvocationImpl request = new InvocationImpl();
    while (numTerminations > 0) {
            int rand = nextRandom(10);
            if (numInvokes < maxInvokes &&
                (numResponses == numInvokes || (rand < 5))) {
                // produce a new INIT request for a random app, class
                int targetid = nextRandom(nappIDs);
                int targetcn = nextRandom(nclassnames);
                request.suiteId = getAppID(targetid);
                request.classname = getClassname(targetcn);
                request.invokingSuiteId = appID;
                request.invokingClassname = classname;
                request.responseRequired = true;
        request.ID = Integer.toString(numInvokes);
                request.status = Invocation.INIT;
                int wait = nextRandom(500);
                sleep(wait);
                InvocationStore.put(request);
        ++numInvokes;
                println("invoke: +" + wait + " ", request);
            }

        // Consume a response; block until some
        InvocationImpl response = new InvocationImpl();
            response = InvocationStore.getResponse(response,
                           appID, classname, true);
            if (response != null) {
                if (response.status == Invocation.OK) {
            testcase.assertEquals(appID +
                      " verify target appID",
                      appID, response.suiteId);
            testcase.assertEquals(appID +
                      " verify target classname",
                      classname, response.classname);
            println("response", response);

            if ("terminate".equals(response.action)) {
            numTerminations--;
            } else {
            // Keep track of responses
            scorecard[Integer.parseInt(response.ID)] += 1;
            ++numResponses;

            /*
             * If just finished receiving the max responses;
             * send the terminations
             */
            if (numResponses == maxInvokes) {
                sendAll(Invocation.INIT, "terminate");
            }
            }
        } else {
            testcase.assertNull(appID + " illegal response:",
                    response);
                }
        }
    }

        sleep(2000L);
        do {
            request = InvocationStore.getResponse(new InvocationImpl(),
                          appID, classname, false);
        } while (request != null);

    // Terminate responder thread
    stop();

        // Verify that every invocation received a response.
        testcase.assertEquals(appID + " verify each invoke got a response",
                  numInvokes, numResponses);
        for (int i = 0; i < maxInvokes; i++) {
            testcase.assertEquals(appID + " verify received a response " + i,
                  1, scorecard[i]);
        }
    
voiddoResponder()
Handle and respond to any response that comes back.

    InvocationImpl request;
    while (!stopping) {

            // consume any request and send a response
            request = InvocationStore.getRequest(appID, classname, true);
            if (request != null) {
        testcase.assertEquals("verify only ACTIVE requests",
                      Invocation.ACTIVE, request.status);
        testcase.assertTrue("verify responseRequired",
                    request.responseRequired);

        // An ACTIVE request; send a reply
        request.status = Invocation.OK;
        InvocationStore.setStatus(request);
        println("reply:    ", request);
            }
    }
    
intgetAppID(int n)
Generate the application ID for app(n).

param
n the index of the applicationid.
return
the string application id.

        return n + 1000;
    
java.lang.StringgetClassname(int n)
Generate the classname for content handler (n).

param
n the index of the content handler.
return
the string classname

        return "class-".concat(Integer.toString(n));
    
private intnextRandom(int n)
Generate the next random number between 0 (inclusive) and max (exclusive).

param
n limit numbers from 0 to n-1.
return
a random number

        int val = random.nextInt() % n;
        if (val < 0) {
            val = -val;
        }
        return val;
    
voidprintln(java.lang.String msg, com.sun.midp.content.InvocationImpl invoc)
Print the interesting fields of an Invocation.

param
msg a message to print before the invocation
param
invoc an Invocation

        if (false) {
            System.out.println(appID + " " + msg +
                   ": status = " + invoc.status +
                   ", tid: " + invoc.tid +
                   ", target " + invoc.suiteId +
                   ", action = " + invoc.action);
        }
    
public voidrun()
Run the test.

    try {
        if (thread == null) {
        thread = Thread.currentThread();
        numTerminations = nappIDs * nclassnames;
        // Start a thread for the responder side
        new Thread(this).start();
        doInvoker();
        } else {
        doResponder();
        }
    } catch (Throwable t) {
        testcase.assertNull("Unexpected exception", t);
        t.printStackTrace();
    }
    
voidsendAll(int status, java.lang.String action)
Send a request to every other consumer.

param
status a status to send
param
action an action string to send

        InvocationImpl request = new InvocationImpl();
        request.invokingSuiteId = appID;
        request.invokingClassname = classname;
        request.status = status;
    request.action = action;
    request.ID = Integer.toString(numInvokes);

        for (int i = 0; i < nappIDs; i++) {
            for (int j = 0; j < nclassnames; j++) {
                request.suiteId = getAppID(i);
                request.classname = getClassname(j);
                InvocationStore.put(request);
        println("sending terminate", request);
            }
        }
    
voidsleep(long millis)
Sleep a bit.

param
millis millseconds to sleep

        try {
            Thread.sleep(millis);
        } catch (InterruptedException ie) {
        }
    
voidstop()
Stop this thread and cleanup.

    stopping = true;