1 /***
2 * Redistribution and use of this software and associated documentation
3 * ("Software"), with or without modification, are permitted provided
4 * that the following conditions are met:
5 *
6 * 1. Redistributions of source code must retain copyright
7 * statements and notices. Redistributions must also contain a
8 * copy of this document.
9 *
10 * 2. Redistributions in binary form must reproduce the
11 * above copyright notice, this list of conditions and the
12 * following disclaimer in the documentation and/or other
13 * materials provided with the distribution.
14 *
15 * 3. The name "Exolab" must not be used to endorse or promote
16 * products derived from this Software without prior written
17 * permission of Exoffice Technologies. For written permission,
18 * please contact tma@netspace.net.au.
19 *
20 * 4. Products derived from this Software may not be called "Exolab"
21 * nor may "Exolab" appear in their names without prior written
22 * permission of Exoffice Technologies. Exolab is a registered
23 * trademark of Exoffice Technologies.
24 *
25 * 5. Due credit should be given to the Exolab Project
26 * (http://www.exolab.org/).
27 *
28 * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32 * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39 * OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Copyright 2003-2004 (C) Exoffice Technologies Inc. All Rights Reserved.
42 *
43 * $Id: SendReceiveStressTestCase.java,v 1.3 2004/02/03 07:34:54 tanderson Exp $
44 */
45 package org.exolab.jmscts.stress;
46
47 import javax.jms.Message;
48 import javax.jms.Session;
49
50 import org.apache.log4j.Category;
51
52 import org.exolab.jmscts.core.AckType;
53 import org.exolab.jmscts.core.AbstractSendReceiveTestCase;
54 import org.exolab.jmscts.core.CountingListener;
55 import org.exolab.jmscts.core.MessageReceiver;
56 import org.exolab.jmscts.core.MessageSender;
57 import org.exolab.jmscts.core.ReceiptType;
58 import org.exolab.jmscts.core.TestContext;
59 import org.exolab.jmscts.core.TestContextHelper;
60 import org.exolab.jmscts.core.TestProperties;
61 import org.exolab.jmscts.core.TestStatistics;
62 import org.exolab.jmscts.core.ThreadedAction;
63 import org.exolab.jmscts.core.ThreadedActions;
64 import org.exolab.jmscts.core.ThreadedReceiver;
65 import org.exolab.jmscts.core.ThreadedSender;
66 import org.exolab.jmscts.report.types.StatisticType;
67
68
69 /***
70 * This class provides functionality for stress tests involving a single
71 * producer, and multiple concurrent consumers, for a single destination
72 *
73 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
74 * @version $Revision: 1.3 $
75 */
76 public class SendReceiveStressTestCase extends AbstractSendReceiveTestCase {
77
78 /***
79 * The number of messages to send
80 */
81 private int _count;
82
83 /***
84 * The destination to use
85 */
86 private final String _destination;
87
88 /***
89 * The message sender
90 */
91 private MessageSender _sender;
92
93 /***
94 * The number of receivers to user
95 */
96 private final int _receiverCount;
97
98 /***
99 * The message listener
100 */
101 private CountingListener _listener;
102
103 /***
104 * The message receivers
105 */
106 private MessageReceiver[] _receivers;
107
108 /***
109 * The contexts used by the receivers
110 */
111 private TestContext[] _contexts;
112
113 /***
114 * The logger
115 */
116 private static final Category log = Category.getInstance(
117 SendReceiveStressTestCase.class);
118
119
120 /***
121 * Construct a <code>SendReceiveStressTestCase</code> for a specific
122 * test case
123 *
124 * @param name the name of test case
125 * @param destination the name of the destination to use
126 * @param receivers the number of receivers
127 */
128 public SendReceiveStressTestCase(String name, String destination,
129 int receivers) {
130 super(name);
131 _destination = destination;
132 _receiverCount = receivers;
133 }
134
135 /***
136 * Runs the stress test
137 *
138 * @throws Exception for any error
139 */
140 protected void runStress() throws Exception {
141 // the timeout defaults to 2 seconds, but can be overriden by defining
142 // a property
143 final int defaultTimeout = 2000;
144 long timeout = TestProperties.getInt(getClass(), "timeout",
145 defaultTimeout);
146
147 TestContext context = getContext();
148
149 // starts and monitors the completion of the receivers.
150 ThreadedActions receive = new ThreadedActions();
151 for (int i = 0; i < _receiverCount; ++i) {
152 ThreadedAction action = new ThreadedReceiver(
153 _receivers[i], timeout, _listener);
154 receive.addAction(action);
155 }
156
157 TestStatistics stats = context.getStatistics();
158
159 Message message = context.getMessage();
160 ThreadedSender send = new ThreadedSender(_sender, message, _count);
161
162 receive.start(); // run the receivers in a separate thread
163 send.run(); // run the send in the current thread
164
165 receive.waitForCompletion();
166
167 if (receive.getException() != null) {
168 throw receive.getException();
169 }
170 if (send.getException() != null) {
171 throw send.getException();
172 }
173
174 int expected = _listener.getExpected();
175 int received = _listener.getReceived();
176 assertEquals("Expected " + expected + " messages to be received, "
177 + "but got " + received, expected, received);
178
179 stats.log(StatisticType.SEND, _count, send.getElapsedTime());
180 stats.log(StatisticType.RECEIVE, _count, receive.getElapsedTime());
181 }
182
183 /***
184 * Sets up the test
185 *
186 * @throws Exception for any error
187 */
188 protected void setUp() throws Exception {
189 // the number of messages to send/receive defaults to 1000, but
190 // can be overriden by defining a property
191 final int defaultCount = 1000;
192 _count = TestProperties.getInt(getClass(), "count", defaultCount);
193
194 TestContext context = getContext();
195 _sender = createSender(_destination);
196 _receivers = new MessageReceiver[_receiverCount];
197 _contexts = new TestContext[_receiverCount];
198 AckType ack = context.getAckType();
199 ReceiptType receipt = context.getMessagingBehaviour().getReceiptType();
200
201 // all message handling is handled via a single listener.
202 // Queue messages will be shared across all receivers, whereas
203 // each topic message will be received by all subscribers.
204 int expected;
205 if (context.isQueue() && !receipt.equals(ReceiptType.BROWSER)) {
206 expected = _count;
207 } else {
208 expected = _count * _receiverCount;
209 }
210 if (ack.getAcknowledgeMode() == Session.CLIENT_ACKNOWLEDGE
211 && !receipt.equals(ReceiptType.BROWSER)) {
212 // for client ack sessions with consumers, ack each message
213 _listener = new AckingListener(expected);
214 } else {
215 _listener = new CountingListener(expected);
216 }
217
218 // set up the receivers. These will use the same connection as
219 // the sender, but different sessions
220 for (int i = 0; i < _receiverCount; ++i) {
221 TestContext receiveContext =
222 TestContextHelper.createSendReceiveContext(context, false);
223 _contexts[i] = receiveContext;
224
225 MessageReceiver receiver =
226 createReceiver(receiveContext, _destination);
227 _receivers[i] = receiver;
228 }
229 }
230
231 /***
232 * Cleans up after the test
233 *
234 * @throws Exception for any error
235 */
236 protected void tearDown() throws Exception {
237 close(_sender);
238 close(_receivers);
239 for (int i = 0; i < _contexts.length; ++i) {
240 TestContext context = _contexts[i];
241 if (context != null) {
242 context.getSession().close();
243 }
244 }
245 }
246
247 }
This page was automatically generated by Maven