InvocStoreStresspublic class InvocStoreStress extends Object implements RunnableA test thread to pound on the InvocationStore. |
Fields Summary |
---|
Thread | threadThe thread that is running this Stress case. | int | appIDThe application ID consumed by this thread. | String | classnameThe classname consumed by this thread. | int | nappIDsThe number of applicationIDs to target. | int | nclassnamesThe number of content handlers per applicationID. | boolean | stoppingFlag to stop this thread. | int | maxInvokesThe maximum number of iterations. | int | numInvokesNumber of invocations queued by this test. | int | numResponsesNumber of responses to invocations. | com.sun.midp.i3test.TestCase | testcaseThe TestCase to handle the assertions. | Random | randomThe random number generator. | int[] | scorecardCounters for received responses. | int | numTerminationsNumber of terminations pending to be received; zero = done. | AppProxy | applThe 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.
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 |
---|
void | doInvoker()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]);
}
| void | doResponder()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);
}
}
| int | getAppID(int n)Generate the application ID for app(n).
return n + 1000;
| java.lang.String | getClassname(int n)Generate the classname for content handler (n).
return "class-".concat(Integer.toString(n));
| private int | nextRandom(int n)Generate the next random number between 0 (inclusive)
and max (exclusive).
int val = random.nextInt() % n;
if (val < 0) {
val = -val;
}
return val;
| void | println(java.lang.String msg, com.sun.midp.content.InvocationImpl invoc)Print the interesting fields of an Invocation.
if (false) {
System.out.println(appID + " " + msg +
": status = " + invoc.status +
", tid: " + invoc.tid +
", target " + invoc.suiteId +
", action = " + invoc.action);
}
| public void | run()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();
}
| void | sendAll(int status, java.lang.String action)Send a request to every other consumer.
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);
}
}
| void | sleep(long millis)Sleep a bit.
try {
Thread.sleep(millis);
} catch (InterruptedException ie) {
}
| void | stop()Stop this thread and cleanup.
stopping = true;
|
|