FELIX-4361 Applied the patch, including the suggestion from Sander Mak. Modified the existing test case to correctly terminate instead of waiting 30 seconds. Added a simpler test case that shows the problem without relying on multiple threads.
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1551928 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/dependencymanager/core/src/main/java/org/apache/felix/dm/DependencyManager.java b/dependencymanager/core/src/main/java/org/apache/felix/dm/DependencyManager.java
index 8a76180..3d97b42 100644
--- a/dependencymanager/core/src/main/java/org/apache/felix/dm/DependencyManager.java
+++ b/dependencymanager/core/src/main/java/org/apache/felix/dm/DependencyManager.java
@@ -595,7 +595,13 @@
* @return a list of services
*/
public List getComponents() {
- return Collections.unmodifiableList(m_components);
+ synchronized (m_components) {
+ List copyComponents = new ArrayList(m_components.size());
+ for (int i = 0; i < m_components.size(); i++) {
+ copyComponents.add(m_components.get(i));
+ }
+ return copyComponents;
+ }
}
/**
diff --git a/dependencymanager/test/src/test/java/org/apache/felix/dm/test/integration/api/FELIX4361_ConcurrentComponentListingTest.java b/dependencymanager/test/src/test/java/org/apache/felix/dm/test/integration/api/FELIX4361_ConcurrentComponentListingTest.java
new file mode 100644
index 0000000..e93bda2
--- /dev/null
+++ b/dependencymanager/test/src/test/java/org/apache/felix/dm/test/integration/api/FELIX4361_ConcurrentComponentListingTest.java
@@ -0,0 +1,108 @@
+/*
+ * 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.felix.dm.test.integration.api;
+
+import java.util.Iterator;
+import java.util.List;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import junit.framework.Assert;
+
+import org.apache.felix.dm.DependencyManager;
+import org.apache.felix.dm.test.integration.common.TestBase;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.ops4j.pax.exam.junit.PaxExam;
+
+/**
+ * Test for FELIX-4361 The DependencyManager.getComponents method failed in a concurrent situation on iterating the
+ * result of the method.
+ */
+@RunWith(PaxExam.class)
+public class FELIX4361_ConcurrentComponentListingTest extends TestBase {
+
+ @Test
+ public void testConcurrentGetComponentsManipulation() {
+ DependencyManager dm = new DependencyManager(context);
+ dm.add(dm.createComponent().setImplementation(Object.class));
+ Iterator iterator = dm.getComponents().iterator();
+ dm.add(dm.createComponent().setImplementation(Object.class));
+ iterator.next();
+ }
+
+ @Test
+ public void testConcurrentGetComponentsMultipleThreads() {
+ final DependencyManager m = new DependencyManager(context);
+ final AtomicInteger errors = new AtomicInteger(0);
+ final AtomicInteger componentsAdded = new AtomicInteger(0);
+ final int max = 10000;
+ final AtomicBoolean isRunning = new AtomicBoolean(true);
+
+ ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() + 1);
+ Runnable readTask = new Runnable() {
+ public void run() {
+ while (isRunning.get()) {
+ try {
+ List components = m.getComponents();
+ for (Object component : components) {
+ // Just iterating the components should check for concurrent modifications
+ }
+ }
+ catch (Exception ex) {
+ errors.addAndGet(1);
+ ex.printStackTrace();
+ }
+ }
+ }
+ };
+
+ Callable<Boolean> modifyTask = new Callable<Boolean>() {
+ public Boolean call() throws Exception {
+ try {
+ m.add(m.createComponent().setImplementation(Object.class));
+ componentsAdded.addAndGet(1);
+ return true;
+ }
+ catch (Exception ex) {
+ return false;
+ }
+ }
+ };
+
+ executorService.submit(readTask);
+ for (int i = 0; i < max; i++) {
+ executorService.submit(modifyTask);
+ }
+ isRunning.set(false);
+ executorService.shutdown();
+
+ try {
+ executorService.awaitTermination(30, TimeUnit.SECONDS);
+ }
+ catch (InterruptedException e) {
+ }
+ Assert.assertEquals(0, errors.get());
+ Assert.assertEquals(max, componentsAdded.get());
+ }
+}