diff options
Diffstat (limited to 'context/context-test/src/main/java/org/onap/policy/apex/context/test/locking/ConcurrentContextThread.java')
-rw-r--r-- | context/context-test/src/main/java/org/onap/policy/apex/context/test/locking/ConcurrentContextThread.java | 149 |
1 files changed, 149 insertions, 0 deletions
diff --git a/context/context-test/src/main/java/org/onap/policy/apex/context/test/locking/ConcurrentContextThread.java b/context/context-test/src/main/java/org/onap/policy/apex/context/test/locking/ConcurrentContextThread.java new file mode 100644 index 000000000..0691b8ae5 --- /dev/null +++ b/context/context-test/src/main/java/org/onap/policy/apex/context/test/locking/ConcurrentContextThread.java @@ -0,0 +1,149 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.context.test.locking; + +import org.onap.policy.apex.context.ContextAlbum; +import org.onap.policy.apex.context.ContextException; +import org.onap.policy.apex.context.Distributor; +import org.onap.policy.apex.context.impl.distribution.DistributorFactory; +import org.onap.policy.apex.context.parameters.ContextParameters; +import org.onap.policy.apex.context.test.concepts.TestContextItem003; +import org.onap.policy.apex.context.test.factory.TestContextAlbumFactory; +import org.onap.policy.apex.model.basicmodel.concepts.ApexException; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextModel; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * The Class TestConcurrentContextThread tests concurrent use of context. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class ConcurrentContextThread implements Runnable { + // Logger for this class + private static final XLogger LOGGER = XLoggerFactory.getXLogger(ConcurrentContextThread.class); + private final Distributor distributor; + private final int jvm; + private final int instance; + private final int threadLoops; + + /** + * The Constructor. + * + * @param jvm the jvm + * @param instance the instance + * @param threadLoops the thread loops + * @throws ApexException the apex exception + */ + public ConcurrentContextThread(final int jvm, final int instance, final int threadLoops) throws ApexException { + this.jvm = jvm; + this.instance = instance; + this.threadLoops = threadLoops; + + final AxArtifactKey distributorKey = new AxArtifactKey("ApexDistributor_" + jvm + "_" + instance, "0.0.1"); + + new ContextParameters(); + distributor = new DistributorFactory().getDistributor(distributorKey); + final AxContextModel albumsModel = TestContextAlbumFactory.createMultiAlbumsContextModel(); + distributor.registerModel(albumsModel); + } + + /* + * (non-Javadoc) + * + * @see java.lang.Runnable#run() + */ + @Override + public void run() { + LOGGER.info("running TestConcurrentContextThread_" + jvm + "_" + instance + " . . ."); + + ContextAlbum lTypeAlbum = null; + + try { + lTypeAlbum = distributor.createContextAlbum(new AxArtifactKey("LTypeContextAlbum", "0.0.1")); + } catch (final Exception e) { + LOGGER.error("could not get the test context album", e); + LOGGER.error("failed TestConcurrentContextThread_" + jvm + "_" + instance); + return; + } + + if (lTypeAlbum == null) { + LOGGER.error("could not find the test context album"); + LOGGER.error("failed TestConcurrentContextThread_" + jvm + "_" + instance); + return; + } + + // @formatter:off + final AxArtifactKey[] usedArtifactStackArray = { + new AxArtifactKey("testC-top", "0.0.1"), + new AxArtifactKey("testC-next", "0.0.1"), + new AxArtifactKey("testC-bot", "0.0.1") + }; + // @formatter:on + + lTypeAlbum.setUserArtifactStack(usedArtifactStackArray); + + for (int i = 0; i < threadLoops; i++) { + try { + lTypeAlbum.lockForWriting("testValue"); + TestContextItem003 item = (TestContextItem003) lTypeAlbum.get("testValue"); + if (item != null) { + long value = item.getLongValue(); + item.setLongValue(++value); + } else { + item = new TestContextItem003(0L); + } + lTypeAlbum.put("testValue", item); + } catch (final Exception e) { + LOGGER.error("could not set the value in the test context album", e); + LOGGER.error("failed TestConcurrentContextThread_" + jvm + "_" + instance); + return; + } finally { + try { + lTypeAlbum.unlockForWriting("testValue"); + } catch (final ContextException e) { + LOGGER.error("could not unlock test context album item", e); + LOGGER.error("failed TestConcurrentContextThread_" + jvm + "_" + instance); + return; + } + } + } + + try { + lTypeAlbum.lockForWriting("testValue"); + final TestContextItem003 item = (TestContextItem003) lTypeAlbum.get("testValue"); + final long value = item.getLongValue(); + LOGGER.info("completed TestConcurrentContextThread_" + jvm + "_" + instance + ", value=" + value); + } catch (final Exception e) { + LOGGER.error("could not read the value in the test context album", e); + LOGGER.error("failed TestConcurrentContextThread_" + jvm + "_" + instance); + } finally { + try { + lTypeAlbum.unlockForWriting("testValue"); + distributor.shutdown(); + } catch (final ContextException e) { + LOGGER.error("could not unlock test context album item", e); + LOGGER.error("failed TestConcurrentContextThread_" + jvm + "_" + instance); + } + } + } +} |