KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jbossmessaging > test > SessionCloseStressTestCase


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.jbossmessaging.test;
23
24 import java.util.Random JavaDoc;
25
26 import javax.jms.JMSException JavaDoc;
27 import javax.jms.Message JavaDoc;
28 import javax.jms.MessageConsumer JavaDoc;
29 import javax.jms.MessageListener JavaDoc;
30 import javax.jms.MessageProducer JavaDoc;
31 import javax.jms.Queue JavaDoc;
32 import javax.jms.QueueConnection JavaDoc;
33 import javax.jms.QueueConnectionFactory JavaDoc;
34 import javax.jms.QueueSession JavaDoc;
35 import javax.jms.Session JavaDoc;
36 import javax.naming.Context JavaDoc;
37
38 import junit.framework.Test;
39
40 import org.jboss.test.jbossmessaging.JMSTestCase;
41
42 /**
43  * Tests for receiving while closing the session
44  *
45  * @author <a HREF="mailto:richard.achmatowicz@jboss.com">Richard Achmatowicz</a>
46  * @author <a HREF="mailto:adrian@jboss.org>Adrian Brock</a>
47  * @version <tt>$Revision: 42018 $</tt>
48  */

49 public class SessionCloseStressTestCase extends JMSTestCase
50 {
51    static String JavaDoc QUEUE_FACTORY = "ConnectionFactory";
52    static String JavaDoc QUEUE = "queue/testQueue";
53
54    QueueConnection JavaDoc queueConnection;
55    Queue JavaDoc queue;
56    
57    public SessionCloseStressTestCase(String JavaDoc name) throws Exception JavaDoc
58    {
59       super(name);
60    }
61
62    public abstract class TestRunnable implements Runnable JavaDoc
63    {
64       public Throwable JavaDoc error = null;
65       
66       public abstract void doRun() throws Exception JavaDoc;
67       
68       public void run()
69       {
70          try
71          {
72             doRun();
73          }
74          catch (Throwable JavaDoc t)
75          {
76             log.error("Error in " + Thread.currentThread(), t);
77             error = t;
78          }
79       }
80    }
81    
82    public class SessionRunnable extends TestRunnable
83    {
84       MessageConsumer JavaDoc consumer;
85       
86       int received = 0;
87       
88       public void doRun() throws Exception JavaDoc
89       {
90          QueueSession JavaDoc session = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
91          MessageProducer JavaDoc producer = session.createProducer(queue);
92          for (int i = 0; i < getIterationCount(); ++i)
93          {
94             Message JavaDoc message = session.createTextMessage("" + i);
95             producer.send(message);
96          }
97          producer.close();
98          consumer = session.createConsumer(queue);
99          waitForMessages();
100          session.close();
101       }
102       
103       public synchronized MessageConsumer JavaDoc getConsumer() throws Exception JavaDoc
104       {
105          while (true)
106          {
107             if (consumer != null)
108                return consumer;
109             wait();
110          }
111       }
112       
113       public synchronized void incReceived()
114       {
115          ++received;
116          notifyAll();
117       }
118       
119       public synchronized void waitForMessages() throws Exception JavaDoc
120       {
121          notifyAll();
122          int target = new Random JavaDoc().nextInt(getIterationCount());
123          while (received < target)
124             wait();
125       }
126    }
127    
128    public class ReceiverRunnable extends TestRunnable
129    {
130       SessionRunnable sessionRunnable;
131       
132       public ReceiverRunnable(SessionRunnable sessionRunnable)
133       {
134          this.sessionRunnable = sessionRunnable;
135       }
136       
137       public void doRun() throws Exception JavaDoc
138       {
139          MessageConsumer JavaDoc consumer = sessionRunnable.getConsumer();
140          try
141          {
142             while (true)
143             {
144                consumer.receive();
145                sessionRunnable.incReceived();
146             }
147          }
148          catch (JMSException JavaDoc expected)
149          {
150             if (expected.getMessage().indexOf("closed") == -1)
151                throw expected;
152          }
153       }
154    }
155    
156    public class ReceiverNoWaitRunnable extends TestRunnable
157    {
158       SessionRunnable sessionRunnable;
159       
160       public ReceiverNoWaitRunnable(SessionRunnable sessionRunnable)
161       {
162          this.sessionRunnable = sessionRunnable;
163       }
164       
165       public void doRun() throws Exception JavaDoc
166       {
167          MessageConsumer JavaDoc consumer = sessionRunnable.getConsumer();
168          try
169          {
170             while (true)
171             {
172                if (consumer.receiveNoWait() != null)
173                   sessionRunnable.incReceived();
174             }
175          }
176          catch (JMSException JavaDoc expected)
177          {
178             if (expected.getMessage().indexOf("closed") == -1)
179                throw expected;
180          }
181       }
182    }
183    
184    public class ReceiverMessageListenerRunnable extends TestRunnable implements MessageListener JavaDoc
185    {
186       SessionRunnable sessionRunnable;
187       
188       public ReceiverMessageListenerRunnable(SessionRunnable sessionRunnable)
189       {
190          this.sessionRunnable = sessionRunnable;
191       }
192       
193       public void onMessage(Message JavaDoc message)
194       {
195          sessionRunnable.incReceived();
196       }
197       
198       public void doRun() throws Exception JavaDoc
199       {
200          MessageConsumer JavaDoc consumer = sessionRunnable.getConsumer();
201          try
202          {
203             consumer.setMessageListener(this);
204          }
205          catch (JMSException JavaDoc expected)
206          {
207             if (expected.getMessage().indexOf("closed") == -1)
208                throw expected;
209          }
210       }
211    }
212    
213    public void testSessionCloseCompetesWithReceive() throws Exception JavaDoc
214    {
215       connect();
216       try
217       {
218          for (int i = 0; i < getThreadCount(); ++i)
219          {
220             SessionRunnable sessionRunnable = new SessionRunnable();
221             Thread JavaDoc sessionThread = new Thread JavaDoc(sessionRunnable, "Session");
222             Thread JavaDoc consumerThread = new Thread JavaDoc(new ReceiverRunnable(sessionRunnable), "Consumer");
223             consumerThread.start();
224             sessionThread.start();
225             sessionThread.join();
226             consumerThread.join();
227             assertNull(sessionRunnable.error);
228
229             // Drain the queue
230
QueueSession JavaDoc session = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
231             MessageConsumer JavaDoc consumer = session.createConsumer(queue);
232             while (consumer.receiveNoWait() != null);
233             session.close();
234          }
235       }
236       finally
237       {
238          disconnect();
239       }
240    }
241    
242    public void testSessionCloseCompetesWithReceiveNoWait() throws Exception JavaDoc
243    {
244       connect();
245       try
246       {
247          for (int i = 0; i < getThreadCount(); ++i)
248          {
249             SessionRunnable sessionRunnable = new SessionRunnable();
250             Thread JavaDoc sessionThread = new Thread JavaDoc(sessionRunnable, "Session");
251             Thread JavaDoc consumerThread = new Thread JavaDoc(new ReceiverNoWaitRunnable(sessionRunnable), "Consumer");
252             consumerThread.start();
253             sessionThread.start();
254             sessionThread.join();
255             consumerThread.join();
256             assertNull(sessionRunnable.error);
257
258             // Drain the queue
259
QueueSession JavaDoc session = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
260             MessageConsumer JavaDoc consumer = session.createConsumer(queue);
261             while (consumer.receiveNoWait() != null);
262             session.close();
263          }
264       }
265       finally
266       {
267          disconnect();
268       }
269    }
270    
271    public void testSessionCloseCompetesWithMessageListener() throws Exception JavaDoc
272    {
273       connect();
274       try
275       {
276          for (int i = 0; i < getThreadCount(); ++i)
277          {
278             SessionRunnable sessionRunnable = new SessionRunnable();
279             Thread JavaDoc sessionThread = new Thread JavaDoc(sessionRunnable, "Session");
280             Thread JavaDoc consumerThread = new Thread JavaDoc(new ReceiverMessageListenerRunnable(sessionRunnable), "Consumer");
281             consumerThread.start();
282             sessionThread.start();
283             sessionThread.join();
284             consumerThread.join();
285             assertNull(sessionRunnable.error);
286
287             // Drain the queue
288
QueueSession JavaDoc session = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
289             MessageConsumer JavaDoc consumer = session.createConsumer(queue);
290             while (consumer.receiveNoWait() != null);
291             session.close();
292          }
293       }
294       finally
295       {
296          disconnect();
297       }
298    }
299
300    protected void connect() throws Exception JavaDoc
301    {
302       Context JavaDoc context = getInitialContext();
303       queue = (Queue JavaDoc) context.lookup(QUEUE);
304       QueueConnectionFactory JavaDoc queueFactory = (QueueConnectionFactory JavaDoc) context.lookup(QUEUE_FACTORY);
305       queueConnection = queueFactory.createQueueConnection();
306       queueConnection.start();
307
308       getLog().debug("Connection established.");
309    }
310
311    protected void disconnect()
312    {
313       try
314       {
315          if (queueConnection != null)
316             queueConnection.close();
317       }
318       catch (Exception JavaDoc ignored)
319       {
320       }
321
322       getLog().debug("Connection closed.");
323    }
324
325    public static Test suite() throws Exception JavaDoc
326    {
327        ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
328        String JavaDoc resourceName = getJMSResourceRelativePathname("tests-destinations-service.xml") ;
329
330        return getDeploySetup(SessionCloseStressTestCase.class,
331                loader.getResource(resourceName).toString());
332    }
333 }
334
Popular Tags