2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.context.test.locking;
23 import java.io.Closeable;
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.concurrent.ExecutorService;
28 import java.util.concurrent.Executors;
29 import java.util.concurrent.TimeUnit;
31 import org.onap.policy.apex.context.ContextAlbum;
32 import org.onap.policy.apex.context.ContextException;
33 import org.onap.policy.apex.context.Distributor;
34 import org.onap.policy.apex.context.impl.distribution.DistributorFactory;
35 import org.onap.policy.apex.context.test.concepts.TestContextLongItem;
36 import org.onap.policy.apex.context.test.factory.TestContextAlbumFactory;
37 import org.onap.policy.apex.context.test.utils.IntegrationThreadFactory;
38 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
39 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
40 import org.onap.policy.apex.model.basicmodel.handling.ApexModelException;
41 import org.onap.policy.apex.model.contextmodel.concepts.AxContextModel;
42 import org.slf4j.ext.XLogger;
43 import org.slf4j.ext.XLoggerFactory;
46 * The Class TestConcurrentContext tests concurrent use of context.
48 * @author Liam Fallon (liam.fallon@ericsson.com)
50 public class ConcurrentContext {
51 // Logger for this class
52 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ConcurrentContext.class);
54 // The context distributor and map used by each test
55 private Distributor contextDistributor = null;
56 private ContextAlbum lTypeAlbum = null;
59 * Test concurrent context.
61 * @param testType the test type
62 * @param jvmCount the jvm count
63 * @param threadCount the thread count
64 * @param threadLoops the thread loops
66 * @throws ApexModelException the apex model exception
67 * @throws IOException the IO exception
68 * @throws ApexException the apex exception
70 public long testConcurrentContext(final String testType, final int jvmCount, final int threadCount,
71 final int threadLoops) throws ApexModelException, IOException, ApexException {
72 final ConcurrentContext concurrentContext = new ConcurrentContext();
75 concurrentContext.setupAndVerifyContext();
76 } catch (final Exception exception) {
77 LOGGER.error("Error occured while setting up and verifying concurrent context", exception);
81 LOGGER.debug("starting JVMs and threads . . .");
83 final String name = getThreadFactoryName(jvmCount, testType);
84 final IntegrationThreadFactory threadFactory = new IntegrationThreadFactory(name);
85 final ExecutorService executorService = Executors.newFixedThreadPool(threadCount, threadFactory);
87 final List<Closeable> tasks = new ArrayList<>(threadCount);
89 addShutDownHook(tasks);
91 // Check if we have a single JVM or multiple JVMs
93 // Run everything in this JVM
94 for (int t = 0; t < threadCount; t++) {
95 final ConcurrentContextThread task = new ConcurrentContextThread(0, t, threadLoops);
97 executorService.execute(task);
101 // Spawn JVMs to run the tests
102 for (int j = 0; j < jvmCount; j++) {
103 final ConcurrentContextJVMThread task =
104 new ConcurrentContextJVMThread(testType, j, threadCount, threadLoops);
106 executorService.execute(task);
111 executorService.shutdown();
112 // wait for threads to finish, if not Timeout
113 executorService.awaitTermination(10, TimeUnit.MINUTES);
114 } catch (final InterruptedException interruptedException) {
115 LOGGER.error("Exception while waiting for threads to finish", interruptedException);
116 // restore the interrupt status
117 Thread.currentThread().interrupt();
120 LOGGER.info("Shutting down now ...");
121 executorService.shutdownNow();
123 return concurrentContext.verifyAndClearContext(jvmCount, threadCount, threadLoops);
127 private void addShutDownHook(final List<Closeable> tasks) {
128 Runtime.getRuntime().addShutdownHook(new Thread() {
131 LOGGER.info("Shutting down ...");
132 for (final Closeable task : tasks) {
135 } catch (final IOException ioException) {
136 LOGGER.error("Unable to close task ... ", ioException);
144 private String getThreadFactoryName(final int jvmCount, final String testType) {
145 return jvmCount == 1 ? testType + ":TestConcurrentContextThread_0_"
146 : testType + ":TestConcurrentContextJVMThread_";
150 * Setup and verify context.
152 * @throws ContextException the context exception
154 private void setupAndVerifyContext() throws ContextException {
155 final AxArtifactKey distributorKey = new AxArtifactKey("ApexDistributor", "0.0.1");
156 contextDistributor = new DistributorFactory().getDistributor(distributorKey);
159 final AxArtifactKey[] usedArtifactStackArray = {
160 new AxArtifactKey("testC-top", "0.0.1"),
161 new AxArtifactKey("testC-next", "0.0.1"),
162 new AxArtifactKey("testC-bot", "0.0.1")
166 final AxContextModel albumsModel = TestContextAlbumFactory.createMultiAlbumsContextModel();
167 contextDistributor.registerModel(albumsModel);
169 lTypeAlbum = contextDistributor.createContextAlbum(new AxArtifactKey("LTypeContextAlbum", "0.0.1"));
170 assert (lTypeAlbum != null);
171 lTypeAlbum.setUserArtifactStack(usedArtifactStackArray);
173 // CHECKSTYLE:OFF: checkstyle:magicNumber
174 lTypeAlbum.put("lTypeValue0", new TestContextLongItem(0xFFFFFFFFFFFFFFFFL));
175 lTypeAlbum.put("lTypeValue1", new TestContextLongItem(0xFFFFFFFFFFFFFFFEL));
176 lTypeAlbum.put("lTypeValue2", new TestContextLongItem(0xFFFFFFFFFFFFFFFDL));
177 lTypeAlbum.put("lTypeValue3", new TestContextLongItem(0xFFFFFFFFFFFFFFFCL));
178 lTypeAlbum.put("lTypeValue4", new TestContextLongItem(0xFFFFFFFFFFFFFFFBL));
179 lTypeAlbum.put("lTypeValue5", new TestContextLongItem(0xFFFFFFFFFFFFFFFAL));
180 lTypeAlbum.put("lTypeValue6", new TestContextLongItem(0xFFFFFFFFFFFFFFF9L));
181 lTypeAlbum.put("lTypeValue7", new TestContextLongItem(0xFFFFFFFFFFFFFFF8L));
182 lTypeAlbum.put("lTypeValue8", new TestContextLongItem(0xFFFFFFFFFFFFFFF7L));
183 lTypeAlbum.put("lTypeValue9", new TestContextLongItem(0xFFFFFFFFFFFFFFF6L));
184 lTypeAlbum.put("lTypeValueA", new TestContextLongItem(0xFFFFFFFFFFFFFFF5L));
185 lTypeAlbum.put("lTypeValueB", new TestContextLongItem(0xFFFFFFFFFFFFFFF4L));
186 lTypeAlbum.put("lTypeValueC", new TestContextLongItem(0xFFFFFFFFFFFFFFF3L));
187 lTypeAlbum.put("lTypeValueD", new TestContextLongItem(0xFFFFFFFFFFFFFFF2L));
188 lTypeAlbum.put("lTypeValueE", new TestContextLongItem(0xFFFFFFFFFFFFFFF1L));
189 lTypeAlbum.put("lTypeValueF", new TestContextLongItem(0xFFFFFFFFFFFFFFF0L));
190 LOGGER.debug(lTypeAlbum.toString());
191 assert (lTypeAlbum.size() >= 16);
192 // CHECKSTYLE:ON: checkstyle:magicNumber
194 // The initial value for concurrent testing
195 final TestContextLongItem item = new TestContextLongItem(0L);
196 lTypeAlbum.put("testValue", item);
201 * Verify and clear context.
203 * @param jvmCount the jvm count
204 * @param threadCount the thread count
205 * @param threadLoops the thread loops
207 * @throws ContextException the context exception
209 private long verifyAndClearContext(final int jvmCount, final int threadCount, final int threadLoops)
210 throws ContextException {
212 LOGGER.debug("threads finished, end value is {}",
213 ((TestContextLongItem) lTypeAlbum.get("testValue")).getLongValue());
214 } catch (final Exception exception) {
215 LOGGER.error("Error: ", exception);
217 final long total = ((TestContextLongItem) lTypeAlbum.get("testValue")).getLongValue();
219 contextDistributor.clear();
220 contextDistributor = null;