You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@onami.apache.org by as...@apache.org on 2013/03/30 16:07:23 UTC

svn commit: r1462773 - in /incubator/onami/trunk/lifecycle/core/src: main/java/org/apache/onami/lifecycle/core/DefaultStager.java test/java/org/apache/onami/lifecycle/core/DefaultStagerTestCase.java

Author: ash2k
Date: Sat Mar 30 15:07:23 2013
New Revision: 1462773

URL: http://svn.apache.org/r1462773
Log:
[ONAMI-104] Process Stageables registered while staging is running 

Added:
    incubator/onami/trunk/lifecycle/core/src/test/java/org/apache/onami/lifecycle/core/DefaultStagerTestCase.java   (with props)
Modified:
    incubator/onami/trunk/lifecycle/core/src/main/java/org/apache/onami/lifecycle/core/DefaultStager.java

Modified: incubator/onami/trunk/lifecycle/core/src/main/java/org/apache/onami/lifecycle/core/DefaultStager.java
URL: http://svn.apache.org/viewvc/incubator/onami/trunk/lifecycle/core/src/main/java/org/apache/onami/lifecycle/core/DefaultStager.java?rev=1462773&r1=1462772&r2=1462773&view=diff
==============================================================================
--- incubator/onami/trunk/lifecycle/core/src/main/java/org/apache/onami/lifecycle/core/DefaultStager.java (original)
+++ incubator/onami/trunk/lifecycle/core/src/main/java/org/apache/onami/lifecycle/core/DefaultStager.java Sat Mar 30 15:07:23 2013
@@ -20,10 +20,8 @@ package org.apache.onami.lifecycle.core;
  */
 
 import java.lang.annotation.Annotation;
-import java.util.ArrayList;
 import java.util.Collections;
 import java.util.LinkedList;
-import java.util.List;
 import java.util.Queue;
 
 /**
@@ -96,9 +94,12 @@ public class DefaultStager<A extends Ann
     /**
      * {@inheritDoc}
      */
-    public synchronized void register( Stageable stageable )
+    public void register( Stageable stageable )
     {
-        stageables.add( stageable );
+        synchronized ( stageables )
+        {
+            stageables.add( stageable );
+        }
     }
 
     /**
@@ -119,15 +120,17 @@ public class DefaultStager<A extends Ann
             stageHandler = new NoOpStageHandler();
         }
 
-        List<Stageable> localStageables;
-        synchronized ( this )
-        {
-            localStageables = new ArrayList<Stageable>(stageables);
-            stageables.clear();
-        }
-
-        for ( Stageable stageable : localStageables )
+        while ( true )
         {
+            Stageable stageable;
+            synchronized ( stageables )
+            {
+                stageable = stageables.poll();
+            }
+            if ( stageable == null )
+            {
+                break;
+            }
             stageable.stage( stageHandler );
         }
     }

Added: incubator/onami/trunk/lifecycle/core/src/test/java/org/apache/onami/lifecycle/core/DefaultStagerTestCase.java
URL: http://svn.apache.org/viewvc/incubator/onami/trunk/lifecycle/core/src/test/java/org/apache/onami/lifecycle/core/DefaultStagerTestCase.java?rev=1462773&view=auto
==============================================================================
--- incubator/onami/trunk/lifecycle/core/src/test/java/org/apache/onami/lifecycle/core/DefaultStagerTestCase.java (added)
+++ incubator/onami/trunk/lifecycle/core/src/test/java/org/apache/onami/lifecycle/core/DefaultStagerTestCase.java Sat Mar 30 15:07:23 2013
@@ -0,0 +1,100 @@
+package org.apache.onami.lifecycle.core;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+/*
+ * 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.
+ */
+
+public class DefaultStagerTestCase
+{
+
+    @Test
+    public void stagerShouldStageObjectsRegisteredWhileStaging()
+    {
+        final Stager<TestAnnotationA> stager =
+                DefaultStager.newStager( TestAnnotationA.class );
+        final AtomicBoolean staged = new AtomicBoolean();
+        stager.register( new Stageable()
+        {
+            public void stage( StageHandler stageHandler )
+            {
+                stager.register( new Stageable()
+                {
+                    public void stage( StageHandler stageHandler )
+                    {
+                        staged.set( true );
+                    }
+                } );
+            }
+        } );
+
+        stager.stage();
+
+        Assert.assertTrue( staged.get() );
+    }
+
+    /*
+     * Deadlock scenario:
+     * 1. DefaultStager holds lock while calling Stageable.stage();
+     * 2. Stageable.stage() blocks on some thread
+     * 3. the thread blocks on the lock in DefaultStager.register()
+     */
+    @Test
+    public void stagerShouldNotDeadlockWhileStagingObjectChains()
+    {
+        final AtomicBoolean staged = new AtomicBoolean();
+        final Stager<TestAnnotationA> stager =
+                DefaultStager.newStager( TestAnnotationA.class );
+        stager.register( new Stageable()
+        {
+            public void stage( StageHandler stageHandler )
+            {
+                Thread thread = new Thread( new Runnable()
+                {
+                    public void run()
+                    {
+                        stager.register( new Stageable()
+                        {
+                            public void stage( StageHandler stageHandler )
+                            {
+                                staged.set( true );
+                            }
+                        } );
+                    }
+                } );
+                thread.start();
+                try
+                {
+                    thread.join();
+                }
+                catch ( InterruptedException e )
+                {
+                    Thread.currentThread().interrupt();
+                }
+            }
+        } );
+
+        stager.stage();
+
+        Assert.assertTrue( staged.get() );
+    }
+}

Propchange: incubator/onami/trunk/lifecycle/core/src/test/java/org/apache/onami/lifecycle/core/DefaultStagerTestCase.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain