View Javadoc

1   /*
2    * $Id: MuleEventWorkTestCase.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.work;
12  
13  import org.mule.DefaultMuleMessage;
14  import org.mule.OptimizedRequestContext;
15  import org.mule.RequestContext;
16  import org.mule.api.MuleEvent;
17  import org.mule.api.ThreadSafeAccess;
18  import org.mule.tck.AbstractMuleTestCase;
19  import org.mule.util.concurrent.Latch;
20  
21  import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
22  
23  /**
24   * Test case to reproduce issue described in MULE-4407 and validate fix.
25   */
26  public class MuleEventWorkTestCase extends AbstractMuleTestCase
27  {
28  
29      protected MuleEvent originalEvent;
30      protected Latch latch = new Latch();
31  
32      @Override
33      protected void doSetUp() throws Exception
34      {
35          super.doSetUp();
36          // Create a dummy event and give it some properties
37          originalEvent = getTestEvent("test");
38          originalEvent.getMessage().setOutboundProperty("test", "val");
39          originalEvent.getMessage().setOutboundProperty("test2", "val2");
40          OptimizedRequestContext.unsafeSetEvent(originalEvent);
41      }
42  
43      public void testScheduleMuleEventWork() throws Exception
44      {
45          muleContext.getWorkManager().scheduleWork(new TestMuleEventWork(originalEvent));
46  
47          assertTrue("Timed out waiting for latch", latch.await(2000, TimeUnit.MILLISECONDS));
48  
49          assertSame(originalEvent, RequestContext.getEvent());
50  
51          try
52          {
53              // Ensure that even after Work has been created, scheduled and executed
54              // the original event instance is still owned by this thread and still
55              // mutable.
56              ((DefaultMuleMessage) originalEvent.getMessage()).assertAccess(ThreadSafeAccess.WRITE);
57          }
58          catch (Exception e)
59          {
60              fail(e.getMessage());
61          }
62      }
63  
64      public void testRunMuleEventWork() throws Exception
65      {
66          new TestMuleEventWork(originalEvent).run();
67  
68          // NOTE: This assertion documents/tests current behaviour but does not seem
69          // correct.
70          // In scenarios where Work implementations are run in the same thread rather
71          // than being scheduled then the RequestContext ThreadLocal value is
72          // overwritten with a new copy which is not desirable.
73          // See: MULE-4409
74          assertNotSame(originalEvent, RequestContext.getEvent());
75  
76          try
77          {
78              // Ensure that even after Work has been created, scheduled and executed
79              // the original event instance is still owned by this thread and still
80              // mutable.
81              ((DefaultMuleMessage) originalEvent.getMessage()).assertAccess(ThreadSafeAccess.WRITE);
82          }
83          catch (Exception e)
84          {
85              fail(e.getMessage());
86          }
87  
88      }
89  
90      private class TestMuleEventWork extends AbstractMuleEventWork
91      {
92  
93          public TestMuleEventWork(MuleEvent event)
94          {
95              super(event);
96          }
97  
98          @Override
99          protected void doRun()
100         {
101             assertNotSame("MuleEvent", event, originalEvent);
102             assertNotNull("RequestContext.getEvent() is null", RequestContext.getEvent());
103 
104             try
105             {
106                 // Ensure that the new event copied for this event is owned by the
107                 // thread that is executing this work and is mutable
108                 ((DefaultMuleMessage) event.getMessage()).assertAccess(ThreadSafeAccess.WRITE);
109             }
110             catch (Exception e)
111             {
112                 fail(e.getMessage());
113             }
114             latch.countDown();
115         }
116     }
117 
118 }