View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.registry;
8   
9   import org.mule.DefaultMuleMessage;
10  import org.mule.OptimizedRequestContext;
11  import org.mule.RequestContext;
12  import org.mule.api.MuleContext;
13  import org.mule.api.MuleEvent;
14  import org.mule.api.MuleException;
15  import org.mule.api.MuleMessage;
16  import org.mule.api.MuleSession;
17  import org.mule.api.ThreadSafeAccess;
18  import org.mule.api.construct.FlowConstruct;
19  import org.mule.api.endpoint.ImmutableEndpoint;
20  import org.mule.api.security.Credentials;
21  import org.mule.api.transformer.DataType;
22  import org.mule.api.transformer.TransformerException;
23  import org.mule.api.transport.ReplyToHandler;
24  import org.mule.management.stats.ProcessingTime;
25  import org.mule.message.DefaultExceptionPayload;
26  import org.mule.tck.junit4.AbstractMuleTestCase;
27  
28  import java.io.OutputStream;
29  
30  import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicBoolean;
31  import org.junit.Test;
32  
33  import static org.junit.Assert.assertEquals;
34  import static org.mockito.Mockito.mock;
35  
36  public class RequestContextTestCase extends AbstractMuleTestCase
37  {
38  
39      private boolean threadSafeEvent;
40      private MuleContext muleContext = mock(MuleContext.class);
41  
42      @Test
43      public void testSetExceptionPayloadAcrossThreads() throws InterruptedException
44      {
45          threadSafeEvent = true;
46          MuleEvent event = new DummyEvent();
47          runThread(event, false);
48          runThread(event, true);
49      }
50  
51      @Test
52      public void testFailureWithoutThreadSafeEvent() throws InterruptedException
53      {
54          threadSafeEvent = false;
55          MuleEvent event = new DummyEvent();
56          runThread(event, false);
57          runThread(event, true);
58      }
59  
60      protected void runThread(MuleEvent event, boolean doTest) throws InterruptedException
61      {
62          AtomicBoolean success = new AtomicBoolean(false);
63          Thread thread = new Thread(new SetExceptionPayload(event, success));
64          thread.start();
65          thread.join();
66          if (doTest)
67          {
68              assertEquals(threadSafeEvent, success.get());
69          }
70      }
71  
72      private class SetExceptionPayload implements Runnable
73      {
74  
75          private MuleEvent event;
76          private AtomicBoolean success;
77  
78          public SetExceptionPayload(MuleEvent event, AtomicBoolean success)
79          {
80              this.event = event;
81              this.success = success;
82          }
83  
84          public void run()
85          {
86              try
87              {
88                  OptimizedRequestContext.unsafeSetEvent(event);
89                  RequestContext.setExceptionPayload(new DefaultExceptionPayload(new Exception()));
90                  success.set(true);
91              }
92              catch (RuntimeException e)
93              {
94                  logger.error("error in thread", e);
95              }
96          }
97  
98      }
99  
100     private class DummyEvent implements MuleEvent, ThreadSafeAccess
101     {
102 
103         private MuleMessage message = new DefaultMuleMessage(null, muleContext);
104 
105         public MuleMessage getMessage()
106         {
107             return message;
108         }
109 
110         public Credentials getCredentials()
111         {
112             return null;  
113         }
114 
115         public byte[] getMessageAsBytes() throws MuleException
116         {
117             return new byte[0];  
118         }
119 
120         public Object transformMessage() throws TransformerException
121         {
122             return null;  
123         }
124 
125         public Object transformMessage(Class outputType) throws TransformerException
126         {
127             return null;  
128         }
129 
130         @Deprecated
131         public byte[] transformMessageToBytes() throws TransformerException
132         {
133             return new byte[0];  
134         }
135 
136         public String transformMessageToString() throws TransformerException
137         {
138             return null;  
139         }
140 
141         public String getMessageAsString() throws MuleException
142         {
143             return null;  
144         }
145 
146         public <T> T transformMessage(DataType<T> outputType) throws TransformerException
147         {
148             return null;
149         }
150 
151         public String getTransformedMessageAsString(String encoding) throws TransformerException
152         {
153             return null;  
154         }
155 
156         public String getMessageAsString(String encoding) throws MuleException
157         {
158             return null;  
159         }
160 
161         public String getId()
162         {
163             return null;  
164         }
165 
166         public Object getProperty(String name)
167         {
168             return null;  
169         }
170 
171         public Object getProperty(String name, Object defaultValue)
172         {
173             return null;  
174         }
175 
176         public ImmutableEndpoint getEndpoint()
177         {
178             return null;  
179         }
180 
181         public MuleSession getSession()
182         {
183             return null;  
184         }
185 
186         public FlowConstruct getFlowConstruct()
187         {
188             return null;  
189         }
190 
191         public boolean isStopFurtherProcessing()
192         {
193             return false;  
194         }
195 
196         public void setStopFurtherProcessing(boolean stopFurtherProcessing)
197         {
198             // no action
199         }
200 
201         public boolean isSynchronous()
202         {
203             return false;  
204         }
205 
206         public void setSynchronous(boolean value)
207         {
208             // no action
209         }
210 
211         public int getTimeout()
212         {
213             return 0;  
214         }
215 
216         public void setTimeout(int timeout)
217         {
218             // no action
219         }
220 
221         public OutputStream getOutputStream()
222         {
223             return null;  
224         }
225 
226         public String getEncoding()
227         {
228             return null;  
229         }
230 
231         public MuleContext getMuleContext()
232         {
233             return null;  
234         }
235 
236         public void assertAccess(boolean write)
237         {
238             // no action
239         }
240 
241         public void resetAccessControl()
242         {
243             // no action
244         }
245 
246         public ThreadSafeAccess newThreadCopy()
247         {
248             if (threadSafeEvent)
249             {
250                 return new DummyEvent();
251             }
252             else
253             {
254                 return this;
255             }
256         }
257 
258         public ProcessingTime getProcessingTime()
259         {
260             return null;
261         }
262 
263         public ReplyToHandler getReplyToHandler() {
264             return null;
265         }
266 
267         public Object getReplyToDestination() {
268             return null;
269         }
270 
271         public void captureReplyToDestination() {
272             // no action
273         }
274     }
275 
276 }