9826de7252f3e0213c4a6eb634ed19b729bac31c
[policy/apex-pdp.git] / testsuites / integration / integration-context-test / src / test / java / org / onap / policy / apex / testsuites / integration / context / locking / ConcurrentContextThread.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.testsuites.integration.context.locking;
23
24 import java.io.Closeable;
25
26 import org.onap.policy.apex.context.ContextAlbum;
27 import org.onap.policy.apex.context.Distributor;
28 import org.onap.policy.apex.context.parameters.ContextParameters;
29 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
30 import org.onap.policy.apex.testsuites.integration.context.lock.modifier.AlbumModifier;
31 import org.onap.policy.apex.testsuites.integration.context.utils.ConfigrationProvider;
32 import org.slf4j.ext.XLogger;
33 import org.slf4j.ext.XLoggerFactory;
34
35 /**
36  * The Class TestConcurrentContextThread tests concurrent use of context.
37  *
38  * @author Liam Fallon (liam.fallon@ericsson.com)
39  */
40 public class ConcurrentContextThread implements Runnable, Closeable {
41     // Logger for this class
42     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ConcurrentContextThread.class);
43     private final int jvm;
44     private final int instance;
45     private final ConfigrationProvider configrationProvider;
46
47     /**
48      * The Constructor.
49      *
50      * @param jvm the jvm
51      * @param instance the instance
52      * @param configrationProvider the configuration provider
53      */
54     public ConcurrentContextThread(final int jvm, final int instance, final ConfigrationProvider configrationProvider) {
55         this.jvm = jvm;
56         this.instance = instance;
57         this.configrationProvider = configrationProvider;
58
59         new ContextParameters();
60     }
61
62     /**
63      * {@inheritDoc}.
64      */
65     @Override
66     public void run() {
67         LOGGER.info("running TestConcurrentContextThread_" + jvm + "_" + instance + " . . .");
68
69
70         final AxArtifactKey distributorKey = new AxArtifactKey("ApexDistributor_" + jvm + "_" + instance, "0.0.1");
71         final Distributor distributor = configrationProvider.getDistributor(distributorKey);
72
73         try {
74             final long startTime = System.currentTimeMillis();
75             final ContextAlbum contextAlbum = configrationProvider.getContextAlbum(distributor);
76
77             final AlbumModifier albumModifier = configrationProvider.getAlbumModifier();
78             albumModifier.modifyAlbum(contextAlbum, configrationProvider.getLoopSize(),
79                     configrationProvider.getAlbumSize());
80             LOGGER.info("Took {} ms to modify album", (System.currentTimeMillis() - startTime));
81
82         } catch (final Exception e) {
83             LOGGER.error("Unexpected error occured while processing", e);
84         }
85
86         LOGGER.info("finished TestConcurrentContextThread_" + jvm + "_" + instance + " . . .");
87     }
88
89     @Override
90     public void close() {
91         LOGGER.info("Shutting down {} thread ...", Thread.currentThread().getName());
92     }
93 }