1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.ksb.messaging;
17
18 import org.junit.Test;
19 import org.kuali.rice.core.api.config.property.ConfigContext;
20 import org.kuali.rice.ksb.api.KsbApiServiceLocator;
21 import org.kuali.rice.ksb.messaging.remotedservices.TesetHarnessExplodingQueue;
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 import org.kuali.rice.ksb.util.KSBConstants;
26 import org.kuali.rice.test.TestUtilities;
27
28 import javax.xml.namespace.QName;
29 import java.util.List;
30
31 import static org.junit.Assert.assertEquals;
32 import static org.junit.Assert.fail;
33
34
35
36
37
38
39
40
41 public class ExceptionRetryCountTest extends KSBTestCase {
42
43
44 private QName retryCountServiceName = new QName("KEW", "testExplodingRetryCount");
45 private TestCallback callback = new TestCallback();
46
47 @Override
48 public void setUp() throws Exception {
49 System.setProperty(KSBConstants.Config.ROUTE_QUEUE_TIME_INCREMENT_KEY, "500");
50 System.setProperty(KSBConstants.Config.ROUTE_QUEUE_MAX_RETRY_ATTEMPTS_KEY, "2");
51 super.setUp();
52 GlobalCallbackRegistry.getCallbacks().clear();
53 GlobalCallbackRegistry.getCallbacks().add(this.callback);
54 TestCallback.clearCallbacks();
55 TesetHarnessExplodingQueue.NUM_CALLS = 0;
56 }
57
58 @Override
59 public void tearDown() throws Exception {
60 try {
61 KSBServiceLocator.getScheduler().shutdown();
62 } finally {
63 super.tearDown();
64 }
65 }
66
67
68
69
70
71
72 @Test
73 public void testRetryCount() throws Exception {
74
75
76 ConfigContext.getCurrentContextConfig().putProperty(KSBConstants.Config.ROUTE_QUEUE_TIME_INCREMENT_KEY, "100");
77
78 KSBJavaService explodingQueue = (KSBJavaService) KsbApiServiceLocator.getMessageHelper().getServiceAsynchronously(
79 this.retryCountServiceName);
80 explodingQueue.invoke("");
81 TestUtilities.waitForExceptionRouting();
82
83 this.callback.pauseUntilNumberCallbacksUsingStaticCounter(3, this.retryCountServiceName);
84
85
86 int i = 0;
87 while (i++ < 30) {
88 List<PersistedMessageBO> queuedItems = KSBServiceLocator.getMessageQueueService().findAll();
89 if (queuedItems.size() != 1) {
90 fail("test setup wrong should have a single item in the queue.");
91 }
92 PersistedMessageBO message = queuedItems.get(0);
93 if (message.getQueueStatus().equals("E")) {
94 break;
95 }
96 System.out.println("Message not saved to queue in 'E' status. Sleeping 1 sec.");
97 Thread.sleep(1000);
98 }
99
100 assertEquals("Service should have been called 3 times", 3, TesetHarnessExplodingQueue.NUM_CALLS);
101
102 List<PersistedMessageBO> messagesQueued = KSBServiceLocator.getMessageQueueService().findByServiceName(
103 this.retryCountServiceName, "invoke");
104 PersistedMessageBO message = messagesQueued.get(0);
105 assertEquals("Message should be in exception status", KSBConstants.ROUTE_QUEUE_EXCEPTION, message.getQueueStatus());
106 assertEquals("Message retry count not what was configured", new Integer(2), message.getRetryCount());
107 }
108
109 }