diff options
author | waqas.ikram <waqas.ikram@ericsson.com> | 2018-07-17 15:35:47 +0100 |
---|---|---|
committer | waqas.ikram <waqas.ikram@ericsson.com> | 2018-07-18 16:10:29 +0100 |
commit | dcae33d8d9a3dcb8a9249d5d33f103f1e97d2e6c (patch) | |
tree | e9c54c365154595a9fbd996204ee3fa9eca69025 /context/context-test-utils/src/main/java/org | |
parent | 84c349f342da7f67fe728bb2ef3a3b59afb4cd7e (diff) |
Refactoring Metrics test
Change-Id: I99290a3dec61b3ce0f4d5fd33d6b429d091b48ca
Issue-ID: POLICY-954
Signed-off-by: waqas.ikram <waqas.ikram@ericsson.com>
Diffstat (limited to 'context/context-test-utils/src/main/java/org')
18 files changed, 818 insertions, 285 deletions
diff --git a/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/distribution/ContextInstantiation.java b/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/distribution/ContextInstantiation.java index 03ef650da..888394be2 100644 --- a/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/distribution/ContextInstantiation.java +++ b/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/distribution/ContextInstantiation.java @@ -44,7 +44,6 @@ import static org.onap.policy.apex.context.test.utils.Constants.STRING_GLOBAL_VA import static org.onap.policy.apex.context.test.utils.Constants.STRING_VAL; import static org.onap.policy.apex.context.test.utils.Constants.TEST_POLICY_CONTEXT_ITEM; import static org.onap.policy.apex.context.test.utils.Constants.TIME_ZONE; -import static org.onap.policy.apex.context.test.utils.Constants.USED_ARTIFACT_STACK_ARRAY; import static org.onap.policy.apex.context.test.utils.Constants.VERSION; import java.util.Collection; @@ -79,6 +78,7 @@ import org.onap.policy.apex.context.test.concepts.TestExternalContextItem; import org.onap.policy.apex.context.test.concepts.TestGlobalContextItem; import org.onap.policy.apex.context.test.concepts.TestPolicyContextItem; import org.onap.policy.apex.context.test.factory.TestContextAlbumFactory; +import org.onap.policy.apex.context.test.utils.Constants; import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult; import org.onap.policy.apex.model.contextmodel.concepts.AxContextModel; @@ -129,7 +129,7 @@ public class ContextInstantiation { contextDistributor.createContextAlbum(new AxArtifactKey(POLICY_CONTEXT_ALBUM, VERSION)); assertNotNull(policyContextAlbum); - policyContextAlbum.setUserArtifactStack(USED_ARTIFACT_STACK_ARRAY); + policyContextAlbum.setUserArtifactStack(Constants.getAxArtifactKeyArray()); final Date testDate = new Date(); @@ -204,7 +204,7 @@ public class ContextInstantiation { final AxArtifactKey axContextAlbumKey = new AxArtifactKey(EXTERNAL_CONTEXT_ALBUM, VERSION); final ContextAlbum externalContextAlbum = contextDistributor.createContextAlbum(axContextAlbumKey); assertNotNull(externalContextAlbum); - externalContextAlbum.setUserArtifactStack(USED_ARTIFACT_STACK_ARRAY); + externalContextAlbum.setUserArtifactStack(Constants.getAxArtifactKeyArray()); externalContextAlbum.putAll(valueMap2); externalContextAlbum.getAlbumDefinition().setWritable(false); @@ -363,7 +363,7 @@ public class ContextInstantiation { final ContextAlbum globalContextAlbum = contextDistributor.createContextAlbum(new AxArtifactKey(GLOBAL_CONTEXT_ALBUM, VERSION)); assertNotNull(globalContextAlbum); - globalContextAlbum.setUserArtifactStack(USED_ARTIFACT_STACK_ARRAY); + globalContextAlbum.setUserArtifactStack(Constants.getAxArtifactKeyArray()); return globalContextAlbum; } diff --git a/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/distribution/ContextUpdate.java b/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/distribution/ContextUpdate.java index 75d559724..33100165b 100644 --- a/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/distribution/ContextUpdate.java +++ b/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/distribution/ContextUpdate.java @@ -31,8 +31,8 @@ import static org.onap.policy.apex.context.test.utils.Constants.EXCEPTION_MESSAG import static org.onap.policy.apex.context.test.utils.Constants.LONG_CONTEXT_ALBUM; import static org.onap.policy.apex.context.test.utils.Constants.MAP_CONTEXT_ALBUM; import static org.onap.policy.apex.context.test.utils.Constants.TIME_ZONE; -import static org.onap.policy.apex.context.test.utils.Constants.USED_ARTIFACT_STACK_ARRAY; import static org.onap.policy.apex.context.test.utils.Constants.VERSION; +import static org.onap.policy.apex.context.test.utils.Constants.getAxArtifactKeyArray; import java.io.IOException; import java.util.Date; @@ -160,7 +160,7 @@ public class ContextUpdate { final ContextAlbum longContextAlbum = contextDistributor.createContextAlbum(new AxArtifactKey(albumKey, VERSION)); assertNotNull(longContextAlbum); - longContextAlbum.setUserArtifactStack(USED_ARTIFACT_STACK_ARRAY); + longContextAlbum.setUserArtifactStack(getAxArtifactKeyArray()); return longContextAlbum; } diff --git a/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/distribution/SequentialContextInstantiation.java b/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/distribution/SequentialContextInstantiation.java index dbf5ab2c6..4041c079d 100644 --- a/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/distribution/SequentialContextInstantiation.java +++ b/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/distribution/SequentialContextInstantiation.java @@ -36,7 +36,6 @@ import static org.onap.policy.apex.context.test.utils.Constants.LONG_VAL; import static org.onap.policy.apex.context.test.utils.Constants.PI_VAL; import static org.onap.policy.apex.context.test.utils.Constants.STRING_GLOBAL_VAL; import static org.onap.policy.apex.context.test.utils.Constants.TIME_ZONE; -import static org.onap.policy.apex.context.test.utils.Constants.USED_ARTIFACT_STACK_ARRAY; import static org.onap.policy.apex.context.test.utils.Constants.VERSION; import java.util.Collection; @@ -63,6 +62,7 @@ import org.onap.policy.apex.context.test.concepts.TestContextIntItem; import org.onap.policy.apex.context.test.concepts.TestContextLongItem; import org.onap.policy.apex.context.test.concepts.TestContextLongObjectItem; import org.onap.policy.apex.context.test.concepts.TestContextStringItem; +import org.onap.policy.apex.context.test.utils.Constants; import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; import org.slf4j.ext.XLogger; import org.slf4j.ext.XLoggerFactory; @@ -351,7 +351,7 @@ public class SequentialContextInstantiation { throws ContextException { final ContextAlbum dateAlbum = contextDistributor.createContextAlbum(new AxArtifactKey(albumName, VERSION)); assertNotNull(dateAlbum); - dateAlbum.setUserArtifactStack(USED_ARTIFACT_STACK_ARRAY); + dateAlbum.setUserArtifactStack(Constants.getAxArtifactKeyArray()); return dateAlbum; } diff --git a/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/lock/modifier/AlbumModifier.java b/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/lock/modifier/AlbumModifier.java new file mode 100644 index 000000000..24e633109 --- /dev/null +++ b/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/lock/modifier/AlbumModifier.java @@ -0,0 +1,29 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 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.lock.modifier; + +import org.onap.policy.apex.context.ContextAlbum; +import org.onap.policy.apex.context.ContextException; + +public interface AlbumModifier { + + void modifyAlbum(final ContextAlbum contextAlbum, final int loopSize, final int arraySize) throws ContextException; + +} diff --git a/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/lock/modifier/LockType.java b/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/lock/modifier/LockType.java new file mode 100644 index 000000000..0dd0e7224 --- /dev/null +++ b/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/lock/modifier/LockType.java @@ -0,0 +1,77 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 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.lock.modifier; + +import org.onap.policy.apex.context.ContextRuntimeException; + +public enum LockType { + + NO_LOCK(0) { + @Override + public AlbumModifier getAlbumModifier() { + return NO_LOCK_MODIFER; + } + }, + READ_LOCK(1) { + @Override + public AlbumModifier getAlbumModifier() { + return READ_LOCK_MODIFER; + } + }, + WRITE_LOCK(2) { + @Override + public AlbumModifier getAlbumModifier() { + return WRITE_LOCK_MODIFER; + } + }, + WRITE_LOCK_SINGLE_VALUE_UPDATE(3) { + @Override + public AlbumModifier getAlbumModifier() { + return WRITE_LOCK_SINGLE_VALUE_MODIFER; + } + }; + + private static final AlbumModifier NO_LOCK_MODIFER = new NoLockAlbumModifier(); + private static final AlbumModifier READ_LOCK_MODIFER = new ReadLockAlbumModifier(); + private static final AlbumModifier WRITE_LOCK_MODIFER = new WriteLockAlbumModifier(); + private static final AlbumModifier WRITE_LOCK_SINGLE_VALUE_MODIFER = new SingleValueWriteLockAlbumModifier(); + + private final int value; + + private LockType(final int value) { + this.value = value; + } + + public int getValue() { + return value; + } + + public static LockType getLockType(final int value) { + for (final LockType lockType : LockType.values()) { + if (lockType.getValue() == value) { + return lockType; + } + } + throw new ContextRuntimeException("Invalid Lock type value: " + value); + } + + public abstract AlbumModifier getAlbumModifier(); + +} diff --git a/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/lock/modifier/NoLockAlbumModifier.java b/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/lock/modifier/NoLockAlbumModifier.java new file mode 100644 index 000000000..b6ee1b8eb --- /dev/null +++ b/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/lock/modifier/NoLockAlbumModifier.java @@ -0,0 +1,45 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 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.lock.modifier; + +import java.util.Random; + +import org.onap.policy.apex.context.ContextAlbum; +import org.onap.policy.apex.context.test.concepts.TestContextLongItem; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +public class NoLockAlbumModifier implements AlbumModifier { + private static final XLogger LOGGER = XLoggerFactory.getXLogger(NoLockAlbumModifier.class); + + @Override + public void modifyAlbum(final ContextAlbum contextAlbum, final int loopSize, final int arraySize) { + final Random rand = new Random(); + for (int i = 0; i < loopSize; i++) { + final String nextLongKey = Integer.toString(rand.nextInt(arraySize)); + final TestContextLongItem item = (TestContextLongItem) contextAlbum.get(nextLongKey); + final long value = item.getLongValue(); + if (LOGGER.isTraceEnabled()) { + LOGGER.trace("lock type={}, value={}", LockType.NO_LOCK, value); + } + } + } + +} diff --git a/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/lock/modifier/ReadLockAlbumModifier.java b/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/lock/modifier/ReadLockAlbumModifier.java new file mode 100644 index 000000000..57c6b7524 --- /dev/null +++ b/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/lock/modifier/ReadLockAlbumModifier.java @@ -0,0 +1,60 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 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.lock.modifier; + +import java.util.Random; + +import org.onap.policy.apex.context.ContextAlbum; +import org.onap.policy.apex.context.ContextException; +import org.onap.policy.apex.context.test.concepts.TestContextLongItem; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +public class ReadLockAlbumModifier implements AlbumModifier { + private static final XLogger LOGGER = XLoggerFactory.getXLogger(ReadLockAlbumModifier.class); + + @Override + public void modifyAlbum(final ContextAlbum contextAlbum, final int loopSize, final int arraySize) { + final Random rand = new Random(); + for (int i = 0; i < loopSize; i++) { + final String nextLongKey = Integer.toString(rand.nextInt(arraySize)); + try { + contextAlbum.lockForReading(nextLongKey); + } catch (final ContextException e) { + LOGGER.error("could not acquire read lock on context album, key=" + nextLongKey, e); + continue; + } + + final TestContextLongItem item = (TestContextLongItem) contextAlbum.get(nextLongKey); + final long value = item.getLongValue(); + if (LOGGER.isTraceEnabled()) { + LOGGER.trace("lock type={}, value={}", LockType.READ_LOCK, value); + } + + try { + contextAlbum.unlockForReading(nextLongKey); + } catch (final ContextException e) { + LOGGER.error("could not release read lock on context album, key=" + nextLongKey, e); + } + } + + } + +} diff --git a/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/lock/modifier/SingleValueWriteLockAlbumModifier.java b/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/lock/modifier/SingleValueWriteLockAlbumModifier.java new file mode 100644 index 000000000..a41de8dd7 --- /dev/null +++ b/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/lock/modifier/SingleValueWriteLockAlbumModifier.java @@ -0,0 +1,65 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 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.lock.modifier; + +import static org.onap.policy.apex.context.test.utils.Constants.TEST_VALUE; + +import org.onap.policy.apex.context.ContextAlbum; +import org.onap.policy.apex.context.ContextException; +import org.onap.policy.apex.context.test.concepts.TestContextLongItem; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +public class SingleValueWriteLockAlbumModifier implements AlbumModifier { + private static final XLogger LOGGER = XLoggerFactory.getXLogger(SingleValueWriteLockAlbumModifier.class); + + + @Override + public void modifyAlbum(final ContextAlbum contextAlbum, final int loopSize, final int arraySize) + throws ContextException { + for (int i = 0; i < loopSize; i++) { + try { + contextAlbum.lockForWriting(TEST_VALUE); + TestContextLongItem item = (TestContextLongItem) contextAlbum.get(TEST_VALUE); + if (item != null) { + long value = item.getLongValue(); + item.setLongValue(++value); + } else { + item = new TestContextLongItem(0L); + } + contextAlbum.put(TEST_VALUE, item); + } finally { + contextAlbum.unlockForWriting(TEST_VALUE); + } + } + + try { + contextAlbum.lockForWriting(TEST_VALUE); + final TestContextLongItem item = (TestContextLongItem) contextAlbum.get(TEST_VALUE); + final long value = item.getLongValue(); + LOGGER.info("Value after modification: ", value); + } catch (final Exception e) { + LOGGER.error("could not read the value in the test context album", e); + } finally { + contextAlbum.unlockForWriting(TEST_VALUE); + } + } + +} diff --git a/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/lock/modifier/WriteLockAlbumModifier.java b/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/lock/modifier/WriteLockAlbumModifier.java new file mode 100644 index 000000000..09e280026 --- /dev/null +++ b/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/lock/modifier/WriteLockAlbumModifier.java @@ -0,0 +1,63 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 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.lock.modifier; + +import java.util.Random; + +import org.onap.policy.apex.context.ContextAlbum; +import org.onap.policy.apex.context.ContextException; +import org.onap.policy.apex.context.test.concepts.TestContextLongItem; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +public class WriteLockAlbumModifier implements AlbumModifier { + private static final XLogger LOGGER = XLoggerFactory.getXLogger(WriteLockAlbumModifier.class); + + + @Override + public void modifyAlbum(final ContextAlbum contextAlbum, final int loopSize, final int arraySize) { + final Random rand = new Random(); + for (int i = 0; i < loopSize; i++) { + final String nextLongKey = Integer.toString(rand.nextInt(arraySize)); + try { + contextAlbum.lockForWriting(nextLongKey); + } catch (final ContextException e) { + LOGGER.error("could not acquire write lock on context album, key=" + nextLongKey, e); + continue; + } + + final TestContextLongItem item = (TestContextLongItem) contextAlbum.get(nextLongKey); + long value = item.getLongValue(); + if (LOGGER.isTraceEnabled()) { + LOGGER.trace("lock type={}, value={}", LockType.WRITE_LOCK, value); + } + item.setLongValue(++value); + contextAlbum.put(nextLongKey, item); + + try { + contextAlbum.unlockForWriting(nextLongKey); + } catch (final ContextException e) { + LOGGER.error("could not release write lock on context album, key=" + nextLongKey, e); + } + } + + } + +} diff --git a/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/locking/ConcurrentContext.java b/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/locking/ConcurrentContext.java index b62364e9d..b373e9fa0 100644 --- a/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/locking/ConcurrentContext.java +++ b/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/locking/ConcurrentContext.java @@ -23,22 +23,20 @@ package org.onap.policy.apex.context.test.locking; import java.io.Closeable; import java.io.IOException; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import java.util.Map.Entry; import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; 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.test.concepts.TestContextLongItem; -import org.onap.policy.apex.context.test.factory.TestContextAlbumFactory; -import org.onap.policy.apex.context.test.utils.IntegrationThreadFactory; +import org.onap.policy.apex.context.test.utils.ConfigrationProvider; import org.onap.policy.apex.model.basicmodel.concepts.ApexException; -import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; import org.onap.policy.apex.model.basicmodel.handling.ApexModelException; -import org.onap.policy.apex.model.contextmodel.concepts.AxContextModel; import org.slf4j.ext.XLogger; import org.slf4j.ext.XLoggerFactory; @@ -55,24 +53,17 @@ public class ConcurrentContext { private Distributor contextDistributor = null; private ContextAlbum lTypeAlbum = null; - /** - * Test concurrent context. - * - * @param testType the test type - * @param jvmCount the jvm count - * @param threadCount the thread count - * @param threadLoops the thread loops - * @return the long - * @throws ApexModelException the apex model exception - * @throws IOException the IO exception - * @throws ApexException the apex exception - */ - public long testConcurrentContext(final String testType, final int jvmCount, final int threadCount, - final int threadLoops) throws ApexModelException, IOException, ApexException { - final ConcurrentContext concurrentContext = new ConcurrentContext(); + private final ConfigrationProvider configrationProvider; + + public ConcurrentContext(final ConfigrationProvider configrationProvider) { + this.configrationProvider = configrationProvider; + } + + public Map<String, TestContextLongItem> testConcurrentContext() + throws ApexModelException, IOException, ApexException { try { - concurrentContext.setupAndVerifyContext(); + setupAndVerifyContext(); } catch (final Exception exception) { LOGGER.error("Error occured while setting up and verifying concurrent context", exception); throw exception; @@ -80,28 +71,25 @@ public class ConcurrentContext { LOGGER.debug("starting JVMs and threads . . ."); - final String name = getThreadFactoryName(jvmCount, testType); - final IntegrationThreadFactory threadFactory = new IntegrationThreadFactory(name); - final ExecutorService executorService = Executors.newFixedThreadPool(threadCount, threadFactory); + final ExecutorService executorService = configrationProvider.getExecutorService(); - final List<Closeable> tasks = new ArrayList<>(threadCount); + final List<Closeable> tasks = new ArrayList<>(configrationProvider.getThreadCount()); addShutDownHook(tasks); // Check if we have a single JVM or multiple JVMs - if (jvmCount == 1) { + if (configrationProvider.getJvmCount() == 1) { // Run everything in this JVM - for (int t = 0; t < threadCount; t++) { - final ConcurrentContextThread task = new ConcurrentContextThread(0, t, threadLoops); + for (int t = 0; t < configrationProvider.getThreadCount(); t++) { + final ConcurrentContextThread task = new ConcurrentContextThread(0, t, configrationProvider); tasks.add(task); executorService.execute(task); } } else { // Spawn JVMs to run the tests - for (int j = 0; j < jvmCount; j++) { - final ConcurrentContextJVMThread task = - new ConcurrentContextJVMThread(testType, j, threadCount, threadLoops); + for (int j = 0; j < configrationProvider.getJvmCount(); j++) { + final ConcurrentContextJVMThread task = new ConcurrentContextJVMThread(j, configrationProvider); tasks.add(task); executorService.execute(task); } @@ -120,7 +108,7 @@ public class ConcurrentContext { LOGGER.info("Shutting down now ..."); executorService.shutdownNow(); - return concurrentContext.verifyAndClearContext(jvmCount, threadCount, threadLoops); + return verifyAndClearContext(); } @@ -140,85 +128,34 @@ public class ConcurrentContext { }); } - - private String getThreadFactoryName(final int jvmCount, final String testType) { - return jvmCount == 1 ? testType + ":TestConcurrentContextThread_0_" - : testType + ":TestConcurrentContextJVMThread_"; - } - /** * Setup and verify context. * * @throws ContextException the context exception */ private void setupAndVerifyContext() throws ContextException { - final AxArtifactKey distributorKey = new AxArtifactKey("ApexDistributor", "0.0.1"); - contextDistributor = new DistributorFactory().getDistributor(distributorKey); - - // @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 - - final AxContextModel albumsModel = TestContextAlbumFactory.createMultiAlbumsContextModel(); - contextDistributor.registerModel(albumsModel); - - lTypeAlbum = contextDistributor.createContextAlbum(new AxArtifactKey("LTypeContextAlbum", "0.0.1")); - assert (lTypeAlbum != null); - lTypeAlbum.setUserArtifactStack(usedArtifactStackArray); - - // CHECKSTYLE:OFF: checkstyle:magicNumber - lTypeAlbum.put("lTypeValue0", new TestContextLongItem(0xFFFFFFFFFFFFFFFFL)); - lTypeAlbum.put("lTypeValue1", new TestContextLongItem(0xFFFFFFFFFFFFFFFEL)); - lTypeAlbum.put("lTypeValue2", new TestContextLongItem(0xFFFFFFFFFFFFFFFDL)); - lTypeAlbum.put("lTypeValue3", new TestContextLongItem(0xFFFFFFFFFFFFFFFCL)); - lTypeAlbum.put("lTypeValue4", new TestContextLongItem(0xFFFFFFFFFFFFFFFBL)); - lTypeAlbum.put("lTypeValue5", new TestContextLongItem(0xFFFFFFFFFFFFFFFAL)); - lTypeAlbum.put("lTypeValue6", new TestContextLongItem(0xFFFFFFFFFFFFFFF9L)); - lTypeAlbum.put("lTypeValue7", new TestContextLongItem(0xFFFFFFFFFFFFFFF8L)); - lTypeAlbum.put("lTypeValue8", new TestContextLongItem(0xFFFFFFFFFFFFFFF7L)); - lTypeAlbum.put("lTypeValue9", new TestContextLongItem(0xFFFFFFFFFFFFFFF6L)); - lTypeAlbum.put("lTypeValueA", new TestContextLongItem(0xFFFFFFFFFFFFFFF5L)); - lTypeAlbum.put("lTypeValueB", new TestContextLongItem(0xFFFFFFFFFFFFFFF4L)); - lTypeAlbum.put("lTypeValueC", new TestContextLongItem(0xFFFFFFFFFFFFFFF3L)); - lTypeAlbum.put("lTypeValueD", new TestContextLongItem(0xFFFFFFFFFFFFFFF2L)); - lTypeAlbum.put("lTypeValueE", new TestContextLongItem(0xFFFFFFFFFFFFFFF1L)); - lTypeAlbum.put("lTypeValueF", new TestContextLongItem(0xFFFFFFFFFFFFFFF0L)); - LOGGER.debug(lTypeAlbum.toString()); - assert (lTypeAlbum.size() >= 16); - // CHECKSTYLE:ON: checkstyle:magicNumber - - // The initial value for concurrent testing - final TestContextLongItem item = new TestContextLongItem(0L); - lTypeAlbum.put("testValue", item); + contextDistributor = configrationProvider.getDistributor(); + lTypeAlbum = configrationProvider.getContextAlbum(contextDistributor); + final Map<String, Object> initValues = configrationProvider.getContextAlbumInitValues(); + for (final Entry<String, Object> entry : initValues.entrySet()) { + lTypeAlbum.put(entry.getKey(), entry.getValue()); + } } - /** - * Verify and clear context. - * - * @param jvmCount the jvm count - * @param threadCount the thread count - * @param threadLoops the thread loops - * @return the long - * @throws ContextException the context exception - */ - private long verifyAndClearContext(final int jvmCount, final int threadCount, final int threadLoops) - throws ContextException { + private Map<String, TestContextLongItem> verifyAndClearContext() throws ContextException { + final Map<String, TestContextLongItem> values = new HashMap<>(); try { - LOGGER.debug("threads finished, end value is {}", - ((TestContextLongItem) lTypeAlbum.get("testValue")).getLongValue()); + + for (Entry<String, Object> entry : lTypeAlbum.entrySet()) { + values.put(entry.getKey(), (TestContextLongItem) entry.getValue()); + } } catch (final Exception exception) { LOGGER.error("Error: ", exception); } - final long total = ((TestContextLongItem) lTypeAlbum.get("testValue")).getLongValue(); - contextDistributor.clear(); contextDistributor = null; - return total; + return values; } } diff --git a/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/locking/ConcurrentContextJVM.java b/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/locking/ConcurrentContextJVM.java index e2a6b0da0..fc1c830e7 100644 --- a/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/locking/ConcurrentContextJVM.java +++ b/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/locking/ConcurrentContextJVM.java @@ -30,20 +30,19 @@ import java.util.List; import java.util.Map.Entry; import java.util.TreeSet; import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import org.onap.policy.apex.context.ContextAlbum; import org.onap.policy.apex.context.Distributor; -import org.onap.policy.apex.context.impl.distribution.DistributorFactory; -import org.onap.policy.apex.context.test.factory.TestContextAlbumFactory; -import org.onap.policy.apex.context.test.utils.IntegrationThreadFactory; +import org.onap.policy.apex.context.test.utils.ConfigrationProvider; +import org.onap.policy.apex.context.test.utils.ConfigrationProviderImpl; +import org.onap.policy.apex.context.test.utils.Constants; import org.onap.policy.apex.model.basicmodel.concepts.ApexException; +import org.onap.policy.apex.model.basicmodel.concepts.ApexRuntimeException; import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; import org.onap.policy.apex.model.basicmodel.service.AbstractParameters; import org.onap.policy.apex.model.basicmodel.service.ParameterService; -import org.onap.policy.apex.model.contextmodel.concepts.AxContextModel; import org.slf4j.ext.XLogger; import org.slf4j.ext.XLoggerFactory; @@ -62,55 +61,29 @@ public final class ConcurrentContextJVM { private final int jvmNo; - private final int threadCount; - - private final int threadLoops; - private final ExecutorService executorService; - /** - * The Constructor. - * - * @param testType the test type - * @param jvmNo the jvm no - * @param threadCount the thread count - * @param threadLoops the thread loops - * @throws ApexException the apex exception - */ - private ConcurrentContextJVM(final String testType, final int jvmNo, final int threadCount, final int threadLoops) { + private final ConfigrationProvider configrationProvider; + + private ConcurrentContextJVM(final int jvmNo, final ConfigrationProvider configrationProvider) { this.jvmNo = jvmNo; - this.threadCount = threadCount; - this.threadLoops = threadLoops; - final String name = testType + ":ConcurrentContextThread_" + jvmNo; - this.executorService = Executors.newFixedThreadPool(threadCount, new IntegrationThreadFactory(name)); + this.configrationProvider = configrationProvider; + final String name = configrationProvider.getTestName() + ":ConcurrentContextThread_" + jvmNo; + this.executorService = configrationProvider.getExecutorService(name, configrationProvider.getThreadCount()); } public void execute() throws ApexException { LOGGER.debug("starting JVMs and threads . . ."); final AxArtifactKey distributorKey = new AxArtifactKey("ApexDistributor" + jvmNo, "0.0.1"); - final Distributor contextDistributor = new DistributorFactory().getDistributor(distributorKey); - - // @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 - - final AxContextModel albumsModel = TestContextAlbumFactory.createMultiAlbumsContextModel(); - contextDistributor.registerModel(albumsModel); + final Distributor distributor = configrationProvider.getDistributor(distributorKey); + final ContextAlbum contextAlbum = configrationProvider.getContextAlbum(distributor); + assert (contextAlbum != null); - final ContextAlbum lTypeAlbum = - contextDistributor.createContextAlbum(new AxArtifactKey("LTypeContextAlbum", "0.0.1")); - assert (lTypeAlbum != null); - lTypeAlbum.setUserArtifactStack(usedArtifactStackArray); + final List<Future<?>> tasks = new ArrayList<>(configrationProvider.getThreadCount()); - final List<Future<?>> tasks = new ArrayList<>(threadCount); - - for (int t = 0; t < threadCount; t++) { - tasks.add(executorService.submit(new ConcurrentContextThread(jvmNo, t, threadLoops))); + for (int t = 0; t < configrationProvider.getThreadCount(); t++) { + tasks.add(executorService.submit(new ConcurrentContextThread(jvmNo, t, configrationProvider))); } try { @@ -123,8 +96,8 @@ public final class ConcurrentContextJVM { Thread.currentThread().interrupt(); } - LOGGER.debug("threads finished, end value is {}", lTypeAlbum.get("testValue")); - contextDistributor.clear(); + LOGGER.debug("threads finished, end value is {}", contextAlbum.get(Constants.TEST_VALUE)); + distributor.clear(); LOGGER.info("Shutting down now ... "); executorService.shutdownNow(); } @@ -145,49 +118,25 @@ public final class ConcurrentContextJVM { // CHECKSTYLE:OFF: checkstyle:magicNumber // An even number of arguments greater than 3 - if (args.length < 7) { + if (args.length < 9) { LOGGER.error("invalid arguments: " + Arrays.toString(args)); LOGGER.error( - "usage: TestConcurrentContextJVM testType jvmNo threadCount threadLoops [parameterKey parameterJson].... "); - return; - } - - int jvmNo = -1; - int threadCount = -1; - int threadLoops = -1; - String hazelCastfileLocation = null; - - try { - jvmNo = Integer.parseInt(args[1]); - } catch (final Exception e) { - LOGGER.error("invalid argument jvmNo", e); - return; - } - - try { - threadCount = Integer.parseInt(args[2]); - } catch (final Exception e) { - LOGGER.error("invalid argument threadCount", e); + "usage: TestConcurrentContextJVM testType jvmNo threadCount threadLoops albumSize lockType [parameterKey parameterJson].... "); return; } - try { - threadLoops = Integer.parseInt(args[3]); - } catch (final Exception e) { - LOGGER.error("invalid argument threadLoops", e); - return; - } - try { - hazelCastfileLocation = args[4].trim(); - } catch (final Exception e) { - LOGGER.error("invalid argument hazelcast file location", e); - return; - } + final String testName = getStringValue("testType", args, 0); + final int jvmNo = getIntValue("jvmNo", args, 1); + final int threadCount = getIntValue("threadCount", args, 2); + final int threadLoops = getIntValue("threadLoops", args, 3); + final int albumSize = getIntValue("albumSize", args, 4); + final int lockType = getIntValue("lockType", args, 5); + final String hazelCastfileLocation = getStringValue("hazelcast file location", args, 6);; System.setProperty("hazelcast.config", hazelCastfileLocation); - for (int p = 5; p < args.length - 1; p += 2) { + for (int p = 7; p < args.length - 1; p += 2) { @SuppressWarnings("rawtypes") final Class parametersClass = Class.forName(args[p]); final AbstractParameters parameters = @@ -201,8 +150,9 @@ public final class ConcurrentContextJVM { } try { - final ConcurrentContextJVM concurrentContextJVM = - new ConcurrentContextJVM(args[0], jvmNo, threadCount, threadLoops); + final ConfigrationProvider configrationProvider = + new ConfigrationProviderImpl(testName, 1, threadCount, threadLoops, albumSize, lockType); + final ConcurrentContextJVM concurrentContextJVM = new ConcurrentContextJVM(jvmNo, configrationProvider); concurrentContextJVM.execute(); } catch (final Exception e) { @@ -211,9 +161,30 @@ public final class ConcurrentContextJVM { } // CHECKSTYLE:ON: checkstyle:magicNumber + } + private static String getStringValue(final String key, final String[] args, final int position) { + try { + return args[position]; + } catch (final Exception e) { + final String msg = "invalid argument " + key; + LOGGER.error(msg, e); + throw new ApexRuntimeException(msg, e); + } } + private static int getIntValue(final String key, final String[] args, final int position) { + final String value = getStringValue(key, args, position); + try { + return Integer.parseInt(value); + } catch (final Exception e) { + final String msg = "Expects number found " + value; + LOGGER.error(msg, e); + throw new ApexRuntimeException(msg, e); + } + } + + /** * This method setus up any static configuration required by the JVM. * diff --git a/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/locking/ConcurrentContextJVMThread.java b/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/locking/ConcurrentContextJVMThread.java index 995772f05..049754802 100644 --- a/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/locking/ConcurrentContextJVMThread.java +++ b/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/locking/ConcurrentContextJVMThread.java @@ -22,14 +22,13 @@ package org.onap.policy.apex.context.test.locking; import java.io.BufferedReader; import java.io.Closeable; -import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import java.util.Map.Entry; -import org.onap.policy.apex.model.basicmodel.concepts.ApexException; +import org.onap.policy.apex.context.test.utils.ConfigrationProvider; import org.onap.policy.apex.model.basicmodel.service.AbstractParameters; import org.onap.policy.apex.model.basicmodel.service.ParameterService; import org.slf4j.ext.XLogger; @@ -46,27 +45,13 @@ public class ConcurrentContextJVMThread implements Runnable, Closeable { // Logger for this class private static final XLogger LOGGER = XLoggerFactory.getXLogger(ConcurrentContextJVMThread.class); - private final String testType; private final int jvm; - private final int threadCount; - private final int target; + private final ConfigrationProvider configrationProvider; private Process process = null; - /** - * The Constructor. - * - * @param testType the test type - * @param jvm the jvm - * @param threadCount the thread count - * @param target the target - * @throws ApexException the apex exception - */ - public ConcurrentContextJVMThread(final String testType, final int jvm, final int threadCount, final int target) - throws ApexException { - this.testType = testType; + public ConcurrentContextJVMThread(final int jvm, final ConfigrationProvider configrationProvider) { this.jvm = jvm; - this.threadCount = threadCount; - this.target = target; + this.configrationProvider = configrationProvider; } /* @@ -84,11 +69,13 @@ public class ConcurrentContextJVMThread implements Runnable, Closeable { commandList.add("-cp"); commandList.add(System.getProperty("java.class.path")); commandList.add(ConcurrentContextJVM.class.getCanonicalName()); - commandList.add(testType); + commandList.add(configrationProvider.getTestName()); commandList.add(new Integer(jvm).toString()); - commandList.add(new Integer(threadCount).toString()); - commandList.add(new Integer(target).toString()); - commandList.add(System.getProperty("hazelcast.config")); + commandList.add(new Integer(configrationProvider.getThreadCount()).toString()); + commandList.add(new Integer(configrationProvider.getLoopSize()).toString()); + commandList.add(new Integer(configrationProvider.getAlbumSize()).toString()); + commandList.add(new Integer(configrationProvider.getLockType().getValue()).toString()); + commandList.add(System.getProperty("hazelcast.config", "")); for (final Entry<Class<?>, AbstractParameters> parameterServiceEntry : ParameterService.getAll()) { commandList.add(parameterServiceEntry.getKey().getCanonicalName()); @@ -101,7 +88,6 @@ public class ConcurrentContextJVMThread implements Runnable, Closeable { final ProcessBuilder processBuilder = new ProcessBuilder(commandList); processBuilder.redirectErrorStream(true); - try { process = processBuilder.start(); @@ -122,7 +108,7 @@ public class ConcurrentContextJVMThread implements Runnable, Closeable { LOGGER.warn("Thread was interrupted"); Thread.currentThread().interrupt(); } - } catch (final IOException ioException) { + } catch (final Exception ioException) { LOGGER.error("Error occured while writing JVM Output for command ", ioException); } } diff --git a/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/locking/ConcurrentContextThread.java b/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/locking/ConcurrentContextThread.java index 75a704557..726880f3b 100644 --- a/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/locking/ConcurrentContextThread.java +++ b/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/locking/ConcurrentContextThread.java @@ -25,13 +25,10 @@ import java.io.Closeable; 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.TestContextLongItem; -import org.onap.policy.apex.context.test.factory.TestContextAlbumFactory; -import org.onap.policy.apex.model.basicmodel.concepts.ApexException; +import org.onap.policy.apex.context.test.lock.modifier.AlbumModifier; +import org.onap.policy.apex.context.test.utils.ConfigrationProvider; 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; @@ -41,33 +38,25 @@ import org.slf4j.ext.XLoggerFactory; * @author Liam Fallon (liam.fallon@ericsson.com) */ public class ConcurrentContextThread implements Runnable, Closeable { - private static final String VALUE = "testValue"; // 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; + private final ConfigrationProvider configrationProvider; /** * The Constructor. * * @param jvm the jvm * @param instance the instance - * @param threadLoops the thread loops - * @throws ApexException the apex exception + * @param configrationProvider the configuration provider */ - public ConcurrentContextThread(final int jvm, final int instance, final int threadLoops) throws ApexException { + public ConcurrentContextThread(final int jvm, final int instance, final ConfigrationProvider configrationProvider) { this.jvm = jvm; this.instance = instance; - this.threadLoops = threadLoops; - - final AxArtifactKey distributorKey = new AxArtifactKey("ApexDistributor_" + jvm + "_" + instance, "0.0.1"); + this.configrationProvider = configrationProvider; new ContextParameters(); - distributor = new DistributorFactory().getDistributor(distributorKey); - final AxContextModel albumsModel = TestContextAlbumFactory.createMultiAlbumsContextModel(); - distributor.registerModel(albumsModel); } /* @@ -79,72 +68,30 @@ public class ConcurrentContextThread implements Runnable, Closeable { 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); + final AxArtifactKey distributorKey = new AxArtifactKey("ApexDistributor_" + jvm + "_" + instance, "0.0.1"); + final Distributor distributor = configrationProvider.getDistributor(distributorKey); try { - updateAlbum(lTypeAlbum); - } catch (final Exception exception) { - LOGGER.error("could not set the value in the test context album", exception); - LOGGER.error("failed TestConcurrentContextThread_" + jvm + "_" + instance); - return; - } + final long startTime = System.currentTimeMillis(); + final ContextAlbum contextAlbum = configrationProvider.getContextAlbum(distributor); + + final AlbumModifier albumModifier = configrationProvider.getAlbumModifier(); + albumModifier.modifyAlbum(contextAlbum, configrationProvider.getLoopSize(), + configrationProvider.getAlbumSize()); + LOGGER.info("Took {} ms to modify album", (System.currentTimeMillis() - startTime)); - try { - lTypeAlbum.lockForWriting(VALUE); - final TestContextLongItem item = (TestContextLongItem) lTypeAlbum.get(VALUE); - 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); + LOGGER.error("Unexpected error occured while processing", e); } finally { try { - lTypeAlbum.unlockForWriting(VALUE); distributor.shutdown(); } catch (final ContextException e) { - LOGGER.error("could not unlock test context album item", e); - LOGGER.error("failed TestConcurrentContextThread_" + jvm + "_" + instance); + LOGGER.error("Unable to shutdown distributor", e); } } - } + LOGGER.info("finished TestConcurrentContextThread_" + jvm + "_" + instance + " . . ."); - private void updateAlbum(final ContextAlbum lTypeAlbum) throws Exception { - for (int i = 0; i < threadLoops; i++) { - try { - lTypeAlbum.lockForWriting(VALUE); - TestContextLongItem item = (TestContextLongItem) lTypeAlbum.get(VALUE); - if (item != null) { - long value = item.getLongValue(); - item.setLongValue(++value); - } else { - item = new TestContextLongItem(0L); - } - lTypeAlbum.put(VALUE, item); - } finally { - lTypeAlbum.unlockForWriting(VALUE); - } - } } @Override diff --git a/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/utils/ConfigrationProvider.java b/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/utils/ConfigrationProvider.java new file mode 100644 index 000000000..d17f32269 --- /dev/null +++ b/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/utils/ConfigrationProvider.java @@ -0,0 +1,63 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 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.utils; + +import java.util.Map; +import java.util.concurrent.ExecutorService; + +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.test.lock.modifier.AlbumModifier; +import org.onap.policy.apex.context.test.lock.modifier.LockType; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; + +public interface ConfigrationProvider { + + String getTestName(); + + int getLoopSize(); + + int getThreadCount(); + + int getJvmCount(); + + int getAlbumSize(); + + ExecutorService getExecutorService(); + + ExecutorService getExecutorService(final String threadFactoryName, final int threadPoolSize); + + Distributor getDistributor(final AxArtifactKey key); + + Distributor getDistributor(); + + ContextAlbum getContextAlbum(final Distributor distributor); + + ContextAlbum getContextAlbum(final Distributor distributor, AxArtifactKey axContextAlbumKey, + AxArtifactKey[] artifactKeys) throws ContextException; + + Map<String, Object> getContextAlbumInitValues(); + + AlbumModifier getAlbumModifier(); + + LockType getLockType(); + +} diff --git a/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/utils/ConfigrationProviderImpl.java b/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/utils/ConfigrationProviderImpl.java new file mode 100644 index 000000000..50429099e --- /dev/null +++ b/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/utils/ConfigrationProviderImpl.java @@ -0,0 +1,166 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 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.utils; + +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +import org.onap.policy.apex.context.ContextAlbum; +import org.onap.policy.apex.context.ContextException; +import org.onap.policy.apex.context.ContextRuntimeException; +import org.onap.policy.apex.context.Distributor; +import org.onap.policy.apex.context.impl.distribution.DistributorFactory; +import org.onap.policy.apex.context.test.concepts.TestContextLongItem; +import org.onap.policy.apex.context.test.factory.TestContextAlbumFactory; +import org.onap.policy.apex.context.test.lock.modifier.AlbumModifier; +import org.onap.policy.apex.context.test.lock.modifier.LockType; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextModel; +import org.onap.policy.apex.model.utilities.Assertions; + +public class ConfigrationProviderImpl implements ConfigrationProvider { + + private final String testType; + private final int jvmCount; + private final int threadCount; + private final int loopSize; + private final int albumSize; + private final LockType lockType; + + public ConfigrationProviderImpl(final String testType, final int jvmCount, final int threadCount, + final int loopSize, final int albumSize, final int lockType) { + this.testType = testType; + this.jvmCount = jvmCount; + this.threadCount = threadCount; + this.loopSize = loopSize; + this.albumSize = albumSize; + this.lockType = LockType.getLockType(lockType); + } + + @Override + public String getTestName() { + return testType; + } + + @Override + public int getLoopSize() { + return loopSize; + } + + @Override + public int getThreadCount() { + return threadCount; + } + + @Override + public int getJvmCount() { + return jvmCount; + } + + @Override + public int getAlbumSize() { + return albumSize; + } + + @Override + public ExecutorService getExecutorService() { + final String name = getThreadFactoryName(jvmCount, testType); + final IntegrationThreadFactory threadFactory = new IntegrationThreadFactory(name); + final ExecutorService executorService = Executors.newFixedThreadPool(threadCount, threadFactory); + return executorService; + } + + @Override + public ExecutorService getExecutorService(final String threadFactoryName, final int threadPoolSize) { + final IntegrationThreadFactory threadFactory = new IntegrationThreadFactory(threadFactoryName); + final ExecutorService executorService = Executors.newFixedThreadPool(threadPoolSize, threadFactory); + return executorService; + } + + @Override + public Distributor getDistributor(final AxArtifactKey key) { + try { + return new DistributorFactory().getDistributor(key); + } catch (ContextException e) { + throw new ContextRuntimeException("Unable to create Distributor", e); + } + } + + @Override + public Distributor getDistributor() { + final AxArtifactKey distributorKey = new AxArtifactKey("ApexDistributor", "0.0.1"); + return getDistributor(distributorKey); + } + + @Override + public ContextAlbum getContextAlbum(final Distributor distributor) { + return getContextAlbum(distributor, Constants.L_TYPE_CONTEXT_ALBUM, Constants.getAxArtifactKeyArray()); + } + + @Override + public ContextAlbum getContextAlbum(final Distributor distributor, final AxArtifactKey axContextAlbumKey, + final AxArtifactKey[] artifactKeys) { + final AxContextModel axContextModel = TestContextAlbumFactory.createMultiAlbumsContextModel(); + try { + distributor.registerModel(axContextModel); + final ContextAlbum contextAlbum = distributor.createContextAlbum(axContextAlbumKey); + Assertions.argumentNotNull(contextAlbum, "ContextAlbum should not be null"); + contextAlbum.setUserArtifactStack(artifactKeys); + return contextAlbum; + } catch (ContextException e) { + throw new ContextRuntimeException("Unable to create ContextAlbum", e); + } + } + + @Override + public Map<String, Object> getContextAlbumInitValues() { + final Map<String, Object> values = new HashMap<>(); + for (int i = 0; i < albumSize; i++) { + values.put(Integer.toString(i), new TestContextLongItem(0l)); + } + return values; + } + + @Override + public AlbumModifier getAlbumModifier() { + return lockType.getAlbumModifier(); + } + + @Override + public LockType getLockType() { + return lockType; + } + + + private String getThreadFactoryName(final int jvmCount, final String testType) { + return jvmCount == 1 ? testType + ":TestConcurrentContextThread_0_" + : testType + ":TestConcurrentContextJVMThread_"; + } + + @Override + public String toString() { + return "ConfigrationProviderImpl [testType=" + testType + ", jvmCount=" + jvmCount + ", threadCount=" + + threadCount + ", loopSize=" + loopSize + ", albumSize=" + albumSize + ", lockType=" + lockType + "]"; + } + + +} diff --git a/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/utils/Constants.java b/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/utils/Constants.java index 79687139a..933c68b11 100644 --- a/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/utils/Constants.java +++ b/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/utils/Constants.java @@ -52,14 +52,17 @@ public class Constants { public static final String TEST_POLICY_CONTEXT_ITEM = TestPolicyContextItem.class.getName(); public static final TimeZone TIME_ZONE = TimeZone.getTimeZone("Europe/Dublin"); + public static final AxArtifactKey L_TYPE_CONTEXT_ALBUM = new AxArtifactKey("LTypeContextAlbum", VERSION); + public static final String TEST_VALUE = "testValue"; + private static final AxArtifactKey KEY = new AxArtifactKey("testC-top", VERSION); private static final AxArtifactKey KEY3 = new AxArtifactKey("testC-bot", VERSION); private static final AxArtifactKey KEY2 = new AxArtifactKey("testC-next", VERSION); - public static final AxArtifactKey[] USED_ARTIFACT_STACK_ARRAY = getAxArtifactKeyArray(); + private static final AxArtifactKey[] USED_ARTIFACT_STACK_ARRAY = new AxArtifactKey[] {KEY, KEY2, KEY3};; - private static AxArtifactKey[] getAxArtifactKeyArray() { - return new AxArtifactKey[] {KEY, KEY2, KEY3}; + public static final AxArtifactKey[] getAxArtifactKeyArray() { + return USED_ARTIFACT_STACK_ARRAY; } private Constants() {} diff --git a/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/utils/NetworkUtils.java b/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/utils/NetworkUtils.java new file mode 100644 index 000000000..01331f2a7 --- /dev/null +++ b/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/utils/NetworkUtils.java @@ -0,0 +1,58 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 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.utils; + +import java.net.InetAddress; +import java.net.NetworkInterface; +import java.net.SocketException; +import java.util.Collections; +import java.util.Enumeration; +import java.util.TreeSet; + +public class NetworkUtils { + + private NetworkUtils() {} + + /** + * The JGroups IP address must be set to a real (not loopback) IP address for Infinispan to + * work. In order to ensure that all the JVMs in a test pick up the same IP address, this + * function sets the address to be the first non-loopback IPv4 address on a host + * + * @return Set of IPv4 addresses + * @throws SocketException + */ + public static TreeSet<String> getIPv4NonLoopAddresses() throws SocketException { + final TreeSet<String> ipAddressSet = new TreeSet<String>(); + + final Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces(); + for (final NetworkInterface netint : Collections.list(nets)) { + final Enumeration<InetAddress> inetAddresses = netint.getInetAddresses(); + for (final InetAddress inetAddress : Collections.list(inetAddresses)) { + // Look for real IPv4 internet addresses + if (!inetAddress.isLoopbackAddress() && inetAddress.getAddress().length == 4) { + ipAddressSet.add(inetAddress.getHostAddress()); + } + } + } + return ipAddressSet; + } + + +} diff --git a/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/utils/ZooKeeperServerServiceProvider.java b/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/utils/ZooKeeperServerServiceProvider.java new file mode 100644 index 000000000..67bb8878b --- /dev/null +++ b/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/utils/ZooKeeperServerServiceProvider.java @@ -0,0 +1,63 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 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.utils; + +import java.io.File; +import java.io.IOException; +import java.net.InetSocketAddress; + +import org.apache.zookeeper.server.NIOServerCnxnFactory; +import org.apache.zookeeper.server.ZooKeeperServer; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +public class ZooKeeperServerServiceProvider { + private static final XLogger LOGGER = XLoggerFactory.getXLogger(ZooKeeperServerServiceProvider.class); + + private NIOServerCnxnFactory zookeeperFactory; + private File zookeeperDirectory; + private InetSocketAddress addr; + + public ZooKeeperServerServiceProvider(final File zookeeperDirectory, final InetSocketAddress addr) { + this.zookeeperDirectory = zookeeperDirectory; + this.addr = addr; + } + + public ZooKeeperServerServiceProvider(final File zookeeperDirectory, final String addr, final int port) { + this.zookeeperDirectory = zookeeperDirectory; + this.addr = new InetSocketAddress(addr, port); + } + + public void startZookeeperServer() throws IOException, InterruptedException { + LOGGER.info("Starting up ZooKeeperServer using address: {} and port: {}", addr.getAddress(), addr.getPort()); + final ZooKeeperServer server = new ZooKeeperServer(zookeeperDirectory, zookeeperDirectory, 5000); + zookeeperFactory = new NIOServerCnxnFactory(); + zookeeperFactory.configure(addr, 100); + zookeeperFactory.startup(server); + } + + public void stopZookeeperServer() { + LOGGER.info("Stopping ZooKeeperServer for address: {} and port: {}", addr.getAddress(), addr.getPort()); + if (zookeeperFactory != null) { + zookeeperFactory.shutdown(); + } + } + +} |