KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Durable subscriber tests.
31  *
32  * @author <a HREF="mailto:pra@tim.se">Peter Antman</a>
33  * @version $Revision: 37406 $
34  */

35
36 public class DurableSubscriberTest extends MQBase
37 {
38
39    public DurableSubscriberTest(String JavaDoc name)
40    {
41       super(name);
42    }
43
44    /**
45     * Test setting up a durable subscription. Disconnect after half
46     * the messages have been sent. Connect later to see if they are still there.
47     * This test is done it two parts to be able to take down the server in
48     * between
49     */

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

117    public void runDurableSubscriberPartTwo() throws Exception JavaDoc
118    {
119       try
120       {
121          int ic = getIterationCount();
122          // Set up a durable subscriber
123
IntRangeMessageFilter f1 = new IntRangeMessageFilter(javax.jms.Message JavaDoc.class,
124             "DURABLE_NR",
125             0,
126             ic / 2);
127
128          TopicWorker sub1 = new TopicWorker(SUBSCRIBER,
129             TRANS_NONE,
130             f1);
131          sub1.setDurable("john", "needle", "sub2");
132
133          // Start up subscription again
134
Thread JavaDoc t2 = new Thread JavaDoc(sub1);
135          t2.start();
136
137          log.debug("Sleeping for " + ((ic * 10) / 60000) + " minutes");
138          sleep(ic * 10);
139          Assert.assertEquals("Subscriber did not get correct number of messages " + sub1.getMessageHandled(), ic / 2,
140             sub1.getMessageHandled());
141
142          //OK, take everything down
143
sub1.unsubscribe();
144          sub1.close();
145          t2.interrupt();
146
147       }
148       catch (Throwable JavaDoc t)
149       {
150          log.error("Error in test: " + t, t);
151          throw new Exception JavaDoc(t.getMessage());
152       }
153    }
154
155    public void testDurableSubscriber() throws Exception JavaDoc
156    {
157       runDurableSubscriberPartOne();
158       runDurableSubscriberPartTwo();
159    }
160
161    public void runGoodClient() throws Exception JavaDoc
162    {
163       TopicWorker sub1 = new TopicWorker(CONNECTOR,
164          TRANS_NONE,
165          null);
166       sub1.setDurable("john", "needle", "sub2");
167       Thread JavaDoc t1 = new Thread JavaDoc(sub1);
168       t1.start();
169       try
170       {
171          Thread.sleep(2000);
172       }
173       catch (InterruptedException JavaDoc e)
174       {
175       }
176       // Take it down abruptly
177
t1.interrupt();
178       sub1.close();
179       Assert.assertNull("Error in connecting durable sub", sub1.getException());
180
181    }
182
183    /**
184     * Test connecting as a durable subscriber and diconecction without taking
185     * the connection down properly
186     */

187    public void runBadClient() throws Exception JavaDoc
188    {
189       TopicWorker sub1 = new TopicWorker(CONNECTOR,
190          TRANS_NONE,
191          null);
192       sub1.setDurable("john", "needle", "sub2");
193       Thread JavaDoc t1 = new Thread JavaDoc(sub1);
194       t1.start();
195       try
196       {
197          Thread.sleep(2000);
198       }
199       catch (InterruptedException JavaDoc e)
200       {
201       }
202       // Take it down abruptly
203
t1.interrupt();
204       //sub1.close();
205
Assert.assertNull("Error in connecting durable sub", sub1.getException());
206    }
207
208    public static junit.framework.Test suite() throws Exception JavaDoc
209    {
210
211       TestSuite suite = new TestSuite();
212       suite.addTest(new DurableSubscriberTest("runGoodClient"));
213       suite.addTest(new DurableSubscriberTest("testDurableSubscriber"));
214       
215       //suite.addTest(new DurableSubscriberTest("testBadClient"));
216
return suite;
217    }
218
219    public static void main(String JavaDoc[] args)
220    {
221
222    }
223
224 } // DurableSubscriberTest
225
Popular Tags