Pierre De Rop | 3a00a21 | 2015-03-01 09:27:46 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.apache.felix.dm.itest.api; |
| 20 | |
| 21 | import java.util.Iterator; |
| 22 | import java.util.List; |
| 23 | import java.util.concurrent.Callable; |
| 24 | import java.util.concurrent.ExecutorService; |
| 25 | import java.util.concurrent.Executors; |
| 26 | import java.util.concurrent.TimeUnit; |
| 27 | import java.util.concurrent.atomic.AtomicBoolean; |
| 28 | import java.util.concurrent.atomic.AtomicInteger; |
| 29 | |
| 30 | import org.junit.Assert; |
| 31 | |
| 32 | import org.apache.felix.dm.DependencyManager; |
| 33 | import org.apache.felix.dm.itest.util.TestBase; |
| 34 | |
| 35 | |
| 36 | /** |
| 37 | * Test for FELIX-4361 The DependencyManager.getComponents method failed in a concurrent situation on iterating the |
| 38 | * result of the method. |
| 39 | * |
| 40 | * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a> |
| 41 | */ |
| 42 | public class FELIX4361_ConcurrentComponentListingTest extends TestBase { |
| 43 | |
| 44 | @SuppressWarnings("rawtypes") |
| 45 | public void testConcurrentGetComponentsManipulation() { |
| 46 | DependencyManager dm = getDM(); |
| 47 | dm.add(dm.createComponent().setImplementation(Object.class)); |
| 48 | Iterator iterator = dm.getComponents().iterator(); |
| 49 | dm.add(dm.createComponent().setImplementation(Object.class)); |
| 50 | iterator.next(); |
| 51 | dm.clear(); |
| 52 | } |
| 53 | |
| 54 | public void testConcurrentGetComponentsMultipleThreads() { |
| 55 | final DependencyManager m = getDM(); |
| 56 | final AtomicInteger errors = new AtomicInteger(0); |
| 57 | final AtomicInteger componentsAdded = new AtomicInteger(0); |
| 58 | final int max = 10000; |
| 59 | final AtomicBoolean isRunning = new AtomicBoolean(true); |
| 60 | |
| 61 | ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() + 1); |
| 62 | Runnable readTask = new Runnable() { |
| 63 | @SuppressWarnings({ "rawtypes", "unused" }) |
| 64 | public void run() { |
| 65 | while (isRunning.get()) { |
| 66 | try { |
| 67 | List components = m.getComponents(); |
| 68 | for (Object component : components) { |
| 69 | // Just iterating the components should check for concurrent modifications |
| 70 | } |
| 71 | } |
| 72 | catch (Exception ex) { |
| 73 | errors.addAndGet(1); |
| 74 | ex.printStackTrace(); |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | }; |
| 79 | |
| 80 | Callable<Boolean> modifyTask = new Callable<Boolean>() { |
| 81 | public Boolean call() throws Exception { |
| 82 | try { |
| 83 | m.add(m.createComponent().setImplementation(Object.class)); |
| 84 | componentsAdded.addAndGet(1); |
| 85 | return true; |
| 86 | } |
| 87 | catch (Exception ex) { |
| 88 | return false; |
| 89 | } |
| 90 | } |
| 91 | }; |
| 92 | |
| 93 | executorService.submit(readTask); |
| 94 | for (int i = 0; i < max; i++) { |
| 95 | executorService.submit(modifyTask); |
| 96 | } |
| 97 | isRunning.set(false); |
| 98 | executorService.shutdown(); |
| 99 | |
| 100 | try { |
| 101 | executorService.awaitTermination(30, TimeUnit.SECONDS); |
| 102 | } |
| 103 | catch (InterruptedException e) { |
| 104 | } |
| 105 | Assert.assertEquals(0, errors.get()); |
| 106 | Assert.assertEquals(max, componentsAdded.get()); |
| 107 | m.clear(); |
| 108 | } |
| 109 | } |