001: /*
002: * Copyright 2007 The Kuali Foundation
003: *
004: * Licensed under the Educational Community License, Version 1.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/ecl1.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 edu.iu.uis.eden.messaging;
017:
018: import java.util.List;
019:
020: import javax.xml.namespace.QName;
021:
022: import org.junit.Test;
023: import org.kuali.bus.services.KSBServiceLocator;
024: import org.kuali.bus.test.KSBTestCase;
025:
026: import edu.iu.uis.eden.messaging.bam.BAMService;
027: import edu.iu.uis.eden.messaging.bam.BAMTargetEntry;
028:
029: /**
030: * Tests distributed Queue scenarios
031: *
032: * @author Kuali Rice Team (kuali-rice@googlegroups.com)
033: *
034: */
035: public class DelayedAsynchronousServiceTest extends KSBTestCase {
036:
037: public boolean startClient1() {
038: return true;
039: }
040:
041: public boolean startClient2() {
042: return true;
043: }
044:
045: @Test
046: public void testDelayedAsynchronousServiceCall() throws Exception {
047: KSBTestUtils.setMessagingToAsync();
048:
049: QName serviceName = new QName("testAppsSharedQueue",
050: "sharedQueue");
051:
052: // Queue up the service to be called asynchronously after 5 seconds
053: KEWJavaService testJavaAsyncService = (KEWJavaService) KSBServiceLocator
054: .getMessageHelper().getServiceAsynchronously(
055: serviceName, "context", "value1", "value2",
056: 5000);
057: testJavaAsyncService
058: .invoke(new ClientAppServiceSharedPayloadObj(
059: "message content", false));
060: verifyServiceCalls(serviceName, false);
061:
062: // sleep for 1 second, should not have been called
063: Thread.sleep(1000);
064: verifyServiceCalls(serviceName, false);
065:
066: // sleep for 1 second, should not have been called
067: Thread.sleep(1000);
068: verifyServiceCalls(serviceName, false);
069:
070: // sleep for 1 second, should not have been called
071: Thread.sleep(1000);
072: verifyServiceCalls(serviceName, false);
073:
074: Thread.sleep(1000);
075: verifyServiceCalls(serviceName, false);
076:
077: // TODO this isn't the best test ever because it's relying on waits and timing which is most likely doomed to occasional
078: // failure in the CI environment. If this occurs than I may need to yank this. A better long term solution would be
079: // to allow for the use of callbacks for delayed asynchronous services but that's not something I wanted to try
080: // to tackle at the moment
081:
082: // now sleep for 3 more seconds, the call should be invoked
083: Thread.sleep(3000);
084: verifyServiceCalls(serviceName, true);
085:
086: }
087:
088: private void verifyServiceCalls(QName serviceName,
089: boolean shouldHaveBeenCalled) throws Exception {
090: BAMService bamService = KSBServiceLocator.getBAMService();
091: List<BAMTargetEntry> bamCalls = bamService
092: .getCallsForService(serviceName);
093: if (!shouldHaveBeenCalled) {
094: assertTrue(
095: "A service call should not have been recorded yet.",
096: bamCalls.size() == 0);
097: } else {
098: assertTrue("No service call recorded", bamCalls.size() > 0);
099: boolean foundClientCall = false;
100: boolean foundServiceCall = false;
101: for (BAMTargetEntry bamEntry : bamCalls) {
102: if (bamEntry.getServerInvocation()) {
103: foundServiceCall = true;
104: } else {
105: foundClientCall = true;
106: }
107: }
108: assertTrue("No client call recorded", foundClientCall);
109: assertTrue("No service call recorded", foundServiceCall);
110: assertEquals("Wrong number of calls recorded", 2, bamCalls
111: .size());
112: }
113: }
114:
115: }
|