KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > lock > test > SpinUnitTestCase


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.lock.test;
23
24 import javax.ejb.CreateException JavaDoc;
25 import javax.ejb.RemoveException JavaDoc;
26 import javax.naming.InitialContext JavaDoc;
27 import javax.rmi.PortableRemoteObject JavaDoc;
28
29 import junit.framework.Test;
30
31 import org.jboss.logging.Logger;
32 import org.jboss.test.JBossTestCase;
33 import org.jboss.test.lock.interfaces.EnterpriseEntity;
34 import org.jboss.test.lock.interfaces.EnterpriseEntityHome;
35
36 /**
37  * Test of EJB call invocation overhead.
38  *
39  * @author Scott.Stark@jboss.org
40  * @version $Revision: 58115 $
41  */

42 public class SpinUnitTestCase extends JBossTestCase
43 {
44    /**
45     * Constructor for the SpinUnitTestCase object
46     *
47     * @param name Description of Parameter
48     */

49    public SpinUnitTestCase(String JavaDoc name)
50    {
51       super(name);
52    }
53
54    static void create() throws Exception JavaDoc
55    {
56       InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
57       Object JavaDoc obj = jndiContext.lookup("EnterpriseEntity_A");
58       obj = PortableRemoteObject.narrow(obj, EnterpriseEntityHome.class);
59       EnterpriseEntityHome home = (EnterpriseEntityHome)obj;
60       try
61       {
62          home.create("Bean1");
63       }
64       catch (CreateException JavaDoc e)
65       {
66       }
67    }
68
69    static void remove() throws Exception JavaDoc
70    {
71       InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
72       Object JavaDoc obj = jndiContext.lookup("EnterpriseEntity_A");
73       obj = PortableRemoteObject.narrow(obj, EnterpriseEntityHome.class);
74       EnterpriseEntityHome home = (EnterpriseEntityHome)obj;
75       try
76       {
77          home.remove("Bean1");
78       }
79       catch (RemoveException JavaDoc e)
80       {
81       }
82    }
83
84    /**
85     * A unit test for JUnit
86     *
87     * @exception Exception Description of Exception
88     */

89    public void testContention() throws Exception JavaDoc
90    {
91       getLog().debug("+++ testContention()");
92       InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
93       Object JavaDoc obj = jndiContext.lookup("EnterpriseEntity_A");
94       obj = PortableRemoteObject.narrow(obj, EnterpriseEntityHome.class);
95       EnterpriseEntityHome home = (EnterpriseEntityHome)obj;
96       getLog().debug("Found EnterpriseEntityHome @ jndiName=EnterpriseEntity");
97       Run r0 = new Run(home.findByPrimaryKey("Bean1"), getLog());
98       Run r1 = new Run(home.findByPrimaryKey("Bean1"), getLog());
99       Run r2 = new Run(home.findByPrimaryKey("Bean1"), getLog());
100       Thread JavaDoc t0 = new Thread JavaDoc(r0);
101       Thread JavaDoc t1 = new Thread JavaDoc(r1);
102       Thread JavaDoc t2 = new Thread JavaDoc(r2);
103       t0.start();
104       Thread.sleep(100);
105       t1.start();
106       Thread.sleep(100);
107       t2.start();
108       getLog().debug("Waiting for t0...");
109       try
110       {
111          t0.join(5000);
112          assertTrue(r0.ex == null);
113       }
114       catch (InterruptedException JavaDoc e)
115       {
116          getLog().debug("Timed out waiting for t1");
117       }
118       getLog().debug("Waiting for t1...");
119       try
120       {
121          t1.join(5000);
122          assertTrue(r1.ex == null);
123       }
124       catch (InterruptedException JavaDoc e)
125       {
126          getLog().debug("Timed out waiting for t1");
127       }
128       getLog().debug("Waiting for t2...");
129       try
130       {
131          t2.join(5000);
132          assertTrue(r2.ex == null);
133       }
134       catch (InterruptedException JavaDoc e)
135       {
136          getLog().debug("Timed out waiting for t2");
137       }
138
139       getLog().debug("End threads");
140    }
141
142    /**
143     * The JUnit setup method
144     *
145     * @exception Exception Description of Exception
146     */

147    protected void setUp() throws Exception JavaDoc
148    {
149       try
150       {
151          create();
152       }
153       catch (Exception JavaDoc e)
154       {
155          getLog().error("setup error in create: ", e);
156          throw e;
157       }
158    }
159
160    /**
161     * The teardown method for JUnit
162     *
163     * @exception Exception Description of Exception
164     */

165    protected void tearDown() throws Exception JavaDoc
166    {
167       try
168       {
169          remove();
170       }
171       catch (Exception JavaDoc e)
172       {
173          getLog().error("teardown error in remove: ", e);
174          throw e;
175       }
176    }
177
178    public static Test suite() throws Exception JavaDoc
179    {
180       return getDeploySetup(SpinUnitTestCase.class, "locktest.jar");
181    }
182
183
184
185    /**
186     * #Description of the Class
187     */

188    static class Run implements Runnable JavaDoc
189    {
190       EnterpriseEntity bean;
191       Exception JavaDoc ex;
192       private Logger log;
193
194       Run(EnterpriseEntity bean, Logger log)
195       {
196          this.bean = bean;
197          this.log = log;
198       }
199
200       /**
201        * Main processing method for the Run object
202        */

203       public synchronized void run()
204       {
205          notifyAll();
206          try
207          {
208             long start = System.currentTimeMillis();
209             bean.sleep(5000);
210             long end = System.currentTimeMillis();
211             long elapsed = end - start;
212             log.debug(" bean.sleep() time = " + elapsed + " ms");
213          }
214          catch (Exception JavaDoc e)
215          {
216             ex = e;
217          }
218       }
219    }
220
221 }
222
Popular Tags