001: /*
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: A_bankRead.java 7745 2005-12-05 13:26:16Z durieuxp $
023: * --------------------------------------------------------------------------
024: */
025:
026: package org.objectweb.jonas.jtests.clients.distribution;
027:
028: import javax.rmi.PortableRemoteObject;
029: import org.objectweb.jonas.jtests.beans.bank.Manager;
030: import org.objectweb.jonas.jtests.beans.bank.ManagerHome;
031: import org.objectweb.jonas.jtests.util.JTestCase;
032:
033: public abstract class A_bankRead extends JTestCase {
034:
035: protected static final int OP_READ = 1;
036: protected static final int OP_MOVE = 2;
037: protected static final int OP_CREATE = 3;
038: protected static final int OP_REMOVE = 4;
039: protected static final int OP_ONEMOVE = 5;
040: protected static final int OP_READTX = 6;
041: protected static final int OP_ONEMOVETX = 7;
042: protected static final int OP_MOVETO = 8;
043:
044: static final int initialValue = 1000;
045: static final int accountNb = 20;
046:
047: protected static ManagerHome home = null;
048: protected static Manager manager = null;
049: public static boolean threadfail;
050: public static Throwable threadex = null;
051:
052: public A_bankRead(String name) {
053: super (name);
054: }
055:
056: public abstract String getManagerHomeName();
057:
058: public abstract boolean getPrefetch();
059:
060: protected void setUp() {
061: super .setUp();
062: try {
063: if (home == null) {
064: useBeans("bank", false);
065: home = (ManagerHome) PortableRemoteObject.narrow(ictx
066: .lookup(getManagerHomeName()),
067: ManagerHome.class);
068: }
069: if (manager == null) {
070: manager = home.create(initialValue, getPrefetch());
071: }
072: // Initializes the test by creating accounts
073: manager.createAll(accountNb);
074: threadfail = false;
075: threadex = null;
076: } catch (Exception e) {
077: fail("Exception raised in setup: " + e);
078: }
079: }
080:
081: protected void tearDown() throws Exception {
082: super .tearDown();
083: if (manager != null) {
084: try {
085: try {
086: manager.reinitAll();
087: } finally {
088: manager.remove();
089: }
090: } finally {
091: manager = null;
092: }
093: }
094: }
095:
096: /**
097: * Create test accounts and run test ope
098: */
099: public void createOpe(int action, int accmin, int accmax,
100: int loops, int threads) throws Exception {
101: createOpe(action, accmin, accmax, loops, threads, 10);
102: }
103:
104: /**
105: * Create test accounts and run test ope
106: */
107: public void createOpe(int action, int accmin, int accmax,
108: int loops, int threads, int amount) throws Exception {
109: ope(OP_CREATE, accmin, accmax, accmax - accmin, 1, amount, true);
110: ope(action, accmin, accmax, loops, threads, amount, true);
111: }
112:
113: public void ope(int action, int accmin, int accmax, int loops,
114: int threads) throws Exception {
115: ope(action, accmin, accmax, loops, threads, 10, true);
116: }
117:
118: public void ope(int action, int accmin, int accmax, int loops,
119: int threads, int amount) throws Exception {
120: ope(action, accmin, accmax, loops, threads, amount, true);
121: }
122:
123: /**
124: * generic operation.
125: * @param accmin min range for account nb
126: * @param accmax max range for account nb
127: * @param loops nb of loop in each thread
128: * @param threads nb of threads running the same operations
129: */
130: public void ope(int action, int accmin, int accmax, int loops,
131: int threads, int amount, boolean verify) throws Exception {
132:
133: // Set transaction timeout in the server (global value!)
134: if (amount > 10) {
135: stopTxAt(20);
136: }
137:
138: // Create and start all threads
139: A_thread[] t_thr = new A_thread[threads];
140: for (int i = 0; i < threads; i++) {
141: t_thr[i] = new A_thread(getManagerHomeName(), i, ictx,
142: action, accmin, accmax, loops, amount,
143: getPrefetch());
144: t_thr[i].start();
145: }
146:
147: // Wait end of all threads
148: for (int p = 0; p < threads; p++) {
149: t_thr[p].join();
150: }
151:
152: // Check if all threads run ok
153: if (threadfail) {
154: throw new RuntimeException("Error in a thread", threadex);
155: }
156: if (verify) {
157: stopTxAt(600);
158: if (!manager.checkAll()) {
159: threadfail = true; // to reinit the database
160: fail("Bad global state");
161: }
162: }
163: }
164:
165: public void testCheckAll() throws Exception {
166: if (!manager.checkAll()) {
167: fail("Bad state in database");
168: }
169: }
170:
171: /**
172: * Operation= read
173: * min range for account nb = 0
174: * max range for account nb = 9
175: * nb of loop in each thread = 1
176: * nb of threads running the same operations = 1
177: * no verification
178: * @throws Exception
179: */
180: public void testBasicRead() throws Exception {
181: ope(OP_READ, 0, 9, 1, 1, 10, false);
182: }
183:
184: /**
185: * Operation= read in transaction
186: * min range for account nb = 0
187: * max range for account nb = 9
188: * nb of loop in each thread = 1
189: * nb of threads running the same operations = 1
190: * no verification
191: * @throws Exception
192: */
193: public void testBasicReadTx() throws Exception {
194: ope(OP_READTX, 0, 9, 1, 1, 10, false);
195: }
196:
197: /**
198: * Operation= read
199: * min range for account nb = 0
200: * max range for account nb = 9
201: * nb of loop in each thread = 10
202: * nb of threads running the same operations = 3
203: * verification of base's state
204: * @throws Exception
205: */
206: public void test3Readers() throws Exception {
207: createOpe(OP_READ, 0, 9, 10, 3);
208: }
209:
210: /**
211: * Operation= read
212: * min range for account nb = 0
213: * max range for account nb = 9
214: * nb of loop in each thread = 10
215: * nb of threads running the same operations = 4
216: * verification of base's state
217: * @throws Exception
218: */
219: public void test4Readers() throws Exception {
220: createOpe(OP_READ, 0, 9, 10, 4);
221: }
222:
223: /**
224: * Operation= read
225: * min range for account nb = 0
226: * max range for account nb = 9
227: * nb of loop in each thread = 10
228: * nb of threads running the same operations = 5
229: * verification of base's state
230: * @throws Exception
231: */
232: public void test5Readers() throws Exception {
233: createOpe(OP_READ, 0, 9, 10, 5);
234: }
235:
236: /**
237: * Operation= read
238: * min range for account nb = 0
239: * max range for account nb = 9
240: * nb of loop in each thread = 10
241: * nb of threads running the same operations = 8
242: * verification of base's state
243: * @throws Exception
244: */
245: public void test8Readers() throws Exception {
246: createOpe(OP_READ, 0, 9, 10, 8);
247: }
248:
249: /**
250: * Operation= read in transaction
251: * min range for account nb = 0
252: * max range for account nb = 9
253: * nb of loop in each thread = 10
254: * nb of threads running the same operations = 3
255: * verification of base's state
256: * @throws Exception
257: */
258: public void test3ReadersTx() throws Exception {
259: createOpe(OP_READTX, 0, 9, 10, 3);
260: }
261:
262: /**
263: * Operation= read in transaction
264: * min range for account nb = 0
265: * max range for account nb = 9
266: * nb of loop in each thread = 10
267: * nb of threads running the same operations = 4
268: * verification of base's state
269: * @throws Exception
270: */
271: public void test4ReadersTx() throws Exception {
272: createOpe(OP_READTX, 0, 9, 10, 4);
273: }
274:
275: /**
276: * Operation= read in transaction
277: * min range for account nb = 0
278: * max range for account nb = 9
279: * nb of loop in each thread = 10
280: * nb of threads running the same operations = 5
281: * verification of base's state
282: * @throws Exception
283: */
284: public void test5ReadersTx() throws Exception {
285: createOpe(OP_READTX, 0, 9, 10, 5);
286: }
287:
288: /**
289: * Operation= read in transaction
290: * min range for account nb = 0
291: * max range for account nb = 9
292: * nb of loop in each thread = 10
293: * nb of threads running the same operations = 8
294: * verification of base's state
295: * @throws Exception
296: */
297: public void test8ReadersTx() throws Exception {
298: createOpe(OP_READTX, 0, 9, 100, 8);
299: }
300:
301: /**
302: * Operation= read outside transaction
303: * min range for account nb = 0
304: * max range for account nb = 9
305: * nb of loop in each thread = 1
306: * nb of threads running the same operations = 10
307: * verification of base's state
308: * @throws Exception
309: */
310:
311: public void testMultiRead() throws Exception {
312: ope(OP_READ, 0, 9, 1, 10);
313: }
314:
315: /**
316: * Operation= read inside transaction
317: * min range for account nb = 0
318: * max range for account nb = 9
319: * nb of loop in each thread = 1
320: * nb of threads running the same operations = 10
321: * verification of base's state
322: * @throws Exception
323: */
324:
325: public void testMultiReadTx() throws Exception {
326: ope(OP_READTX, 0, 9, 1, 10);
327: }
328:
329: /**
330: * Operation= read inside transaction
331: * min range for account nb = 0
332: * max range for account nb = 9
333: * nb of loop in each thread = 10
334: * nb of threads running the same operations = 10
335: * verification of base's state
336: * @throws Exception
337: */
338:
339: public void testMultiShortReadTx() throws Exception {
340: createOpe(OP_READTX, 0, 9, 10, 10);
341: }
342:
343: }
|