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.work;
8   
9   import org.mule.api.MuleEventContext;
10  import org.mule.api.service.Service;
11  import org.mule.tck.junit4.FunctionalTestCase;
12  import org.mule.tck.functional.EventCallback;
13  import org.mule.tck.functional.FunctionalTestComponent;
14  import org.mule.util.concurrent.Latch;
15  
16  import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
17  import org.junit.Test;
18  
19  import static org.junit.Assert.assertTrue;
20  
21  public class GracefulShutdownTimeoutTestCase extends FunctionalTestCase
22  {
23  
24      @Override
25      protected String getConfigResources()
26      {
27          return "org/mule/test/integration/work/graceful-shutdown-timeout.xml";
28      }
29  
30      @Override
31      protected boolean isGracefulShutdown()
32      {
33          return true;
34      }
35  
36      /**
37       * Dispatch an event to a service component that takes longer than default
38       * graceful shutdown time to complete and customize the graceful shutdown timeout
39       * in configuration so that component execution is not interrupted. This tests
40       * services but the same applies to the graceful shutdown of
41       * receivers/dispatchers etc.
42       * 
43       * @throws Exception
44       */
45      @Test
46      public void testGracefulShutdownTimeout() throws Exception
47      {
48          final Latch latch = new Latch();
49          Service service = muleContext.getRegistry().lookupService("TestService");
50          FunctionalTestComponent testComponent = (FunctionalTestComponent) getComponent(service);
51          testComponent.setEventCallback(new EventCallback()
52          {
53  
54              public void eventReceived(MuleEventContext context, Object component) throws Exception
55              {
56                  Thread.sleep(5500);
57                  latch.countDown();
58  
59              }
60          });
61          service.dispatchEvent(getTestInboundEvent("test"));
62          Thread.sleep(200);
63          service.dispose();
64          assertTrue(latch.await(1000, TimeUnit.MILLISECONDS));
65      }
66  
67  }