View Javadoc

1   /**
2    * Copyright 2005-2011 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.callbacks.SimpleCallback;
23  import org.kuali.rice.ksb.messaging.service.KSBJavaService;
24  import org.kuali.rice.ksb.service.KSBServiceLocator;
25  import org.kuali.rice.ksb.test.KSBTestCase;
26  
27  import javax.xml.namespace.QName;
28  import java.util.List;
29  
30  import static org.junit.Assert.assertEquals;
31  import static org.junit.Assert.assertTrue;
32  
33  /**
34   * Tests distributed Queue scenarios
35   * 
36   * @author Kuali Rice Team (rice.collab@kuali.org)
37   * 
38   */
39  public class DistributedQueueTest extends KSBTestCase {
40  
41      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(DistributedQueueTest.class);
42      
43      public boolean startClient1() {
44          return true;
45      }
46  
47      public boolean startClient2() {
48          return true;
49      }
50  
51      /**
52       * If calling a queue with multiple subscribers only one subscriber should be called.
53       * 
54       * @throws Exception
55       */
56      @Test
57      public void testSuccessfullyCallingQueueOnce() throws Exception {
58          QName serviceName = new QName("testAppsSharedQueue", "sharedQueue");
59          KSBJavaService testJavaAsyncService = (KSBJavaService) KsbApiServiceLocator.getMessageHelper().getServiceAsynchronously(serviceName);
60          testJavaAsyncService.invoke(new ClientAppServiceSharedPayloadObj("message content", false));
61          verifyServiceCalls(serviceName);
62  
63      }
64  
65      @Test
66      public void testCallingQueueAsnyc() throws Exception {
67          KSBTestUtils.setMessagingToAsync();
68          // Execute this 100 times, previously when only running a single iteration, this test would pass most of the
69          // time but then would occasionally fail, it was possible to reproduce the failure every time by adding
70          // these iterations, this allowed us to fix the underlying problem (an issue with KSB callbacks)
71          for (int i = 0; i < 100; i++) {
72              LOG.info("testCallingQueueAsnyc, iteration: " + i);
73              QName serviceName = new QName("testAppsSharedQueue", "sharedQueue");
74              SimpleCallback callback = new SimpleCallback();
75              KSBJavaService testJavaAsyncService = (KSBJavaService) KsbApiServiceLocator.getMessageHelper().getServiceAsynchronously(serviceName, callback);
76              synchronized (callback) {
77                  testJavaAsyncService.invoke(new ClientAppServiceSharedPayloadObj("message content", false));
78                  callback.waitForAsyncCall();
79              }
80              verifyServiceCalls(serviceName);
81              KSBServiceLocator.getBAMService().clearBAMTables();
82          }
83      }
84  
85      private void verifyServiceCalls(QName serviceName) throws Exception {
86          BAMService bamService = KSBServiceLocator.getBAMService();
87          List<BAMTargetEntry> bamCalls = bamService.getCallsForService(serviceName);
88          assertTrue("No service call recorded", bamCalls.size() > 0);
89          boolean foundClientCall = false;
90          boolean foundServiceCall = false;
91          for (BAMTargetEntry bamEntry : bamCalls) {
92              if (bamEntry.getServerInvocation()) {
93                  foundServiceCall = true;
94              } else {
95                  foundClientCall = true;
96              }
97          }
98          assertTrue("No client call recorded", foundClientCall);
99          assertTrue("No service call recorded", foundServiceCall);
100         assertEquals("Wrong number of calls recorded", 2, bamCalls.size());
101     }
102 
103 }