726880f3b79304db91f52be4a052ceb935ffe31b
[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
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.parameters.ContextParameters;
29 import org.onap.policy.apex.context.test.lock.modifier.AlbumModifier;
30 import org.onap.policy.apex.context.test.utils.ConfigrationProvider;
31 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
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      * (non-Javadoc)
64      *
65      * @see java.lang.Runnable#run()
66      */
67     @Override
68     public void run() {
69         LOGGER.info("running TestConcurrentContextThread_" + jvm + "_" + instance + " . . .");
70
71
72         final AxArtifactKey distributorKey = new AxArtifactKey("ApexDistributor_" + jvm + "_" + instance, "0.0.1");
73         final Distributor distributor = configrationProvider.getDistributor(distributorKey);
74
75         try {
76             final long startTime = System.currentTimeMillis();
77             final ContextAlbum contextAlbum = configrationProvider.getContextAlbum(distributor);
78
79             final AlbumModifier albumModifier = configrationProvider.getAlbumModifier();
80             albumModifier.modifyAlbum(contextAlbum, configrationProvider.getLoopSize(),
81                     configrationProvider.getAlbumSize());
82             LOGGER.info("Took {} ms to modify album", (System.currentTimeMillis() - startTime));
83
84         } catch (final Exception e) {
85             LOGGER.error("Unexpected error occured while processing", e);
86         } finally {
87             try {
88                 distributor.shutdown();
89             } catch (final ContextException e) {
90                 LOGGER.error("Unable to shutdown distributor", e);
91             }
92         }
93         LOGGER.info("finished TestConcurrentContextThread_" + jvm + "_" + instance + " . . .");
94
95     }
96
97     @Override
98     public void close() {
99         LOGGER.info("Shutting down {} thread ...", Thread.currentThread().getName());
100     }
101 }