001 /**
002 * Copyright 2005-2011 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.rice.ksb.messaging.exceptionhandling;
017
018 import static org.junit.Assert.assertEquals;
019 import static org.junit.Assert.assertTrue;
020
021 import java.util.List;
022
023 import javax.xml.namespace.QName;
024
025 import org.junit.Test;
026 import org.kuali.rice.ksb.api.KsbApiServiceLocator;
027 import org.kuali.rice.ksb.messaging.GlobalCallbackRegistry;
028 import org.kuali.rice.ksb.messaging.PersistedMessageBO;
029 import org.kuali.rice.ksb.messaging.TestCallback;
030 import org.kuali.rice.ksb.messaging.remotedservices.TesetHarnessExplodingQueue;
031 import org.kuali.rice.ksb.messaging.service.KSBJavaService;
032 import org.kuali.rice.ksb.service.KSBServiceLocator;
033 import org.kuali.rice.ksb.test.KSBTestCase;
034 import org.kuali.rice.ksb.util.KSBConstants;
035 import org.kuali.rice.test.TestUtilities;
036
037
038 /**
039 * Tests various exception messaging cases
040 *
041 * Millis to live - that a message with no home is still sending messages while it's time to live hasn't expired
042 * Retry count - that a message configured with a retry count will send x number of messages before being marked exception
043 * Being marked as exception - that a message in exception is in the route log and marked with a status of 'E'
044 * Defuault retry count - that a message configured with no retry or time to live is retry the default number of times as
045 * noted in an app constant and a class default if that constant is not a number or doesn't exist
046 * App Constant to determine the default time increment works (we need this to effectively test anyway)
047 * Things work without the timeincrement constant in place
048 *
049 * @author Kuali Rice Team (rice.collab@kuali.org)
050 *
051 */
052 public class ExceptionMessagingTest extends KSBTestCase {
053
054
055 private QName queueTimeToLiveServiceName = new QName("KEW", "explodingQueueTimeLimit");
056 private TestCallback callback = new TestCallback();
057
058 @Override
059 public void setUp() throws Exception {
060 System.setProperty(KSBConstants.Config.ROUTE_QUEUE_TIME_INCREMENT_KEY, "500");
061 System.setProperty(KSBConstants.Config.ROUTE_QUEUE_MAX_RETRY_ATTEMPTS_KEY, "5");
062 super.setUp();
063 GlobalCallbackRegistry.getCallbacks().clear();
064 GlobalCallbackRegistry.getCallbacks().add(this.callback);
065 TestCallback.clearCallbacks();
066 TesetHarnessExplodingQueue.NUM_CALLS = 0;
067 }
068
069 @Override
070 public void tearDown() throws Exception {
071 try {
072 KSBServiceLocator.getScheduler().shutdown();
073 } finally {
074 super.tearDown();
075 }
076 }
077
078
079
080 /**
081 * test that service is in queue marked 'E' when the time to live is expired.
082 * @throws Exception
083 */
084 @Test public void testTimeToLive() throws Exception {
085
086 KSBJavaService explodingQueue = (KSBJavaService) KsbApiServiceLocator.getMessageHelper().getServiceAsynchronously(this.queueTimeToLiveServiceName);
087 explodingQueue.invoke("");
088 TestUtilities.waitForExceptionRouting();
089 //this service is on a 3 second wait the queue is on a 1 sec.
090 Thread.sleep(10000);
091
092 //verify the entry is in exception routing
093 List<PersistedMessageBO> messagesQueued = KSBServiceLocator.getMessageQueueService().findByServiceName(this.queueTimeToLiveServiceName, "invoke");
094 PersistedMessageBO message = messagesQueued.get(0);
095 assertEquals("Message should be in exception status", KSBConstants.ROUTE_QUEUE_EXCEPTION, message.getQueueStatus());
096 assertTrue("Message expiration date should be equal to or earlier than last queue date", message.getExpirationDate().getTime() <= message.getQueueDate().getTime());
097 }
098
099
100 }