KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > clients > distribution > F_Sequence


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: F_Sequence.java,v 1.2 2005/04/22 07:43:55 durieuxp Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.jtests.clients.distribution;
27
28
29 import javax.naming.NamingException JavaDoc;
30 import javax.rmi.PortableRemoteObject JavaDoc;
31
32 import junit.framework.Test;
33 import junit.framework.TestSuite;
34
35 import org.objectweb.jonas.jtests.beans.sequence.WoSes;
36 import org.objectweb.jonas.jtests.beans.sequence.WoSesHome;
37 import org.objectweb.jonas.jtests.beans.sequence.SequenceSesHome;
38 import org.objectweb.jonas.jtests.util.JTestCase;
39
40
41
42 /**
43  * Test for reproducing the Bug #303488
44  * @author Philippe Coq
45  */

46
47 /**
48  * Thread that does the work
49  */

50 class B_thread extends Thread JavaDoc {
51     WoSesHome home;
52     int loops;
53
54     public B_thread(WoSesHome home, int loops) {
55         this.home = home;
56         this.loops = loops;
57     }
58
59     public void run() {
60         try {
61             WoSes wos = home.create();
62             for (int i = 0; i < loops; i++) {
63                 wos.schedule(Thread.currentThread().getName(),i);
64             }
65             wos.remove();
66         } catch(Exception JavaDoc e) {
67             e.printStackTrace();
68         }
69     }
70
71 }
72
73 public class F_Sequence extends JTestCase {
74
75     protected static WoSesHome woseshome = null;
76
77
78     public F_Sequence(String JavaDoc name) {
79         super(name);
80     }
81
82     protected void setUp() {
83         super.setUp();
84         if (woseshome == null) {
85             // Create table with sql (not a create) because we don't want
86
// that an instance is mapped in memory before doing the test.
87
useBeans("sequence", true);
88             try {
89                 woseshome= (WoSesHome ) PortableRemoteObject.narrow(ictx.lookup("WoSesHome"), WoSesHome.class);
90                 assertNotNull("home of clusterId_1 is null", woseshome);
91             } catch (NamingException JavaDoc e) {
92                 fail("Cannot get bean home: " + e.getMessage());
93             }
94         }
95     }
96
97     protected void tearDown() throws Exception JavaDoc {
98         super.tearDown();
99     }
100
101     /**
102      * Several threads run a findByPrimreyKey on the same element in the
103      * table jt2_Sequence and modify the next Number field
104      * so if the bug isn't fixed we got duplicate key exception (constraints violation)
105      *
106      */

107     public void multiThreadTest(int threads, int loops) throws Exception JavaDoc {
108
109         // Create and start all threads
110
B_thread[] t_thr = new B_thread[threads];
111         for (int i = 0; i < threads; i++) {
112             t_thr[i] = new B_thread(woseshome, loops);
113         }
114         for (int i = 0; i < threads; i++) {
115             t_thr[i].start();
116         }
117         // Wait end of all threads
118
for (int p = 0; p < threads; p++) {
119             t_thr[p].join();
120         }
121
122     }
123
124     public void testTh20L10() throws Exception JavaDoc {
125         multiThreadTest(20, 10);
126     }
127
128     public void testTh2L100() throws Exception JavaDoc {
129         multiThreadTest(2, 100);
130     }
131
132     public void testTh3L1() throws Exception JavaDoc {
133         multiThreadTest(3, 1);
134     }
135
136
137     public void testTh5L10() throws Exception JavaDoc {
138         multiThreadTest(5, 10);
139     }
140
141     public void testTh1L2() throws Exception JavaDoc {
142         multiThreadTest(1, 2);
143     }
144
145
146     public void testTh10L10() throws Exception JavaDoc {
147         multiThreadTest(10, 10);
148     }
149
150
151
152     public void testTh50L1() throws Exception JavaDoc {
153         multiThreadTest(50, 1);
154     }
155
156
157     public static Test suite() {
158         return new TestSuite(F_Sequence.class);
159     }
160
161     public static void main (String JavaDoc args[]) {
162         String JavaDoc testtorun = null;
163         // Get args
164
for (int argn = 0; argn < args.length; argn++) {
165             String JavaDoc s_arg = args[argn];
166             Integer JavaDoc i_arg;
167             if (s_arg.equals("-n")) {
168                 testtorun = args[++argn];
169             }
170         }
171         if (testtorun == null) {
172             junit.textui.TestRunner.run(suite());
173         } else {
174             junit.textui.TestRunner.run(new F_Sequence(testtorun));
175         }
176     }
177 }
178
Popular Tags