KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > broker > DoubleSubscriptionTest


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.broker;
19
20 import javax.jms.DeliveryMode JavaDoc;
21 import junit.framework.Test;
22 import org.apache.activemq.broker.StubConnection;
23 import org.apache.activemq.command.ActiveMQDestination;
24 import org.apache.activemq.command.ActiveMQQueue;
25 import org.apache.activemq.command.ConnectionInfo;
26 import org.apache.activemq.command.ConsumerInfo;
27 import org.apache.activemq.command.Message;
28 import org.apache.activemq.command.MessageAck;
29 import org.apache.activemq.command.ProducerInfo;
30 import org.apache.activemq.command.SessionInfo;
31 import org.apache.activemq.network.NetworkTestSupport;
32
33 /**
34  * Pretend to be an abusive client that sends multiple
35  * identical ConsumerInfo commands and make sure the
36  * broker doesn't stall because of it.
37  */

38
39 public class DoubleSubscriptionTest extends NetworkTestSupport {
40
41     public ActiveMQDestination destination;
42     public int deliveryMode;
43
44     private String JavaDoc remoteURI = "tcp://localhost:0?wireFormat.tcpNoDelayEnabled=true";
45
46     public static Test suite() {
47         return suite(DoubleSubscriptionTest.class);
48     }
49
50     public static void main(String JavaDoc[] args) {
51         junit.textui.TestRunner.run(suite());
52     }
53
54     public void initCombosForTestDoubleSubscription() {
55         addCombinationValues("destination", new Object JavaDoc[] { new ActiveMQQueue("TEST"), new ActiveMQQueue("TEST"), });
56     }
57     public void testDoubleSubscription() throws Exception JavaDoc {
58
59         // Start a normal consumer on the remote broker
60
StubConnection connection1 = createRemoteConnection();
61         ConnectionInfo connectionInfo1 = createConnectionInfo();
62         SessionInfo sessionInfo1 = createSessionInfo(connectionInfo1);
63         ConsumerInfo consumerInfo1 = createConsumerInfo(sessionInfo1, destination);
64         connection1.send(connectionInfo1);
65         connection1.send(sessionInfo1);
66         connection1.request(consumerInfo1);
67
68         // Start a normal producer on a remote broker
69
StubConnection connection2 = createRemoteConnection();
70         ConnectionInfo connectionInfo2 = createConnectionInfo();
71         SessionInfo sessionInfo2 = createSessionInfo(connectionInfo2);
72         ProducerInfo producerInfo2 = createProducerInfo(sessionInfo2);
73         connection2.send(connectionInfo2);
74         connection2.send(sessionInfo2);
75         connection2.request(producerInfo2);
76
77         // Send a message to make sure the basics are working
78
connection2.request(createMessage(producerInfo2, destination, DeliveryMode.PERSISTENT));
79
80         Message m1 = receiveMessage(connection1);
81         assertNotNull(m1);
82         assertNoMessagesLeft(connection1);
83
84         connection1.send(createAck(consumerInfo1, m1, 1, MessageAck.STANDARD_ACK_TYPE));
85
86         // Send a message to sit on the broker while we mess with it
87
connection2.request(createMessage(producerInfo2, destination, DeliveryMode.PERSISTENT));
88
89         // Now we're going to resend the same consumer commands again and see if the broker
90
// can handle it.
91
connection1.send(connectionInfo1);
92         connection1.send(sessionInfo1);
93         connection1.request(consumerInfo1);
94
95         // After this there should be 2 messages on the broker...
96
connection2.request(createMessage(producerInfo2, destination, DeliveryMode.PERSISTENT));
97
98         // ... let's start a fresh consumer...
99
connection1.stop();
100         StubConnection connection3 = createRemoteConnection();
101         ConnectionInfo connectionInfo3 = createConnectionInfo();
102         SessionInfo sessionInfo3 = createSessionInfo(connectionInfo3);
103         ConsumerInfo consumerInfo3 = createConsumerInfo(sessionInfo3, destination);
104         connection3.send(connectionInfo3);
105         connection3.send(sessionInfo3);
106         connection3.request(consumerInfo3);
107
108         // ... and then grab the 2 that should be there.
109
assertNotNull(receiveMessage(connection3));
110         assertNotNull(receiveMessage(connection3));
111         assertNoMessagesLeft(connection3);
112     }
113
114     protected String JavaDoc getRemoteURI() {
115         return remoteURI;
116     }
117
118 }
119
Popular Tags