You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicemix.apache.org by ge...@apache.org on 2009/06/15 15:57:16 UTC

svn commit: r784784 - in /servicemix/smx3/branches/servicemix-3.2: core/servicemix-core/src/main/java/org/apache/servicemix/jbi/container/ core/servicemix-core/src/main/java/org/apache/servicemix/jbi/framework/ core/servicemix-core/src/test/java/org/ap...

Author: gertv
Date: Mon Jun 15 13:57:15 2009
New Revision: 784784

URL: http://svn.apache.org/viewvc?rev=784784&view=rev
Log:
SM-1574: Adding unit tests for forced container shutdown

Added:
    servicemix/smx3/branches/servicemix-3.2/core/servicemix-core/src/test/java/org/apache/servicemix/jbi/container/
    servicemix/smx3/branches/servicemix-3.2/core/servicemix-core/src/test/java/org/apache/servicemix/jbi/container/JBIContainerTest.java   (with props)
Modified:
    servicemix/smx3/branches/servicemix-3.2/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/container/JBIContainer.java
    servicemix/smx3/branches/servicemix-3.2/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/framework/Registry.java
    servicemix/smx3/branches/servicemix-3.2/core/servicemix-core/src/test/java/org/apache/servicemix/jbi/framework/RegistryTest.java
    servicemix/smx3/branches/servicemix-3.2/core/servicemix-core/src/test/java/org/apache/servicemix/jbi/messaging/DeliveryChannelImplTest.java
    servicemix/smx3/branches/servicemix-3.2/deployables/bindingcomponents/servicemix-cxf-bc/   (props changed)
    servicemix/smx3/branches/servicemix-3.2/deployables/serviceengines/servicemix-eip/   (props changed)
    servicemix/smx3/branches/servicemix-3.2/platforms/jboss/deployer/   (props changed)

Modified: servicemix/smx3/branches/servicemix-3.2/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/container/JBIContainer.java
URL: http://svn.apache.org/viewvc/servicemix/smx3/branches/servicemix-3.2/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/container/JBIContainer.java?rev=784784&r1=784783&r2=784784&view=diff
==============================================================================
--- servicemix/smx3/branches/servicemix-3.2/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/container/JBIContainer.java (original)
+++ servicemix/smx3/branches/servicemix-3.2/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/container/JBIContainer.java Mon Jun 15 13:57:15 2009
@@ -748,12 +748,21 @@
             }
             LOG.info("Components and service assemblies have been shut down");
         } catch (Exception e) {
-            LOG.warn("Unable to shutdown components and service assemblies normally: " + e, e);
-            LOG.warn("Forcing shutdown by cancelling all pending exchanges");
-            registry.cancelPendingExchanges();
+            forceShutdown(e);
         }
     }
 
+    /**
+     * Force a container shutdown by canceling all pending exchanges
+     * 
+     * @param e the exception that caused the forced container shutdown
+     */
+    protected void forceShutdown(Exception e) {
+        LOG.warn("Unable to shutdown components and service assemblies normally: " + e, e);
+        LOG.warn("Forcing shutdown by cancelling all pending exchanges");
+        registry.cancelPendingExchanges();
+    }
+
     protected void addShutdownHook() {
         if (useShutdownHook) {
             shutdownHook = new Thread("ServiceMix ShutdownHook") {

Modified: servicemix/smx3/branches/servicemix-3.2/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/framework/Registry.java
URL: http://svn.apache.org/viewvc/servicemix/smx3/branches/servicemix-3.2/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/framework/Registry.java?rev=784784&r1=784783&r2=784784&view=diff
==============================================================================
--- servicemix/smx3/branches/servicemix-3.2/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/framework/Registry.java (original)
+++ servicemix/smx3/branches/servicemix-3.2/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/framework/Registry.java Mon Jun 15 13:57:15 2009
@@ -955,8 +955,6 @@
             if (channel != null) {
                 channel.cancelPendingExchanges();
             }
-        }
-        
+        }      
     }
-
 }

