View Javadoc
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 2001-2004 (C) Exoffice Technologies Inc. All Rights Reserved. 42 * 43 * $Id: ReceiverCloseTest.java,v 1.7 2004/02/03 21:52:11 tanderson Exp $ 44 */ 45 package org.exolab.jmscts.test.session; 46 47 import java.util.List; 48 49 import javax.jms.Connection; 50 import javax.jms.Destination; 51 import javax.jms.Session; 52 53 import org.apache.log4j.Category; 54 55 import junit.framework.Test; 56 57 import org.exolab.jmscts.core.AbstractSendReceiveTestCase; 58 import org.exolab.jmscts.core.CompletionListener; 59 import org.exolab.jmscts.core.DelayedAction; 60 import org.exolab.jmscts.core.MessageReceiver; 61 import org.exolab.jmscts.core.SessionHelper; 62 import org.exolab.jmscts.core.TestContext; 63 import org.exolab.jmscts.core.TestCreator; 64 65 66 /*** 67 * This class tests the behaviour of closing a session while a receiver is 68 * active. 69 * 70 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a> 71 * @version $Revision: 1.7 $ 72 * @see AbstractSendReceiveTestCase 73 * @jmscts.message TextMessage 74 * @jmscts.delivery synchronous 75 */ 76 public class ReceiverCloseTest extends AbstractSendReceiveTestCase { 77 78 /*** 79 * The logger 80 */ 81 private static final Category log = 82 Category.getInstance(ReceiverCloseTest.class); 83 84 /*** 85 * The destination used by this test case 86 */ 87 private static final String DESTINATION = "ReceiverCloseTest"; 88 89 90 /*** 91 * Construct a new <code>ReceiverCloseTest</code> 92 * 93 * @param name the name of test case 94 */ 95 public ReceiverCloseTest(String name) { 96 super(name); 97 } 98 99 /*** 100 * Sets up the test suite 101 * 102 * @return an instance of this class that may be run by 103 * {@link org.exolab.jmscts.core.JMSTestRunner} 104 */ 105 public static Test suite() { 106 return TestCreator.createSendReceiveTest(ReceiverCloseTest.class); 107 } 108 109 /*** 110 * Returns if this test can share resources with other test cases. 111 * This implementation always returns <code>false</code>, to 112 * ensure that a new connection is created for each test. 113 * 114 * @return <code>false</code> 115 */ 116 public boolean share() { 117 return false; 118 } 119 120 /*** 121 * Returns if the connection should be started prior to running the test. 122 * This implementation always returns <code>false</code> to avoid 123 * conflicts with test cases 124 * 125 * @return <code>false</code> 126 */ 127 public boolean startConnection() { 128 return false; 129 } 130 131 /*** 132 * Returns the list of destination names used by this test case. These 133 * are used to pre-create destinations prior to running the test case. 134 * 135 * @return the list of destinations used by this test case 136 */ 137 public String[] getDestinations() { 138 return new String[]{DESTINATION}; 139 } 140 141 /*** 142 * Verifies that the receive timers for a closed session continue to 143 * advance, so receives may time out and return a null message while the 144 * session is stopped. 145 * 146 * @jmscts.requirement session.close.receivers 147 * @throws Exception for any error 148 */ 149 public void testSessionClose() throws Exception { 150 final long delayTime = 1000; // one second 151 final long completionTime = 30000; // 30 seconds 152 TestContext context = getContext(); 153 Connection connection = context.getConnection(); 154 final Session session = context.getSession(); 155 Destination destination = getDestination(DESTINATION); 156 MessageReceiver receiver = SessionHelper.createReceiver( 157 context, destination); 158 159 CompletionListener listener = new CompletionListener(1); 160 161 DelayedAction action = new DelayedAction(delayTime, listener) { 162 public void runProtected() throws Exception { 163 log.debug("Starting to close session"); 164 session.close(); 165 log.debug("Session closed"); 166 } 167 }; 168 169 // start the connection 170 connection.start(); 171 172 // close the session in a separate thread to allow the receiver 173 // to invoke receive() 174 action.start(); 175 176 try { 177 // wait indefinitely for a non-existent message 178 log.debug("Starting to receive messages"); 179 List result = receiver.receive(0, 0); 180 log.debug("Receive returned"); 181 } catch (Exception exception) { 182 fail("Expected synchronous consumer to time out and return " 183 + "null when session closed, but threw exception=" 184 + exception.getClass().getName() + ", message=" 185 + exception.getMessage()); 186 } 187 188 // wait for up to 30 seconds for the session to close 189 listener.waitForCompletion(completionTime); 190 191 if (listener.getCompleted() != 1) { 192 // The session didn't close. Invalidate the 193 // connection so that the test runner doesn't attempt to close 194 // it, which could result in the suite to hang. 195 context.invalidate(); 196 fail("Session.close() didn't return. Appears to have blocked"); 197 } 198 199 // receiver returned null. Verify that the session was closed 200 // successfully 201 if (action.getException() != null) { 202 Exception exception = action.getException(); 203 fail("Failed to close session, exception=" 204 + exception.getClass().getName() + ", message=" 205 + exception.getMessage()); 206 } 207 208 try { 209 // cannot unsubscribe durable consumers if the session 210 // is closed. Closing a consumer for a closed session 211 // should be ignored 212 receiver.close(); 213 } catch (Exception exception) { 214 fail("Attempting to invoke close() for a consumer on a " 215 + "closed session threw exception=" 216 + exception.getClass().getName() + ", message=" 217 + exception.getMessage()); 218 } 219 } 220 221 }

This page was automatically generated by Maven