1
2
3
4
5
6
7
8
9
10
11 package org.mule.routing.requestreply;
12
13 import org.mule.MessageExchangePattern;
14 import org.mule.api.MuleEvent;
15 import org.mule.api.MuleException;
16 import org.mule.api.context.WorkManager;
17 import org.mule.api.context.WorkManagerSource;
18 import org.mule.api.endpoint.InboundEndpoint;
19 import org.mule.api.processor.RequestReplyRequesterMessageProcessor;
20 import org.mule.api.routing.ResponseTimeoutException;
21 import org.mule.api.service.Service;
22 import org.mule.processor.AsyncInterceptingMessageProcessor;
23 import org.mule.routing.requestreply.AbstractAsyncRequestReplyRequester;
24 import org.mule.tck.AbstractMuleTestCase;
25 import org.mule.tck.SensingNullMessageProcessor;
26
27 import java.beans.ExceptionListener;
28
29 import javax.resource.spi.work.Work;
30
31 public class AsyncRequestReplyRequesterTestCase extends AbstractMuleTestCase
32 implements ExceptionListener
33 {
34
35 public void testSingleEventNoTimeout() throws Exception
36 {
37 RequestReplyRequesterMessageProcessor asyncReplyMP = new TestAsyncRequestReplyRequester();
38 SensingNullMessageProcessor target = getSensingNullMessageProcessor();
39
40 asyncReplyMP.setListener(target);
41 asyncReplyMP.setReplySource(target.getMessageSource());
42
43 MuleEvent event = getTestEvent(TEST_MESSAGE, getTestService());
44
45 MuleEvent resultEvent = asyncReplyMP.process(event);
46
47
48 assertEquals(event.getMessageAsString(), resultEvent.getMessageAsString());
49 assertEquals(event.getMessage().getUniqueId(), resultEvent.getMessage().getUniqueId());
50 }
51
52 public void testSingleEventNoTimeoutAsync() throws Exception
53 {
54 RequestReplyRequesterMessageProcessor asyncReplyMP = new TestAsyncRequestReplyRequester();
55 SensingNullMessageProcessor target = getSensingNullMessageProcessor();
56 AsyncInterceptingMessageProcessor asyncMP = new AsyncInterceptingMessageProcessor(
57 new WorkManagerSource()
58 {
59
60 public WorkManager getWorkManager() throws MuleException
61 {
62 return muleContext.getWorkManager();
63 }
64 }, true);
65
66 asyncMP.setListener(target);
67 asyncReplyMP.setListener(asyncMP);
68 asyncReplyMP.setReplySource(target.getMessageSource());
69
70 MuleEvent event = getTestEvent(TEST_MESSAGE, getTestService(),
71 getTestInboundEndpoint(MessageExchangePattern.ONE_WAY));
72
73 MuleEvent resultEvent = asyncReplyMP.process(event);
74
75
76 assertEquals(event.getMessageAsString(), resultEvent.getMessageAsString());
77 assertEquals(event.getMessage().getUniqueId(), resultEvent.getMessage().getUniqueId());
78 }
79
80 public void testSingleEventTimeout() throws Exception
81 {
82 TestAsyncRequestReplyRequester asyncReplyMP = new TestAsyncRequestReplyRequester();
83 asyncReplyMP.setTimeout(1);
84 SensingNullMessageProcessor target = getSensingNullMessageProcessor();
85 target.setWaitTime(50);
86 AsyncInterceptingMessageProcessor asyncMP = new AsyncInterceptingMessageProcessor(
87 new WorkManagerSource()
88 {
89
90 public WorkManager getWorkManager() throws MuleException
91 {
92 return muleContext.getWorkManager();
93 }
94 }, true);
95
96 asyncMP.setListener(target);
97 asyncReplyMP.setListener(asyncMP);
98 asyncReplyMP.setReplySource(target.getMessageSource());
99
100 MuleEvent event = getTestEvent(TEST_MESSAGE, getTestService(),
101 getTestInboundEndpoint(MessageExchangePattern.ONE_WAY));
102
103 try
104 {
105 asyncReplyMP.process(event);
106 fail("ResponseTimeoutException expected");
107 }
108 catch (Exception e)
109 {
110 assertEquals(ResponseTimeoutException.class, e.getClass());
111 }
112 }
113
114 public void testMultiple() throws Exception
115 {
116 final RequestReplyRequesterMessageProcessor asyncReplyMP = new TestAsyncRequestReplyRequester();
117 SensingNullMessageProcessor target = getSensingNullMessageProcessor();
118 target.setWaitTime(50);
119 AsyncInterceptingMessageProcessor asyncMP = new AsyncInterceptingMessageProcessor(
120 new WorkManagerSource()
121 {
122
123 public WorkManager getWorkManager() throws MuleException
124 {
125 return muleContext.getWorkManager();
126 }
127 }, true);
128
129 asyncMP.setListener(target);
130 asyncReplyMP.setListener(asyncMP);
131 asyncReplyMP.setReplySource(target.getMessageSource());
132
133 final InboundEndpoint inboundEndpoint = getTestInboundEndpoint(MessageExchangePattern.ONE_WAY);
134 final Service service = getTestService();
135
136 for (int i = 0; i < 500; i++)
137 {
138 muleContext.getWorkManager().scheduleWork(new Work()
139 {
140 public void run()
141 {
142 MuleEvent event;
143 try
144 {
145 event = getTestEvent(TEST_MESSAGE, service, inboundEndpoint);
146 MuleEvent resultEvent = asyncReplyMP.process(event);
147
148
149 assertEquals(event.getMessageAsString(), resultEvent.getMessageAsString());
150 assertEquals(event.getMessage().getUniqueId(), resultEvent.getMessage().getUniqueId());
151 }
152 catch (Exception e)
153 {
154 throw new RuntimeException(e);
155 }
156 }
157
158 public void release()
159 {
160
161 }
162 });
163 }
164 }
165
166 public void exceptionThrown(Exception e)
167 {
168 e.printStackTrace();
169 fail(e.getMessage());
170 }
171
172 class TestAsyncRequestReplyRequester extends AbstractAsyncRequestReplyRequester
173 {
174
175 }
176 }