Added: servicemix/smx3/branches/servicemix-3.2/core/servicemix-core/src/test/java/org/apache/servicemix/jbi/container/JBIContainerTest.java
URL: http://svn.apache.org/viewvc/servicemix/smx3/branches/servicemix-3.2/core/servicemix-core/src/test/java/org/apache/servicemix/jbi/container/JBIContainerTest.java?rev=784784&view=auto
==============================================================================
--- servicemix/smx3/branches/servicemix-3.2/core/servicemix-core/src/test/java/org/apache/servicemix/jbi/container/JBIContainerTest.java (added)
+++ servicemix/smx3/branches/servicemix-3.2/core/servicemix-core/src/test/java/org/apache/servicemix/jbi/container/JBIContainerTest.java Mon Jun 15 13:57:15 2009
@@ -0,0 +1,102 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.servicemix.jbi.container;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import javax.jbi.JBIException;
+import javax.xml.namespace.QName;
+
+import junit.framework.TestCase;
+
+import org.apache.servicemix.jbi.framework.Registry;
+import org.apache.servicemix.tck.ReceiverComponent;
+
+/**
+ * Test cases for {@link JBIContainer}
+ */
+public class JBIContainerTest extends TestCase {
+    
+    private static final long SHUTDOWN_DELAY = 10000;
+    private static final long FORCE_SHUTDOWN_DELAY = SHUTDOWN_DELAY / 10;
+
+    public void testForceShutdown() throws Exception {
+        final AtomicBoolean forcedShutdown = new AtomicBoolean(false);
+        final JBIContainer container = new JBIContainer() {
+            @Override
+            protected void forceShutdown(Exception e) {
+                forcedShutdown.set(true);
+                super.forceShutdown(e);
+            }
+        };
+        container.registry = new Registry() {
+            @Override
+            public void shutDown() throws JBIException {
+                try {
+                    // let's simulate pending exchanges by slowing down the shutdown of the registry  
+                    Thread.sleep(SHUTDOWN_DELAY);
+                } catch (InterruptedException e) {
+                    // ignore it
+                }
+                super.shutDown();
+            }
+        };
+        container.setForceShutdown(FORCE_SHUTDOWN_DELAY);
+        container.init();
+        container.start();
+
+        long start = System.currentTimeMillis();
+        
+        // now let's shutdown the container and await the termination
+        container.shutDown();
+        assertTrue("Container should have used forced shutdown", forcedShutdown.get());
+        assertTrue("Should have taken less than the shutdown delay (" + SHUTDOWN_DELAY  + "ms)", 
+                   System.currentTimeMillis() - start < SHUTDOWN_DELAY);
+    }
+    
+    public void testForceShutdownWithPendingSyncExchanges() throws Exception {
+        final JBIContainer container = new JBIContainer();
+        container.setForceShutdown(FORCE_SHUTDOWN_DELAY);
+        container.init();
+        container.start();
+
+        ActivationSpec spec = new ActivationSpec("receiver", new ReceiverComponent() {
+            public void stop() throws JBIException {
+                try {
+                    // this component will now wait before shutting down
+                    // (i.e. simulate component waiting for pending exchanges or a process to end)
+                    Thread.sleep(SHUTDOWN_DELAY);
+                } catch (InterruptedException e) {
+                    // TODO Auto-generated catch block
+                    e.printStackTrace();
+                }
+                super.stop();
+            }
+        });
+        spec.setService(new QName("urn:test", "receiver"));
+        container.activateComponent(spec);
+        
+        long start = System.currentTimeMillis();
+        
+        // now let's shutdown the container and await the termination
+        container.shutDown();
+        long delay = System.currentTimeMillis() - start;
+        assertTrue("Should have taken less than " + delay  + "ms (estimated ca. " + FORCE_SHUTDOWN_DELAY + "ms)",
+                   delay < SHUTDOWN_DELAY);
+    }
+    
+}

Propchange: servicemix/smx3/branches/servicemix-3.2/core/servicemix-core/src/test/java/org/apache/servicemix/jbi/container/JBIContainerTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: servicemix/smx3/branches/servicemix-3.2/core/servicemix-core/src/test/java/org/apache/servicemix/jbi/framework/RegistryTest.java
URL: http://svn.apache.org/viewvc/servicemix/smx3/branches/servicemix-3.2/core/servicemix-core/src/test/java/org/apache/servicemix/jbi/framework/RegistryTest.java?rev=784784&r1=784783&r2=784784&view=diff
==============================================================================
--- servicemix/smx3/branches/servicemix-3.2/core/servicemix-core/src/test/java/org/apache/servicemix/jbi/framework/RegistryTest.java (original)
+++ servicemix/smx3/branches/servicemix-3.2/core/servicemix-core/src/test/java/org/apache/servicemix/jbi/framework/RegistryTest.java Mon Jun 15 13:57:15 2009
@@ -16,6 +16,8 @@
  */
 package org.apache.servicemix.jbi.framework;
 
+import java.util.concurrent.atomic.AtomicInteger;
+
 import javax.jbi.servicedesc.ServiceEndpoint;
 import javax.xml.namespace.QName;
 
@@ -24,8 +26,11 @@
 import junit.framework.TestCase;
 
 import org.apache.servicemix.components.util.EchoComponent;
+import org.apache.servicemix.jbi.container.ActivationSpec;
 import org.apache.servicemix.jbi.container.JBIContainer;
