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.ejb3.test.strictpool.unit;
023:
024: import javax.jms.*;
025: import javax.naming.InitialContext;
026: import org.jboss.ejb3.test.strictpool.MDBInvoker;
027: import org.jboss.ejb3.test.strictpool.SessionInvoker;
028: import org.jboss.ejb3.test.strictpool.StrictlyPooledSession;
029: import org.jboss.ejb3.test.strictpool.Counter;
030: import org.jboss.test.JBossTestCase;
031: import EDU.oswego.cs.dl.util.concurrent.CountDown;
032: import junit.framework.Test;
033:
034: /**
035: * Adapted from the EJB 2.1 tests (org.jboss.test.cts.test.StatefulSessionUnitTestCase and
036: * org.jboss.test.cts.test.MDBUnitTestCase)
037: * @author <a href="mailto:kabir.khan@jboss.org">Kabir Khan</a>
038: * @version $Revision: 60712 $
039: */
040: public class StrictPoolUnitTestCase extends JBossTestCase {
041: static final int MAX_SIZE = 20;
042: static String QUEUE_FACTORY = "ConnectionFactory";
043:
044: public StrictPoolUnitTestCase(String name) {
045: super (name);
046: }
047:
048: public void testSession() throws Exception {
049: System.out.println("*** testSession");
050: CountDown done = new CountDown(MAX_SIZE);
051: InitialContext ctx = new InitialContext();
052: StrictlyPooledSession session = (StrictlyPooledSession) ctx
053: .lookup("StrictlyPooledSessionBean/remote");
054: SessionInvoker[] threads = new SessionInvoker[MAX_SIZE];
055: for (int n = 0; n < MAX_SIZE; n++) {
056: SessionInvoker t = new SessionInvoker(n, done, session);
057: threads[n] = t;
058: t.start();
059: }
060: boolean ok = done.attempt(1500 * MAX_SIZE);
061: super .assertTrue("Acquired done, remaining="
062: + done.currentCount(), ok);
063:
064: for (int n = 0; n < MAX_SIZE; n++) {
065: SessionInvoker t = threads[n];
066: if (t.runEx != null) {
067: t.runEx.printStackTrace();
068: System.err.println("SessionInvoker.runEx != null");
069: t.runEx.printStackTrace();
070: fail("SessionInvoker.runEx != null");
071: }
072: }
073: }
074:
075: public void testMessageDriven() throws Exception {
076: System.out.println("*** testMessageDriven");
077: CountDown done = new CountDown(MAX_SIZE);
078: InitialContext ctx = new InitialContext();
079: QueueConnectionFactory factory = (QueueConnectionFactory) ctx
080: .lookup(QUEUE_FACTORY);
081: QueueConnection queConn = factory.createQueueConnection();
082: QueueSession session = queConn.createQueueSession(false,
083: QueueSession.AUTO_ACKNOWLEDGE);
084: Queue queueA = (Queue) ctx.lookup("queue/queueA");
085: Queue queueB = (Queue) ctx.lookup("queue/queueB");
086: queConn.start();
087: MDBInvoker[] threads = new MDBInvoker[MAX_SIZE];
088: for (int n = 0; n < MAX_SIZE; n++) {
089: MDBInvoker t = new MDBInvoker(session, queueA, queueB, n,
090: done);
091: threads[n] = t;
092: t.start();
093: }
094: assertTrue("Acquired done", done.attempt(1500 * MAX_SIZE));
095: session.close();
096: queConn.close();
097:
098: for (int n = 0; n < MAX_SIZE; n++) {
099: MDBInvoker t = threads[n];
100: if (t.runEx != null) {
101: t.runEx.printStackTrace();
102: fail("Inovker.runEx != null, msg="
103: + t.runEx.getMessage());
104: }
105: }
106: }
107:
108: public void testPoolTimeout() throws Exception {
109: InitialContext ctx = new InitialContext();
110: ConnectionFactory factory = (ConnectionFactory) ctx
111: .lookup("ConnectionFactory");
112: Connection conn = factory.createConnection();
113: Session session = conn.createSession(false,
114: Session.AUTO_ACKNOWLEDGE);
115: Queue queueC = (Queue) ctx.lookup("queue/queueC");
116: conn.start();
117: MessageProducer sender = session.createProducer(queueC);
118: TextMessage msg = session.createTextMessage("hello world");
119: msg.setIntProperty("JMS_JBOSS_REDELIVERY_LIMIT", 1);
120: sender.send(msg);
121: // the second message will timeout
122: TextMessage msg2 = session.createTextMessage("hello world 2");
123: msg2.setIntProperty("JMS_JBOSS_REDELIVERY_LIMIT", 1);
124: sender.send(msg2);
125:
126: Thread.sleep(5000);
127: Counter counter = (Counter) ctx.lookup("CounterBean/remote");
128: assertEquals(1, counter.getCount());
129:
130: }
131:
132: public static Test suite() throws Exception {
133: return getDeploySetup(StrictPoolUnitTestCase.class,
134: "strictpool_mdbtest-service.xml, strictpool-test.jar");
135:
136: }
137:
138: }
|