KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.jms.Message JavaDoc;
25 import javax.jms.BytesMessage JavaDoc;
26 import javax.jms.JMSException JavaDoc;
27
28 import junit.framework.TestSuite;
29 import junit.framework.Assert;
30
31 import org.jboss.test.jbossmq.MQBase;
32
33 /**
34  * @author <a HREF="mailto:pra@tim.se">Peter Antman</a>
35  * @version $Revision: 37406 $
36  */

37
38 public class MassiveTest extends MQBase
39    {
40    static byte[] PERFORMANCE_TEST_DATA_PAYLOAD = new byte[10 * 1024];
41
42    public MassiveTest(String JavaDoc name)
43    {
44       super(name);
45    }
46
47    /**
48     * Should be run with large iteration count!!!!!
49     */

50    public void runMassiveTest() throws Exception JavaDoc
51    {
52       // Clean testarea up
53
drainTopic();
54
55       int ic = getIterationCount();
56       
57       // Set up a durable subscriber
58
IntRangeMessageFilter f1 = new IntRangeMessageFilter(javax.jms.BytesMessage JavaDoc.class,
59          "MASSIVE_NR",
60          0,
61          ic);
62
63       TopicWorker sub1 = new TopicWorker(SUBSCRIBER,
64          TRANS_NONE,
65          f1);
66       Thread JavaDoc t1 = new Thread JavaDoc(sub1);
67       t1.start();
68
69       // Publish
70
ByteIntRangeMessageCreator c1 = new ByteIntRangeMessageCreator("MASSIVE_NR",
71          0);
72       TopicWorker pub1 = new TopicWorker(PUBLISHER,
73          TRANS_NONE,
74          c1,
75          ic);
76       pub1.connect();
77       pub1.publish();
78
79       Assert.assertEquals("Publisher did not publish correct number of messages " + pub1.getMessageHandled(),
80          ic,
81          pub1.getMessageHandled());
82
83       log.debug("Sleeping for " + ((ic * 10) / 60000) + " minutes");
84       // let sub1 have some time to handle the messages.
85
try
86       {
87          Thread.sleep(ic * 10);
88       }
89       catch (InterruptedException JavaDoc e)
90       {
91       }
92       log.debug("Woke up");
93       Assert.assertEquals("Subscriber did not get correct number of messages " + sub1.getMessageHandled(),
94          ic,
95          sub1.getMessageHandled());
96       
97       // Take down first sub
98
sub1.close();
99       t1.interrupt();
100       pub1.close();
101    }
102
103    public void runMassivTestFailingSub() throws Exception JavaDoc
104    {
105       // Clean testarea up
106
drainTopic();
107
108       int ic = getIterationCount();
109       
110       // Set up a subscriber
111
IntRangeMessageFilter f1 = new IntRangeMessageFilter(javax.jms.BytesMessage JavaDoc.class,
112          "MASSIVE_NR",
113          0,
114          ic);
115
116       TopicWorker sub1 = new TopicWorker(SUBSCRIBER,
117          TRANS_NONE,
118          f1);
119       Thread JavaDoc t1 = new Thread JavaDoc(sub1);
120       t1.start();
121
122       // Set up a failing sub
123
FailingSubWorker sub2 = new FailingSubWorker();
124       sub2.setSubscriberAttrs(SUBSCRIBER,
125          TRANS_NONE,
126          f1);
127       Thread JavaDoc tf = new Thread JavaDoc(sub2);
128       tf.start();
129          
130       // Publish
131
ByteIntRangeMessageCreator c1 = new ByteIntRangeMessageCreator("MASSIVE_NR",
132          0);
133       TopicWorker pub1 = new TopicWorker(PUBLISHER,
134          TRANS_NONE,
135          c1,
136          ic);
137       pub1.connect();
138       pub1.publish();
139
140       Assert.assertEquals("Publisher did not publish correct number of messages " + pub1.getMessageHandled(),
141          ic,
142          pub1.getMessageHandled());
143
144       log.debug("Sleeping for " + ((ic * 10) / 60000) + " minutes");
145       // let sub1 have some time to handle the messages.
146
try
147       {
148          Thread.sleep(ic * 10);
149       }
150       catch (InterruptedException JavaDoc e)
151       {
152       }
153       log.debug("Woke up");
154       Assert.assertEquals("Subscriber did not get correct number of messages " + sub1.getMessageHandled(),
155          ic,
156          sub1.getMessageHandled());
157       
158       // Take down
159
sub1.close();
160       t1.interrupt();
161       pub1.close();
162       sub2.setStoped();
163       tf.interrupt();
164       tf.interrupt();
165       sub2.close();
166    }
167
168    public static junit.framework.Test suite() throws Exception JavaDoc
169    {
170
171       TestSuite suite = new TestSuite();
172       suite.addTest(new MassiveTest("runMassiveTest"));
173       
174       //suite.addTest(new DurableSubscriberTest("testBadClient"));
175
return suite;
176    }
177
178    public static void main(String JavaDoc[] args)
179    {
180       try
181       {
182          MassiveTest t = new MassiveTest("runMassiveTest");
183          t.setUp();
184          t.runMassiveTest();
185       }
186       catch (Exception JavaDoc ex)
187       {
188          System.err.println("Ex: " + ex);
189          ex.printStackTrace();
190       }
191
192    }
193
194    public class ByteIntRangeMessageCreator extends IntRangeMessageCreator
195       {
196       int start = 0;
197
198       public ByteIntRangeMessageCreator(String JavaDoc property)
199       {
200          super(property);
201       }
202
203       public ByteIntRangeMessageCreator(String JavaDoc property, int start)
204       {
205          super(property);
206          this.start = start;
207       }
208
209       public Message JavaDoc createMessage(int nr) throws JMSException JavaDoc
210       {
211          if (session == null)
212             throw new JMSException JavaDoc("Session not allowed to be null");
213
214          BytesMessage JavaDoc msg = session.createBytesMessage();
215          msg.writeBytes(PERFORMANCE_TEST_DATA_PAYLOAD);
216          msg.setStringProperty(property, String.valueOf(start + nr));
217          return msg;
218       }
219    }
220
221    public class FailingSubWorker extends TopicWorker
222    {
223       int check = 0;
224
225       //Only reveice firts message
226
public void onMessage(Message JavaDoc msg)
227       {
228          check++;
229          if (check > 1)
230             log.warn("Got called while asleep!! " + check);
231          while (!stopRequested)
232          {
233             sleep(2000);
234          }
235       }
236    }
237 } // MassiveTest
238

239
240
241
242
243
244
245
246
247
248
Popular Tags