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.Closeable;
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.parameters.ContextParameters;
30 import org.onap.policy.apex.context.test.concepts.TestContextLongItem;
31 import org.onap.policy.apex.context.test.factory.TestContextAlbumFactory;
32 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
33 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
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 TestConcurrentContextThread tests concurrent use of context.
41 * @author Liam Fallon (liam.fallon@ericsson.com)
43 public class ConcurrentContextThread implements Runnable, Closeable {
44 private static final String VALUE = "testValue";
45 // Logger for this class
46 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ConcurrentContextThread.class);
47 private final Distributor distributor;
48 private final int jvm;
49 private final int instance;
50 private final int threadLoops;
56 * @param instance the instance
57 * @param threadLoops the thread loops
58 * @throws ApexException the apex exception
60 public ConcurrentContextThread(final int jvm, final int instance, final int threadLoops) throws ApexException {
62 this.instance = instance;
63 this.threadLoops = threadLoops;
65 final AxArtifactKey distributorKey = new AxArtifactKey("ApexDistributor_" + jvm + "_" + instance, "0.0.1");
67 new ContextParameters();
68 distributor = new DistributorFactory().getDistributor(distributorKey);
69 final AxContextModel albumsModel = TestContextAlbumFactory.createMultiAlbumsContextModel();
70 distributor.registerModel(albumsModel);
76 * @see java.lang.Runnable#run()
80 LOGGER.info("running TestConcurrentContextThread_" + jvm + "_" + instance + " . . .");
82 ContextAlbum lTypeAlbum = null;
85 lTypeAlbum = distributor.createContextAlbum(new AxArtifactKey("LTypeContextAlbum", "0.0.1"));
86 } catch (final Exception e) {
87 LOGGER.error("could not get the test context album", e);
88 LOGGER.error("failed TestConcurrentContextThread_" + jvm + "_" + instance);
92 if (lTypeAlbum == null) {
93 LOGGER.error("could not find the test context album");
94 LOGGER.error("failed TestConcurrentContextThread_" + jvm + "_" + instance);
99 final AxArtifactKey[] usedArtifactStackArray = {new AxArtifactKey("testC-top", "0.0.1"),
100 new AxArtifactKey("testC-next", "0.0.1"), new AxArtifactKey("testC-bot", "0.0.1")};
103 lTypeAlbum.setUserArtifactStack(usedArtifactStackArray);
106 updateAlbum(lTypeAlbum);
107 } catch (final Exception exception) {
108 LOGGER.error("could not set the value in the test context album", exception);
109 LOGGER.error("failed TestConcurrentContextThread_" + jvm + "_" + instance);
114 lTypeAlbum.lockForWriting(VALUE);
115 final TestContextLongItem item = (TestContextLongItem) lTypeAlbum.get(VALUE);
116 final long value = item.getLongValue();
117 LOGGER.info("completed TestConcurrentContextThread_" + jvm + "_" + instance + ", value=" + value);
118 } catch (final Exception e) {
119 LOGGER.error("could not read the value in the test context album", e);
120 LOGGER.error("failed TestConcurrentContextThread_" + jvm + "_" + instance);
123 lTypeAlbum.unlockForWriting(VALUE);
124 distributor.shutdown();
125 } catch (final ContextException e) {
126 LOGGER.error("could not unlock test context album item", e);
127 LOGGER.error("failed TestConcurrentContextThread_" + jvm + "_" + instance);
132 private void updateAlbum(final ContextAlbum lTypeAlbum) throws Exception {
133 for (int i = 0; i < threadLoops; i++) {
135 lTypeAlbum.lockForWriting(VALUE);
136 TestContextLongItem item = (TestContextLongItem) lTypeAlbum.get(VALUE);
138 long value = item.getLongValue();
139 item.setLongValue(++value);
141 item = new TestContextLongItem(0L);
143 lTypeAlbum.put(VALUE, item);
145 lTypeAlbum.unlockForWriting(VALUE);
151 public void close() {
152 LOGGER.info("Shutting down {} thread ...", Thread.currentThread().getName());