View Javadoc

1   /*
2    * $Id: GracefulShutdownTimeoutTestCase.java 22421 2011-07-15 05:05:06Z 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.test.integration.work;
12  
13  import org.mule.api.MuleEventContext;
14  import org.mule.api.construct.FlowConstruct;
15  import org.mule.api.service.Service;
16  import org.mule.construct.Flow;
17  import org.mule.tck.AbstractServiceAndFlowTestCase;
18  import org.mule.tck.functional.EventCallback;
19  import org.mule.tck.functional.FunctionalTestComponent;
20  import org.mule.util.concurrent.Latch;
21  
22  import java.util.Arrays;
23  import java.util.Collection;
24  import java.util.concurrent.TimeUnit;
25  
26  import org.junit.Test;
27  import org.junit.runners.Parameterized.Parameters;
28  
29  import static org.junit.Assert.assertTrue;
30  
31  public class GracefulShutdownTimeoutTestCase extends AbstractServiceAndFlowTestCase
32  {
33      @Parameters
34      public static Collection<Object[]> parameters()
35      {
36          return Arrays.asList(new Object[][]{
37              {ConfigVariant.SERVICE, "org/mule/test/integration/work/graceful-shutdown-timeout-service.xml"},
38              {ConfigVariant.FLOW, "org/mule/test/integration/work/graceful-shutdown-timeout-flow.xml"}});
39      }
40  
41      public GracefulShutdownTimeoutTestCase(ConfigVariant variant, String configResources)
42      {
43          super(variant, configResources);
44      }
45  
46      @Override
47      protected boolean isGracefulShutdown()
48      {
49          return true;
50      }
51  
52      /**
53       * Dispatch an event to a service component that takes longer than default
54       * graceful shutdown time to complete and customize the graceful shutdown timeout
55       * in configuration so that component execution is not interrupted. This tests
56       * services but the same applies to the graceful shutdown of
57       * receivers/dispatchers etc.
58       *
59       * @throws Exception
60       */
61      @Test
62      public void testGracefulShutdownTimeout() throws Exception
63      {
64          final Latch latch = new Latch();
65  
66          FlowConstruct service = muleContext.getRegistry().lookupFlowConstruct("TestService");
67          FunctionalTestComponent testComponent = (FunctionalTestComponent) getComponent(service);
68          testComponent.setEventCallback(new EventCallback()
69          {
70              @Override
71              public void eventReceived(MuleEventContext context, Object component) throws Exception
72              {
73                  Thread.sleep(5500);
74                  latch.countDown();
75  
76              }
77          });
78  
79          if (variant.equals(ConfigVariant.FLOW))
80          {
81              ((Flow) service).process(getTestEvent("test"));
82              Thread.sleep(200);
83              ((Flow) service).dispose();
84              assertTrue(latch.await(1000, TimeUnit.MILLISECONDS));
85          }
86          else
87          {
88              ((Service) service).dispatchEvent(getTestEvent("test"));
89              Thread.sleep(200);
90              ((Service) service).dispose();
91              assertTrue(latch.await(1000, TimeUnit.MILLISECONDS));
92          }
93      }
94  }