View Javadoc
1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.ksb.messaging;
17  
18  import org.junit.Test;
19  import org.kuali.rice.ksb.api.KsbApiServiceLocator;
20  import org.kuali.rice.ksb.messaging.bam.BAMTargetEntry;
21  import org.kuali.rice.ksb.messaging.bam.service.BAMService;
22  import org.kuali.rice.ksb.messaging.service.KSBJavaService;
23  import org.kuali.rice.ksb.service.KSBServiceLocator;
24  import org.kuali.rice.ksb.test.KSBTestCase;
25  
26  import javax.xml.namespace.QName;
27  import java.util.List;
28  
29  import static org.junit.Assert.assertEquals;
30  import static org.junit.Assert.assertTrue;
31  
32  
33  /**
34   * Tests distributed Queue scenarios
35   *
36   * @author Kuali Rice Team (rice.collab@kuali.org)
37   *
38   */
39  public class DelayedAsynchronousServiceTest extends KSBTestCase {
40  
41      public boolean startClient1() {
42          return true;
43      }
44  
45      public boolean startClient2() {
46          return true;
47      }
48  
49      @Test public void testDelayedAsynchronousServiceCall() throws Exception {
50          KSBTestUtils.setMessagingToAsync();
51  
52          QName serviceName = new QName("testAppsSharedQueue", "sharedQueue");
53  
54          // Queue up the service to be called asynchronously after 5 seconds
55          KSBJavaService testJavaAsyncService = (KSBJavaService) KsbApiServiceLocator.getMessageHelper().getServiceAsynchronously(serviceName, "context", "value1", "value2", 5000);
56          testJavaAsyncService.invoke(new ClientAppServiceSharedPayloadObj("message content", false));
57          verifyServiceCalls(serviceName, false);
58  
59          // sleep for 1 second, should not have been called
60          Thread.sleep(1000);
61          verifyServiceCalls(serviceName, false);
62  
63          // sleep for 1 second, should not have been called
64          Thread.sleep(1000);
65          verifyServiceCalls(serviceName, false);
66  
67          // sleep for 1 second, should not have been called
68          Thread.sleep(1000);
69          verifyServiceCalls(serviceName, false);
70  
71          Thread.sleep(1000);
72          verifyServiceCalls(serviceName, false);
73  
74          // TODO this isn't the best test ever because it's relying on waits and timing which is most likely doomed to occasional
75          // failure in the CI environment.  If this occurs than I may need to yank this.  A better long term solution would be
76          // to allow for the use of callbacks for delayed asynchronous services but that's not something I wanted to try
77          // to tackle at the moment
78  
79          // now keep trying for 60 more seconds
80          verifyServiceCalls(serviceName, true, 60000);
81  
82      }
83  
84      private void verifyServiceCalls(QName serviceName, boolean shouldHaveBeenCalled) throws Exception {
85          verifyServiceCalls(serviceName, shouldHaveBeenCalled, 0);
86      }
87  
88      private void verifyServiceCalls(QName serviceName, boolean shouldHaveBeenCalled, long wait) throws Exception {
89          BAMService bamService = KSBServiceLocator.getBAMService();
90          long start = System.currentTimeMillis();
91          int numBamCalls = 0;
92          boolean foundClientCall = false;
93          boolean foundServiceCall = false;
94          do {
95              List<BAMTargetEntry> bamCalls = bamService.getCallsForService(serviceName);
96              numBamCalls = bamCalls.size();
97              if (!shouldHaveBeenCalled) {
98                  assertTrue("A service call should not have been recorded yet.", numBamCalls == 0);
99                  break;
100             } else {
101                 for (BAMTargetEntry bamEntry : bamCalls) {
102                     if (bamEntry.getServerInvocation()) {
103                         foundServiceCall = true;
104                     } else {
105                         foundClientCall = true;
106                     }
107                 }
108                 if (foundServiceCall && foundClientCall) {
109                     break;
110                 }
111             }
112             Thread.sleep(1000);
113         } while ((System.currentTimeMillis() - start) < wait);
114         if (shouldHaveBeenCalled) {
115             assertTrue("No service call recorded", numBamCalls > 0);
116             assertTrue("No client call recorded", foundClientCall);
117             assertTrue("No service call recorded", foundServiceCall);
118             assertEquals("Wrong number of calls recorded", 2, numBamCalls);
119         }
120     }
121 
122 }