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.IOException;
25 import org.onap.policy.apex.context.ContextAlbum;
26 import org.onap.policy.apex.context.ContextException;
27 import org.onap.policy.apex.context.Distributor;
28 import org.onap.policy.apex.context.impl.distribution.DistributorFactory;
29 import org.onap.policy.apex.context.test.concepts.TestContextLongItem;
30 import org.onap.policy.apex.context.test.factory.TestContextAlbumFactory;
31 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
32 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
33 import org.onap.policy.apex.model.basicmodel.handling.ApexModelException;
34 import org.onap.policy.apex.model.contextmodel.concepts.AxContextModel;
35 import org.slf4j.ext.XLogger;
36 import org.slf4j.ext.XLoggerFactory;
39 * The Class TestConcurrentContext tests concurrent use of context.
41 * @author Liam Fallon (liam.fallon@ericsson.com)
43 public class ConcurrentContext {
44 private static final int TEN_MILLISECONDS = 10;
46 // Logger for this class
47 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ConcurrentContext.class);
49 // The context distributor and map used by each test
50 private Distributor contextDistributor = null;
51 private ContextAlbum lTypeAlbum = null;
54 * Test concurrent context.
56 * @param testType the test type
57 * @param jvmCount the jvm count
58 * @param threadCount the thread count
59 * @param threadLoops the thread loops
61 * @throws ApexModelException the apex model exception
62 * @throws IOException the IO exception
63 * @throws ApexException the apex exception
65 public long testConcurrentContext(final String testType, final int jvmCount, final int threadCount,
66 final int threadLoops) throws ApexModelException, IOException, ApexException {
67 final ConcurrentContext concurrentContext = new ConcurrentContext();
70 concurrentContext.setupAndVerifyContext();
71 } catch (final Exception exception) {
72 LOGGER.error("Error occured while setting up and verifying concurrent context", exception);
76 LOGGER.debug("starting JVMs and threads . . .");
78 final Thread[] threadArray = new Thread[threadCount];
80 // Check if we have a single JVM or multiple JVMs
81 int runningThreadCount = -1;
83 // Run everything in this JVM
84 for (int t = 0; t < threadCount; t++) {
85 threadArray[t] = new Thread(new ConcurrentContextThread(0, t, threadLoops));
86 threadArray[t].setName(testType + ":TestConcurrentContextThread_0_" + t);
87 threadArray[t].start();
90 runningThreadCount = threadCount;
92 // Spawn JVMs to run the tests
93 for (int j = 0; j < jvmCount; j++) {
94 threadArray[j] = new Thread(new ConcurrentContextJVMThread(testType, j, threadCount, threadLoops));
95 threadArray[j].setName(testType + ":TestConcurrentContextJVMThread_" + j);
96 threadArray[j].start();
98 runningThreadCount = jvmCount;
104 for (int i = 0; i < runningThreadCount; i++) {
105 if (threadArray[i].isAlive()) {
108 Thread.sleep(TEN_MILLISECONDS);
109 } catch (final Exception e) {
114 } while (!allFinished);
116 return concurrentContext.verifyAndClearContext(jvmCount, threadCount, threadLoops);
120 * Setup and verify context.
122 * @throws ContextException the context exception
124 private void setupAndVerifyContext() throws ContextException {
125 final AxArtifactKey distributorKey = new AxArtifactKey("ApexDistributor", "0.0.1");
126 contextDistributor = new DistributorFactory().getDistributor(distributorKey);
129 final AxArtifactKey[] usedArtifactStackArray = {
130 new AxArtifactKey("testC-top", "0.0.1"),
131 new AxArtifactKey("testC-next", "0.0.1"),
132 new AxArtifactKey("testC-bot", "0.0.1")
136 final AxContextModel albumsModel = TestContextAlbumFactory.createMultiAlbumsContextModel();
137 contextDistributor.registerModel(albumsModel);
139 lTypeAlbum = contextDistributor.createContextAlbum(new AxArtifactKey("LTypeContextAlbum", "0.0.1"));
140 assert (lTypeAlbum != null);
141 lTypeAlbum.setUserArtifactStack(usedArtifactStackArray);
143 // CHECKSTYLE:OFF: checkstyle:magicNumber
144 lTypeAlbum.put("lTypeValue0", new TestContextLongItem(0xFFFFFFFFFFFFFFFFL));
145 lTypeAlbum.put("lTypeValue1", new TestContextLongItem(0xFFFFFFFFFFFFFFFEL));
146 lTypeAlbum.put("lTypeValue2", new TestContextLongItem(0xFFFFFFFFFFFFFFFDL));
147 lTypeAlbum.put("lTypeValue3", new TestContextLongItem(0xFFFFFFFFFFFFFFFCL));
148 lTypeAlbum.put("lTypeValue4", new TestContextLongItem(0xFFFFFFFFFFFFFFFBL));
149 lTypeAlbum.put("lTypeValue5", new TestContextLongItem(0xFFFFFFFFFFFFFFFAL));
150 lTypeAlbum.put("lTypeValue6", new TestContextLongItem(0xFFFFFFFFFFFFFFF9L));
151 lTypeAlbum.put("lTypeValue7", new TestContextLongItem(0xFFFFFFFFFFFFFFF8L));
152 lTypeAlbum.put("lTypeValue8", new TestContextLongItem(0xFFFFFFFFFFFFFFF7L));
153 lTypeAlbum.put("lTypeValue9", new TestContextLongItem(0xFFFFFFFFFFFFFFF6L));
154 lTypeAlbum.put("lTypeValueA", new TestContextLongItem(0xFFFFFFFFFFFFFFF5L));
155 lTypeAlbum.put("lTypeValueB", new TestContextLongItem(0xFFFFFFFFFFFFFFF4L));
156 lTypeAlbum.put("lTypeValueC", new TestContextLongItem(0xFFFFFFFFFFFFFFF3L));
157 lTypeAlbum.put("lTypeValueD", new TestContextLongItem(0xFFFFFFFFFFFFFFF2L));
158 lTypeAlbum.put("lTypeValueE", new TestContextLongItem(0xFFFFFFFFFFFFFFF1L));
159 lTypeAlbum.put("lTypeValueF", new TestContextLongItem(0xFFFFFFFFFFFFFFF0L));
160 LOGGER.debug(lTypeAlbum.toString());
161 assert (lTypeAlbum.size() >= 16);
162 // CHECKSTYLE:ON: checkstyle:magicNumber
164 // The initial value for concurrent testing
165 final TestContextLongItem item = new TestContextLongItem(0L);
166 lTypeAlbum.put("testValue", item);
171 * Verify and clear context.
173 * @param jvmCount the jvm count
174 * @param threadCount the thread count
175 * @param threadLoops the thread loops
177 * @throws ContextException the context exception
179 private long verifyAndClearContext(final int jvmCount, final int threadCount, final int threadLoops)
180 throws ContextException {
182 LOGGER.debug("threads finished, end value is {}",
183 ((TestContextLongItem) lTypeAlbum.get("testValue")).getLongValue());
184 } catch (final Exception exception) {
185 LOGGER.error("Error: ", exception);
187 final long total = ((TestContextLongItem) lTypeAlbum.get("testValue")).getLongValue();
189 contextDistributor.clear();
190 contextDistributor = null;