View Javadoc

1   /*
2    * $Id: MuleEventWorkTestCase.java 22387 2011-07-12 03:53:36Z dirk.olmes $
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.junit4.AbstractMuleContextTestCase;
19  import org.mule.util.concurrent.Latch;
20  
21  import java.util.concurrent.TimeUnit;
22  import org.junit.Test;
23  
24  import static org.junit.Assert.assertNotNull;
25  import static org.junit.Assert.assertNotSame;
26  import static org.junit.Assert.assertSame;
27  import static org.junit.Assert.assertTrue;
28  import static org.junit.Assert.fail;
29  
30  /**
31   * Test case to reproduce issue described in MULE-4407 and validate fix.
32   */
33  public class MuleEventWorkTestCase extends AbstractMuleContextTestCase
34  {
35  
36      protected MuleEvent originalEvent;
37      protected Latch latch = new Latch();
38  
39      @Override
40      protected void doSetUp() throws Exception
41      {
42          super.doSetUp();
43          // Create a dummy event and give it some properties
44          originalEvent = getTestEvent("test");
45          originalEvent.getMessage().setOutboundProperty("test", "val");
46          originalEvent.getMessage().setOutboundProperty("test2", "val2");
47          OptimizedRequestContext.unsafeSetEvent(originalEvent);
48      }
49  
50      @Test
51      public void testScheduleMuleEventWork() throws Exception
52      {
53          muleContext.getWorkManager().scheduleWork(new TestMuleEventWork(originalEvent));
54  
55          assertTrue("Timed out waiting for latch", latch.await(2000, TimeUnit.MILLISECONDS));
56  
57          assertSame(originalEvent, RequestContext.getEvent());
58  
59          try
60          {
61              // Ensure that even after Work has been created, scheduled and executed
62              // the original event instance is still owned by this thread and still
63              // mutable.
64              ((DefaultMuleMessage) originalEvent.getMessage()).assertAccess(ThreadSafeAccess.WRITE);
65          }
66          catch (Exception e)
67          {
68              fail(e.getMessage());
69          }
70      }
71  
72      @Test
73      public void testRunMuleEventWork() throws Exception
74      {
75          new TestMuleEventWork(originalEvent).run();
76  
77          // NOTE: This assertion documents/tests current behaviour but does not seem
78          // correct.
79          // In scenarios where Work implementations are run in the same thread rather
80          // than being scheduled then the RequestContext ThreadLocal value is
81          // overwritten with a new copy which is not desirable.
82          // See: MULE-4409
83          assertNotSame(originalEvent, RequestContext.getEvent());
84  
85          try
86          {
87              // Ensure that even after Work has been created, scheduled and executed
88              // the original event instance is still owned by this thread and still
89              // mutable.
90              ((DefaultMuleMessage) originalEvent.getMessage()).assertAccess(ThreadSafeAccess.WRITE);
91          }
92          catch (Exception e)
93          {
94              fail(e.getMessage());
95          }
96  
97      }
98  
99      private class TestMuleEventWork extends AbstractMuleEventWork
100     {
101 
102         public TestMuleEventWork(MuleEvent event)
103         {
104             super(event);
105         }
106 
107         @Override
108         protected void doRun()
109         {
110             assertNotSame("MuleEvent", event, originalEvent);
111             assertNotNull("RequestContext.getEvent() is null", RequestContext.getEvent());
112 
113             try
114             {
115                 // Ensure that the new event copied for this event is owned by the
116                 // thread that is executing this work and is mutable
117                 ((DefaultMuleMessage) event.getMessage()).assertAccess(ThreadSafeAccess.WRITE);
118             }
119             catch (Exception e)
120             {
121                 fail(e.getMessage());
122             }
123             latch.countDown();
124         }
125     }
126 
127 }