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);
118 LOGGER.info("Shutting down now ...");
119 executorService.shutdownNow();
121 return concurrentContext.verifyAndClearContext(jvmCount, threadCount, threadLoops);
125 private void addShutDownHook(final List<Closeable> tasks) {
126 Runtime.getRuntime().addShutdownHook(new Thread() {
129 LOGGER.info("Shutting down ...");
130 for (final Closeable task : tasks) {
133 } catch (final IOException ioException) {
134 LOGGER.error("Unable to close task ... ", ioException);
142 private String getThreadFactoryName(final int jvmCount, final String testType) {
143 return jvmCount == 1 ? testType + ":TestConcurrentContextThread_0_"
144 : testType + ":TestConcurrentContextJVMThread_";
148 * Setup and verify context.
150 * @throws ContextException the context exception
152 private void setupAndVerifyContext() throws ContextException {
153 final AxArtifactKey distributorKey = new AxArtifactKey("ApexDistributor", "0.0.1");
154 contextDistributor = new DistributorFactory().getDistributor(distributorKey);
157 final AxArtifactKey[] usedArtifactStackArray = {
158 new AxArtifactKey("testC-top", "0.0.1"),
159 new AxArtifactKey("testC-next", "0.0.1"),
160 new AxArtifactKey("testC-bot", "0.0.1")
164 final AxContextModel albumsModel = TestContextAlbumFactory.createMultiAlbumsContextModel();
165 contextDistributor.registerModel(albumsModel);
167 lTypeAlbum = contextDistributor.createContextAlbum(new AxArtifactKey("LTypeContextAlbum", "0.0.1"));
168 assert (lTypeAlbum != null);
169 lTypeAlbum.setUserArtifactStack(usedArtifactStackArray);
171 // CHECKSTYLE:OFF: checkstyle:magicNumber
172 lTypeAlbum.put("lTypeValue0", new TestContextLongItem(0xFFFFFFFFFFFFFFFFL));
173 lTypeAlbum.put("lTypeValue1", new TestContextLongItem(0xFFFFFFFFFFFFFFFEL));
174 lTypeAlbum.put("lTypeValue2", new TestContextLongItem(0xFFFFFFFFFFFFFFFDL));
175 lTypeAlbum.put("lTypeValue3", new TestContextLongItem(0xFFFFFFFFFFFFFFFCL));
176 lTypeAlbum.put("lTypeValue4", new TestContextLongItem(0xFFFFFFFFFFFFFFFBL));
177 lTypeAlbum.put("lTypeValue5", new TestContextLongItem(0xFFFFFFFFFFFFFFFAL));
178 lTypeAlbum.put("lTypeValue6", new TestContextLongItem(0xFFFFFFFFFFFFFFF9L));
179 lTypeAlbum.put("lTypeValue7", new TestContextLongItem(0xFFFFFFFFFFFFFFF8L));
180 lTypeAlbum.put("lTypeValue8", new TestContextLongItem(0xFFFFFFFFFFFFFFF7L));
181 lTypeAlbum.put("lTypeValue9", new TestContextLongItem(0xFFFFFFFFFFFFFFF6L));
182 lTypeAlbum.put("lTypeValueA", new TestContextLongItem(0xFFFFFFFFFFFFFFF5L));
183 lTypeAlbum.put("lTypeValueB", new TestContextLongItem(0xFFFFFFFFFFFFFFF4L));
184 lTypeAlbum.put("lTypeValueC", new TestContextLongItem(0xFFFFFFFFFFFFFFF3L));
185 lTypeAlbum.put("lTypeValueD", new TestContextLongItem(0xFFFFFFFFFFFFFFF2L));
186 lTypeAlbum.put("lTypeValueE", new TestContextLongItem(0xFFFFFFFFFFFFFFF1L));
187 lTypeAlbum.put("lTypeValueF", new TestContextLongItem(0xFFFFFFFFFFFFFFF0L));
188 LOGGER.debug(lTypeAlbum.toString());
189 assert (lTypeAlbum.size() >= 16);
190 // CHECKSTYLE:ON: checkstyle:magicNumber
192 // The initial value for concurrent testing
193 final TestContextLongItem item = new TestContextLongItem(0L);
194 lTypeAlbum.put("testValue", item);
199 * Verify and clear context.
201 * @param jvmCount the jvm count
202 * @param threadCount the thread count
203 * @param threadLoops the thread loops
205 * @throws ContextException the context exception
207 private long verifyAndClearContext(final int jvmCount, final int threadCount, final int threadLoops)
208 throws ContextException {
210 LOGGER.debug("threads finished, end value is {}",
211 ((TestContextLongItem) lTypeAlbum.get("testValue")).getLongValue());
212 } catch (final Exception exception) {
213 LOGGER.error("Error: ", exception);
215 final long total = ((TestContextLongItem) lTypeAlbum.get("testValue")).getLongValue();
217 contextDistributor.clear();
218 contextDistributor = null;