+import org.apache.servicemix.jbi.messaging.DeliveryChannelImpl;
 import org.apache.servicemix.jbi.resolver.URIResolver;
+import org.apache.servicemix.tck.ReceiverComponent;
 
 public class RegistryTest extends TestCase {
 
@@ -59,4 +64,35 @@
         assertSame(ep, ep2);
     }
     
+    /**
+     * Test canceling exchanges on the Registry will cancel pending exchanges in all the known components' DeliveryChannels
+     */
+    public void testCancelPendingExchanges() throws Exception {
+        JBIContainer container = new JBIContainer();
+        container.init();
+        
+        ActivationSpec spec = new ActivationSpec("component1", new ReceiverComponent());
+        spec.setService(new QName("urn:test", "service1"));
+        container.activateComponent(spec);
+        
+        container.start();
+        
+        final AtomicInteger canceled = new AtomicInteger();
+        
+        // injecting mock delivery channels to check if pending exchanges get canceled
+        for (ComponentMBeanImpl mbean : container.getRegistry().getComponents()) {
+            mbean.setDeliveryChannel(new DeliveryChannelImpl(mbean) {
+                @Override
+                public void cancelPendingExchanges() {
+                    canceled.incrementAndGet();
+                }
+            });
+        }
+        
+        // now let's try to cancel pending exchanges on the registry 
+        container.getRegistry().cancelPendingExchanges();
+        assertEquals("Should have canceled exchanges in all the delivery channels", 
+                     container.getRegistry().getComponents().size(), canceled.get());
+    }
+    
 }

Modified: servicemix/smx3/branches/servicemix-3.2/core/servicemix-core/src/test/java/org/apache/servicemix/jbi/messaging/DeliveryChannelImplTest.java
URL: http://svn.apache.org/viewvc/servicemix/smx3/branches/servicemix-3.2/core/servicemix-core/src/test/java/org/apache/servicemix/jbi/messaging/DeliveryChannelImplTest.java?rev=784784&r1=784783&r2=784784&view=diff
==============================================================================
--- servicemix/smx3/branches/servicemix-3.2/core/servicemix-core/src/test/java/org/apache/servicemix/jbi/messaging/DeliveryChannelImplTest.java (original)
+++ servicemix/smx3/branches/servicemix-3.2/core/servicemix-core/src/test/java/org/apache/servicemix/jbi/messaging/DeliveryChannelImplTest.java Mon Jun 15 13:57:15 2009
@@ -16,6 +16,9 @@
  */
 package org.apache.servicemix.jbi.messaging;
 
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicBoolean;
 
 import javax.jbi.JBIException;
@@ -174,6 +177,32 @@
         channel.autoEnlistInTx(exchange);
         assertNull(exchange.getTransactionContext());
     }
+    
+    public void testCancelPendingExchanges() throws Exception {
+        final DeliveryChannelImpl channel = createDeliveryChannel();
+        final MessageExchangeImpl exchange = createMessageExchange(channel);
+        
+        final CountDownLatch pending = new CountDownLatch(1);
+        Executors.newSingleThreadExecutor().execute(new Runnable() {
+            public void run() {
+                try {
+                    channel.sendSync(exchange);
+                    pending.countDown();
+                } catch (MessagingException e) {
+                    // no need to worry about this
+                }
+            }            
+        });
+        
+        // let's wait for a second until the exchange got sent
+        pending.await(1, TimeUnit.SECONDS);
+        
+        // now let's cancel the pending exchanges 
+        channel.cancelPendingExchanges();
+        pending.await(1, TimeUnit.SECONDS);
+        assertEquals("There should be no more pending exchanges", 0, pending.getCount());
+        assertEquals(ExchangeStatus.ERROR, exchange.getStatus());
+    }
 
     private MessageExchangeImpl createMessageExchange(final DeliveryChannelImpl channel) throws MessagingException {
         MessageExchangeFactory factory = channel.createExchangeFactoryForService(new QName("service"));    

Propchange: servicemix/smx3/branches/servicemix-3.2/deployables/bindingcomponents/servicemix-cxf-bc/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Mon Jun 15 13:57:15 2009
@@ -5,3 +5,4 @@
 .checkstyle
 .pmd
 .settings
+data

Propchange: servicemix/smx3/branches/servicemix-3.2/deployables/serviceengines/servicemix-eip/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Mon Jun 15 13:57:15 2009
@@ -9,3 +9,4 @@
 .ruleset
 .checkstyle
 .pmd
+rootDir

Propchange: servicemix/smx3/branches/servicemix-3.2/platforms/jboss/deployer/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Mon Jun 15 13:57:15 2009
@@ -2,3 +2,6 @@
 *.iml
 *.ipr
 *.iws
+.classpath
+.project
+.settings