2eea957c647061df73e4d9ed29ce6321395f4759
[policy/apex-pdp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.context.test.locking;
22
23 import java.io.IOException;
24
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;
37
38 /**
39  * The Class TestConcurrentContext tests concurrent use of context.
40  *
41  * @author Liam Fallon (liam.fallon@ericsson.com)
42  */
43 public class ConcurrentContext {
44     private static final int TEN_MILLISECONDS = 10;
45
46     // Logger for this class
47     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ConcurrentContext.class);
48
49     // The context distributor and map used by each test
50     private Distributor contextDistributor = null;
51     private ContextAlbum lTypeAlbum = null;
52
53     /**
54      * Test concurrent context.
55      *
56      * @param testType the test type
57      * @param jvmCount the jvm count
58      * @param threadCount the thread count
59      * @param threadLoops the thread loops
60      * @return the long
61      * @throws ApexModelException the apex model exception
62      * @throws IOException the IO exception
63      * @throws ApexException the apex exception
64      */
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();
68
69         try {
70             concurrentContext.setupAndVerifyContext();
71         } catch (final Exception exception) {
72             LOGGER.error("Error occured while setting up and verifying concurrent context", exception);
73             throw exception;
74         }
75
76         LOGGER.debug("starting JVMs and threads . . .");
77
78         final Thread[] threadArray = new Thread[threadCount];
79
80         // Check if we have a single JVM or multiple JVMs
81         int runningThreadCount = -1;
82         if (jvmCount == 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();
88             }
89
90             runningThreadCount = threadCount;
91         } else {
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();
97             }
98             runningThreadCount = jvmCount;
99         }
100
101         boolean allFinished;
102         do {
103             allFinished = true;
104             for (int i = 0; i < runningThreadCount; i++) {
105                 if (threadArray[i].isAlive()) {
106                     allFinished = false;
107                     try {
108                         Thread.sleep(TEN_MILLISECONDS);
109                     } catch (final Exception e) {
110                     }
111                     break;
112                 }
113             }
114         } while (!allFinished);
115
116         return concurrentContext.verifyAndClearContext(jvmCount, threadCount, threadLoops);
117     }
118
119     /**
120      * Setup and verify context.
121      *
122      * @throws ContextException the context exception
123      */
124     private void setupAndVerifyContext() throws ContextException {
125         final AxArtifactKey distributorKey = new AxArtifactKey("ApexDistributor", "0.0.1");
126         contextDistributor = new DistributorFactory().getDistributor(distributorKey);
127
128         // @formatter:off
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")
133                 };
134         // @formatter:on
135
136         final AxContextModel albumsModel = TestContextAlbumFactory.createMultiAlbumsContextModel();
137         contextDistributor.registerModel(albumsModel);
138
139         lTypeAlbum = contextDistributor.createContextAlbum(new AxArtifactKey("LTypeContextAlbum", "0.0.1"));
140         assert (lTypeAlbum != null);
141         lTypeAlbum.setUserArtifactStack(usedArtifactStackArray);
142
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
163
164         // The initial value for concurrent testing
165         final TestContextLongItem item = new TestContextLongItem(0L);
166         lTypeAlbum.put("testValue", item);
167
168     }
169
170     /**
171      * Verify and clear context.
172      *
173      * @param jvmCount the jvm count
174      * @param threadCount the thread count
175      * @param threadLoops the thread loops
176      * @return the long
177      * @throws ContextException the context exception
178      */
179     private long verifyAndClearContext(final int jvmCount, final int threadCount, final int threadLoops)
180             throws ContextException {
181         try {
182             LOGGER.debug("threads finished, end value is {}",
183                     ((TestContextLongItem) lTypeAlbum.get("testValue")).getLongValue());
184         } catch (final Exception exception) {
185             LOGGER.error("Error: ", exception);
186         }
187         final long total = ((TestContextLongItem) lTypeAlbum.get("testValue")).getLongValue();
188
189         contextDistributor.clear();
190         contextDistributor = null;
191
192         return total;
193     }
194 }