001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.lock.test;
023:
024: import java.io.IOException;
025: import java.rmi.RemoteException;
026: import javax.ejb.CreateException;
027: import javax.ejb.RemoveException;
028: import javax.naming.InitialContext;
029: import javax.naming.NamingException;
030: import javax.rmi.PortableRemoteObject;
031:
032: import junit.extensions.TestSetup;
033: import junit.framework.Test;
034: import junit.framework.TestCase;
035: import junit.framework.TestSuite;
036: import org.apache.log4j.Category;
037: import org.jboss.test.JBossTestCase;
038:
039: import org.jboss.test.lock.interfaces.EnterpriseEntity;
040: import org.jboss.test.lock.interfaces.EnterpriseEntityHome;
041:
042: /**
043: * Test of EJB call invocation overhead.
044: *
045: * @author Scott.Stark@jboss.org
046: * @version $Revision: 57211 $
047: */
048: public class SpinUnitTestCase extends JBossTestCase {
049: /**
050: * Constructor for the SpinUnitTestCase object
051: *
052: * @param name Description of Parameter
053: */
054: public SpinUnitTestCase(String name) {
055: super (name);
056: }
057:
058: static void create() throws Exception {
059: InitialContext jndiContext = new InitialContext();
060: Object obj = jndiContext.lookup("EnterpriseEntity_A");
061: obj = PortableRemoteObject.narrow(obj,
062: EnterpriseEntityHome.class);
063: EnterpriseEntityHome home = (EnterpriseEntityHome) obj;
064: try {
065: home.create("Bean1");
066: } catch (CreateException e) {
067: }
068: }
069:
070: static void remove() throws Exception {
071: InitialContext jndiContext = new InitialContext();
072: Object obj = jndiContext.lookup("EnterpriseEntity_A");
073: obj = PortableRemoteObject.narrow(obj,
074: EnterpriseEntityHome.class);
075: EnterpriseEntityHome home = (EnterpriseEntityHome) obj;
076: try {
077: home.remove("Bean1");
078: } catch (RemoveException e) {
079: }
080: }
081:
082: /**
083: * A unit test for JUnit
084: *
085: * @exception Exception Description of Exception
086: */
087: public void testContention() throws Exception {
088: getLog().debug("+++ testContention()");
089: InitialContext jndiContext = new InitialContext();
090: Object obj = jndiContext.lookup("EnterpriseEntity_A");
091: obj = PortableRemoteObject.narrow(obj,
092: EnterpriseEntityHome.class);
093: EnterpriseEntityHome home = (EnterpriseEntityHome) obj;
094: getLog()
095: .debug(
096: "Found EnterpriseEntityHome @ jndiName=EnterpriseEntity");
097: Run r0 = new Run(home.findByPrimaryKey("Bean1"), getLog());
098: Run r1 = new Run(home.findByPrimaryKey("Bean1"), getLog());
099: Run r2 = new Run(home.findByPrimaryKey("Bean1"), getLog());
100: Thread t0 = new Thread(r0);
101: Thread t1 = new Thread(r1);
102: Thread t2 = new Thread(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: t0.join(5000);
111: assertTrue(r0.ex == null);
112: } catch (InterruptedException e) {
113: getLog().debug("Timed out waiting for t1");
114: }
115: getLog().debug("Waiting for t1...");
116: try {
117: t1.join(5000);
118: assertTrue(r1.ex == null);
119: } catch (InterruptedException e) {
120: getLog().debug("Timed out waiting for t1");
121: }
122: getLog().debug("Waiting for t2...");
123: try {
124: t2.join(5000);
125: assertTrue(r2.ex == null);
126: } catch (InterruptedException e) {
127: getLog().debug("Timed out waiting for t2");
128: }
129:
130: getLog().debug("End threads");
131: }
132:
133: /**
134: * The JUnit setup method
135: *
136: * @exception Exception Description of Exception
137: */
138: protected void setUp() throws Exception {
139: try {
140: create();
141: } catch (Exception e) {
142: getLog().error("setup error in create: ", e);
143: throw e;
144: }
145: }
146:
147: /**
148: * The teardown method for JUnit
149: *
150: * @exception Exception Description of Exception
151: */
152: protected void tearDown() throws Exception {
153: try {
154: remove();
155: } catch (Exception e) {
156: getLog().error("teardown error in remove: ", e);
157: throw e;
158: }
159: }
160:
161: public static Test suite() throws Exception {
162: return getDeploySetup(SpinUnitTestCase.class, "locktest.jar");
163: }
164:
165: /**
166: * #Description of the Class
167: */
168: static class Run implements Runnable {
169: EnterpriseEntity bean;
170: Exception ex;
171: private Category log;
172:
173: Run(EnterpriseEntity bean, Category log) {
174: this .bean = bean;
175: this .log = log;
176: }
177:
178: /**
179: * Main processing method for the Run object
180: */
181: public synchronized void run() {
182: notifyAll();
183: try {
184: long start = System.currentTimeMillis();
185: bean.sleep(5000);
186: long end = System.currentTimeMillis();
187: long elapsed = end - start;
188: log.debug(" bean.sleep() time = " + elapsed + " ms");
189: } catch (Exception e) {
190: ex = e;
191: }
192: }
193: }
194:
195: }
|