blob: 0752c3f33d3a7e170a8634c9b462f3f9176460f8 [file] [log] [blame]
Pierre De Rop3a00a212015-03-01 09:27:46 +00001/*
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 */
19package org.apache.felix.dm.itest.api;
20
21import java.util.Iterator;
22import java.util.List;
23import java.util.concurrent.Callable;
24import java.util.concurrent.ExecutorService;
25import java.util.concurrent.Executors;
26import java.util.concurrent.TimeUnit;
27import java.util.concurrent.atomic.AtomicBoolean;
28import java.util.concurrent.atomic.AtomicInteger;
29
30import org.junit.Assert;
31
32import org.apache.felix.dm.DependencyManager;
33import 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 */
42public 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}