View Javadoc

1   /*
2    * Copyright 2007 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 java.util.List;
19  
20  import javax.xml.namespace.QName;
21  
22  import org.junit.Test;
23  import org.kuali.rice.ksb.messaging.bam.BAMTargetEntry;
24  import org.kuali.rice.ksb.messaging.bam.service.BAMService;
25  import org.kuali.rice.ksb.messaging.service.KSBJavaService;
26  import org.kuali.rice.ksb.service.KSBServiceLocator;
27  import org.kuali.rice.ksb.test.KSBTestCase;
28  
29  
30  /**
31   * Tests distributed Queue scenarios
32   *
33   * @author Kuali Rice Team (rice.collab@kuali.org)
34   *
35   */
36  public class DelayedAsynchronousServiceTest extends KSBTestCase {
37  
38      public boolean startClient1() {
39  	return true;
40      }
41  
42      public boolean startClient2() {
43  	return true;
44      }
45  
46      @Test public void testDelayedAsynchronousServiceCall() throws Exception {
47  	KSBTestUtils.setMessagingToAsync();
48  
49  	QName serviceName = new QName("testAppsSharedQueue", "sharedQueue");
50  
51  	// Queue up the service to be called asynchronously after 5 seconds
52  	KSBJavaService testJavaAsyncService = (KSBJavaService) KSBServiceLocator.getMessageHelper().getServiceAsynchronously(serviceName, "context", "value1", "value2", 5000);
53  	testJavaAsyncService.invoke(new ClientAppServiceSharedPayloadObj("message content", false));
54  	verifyServiceCalls(serviceName, false);
55  
56  	// sleep for 1 second, should not have been called
57  	Thread.sleep(1000);
58  	verifyServiceCalls(serviceName, false);
59  
60  	// sleep for 1 second, should not have been called
61  	Thread.sleep(1000);
62  	verifyServiceCalls(serviceName, false);
63  
64  	// sleep for 1 second, should not have been called
65  	Thread.sleep(1000);
66  	verifyServiceCalls(serviceName, false);
67  
68  	Thread.sleep(1000);
69  	verifyServiceCalls(serviceName, false);
70  
71  	// TODO this isn't the best test ever because it's relying on waits and timing which is most likely doomed to occasional
72  	// failure in the CI environment.  If this occurs than I may need to yank this.  A better long term solution would be
73  	// to allow for the use of callbacks for delayed asynchronous services but that's not something I wanted to try
74  	// to tackle at the moment
75  
76  	// now sleep for 3 more seconds, the call should be invoked
77  	Thread.sleep(3000);
78  	verifyServiceCalls(serviceName, true);
79  
80      }
81  
82      private void verifyServiceCalls(QName serviceName, boolean shouldHaveBeenCalled) throws Exception {
83  	BAMService bamService = KSBServiceLocator.getBAMService();
84  	List<BAMTargetEntry> bamCalls = bamService.getCallsForService(serviceName);
85  	if (!shouldHaveBeenCalled) {
86  	    assertTrue("A service call should not have been recorded yet.", bamCalls.size() == 0);
87  	} else {
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 
104 }