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 org.onap.policy.apex.context.ContextAlbum;
24 import org.onap.policy.apex.context.ContextException;
25 import org.onap.policy.apex.context.Distributor;
26 import org.onap.policy.apex.context.impl.distribution.DistributorFactory;
27 import org.onap.policy.apex.context.parameters.ContextParameters;
28 import org.onap.policy.apex.context.test.concepts.TestContextLongItem;
29 import org.onap.policy.apex.context.test.factory.TestContextAlbumFactory;
30 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
31 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
32 import org.onap.policy.apex.model.contextmodel.concepts.AxContextModel;
33 import org.slf4j.ext.XLogger;
34 import org.slf4j.ext.XLoggerFactory;
37 * The Class TestConcurrentContextThread tests concurrent use of context.
39 * @author Liam Fallon (liam.fallon@ericsson.com)
41 public class ConcurrentContextThread implements Runnable {
42 // Logger for this class
43 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ConcurrentContextThread.class);
44 private final Distributor distributor;
45 private final int jvm;
46 private final int instance;
47 private final int threadLoops;
53 * @param instance the instance
54 * @param threadLoops the thread loops
55 * @throws ApexException the apex exception
57 public ConcurrentContextThread(final int jvm, final int instance, final int threadLoops) throws ApexException {
59 this.instance = instance;
60 this.threadLoops = threadLoops;
62 final AxArtifactKey distributorKey = new AxArtifactKey("ApexDistributor_" + jvm + "_" + instance, "0.0.1");
64 new ContextParameters();
65 distributor = new DistributorFactory().getDistributor(distributorKey);
66 final AxContextModel albumsModel = TestContextAlbumFactory.createMultiAlbumsContextModel();
67 distributor.registerModel(albumsModel);
73 * @see java.lang.Runnable#run()
77 LOGGER.info("running TestConcurrentContextThread_" + jvm + "_" + instance + " . . .");
79 ContextAlbum lTypeAlbum = null;
82 lTypeAlbum = distributor.createContextAlbum(new AxArtifactKey("LTypeContextAlbum", "0.0.1"));
83 } catch (final Exception e) {
84 LOGGER.error("could not get the test context album", e);
85 LOGGER.error("failed TestConcurrentContextThread_" + jvm + "_" + instance);
89 if (lTypeAlbum == null) {
90 LOGGER.error("could not find the test context album");
91 LOGGER.error("failed TestConcurrentContextThread_" + jvm + "_" + instance);
96 final AxArtifactKey[] usedArtifactStackArray = {new AxArtifactKey("testC-top", "0.0.1"),
97 new AxArtifactKey("testC-next", "0.0.1"), new AxArtifactKey("testC-bot", "0.0.1")};
100 lTypeAlbum.setUserArtifactStack(usedArtifactStackArray);
103 updateAlbum(lTypeAlbum);
104 } catch (final Exception exception) {
105 LOGGER.error("could not set the value in the test context album", exception);
106 LOGGER.error("failed TestConcurrentContextThread_" + jvm + "_" + instance);
111 lTypeAlbum.lockForWriting("testValue");
112 final TestContextLongItem item = (TestContextLongItem) lTypeAlbum.get("testValue");
113 final long value = item.getLongValue();
114 LOGGER.info("completed TestConcurrentContextThread_" + jvm + "_" + instance + ", value=" + value);
115 } catch (final Exception e) {
116 LOGGER.error("could not read the value in the test context album", e);
117 LOGGER.error("failed TestConcurrentContextThread_" + jvm + "_" + instance);
120 lTypeAlbum.unlockForWriting("testValue");
121 distributor.shutdown();
122 } catch (final ContextException e) {
123 LOGGER.error("could not unlock test context album item", e);
124 LOGGER.error("failed TestConcurrentContextThread_" + jvm + "_" + instance);
129 private void updateAlbum(final ContextAlbum lTypeAlbum) throws Exception {
130 for (int i = 0; i < threadLoops; i++) {
132 lTypeAlbum.lockForWriting("testValue");
133 TestContextLongItem item = (TestContextLongItem) lTypeAlbum.get("testValue");
135 long value = item.getLongValue();
136 item.setLongValue(++value);
138 item = new TestContextLongItem(0L);
140 lTypeAlbum.put("testValue", item);
142 lTypeAlbum.unlockForWriting("testValue");