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.test.integration;
8   
9   import static org.junit.Assert.assertEquals;
10  
11  import org.mule.api.MuleEvent;
12  import org.mule.api.MuleException;
13  import org.mule.api.MuleMessage;
14  import org.mule.api.client.LocalMuleClient;
15  import org.mule.api.lifecycle.Startable;
16  import org.mule.api.processor.MessageProcessor;
17  import org.mule.api.transformer.TransformerException;
18  import org.mule.endpoint.DefaultInboundEndpoint;
19  import org.mule.tck.junit4.FunctionalTestCase;
20  import org.mule.tck.probe.PollingProber;
21  import org.mule.tck.probe.Probe;
22  import org.mule.tck.probe.Prober;
23  import org.mule.transformer.AbstractTransformer;
24  
25  import org.junit.Test;
26  
27  public class CompositeSourceStartDelayTestCase extends FunctionalTestCase
28  {
29  
30      public static volatile boolean awakeMessageSource;
31  
32      public CompositeSourceStartDelayTestCase()
33      {
34          setStartContext(false);
35      }
36  
37      @Override
38      protected String getConfigResources()
39      {
40          return "composite-source-start-delay-config.xml";
41      }
42  
43      @Test
44      public void testProcessMessageWhenAnSourceIsNotStartedYet() throws Exception
45      {
46          Thread thread = new Thread(new Runnable()
47          {
48  
49              public void run()
50              {
51                  try
52                  {
53                      muleContext.start();
54                  }
55                  catch (MuleException e)
56                  {
57                      // Nothing to do
58                  }
59              }
60          });
61  
62          thread.start();
63  
64          waitUntilEndpointIsStarted("testInEndpoint");
65  
66  
67          LocalMuleClient client = muleContext.getClient();
68          MuleMessage response = client.send("vm://testIn", "TEST", null);
69          assertEquals("TEST received", response.getPayloadAsString());
70      }
71  
72      private void waitUntilEndpointIsStarted(final String endpointName)
73      {
74          Prober prober = new PollingProber(30000, 50);
75          prober.check(new Probe()
76          {
77  
78              public boolean isSatisfied()
79              {
80                  DefaultInboundEndpoint endpoint = (DefaultInboundEndpoint) muleContext.getRegistry().lookupObject(endpointName);
81                  return endpoint.getConnector().isStarted();
82              }
83  
84              public String describeFailure()
85              {
86                  return "Endpoint was not started";
87              }
88          });
89      }
90  
91      public static class AwakeSourceMessageProcessor implements MessageProcessor
92      {
93  
94          public MuleEvent process(MuleEvent event) throws MuleException
95          {
96              awakeMessageSource = true;
97  
98              return event;
99          }
100     }
101 
102     public static class StuckTransformer extends AbstractTransformer implements Startable
103     {
104 
105         @Override
106         protected Object doTransform(Object src, String enc) throws TransformerException
107         {
108             return null;
109         }
110 
111         public void start() throws MuleException
112         {
113             while (!awakeMessageSource)
114             {
115                 try
116                 {
117                     Thread.sleep(10);
118                 }
119                 catch (InterruptedException e)
120                 {
121                     // Nothing to do
122                 }
123             }
124         }
125     }
126 }
127 
128