KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > broker > policy > RoundRobinDispatchPolicyTest


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.policy;
19
20 import org.apache.activemq.broker.QueueSubscriptionTest;
21 import org.apache.activemq.broker.BrokerService;
22 import org.apache.activemq.broker.region.policy.PolicyEntry;
23 import org.apache.activemq.broker.region.policy.RoundRobinDispatchPolicy;
24 import org.apache.activemq.broker.region.policy.PolicyMap;
25
26 import javax.jms.Connection JavaDoc;
27 import javax.jms.Destination JavaDoc;
28 import javax.jms.MessageConsumer JavaDoc;
29 import javax.jms.Session JavaDoc;
30
31 public class RoundRobinDispatchPolicyTest extends QueueSubscriptionTest {
32
33     protected BrokerService createBroker() throws Exception JavaDoc {
34         BrokerService broker = super.createBroker();
35
36         PolicyEntry policy = new PolicyEntry();
37         policy.setDispatchPolicy(new RoundRobinDispatchPolicy());
38
39         PolicyMap pMap = new PolicyMap();
40         pMap.setDefaultEntry(policy);
41
42         broker.setDestinationPolicy(pMap);
43
44         return broker;
45     }
46
47     public void testOneProducerTwoConsumersSmallMessagesOnePrefetch() throws Exception JavaDoc {
48         super.testOneProducerTwoConsumersSmallMessagesOnePrefetch();
49
50         // Ensure that each consumer should have received at least one message
51
// We cannot guarantee that messages will be equally divided, since prefetch is one
52
assertEachConsumerReceivedAtLeastXMessages(1);
53     }
54
55     public void testOneProducerTwoConsumersSmallMessagesLargePrefetch() throws Exception JavaDoc {
56         super.testOneProducerTwoConsumersSmallMessagesLargePrefetch();
57         assertMessagesDividedAmongConsumers();
58     }
59
60     public void testOneProducerTwoConsumersLargeMessagesOnePrefetch() throws Exception JavaDoc {
61         super.testOneProducerTwoConsumersLargeMessagesOnePrefetch();
62
63         // Ensure that each consumer should have received at least one message
64
// We cannot guarantee that messages will be equally divided, since prefetch is one
65
assertEachConsumerReceivedAtLeastXMessages(1);
66     }
67
68     public void testOneProducerTwoConsumersLargeMessagesLargePrefetch() throws Exception JavaDoc {
69         super.testOneProducerTwoConsumersLargeMessagesLargePrefetch();
70         assertMessagesDividedAmongConsumers();
71     }
72
73     public void testOneProducerManyConsumersFewMessages() throws Exception JavaDoc {
74         super.testOneProducerManyConsumersFewMessages();
75
76         // Since there are more consumers, each consumer should have received at most one message only
77
assertMessagesDividedAmongConsumers();
78     }
79
80     public void testOneProducerManyConsumersManyMessages() throws Exception JavaDoc {
81         super.testOneProducerManyConsumersManyMessages();
82         assertMessagesDividedAmongConsumers();
83     }
84
85     public void testManyProducersManyConsumers() throws Exception JavaDoc {
86         super.testManyProducersManyConsumers();
87         assertMessagesDividedAmongConsumers();
88     }
89     
90     public void testOneProducerTwoMatchingConsumersOneNotMatchingConsumer() throws Exception JavaDoc {
91     // Create consumer that won't consume any message
92
createMessageConsumer(createConnectionFactory().createConnection(), createDestination(), "JMSPriority<1");
93         super.testOneProducerTwoConsumersSmallMessagesLargePrefetch();
94         assertMessagesDividedAmongConsumers();
95     }
96         
97     protected MessageConsumer JavaDoc createMessageConsumer(Connection JavaDoc conn, Destination JavaDoc dest, String JavaDoc selector) throws Exception JavaDoc {
98         connections.add(conn);
99     
100         Session JavaDoc sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
101         final MessageConsumer JavaDoc consumer = sess.createConsumer(dest, selector);
102         conn.start();
103         
104         return consumer;
105     }
106
107     public void assertMessagesDividedAmongConsumers() {
108         assertEachConsumerReceivedAtLeastXMessages((messageCount * producerCount) / consumerCount);
109         assertEachConsumerReceivedAtMostXMessages(((messageCount * producerCount) / consumerCount) + 1);
110     }
111 }
112
Popular Tags