a400bc79ad2d81194fca71bede283dcd3d874791
[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.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;
30
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;
44
45 /**
46  * The Class TestConcurrentContext tests concurrent use of context.
47  *
48  * @author Liam Fallon (liam.fallon@ericsson.com)
49  */
50 public class ConcurrentContext {
51     // Logger for this class
52     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ConcurrentContext.class);
53
54     // The context distributor and map used by each test
55     private Distributor contextDistributor = null;
56     private ContextAlbum lTypeAlbum = null;
57
58     /**
59      * Test concurrent context.
60      *
61      * @param testType the test type
62      * @param jvmCount the jvm count
63      * @param threadCount the thread count
64      * @param threadLoops the thread loops
65      * @return the long
66      * @throws ApexModelException the apex model exception
67      * @throws IOException the IO exception
68      * @throws ApexException the apex exception
69      */
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();
73
74         try {
75             concurrentContext.setupAndVerifyContext();
76         } catch (final Exception exception) {
77             LOGGER.error("Error occured while setting up and verifying concurrent context", exception);
78             throw exception;
79         }
80
81         LOGGER.debug("starting JVMs and threads . . .");
82
83         final String name = getThreadFactoryName(jvmCount, testType);
84         final IntegrationThreadFactory threadFactory = new IntegrationThreadFactory(name);
85         final ExecutorService executorService = Executors.newFixedThreadPool(threadCount, threadFactory);
86
87         final List<Closeable> tasks = new ArrayList<>(threadCount);
88
89         addShutDownHook(tasks);
90
91         // Check if we have a single JVM or multiple JVMs
92         if (jvmCount == 1) {
93             // Run everything in this JVM
94             for (int t = 0; t < threadCount; t++) {
95                 final ConcurrentContextThread task = new ConcurrentContextThread(0, t, threadLoops);
96                 tasks.add(task);
97                 executorService.execute(task);
98             }
99
100         } else {
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);
105                 tasks.add(task);
106                 executorService.execute(task);
107             }
108         }
109
110         try {
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         }
117
118         LOGGER.info("Shutting down now ...");
119         executorService.shutdownNow();
120
121         return concurrentContext.verifyAndClearContext(jvmCount, threadCount, threadLoops);
122     }
123
124
125     private void addShutDownHook(final List<Closeable> tasks) {
126         Runtime.getRuntime().addShutdownHook(new Thread() {
127             @Override
128             public void run() {
129                 LOGGER.info("Shutting down ...");
130                 for (final Closeable task : tasks) {
131                     try {
132                         task.close();
133                     } catch (final IOException ioException) {
134                         LOGGER.error("Unable to close task ... ", ioException);
135                     }
136                 }
137             }
138         });
139     }
140
141
142     private String getThreadFactoryName(final int jvmCount, final String testType) {
143         return jvmCount == 1 ? testType + ":TestConcurrentContextThread_0_"
144                 : testType + ":TestConcurrentContextJVMThread_";
145     }
146
147     /**
148      * Setup and verify context.
149      *
150      * @throws ContextException the context exception
151      */
152     private void setupAndVerifyContext() throws ContextException {
153         final AxArtifactKey distributorKey = new AxArtifactKey("ApexDistributor", "0.0.1");
154         contextDistributor = new DistributorFactory().getDistributor(distributorKey);
155
156         // @formatter:off
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")
161                 };
162         // @formatter:on
163
164         final AxContextModel albumsModel = TestContextAlbumFactory.createMultiAlbumsContextModel();
165         contextDistributor.registerModel(albumsModel);
166
167         lTypeAlbum = contextDistributor.createContextAlbum(new AxArtifactKey("LTypeContextAlbum", "0.0.1"));
168         assert (lTypeAlbum != null);
169         lTypeAlbum.setUserArtifactStack(usedArtifactStackArray);
170
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
191
192         // The initial value for concurrent testing
193         final TestContextLongItem item = new TestContextLongItem(0L);
194         lTypeAlbum.put("testValue", item);
195
196     }
197
198     /**
199      * Verify and clear context.
200      *
201      * @param jvmCount the jvm count
202      * @param threadCount the thread count
203      * @param threadLoops the thread loops
204      * @return the long
205      * @throws ContextException the context exception
206      */
207     private long verifyAndClearContext(final int jvmCount, final int threadCount, final int threadLoops)
208             throws ContextException {
209         try {
210             LOGGER.debug("threads finished, end value is {}",
211                     ((TestContextLongItem) lTypeAlbum.get("testValue")).getLongValue());
212         } catch (final Exception exception) {
213             LOGGER.error("Error: ", exception);
214         }
215         final long total = ((TestContextLongItem) lTypeAlbum.get("testValue")).getLongValue();
216
217         contextDistributor.clear();
218         contextDistributor = null;
219
220         return total;
221     }
222 }