KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jbossmq > stress > QueueTest


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.jbossmq.stress;
23
24 import junit.framework.TestSuite;
25 import junit.framework.Assert;
26
27 import org.jboss.test.jbossmq.MQBase;
28
29 /**
30  * Test queue recover.
31  *
32  * @author <a HREF="mailto:pra@tim.se">Peter Antman</a>
33  * @version $Revision: 37406 $
34  */

35
36 public class QueueTest extends MQBase
37    {
38
39    public QueueTest(String JavaDoc name)
40    {
41       super(name);
42    }
43
44    /**
45     * This test is done it two parts to be able to take down the server in
46     * between
47     */

48    public void runQueueSubscriberPartOne() throws Exception JavaDoc
49    {
50       try
51       {
52          // Clean testarea up
53
drainQueue();
54
55          int ic = getIterationCount();
56       
57          // Set up a durable subscriber
58
IntRangeMessageFilter f1 = new IntRangeMessageFilter(javax.jms.Message JavaDoc.class,
59             "QUEUE_NR",
60             0,
61             ic / 2);
62
63          QueueWorker sub1 = new QueueWorker(SUBSCRIBER,
64             TRANS_NONE,
65             f1);
66          Thread JavaDoc t1 = new Thread JavaDoc(sub1);
67          t1.start();
68
69          // Publish
70
IntRangeMessageCreator c1 = new IntRangeMessageCreator("QUEUE_NR",
71             0);
72          QueueWorker pub1 = new QueueWorker(PUBLISHER,
73             TRANS_NONE,
74             c1,
75             ic / 2);
76          pub1.connect();
77          pub1.publish();
78
79          Assert.assertEquals("Publisher did not publish correct number of messages " + pub1.getMessageHandled(),
80             ic / 2,
81             pub1.getMessageHandled());
82       
83          // let sub1 have some time to handle the messages.
84
log.debug("Sleeping for " + ((ic * 10) / 60000) + " minutes");
85          // let sub1 have some time to handle the messages.
86
sleep(ic * 10);
87
88          Assert.assertEquals("Subscriber did not get correct number of messages " + sub1.getMessageHandled(),
89             ic / 2,
90             sub1.getMessageHandled());
91       
92          // Take down first sub
93
sub1.close();
94          t1.interrupt();
95       
96          //Publish some more
97
pub1.publish(ic / 2);
98          Assert.assertEquals("Publisher did not publish correct number of messages " + pub1.getMessageHandled(), ic,
99             pub1.getMessageHandled());
100
101          pub1.close();
102       }
103       catch (Throwable JavaDoc t)
104       {
105          log.error("Error in test: " + t, t);
106          throw new Exception JavaDoc(t.getMessage());
107       }
108    }
109
110    /**
111     * Part two of durable subscriber test, part one should be run before
112     * this is run.
113     */

114    public void runQueueSubscriberPartTwo() throws Exception JavaDoc
115    {
116       try
117       {
118          int ic = getIterationCount();
119          // Set up a durable subscriber
120
IntRangeMessageFilter f1 = new IntRangeMessageFilter(javax.jms.Message JavaDoc.class,
121             "QUEUE_NR",
122             0,
123             ic / 2);
124
125          QueueWorker sub1 = new QueueWorker(SUBSCRIBER,
126             TRANS_NONE,
127             f1);
128
129          // Start up subscription again
130
Thread JavaDoc t2 = new Thread JavaDoc(sub1);
131          t2.start();
132
133          log.debug("Sleeping for " + ((ic * 10) / 60000) + " minutes");
134          sleep(ic * 10);
135          Assert.assertEquals("Subscriber did not get correct number of messages " + sub1.getMessageHandled(), ic / 2,
136             sub1.getMessageHandled());
137
138          //OK, take everything down
139
sub1.close();
140          t2.interrupt();
141
142       }
143       catch (Throwable JavaDoc t)
144       {
145          log.error("Error in test: " + t, t);
146          throw new Exception JavaDoc(t.getMessage());
147       }
148    }
149
150    /**
151     * Test queue without taking the server down.
152     */

153    public void testQueueSubscriber() throws Exception JavaDoc
154    {
155       runQueueSubscriberPartOne();
156       runQueueSubscriberPartTwo();
157    }
158
159    public static junit.framework.Test suite() throws Exception JavaDoc
160    {
161
162       TestSuite suite = new TestSuite();
163       suite.addTest(new QueueSubOne("testQueueSubscriber"));
164
165       return suite;
166    }
167
168    public static void main(String JavaDoc[] args)
169    {
170
171    }
172
173 } // QueueTest
174
Popular Tags