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