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.content;
028:
029: import com.sun.midp.i3test.TestCase;
030:
031: import javax.microedition.content.Invocation;
032:
033: import com.sun.midp.content.InvocationStore;
034: import com.sun.midp.content.InvocationImpl;
035:
036: /**
037: * A test thread to test cancel and get interactions.
038: * The test strategy is to block for an Invocation
039: * and then see.
040: * Did the cancel unblock the request?
041: */
042: class InvocStoreCancel implements Runnable {
043:
044: /** The number of gets attempted. */
045: public int numTry;
046: /** The number of null results. */
047: public int numNotPending;
048: /** The number of non-null results. */
049: public int numPending;
050: /** The AppProxy to monitor. */
051: AppProxy appl;
052:
053: /** true to stop processing. */
054: private boolean stopping;
055:
056: /** True to block using get; false to block using select. */
057: private boolean type;
058:
059: /**
060: * Construct a new context.
061: * @param type true to blocking using get; false to use listen.
062: */
063: InvocStoreCancel(boolean type, AppProxy appl) {
064: this .type = type;
065: this .appl = appl;
066: }
067:
068: /**
069: * Reset the counters.
070: */
071: void reset() {
072: numTry = 0;
073: numNotPending = 0;
074: numPending = 0;
075: }
076:
077: /**
078: * Check the counters and return the level.
079: * @return the number of transactions.
080: */
081: public int check() {
082: if (numNotPending + numPending != numTry) {
083: throw new RuntimeException("inconsistent counters");
084: }
085: return numTry;
086: }
087:
088: /**
089: * Loop getting Invocations in the standard form.
090: * Keep track of how many are received that non-null
091: * and null.
092: */
093: public void run() {
094: if (type) {
095: blockingGet();
096: } else {
097: blockingListen();
098: }
099: }
100:
101: /**
102: * Run the test for blocking get and cancel.
103: */
104: private void blockingGet() {
105: while (!stopping) {
106: numTry++;
107: InvocationImpl get = InvocationStore.getResponse(
108: new InvocationImpl(), appl.getStorageId(), appl
109: .getClassname(), true);
110: if (get == null) {
111: numNotPending++;
112: } else {
113: numPending++;
114: }
115: }
116: }
117:
118: /**
119: * Run the test for blocking listen and cancel.
120: */
121: private void blockingListen() {
122: while (!stopping) {
123: numTry++;
124: boolean pending = InvocationStore.listen(appl
125: .getStorageId(), appl.getClassname(), false, true);
126: if (pending) {
127: numPending++;
128: } else {
129: numNotPending++;
130: }
131: }
132: }
133:
134: /**
135: * Called to stop this thread.
136: */
137: void stop() {
138: stopping = true;
139: }
140: }
|