From d100854291559df1426ea1e64351872ae2d3867b Mon Sep 17 00:00:00 2001 From: liamfallon Date: Mon, 10 Sep 2018 17:05:36 +0100 Subject: Checkstyle changes for apex model Fix checkstyle warnings in the apex mode and knock on changes. Issue-ID: POLICY-1034 Change-Id: I10537e4288e9cad5ef18165ed2cdc1d3ab3139c1 Signed-off-by: liamfallon --- .../test/concepts/TestContextDateLocaleItem.java | 12 +- .../test/concepts/TestContextDateTzItem.java | 8 +- .../test/distribution/ContextInstantiation.java | 22 +- .../context/test/distribution/ContextUpdate.java | 6 +- .../SequentialContextInstantiation.java | 14 +- .../context/test/locking/ConcurrentContext.java | 2 +- .../context/test/locking/ConcurrentContextJVM.java | 226 --------------------- .../test/locking/ConcurrentContextJVMThread.java | 127 ------------ .../context/test/locking/ConcurrentContextJvm.java | 226 +++++++++++++++++++++ .../test/locking/ConcurrentContextJvmThread.java | 127 ++++++++++++ 10 files changed, 385 insertions(+), 385 deletions(-) delete mode 100644 context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/locking/ConcurrentContextJVM.java delete mode 100644 context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/locking/ConcurrentContextJVMThread.java create mode 100644 context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/locking/ConcurrentContextJvm.java create mode 100644 context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/locking/ConcurrentContextJvmThread.java (limited to 'context/context-test-utils/src/main/java') diff --git a/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/concepts/TestContextDateLocaleItem.java b/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/concepts/TestContextDateLocaleItem.java index 0c94bcb1d..efad26052 100644 --- a/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/concepts/TestContextDateLocaleItem.java +++ b/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/concepts/TestContextDateLocaleItem.java @@ -107,7 +107,7 @@ public class TestContextDateLocaleItem implements Serializable { * * @return the TZ value */ - public String getTZValue() { + public String getTzValue() { return timeZoneString; } @@ -116,7 +116,7 @@ public class TestContextDateLocaleItem implements Serializable { * * @param tzValue the TZ value */ - public void setTZValue(final String tzValue) { + public void setTzValue(final String tzValue) { this.timeZoneString = TimeZone.getTimeZone(tzValue).getDisplayName(); } @@ -125,7 +125,7 @@ public class TestContextDateLocaleItem implements Serializable { * * @return the dst */ - public boolean getDST() { + public boolean getDst() { return dst; } @@ -134,7 +134,7 @@ public class TestContextDateLocaleItem implements Serializable { * * @param newDst the dst */ - public void setDST(final boolean newDst) { + public void setDst(final boolean newDst) { this.dst = newDst; } @@ -143,7 +143,7 @@ public class TestContextDateLocaleItem implements Serializable { * * @return the UTC offset */ - public int getUTCOffset() { + public int getUtcOffset() { return utcOffset; } @@ -152,7 +152,7 @@ public class TestContextDateLocaleItem implements Serializable { * * @param newUtcOffset the UTC offset */ - public void setUTCOffset(final int newUtcOffset) { + public void setUtcOffset(final int newUtcOffset) { this.utcOffset = newUtcOffset; } diff --git a/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/concepts/TestContextDateTzItem.java b/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/concepts/TestContextDateTzItem.java index 7d3ed80d5..7e1e32924 100644 --- a/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/concepts/TestContextDateTzItem.java +++ b/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/concepts/TestContextDateTzItem.java @@ -91,7 +91,7 @@ public class TestContextDateTzItem implements Serializable { * * @return the TZ value */ - public String getTZValue() { + public String getTzValue() { return timeZoneString; } @@ -100,7 +100,7 @@ public class TestContextDateTzItem implements Serializable { * * @param tzValue the TZ value */ - public void setTZValue(final String tzValue) { + public void setTzValue(final String tzValue) { this.timeZoneString = TimeZone.getTimeZone(tzValue).getDisplayName(); } @@ -109,7 +109,7 @@ public class TestContextDateTzItem implements Serializable { * * @return the dst */ - public boolean getDST() { + public boolean getDst() { return dst; } @@ -118,7 +118,7 @@ public class TestContextDateTzItem implements Serializable { * * @param newDst the dst */ - public void setDST(final boolean newDst) { + public void setDst(final boolean newDst) { this.dst = newDst; } 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 0fd013da7..084220c2f 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 @@ -190,8 +190,8 @@ public class ContextInstantiation { final AxContextModel externalContextModel = TestContextAlbumFactory.createExternalContextModel(); final TestContextDateTzItem tci9A = new TestContextDateTzItem(tci9); - final TestContextDateLocaleItem tciAA = new TestContextDateLocaleItem(tciA); - final TestExternalContextItem externalContext = getTestExternalContextItem(testDate, tci9A, tciAA); + final TestContextDateLocaleItem tciA_A = new TestContextDateLocaleItem(tciA); + final TestExternalContextItem externalContext = getTestExternalContextItem(testDate, tci9A, tciA_A); final Map valueMap2 = new HashMap<>(); valueMap2.put(EXTERNAL_CONTEXT, externalContext); @@ -226,7 +226,7 @@ public class ContextInstantiation { assertEquals(tci9A.getDateValue().getTime(), externalContextItem.getTestExternalContextItem009().getDateValue().getTime()); - assertEquals(tciAA.getDateValue().getTime(), + assertEquals(tciA_A.getDateValue().getTime(), externalContextItem.getTestExternalContextItem00A().getDateValue().getTime()); assertEquals(TEST_TREE_SET, externalContextItem.getTestExternalContextItem00B().getSetValue()); assertEquals(TEST_HASH_MAP, externalContextItem.getTestExternalContextItem00C().getMapValue()); @@ -275,7 +275,7 @@ public class ContextInstantiation { assertEquals("album \"ExternalContextAlbum:0.0.1\" null values are illegal on key " + "\"TestExternalContextItem00A\" for put()", e.getMessage()); } - assertEquals(tciAA, externalContextItem.getTestExternalContextItem00A()); + assertEquals(tciA_A, externalContextItem.getTestExternalContextItem00A()); // Should return the hash set assertEquals(TEST_TREE_SET, externalContextItem.getTestExternalContextItem00B().getSetValue()); @@ -471,9 +471,9 @@ public class ContextInstantiation { private TestContextDateLocaleItem getTestContextDateLocaleItem(final Date testDate) { final TestContextDateLocaleItem tciA = new TestContextDateLocaleItem(); tciA.setDateValue(new TestContextDateItem(testDate)); - tciA.setTZValue(TIME_ZONE.getDisplayName()); - tciA.setDST(true); - tciA.setUTCOffset(-600); + tciA.setTzValue(TIME_ZONE.getDisplayName()); + tciA.setDst(true); + tciA.setUtcOffset(-600); tciA.setLocale(Locale.ENGLISH); return tciA; } @@ -481,13 +481,13 @@ public class ContextInstantiation { private TestContextDateTzItem getTestContextDateTzItem(final Date testDate) { final TestContextDateTzItem tci9 = new TestContextDateTzItem(); tci9.setDateValue(new TestContextDateItem(testDate)); - tci9.setTZValue(TIME_ZONE.getDisplayName()); - tci9.setDST(true); + tci9.setTzValue(TIME_ZONE.getDisplayName()); + tci9.setDst(true); return tci9; } private TestExternalContextItem getTestExternalContextItem(final Date testDate, final TestContextDateTzItem tci9A, - final TestContextDateLocaleItem tciAA) { + final TestContextDateLocaleItem tciAa) { final TestExternalContextItem externalContext = new TestExternalContextItem(); final TestContextBooleanItem testExternalContextItem000 = new TestContextBooleanItem(false); @@ -513,7 +513,7 @@ public class ContextInstantiation { externalContext.setTestExternalContextItem007(testExternalContextItem007); externalContext.setTestExternalContextItem008(testExternalContextItem008); externalContext.setTestExternalContextItem009(tci9A); - externalContext.setTestExternalContextItem00A(tciAA); + externalContext.setTestExternalContextItem00A(tciAa); externalContext.setTestExternalContextItem00B(testExternalContextItem00B); externalContext.setTestExternalContextItem00C(testExternalContextItem00C); return externalContext; 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 8f066c8e3..95d14324c 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 @@ -149,9 +149,9 @@ public class ContextUpdate { private TestContextDateLocaleItem getTestContextDateLocaleItem() { final TestContextDateLocaleItem tciA = new TestContextDateLocaleItem(); tciA.setDateValue(new TestContextDateItem(new Date())); - tciA.setTZValue(TIME_ZONE.getDisplayName()); - tciA.setDST(true); - tciA.setUTCOffset(-600); + tciA.setTzValue(TIME_ZONE.getDisplayName()); + tciA.setDst(true); + tciA.setUtcOffset(-600); tciA.setLocale(Locale.ENGLISH); return tciA; } 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 4041c079d..244bd7d24 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 @@ -253,9 +253,9 @@ public class SequentialContextInstantiation { // Should return null assertNull(dateAlbum.get("ItemNull")); // Put should return the previous contextItem - tciA00.setDST(false); + tciA00.setDst(false); final TestContextDateLocaleItem tciA03_clone = new TestContextDateLocaleItem(tciA03); - tciA03_clone.setDST(true); + tciA03_clone.setDst(true); TestContextDateLocaleItem retItem = (TestContextDateLocaleItem) dateAlbum.put("Item03", tciA03_clone); assertEquals(tciA03, retItem); retItem = (TestContextDateLocaleItem) dateAlbum.put("Item03", tciA03); @@ -332,17 +332,17 @@ public class SequentialContextInstantiation { private TestContextDateTzItem getTestContextDateTzItem(final Date testDate) { final TestContextDateTzItem tci9 = new TestContextDateTzItem(); tci9.setDateValue(new TestContextDateItem(testDate)); - tci9.setTZValue(TimeZone.getTimeZone("Europe/Dublin").getDisplayName()); - tci9.setDST(true); + tci9.setTzValue(TimeZone.getTimeZone("Europe/Dublin").getDisplayName()); + tci9.setDst(true); return tci9; } private TestContextDateLocaleItem getTestContextDateLocaleItem() { final TestContextDateLocaleItem tciA00 = new TestContextDateLocaleItem(); tciA00.setDateValue(new TestContextDateItem(new Date())); - tciA00.setTZValue(TIME_ZONE.getDisplayName()); - tciA00.setDST(true); - tciA00.setUTCOffset(-600); + tciA00.setTzValue(TIME_ZONE.getDisplayName()); + tciA00.setDst(true); + tciA00.setUtcOffset(-600); tciA00.setLocale(Locale.ENGLISH); return tciA00; } 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 4f6dd9b1f..88cdccc88 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 @@ -96,7 +96,7 @@ public class ConcurrentContext { } else { // Spawn JVMs to run the tests for (int j = 0; j < configrationProvider.getJvmCount(); j++) { - final ConcurrentContextJVMThread task = new ConcurrentContextJVMThread(j, configrationProvider); + final ConcurrentContextJvmThread task = new ConcurrentContextJvmThread(j, configrationProvider); tasks.add(task); executorService.execute(task); } 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 deleted file mode 100644 index 0f6900dbb..000000000 --- a/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/locking/ConcurrentContextJVM.java +++ /dev/null @@ -1,226 +0,0 @@ -/*- - * ============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 com.google.gson.Gson; - -import java.net.InetAddress; -import java.net.NetworkInterface; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.Enumeration; -import java.util.List; -import java.util.Map.Entry; -import java.util.TreeSet; -import java.util.concurrent.ExecutorService; -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.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.common.parameters.ParameterGroup; -import org.onap.policy.common.parameters.ParameterService; -import org.slf4j.ext.XLogger; -import org.slf4j.ext.XLoggerFactory; - - -/** - * The Class ConcurrentContextJVM tests concurrent use of context in a single JVM. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - */ -public final class ConcurrentContextJVM { - // Logger for this class - private static final XLogger LOGGER = XLoggerFactory.getXLogger(ConcurrentContextJVM.class); - - private static final int IPV4_ADDRESS_LENGTH = 4; - - private final int jvmNo; - - private final ExecutorService executorService; - - private final ConfigrationProvider configrationProvider; - - private ConcurrentContextJVM(final int jvmNo, final ConfigrationProvider configrationProvider) { - this.jvmNo = jvmNo; - this.configrationProvider = configrationProvider; - final String name = configrationProvider.getTestName() + ":ConcurrentContextThread_" + jvmNo; - this.executorService = configrationProvider.getExecutorService(name, configrationProvider.getThreadCount()); - } - - /** - * This method executes the test of concurrent use of context in a single JVM. - * @throws ApexException the Apex exception occurs while running the test - */ - public void execute() throws ApexException { - LOGGER.debug("starting JVMs and threads . . ."); - - final AxArtifactKey distributorKey = new AxArtifactKey("ApexDistributor" + jvmNo, "0.0.1"); - final Distributor distributor = configrationProvider.getDistributor(distributorKey); - final ContextAlbum contextAlbum = configrationProvider.getContextAlbum(distributor); - assert (contextAlbum != null); - - final List> tasks = new ArrayList<>(configrationProvider.getThreadCount()); - - for (int t = 0; t < configrationProvider.getThreadCount(); t++) { - tasks.add(executorService.submit(new ConcurrentContextThread(jvmNo, t, configrationProvider))); - } - - try { - executorService.shutdown(); - // wait for threads to finish, if not Timeout - executorService.awaitTermination(10, TimeUnit.MINUTES); - } catch (final InterruptedException interruptedException) { - LOGGER.error("Exception while waiting for threads to finish", interruptedException); - // restore the interrupt status - Thread.currentThread().interrupt(); - } - - LOGGER.debug("threads finished, end value is {}", contextAlbum.get(Constants.TEST_VALUE)); - distributor.clear(); - LOGGER.info("Shutting down now ... "); - executorService.shutdownNow(); - } - - - - /** - * The main method. - * - * @param args the args - * @throws Exception Any exception thrown by the test code - */ - @SuppressWarnings("unchecked") - public static void main(final String[] args) throws Exception { - configure(); - - System.out.println("JVM Arguments: " + Arrays.toString(args)); - // CHECKSTYLE:OFF: checkstyle:magicNumber - - // An even number of arguments greater than 3 - if (args.length < 9) { - LOGGER.error("invalid arguments: " + Arrays.toString(args)); - LOGGER.error("usage: TestConcurrentContextJVM testType jvmNo threadCount threadLoops albumSize " - + "lockType [parameterKey parameterJson].... "); - 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 = 7; p < args.length - 1; p += 2) { - @SuppressWarnings("rawtypes") - final Class parametersClass = Class.forName(args[p]); - final ParameterGroup parameters = - (ParameterGroup) new Gson().fromJson(args[p + 1], parametersClass); - ParameterService.register(parameters); - } - - for (final Entry parameterEntry : ParameterService.getAll()) { - LOGGER.info("Parameter class " + parameterEntry.getKey() + "=" - + parameterEntry.getValue().toString()); - } - - try { - final ConfigrationProvider configrationProvider = - new ConfigrationProviderImpl(testName, 1, threadCount, threadLoops, albumSize, lockType); - final ConcurrentContextJVM concurrentContextJVM = new ConcurrentContextJVM(jvmNo, configrationProvider); - concurrentContextJVM.execute(); - - } catch (final Exception e) { - LOGGER.error("error running test in JVM", e); - return; - } - // 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 sets up any static configuration required by the JVM. - * - * @throws Exception on configuration errors - */ - public static void configure() throws Exception { - System.setProperty("java.net.preferIPv4Stack", "true"); - // 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 te address to be the - // first non-loopback - // IPv4 address - // on a host - final TreeSet ipAddressSet = new TreeSet(); - - final Enumeration nets = NetworkInterface.getNetworkInterfaces(); - for (final NetworkInterface netint : Collections.list(nets)) { - final Enumeration inetAddresses = netint.getInetAddresses(); - for (final InetAddress inetAddress : Collections.list(inetAddresses)) { - // Look for real IPv4 Internet addresses - if (!inetAddress.isLoopbackAddress() && inetAddress.getAddress().length == IPV4_ADDRESS_LENGTH) { - ipAddressSet.add(inetAddress.getHostAddress()); - } - } - } - - if (ipAddressSet.size() == 0) { - throw new Exception("cound not find real IP address for test"); - } - System.out.println("Setting jgroups.tcp.address to: " + ipAddressSet.first()); - System.setProperty("jgroups.tcp.address", ipAddressSet.first()); - } -} 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 deleted file mode 100644 index 0c2aa7a90..000000000 --- a/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/locking/ConcurrentContextJVMThread.java +++ /dev/null @@ -1,127 +0,0 @@ -/*- - * ============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 com.google.gson.Gson; - -import java.io.BufferedReader; -import java.io.Closeable; -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.context.test.utils.ConfigrationProvider; -import org.onap.policy.common.parameters.ParameterGroup; -import org.onap.policy.common.parameters.ParameterService; -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 ConcurrentContextJVMThread implements Runnable, Closeable { - // Logger for this class - private static final XLogger LOGGER = XLoggerFactory.getXLogger(ConcurrentContextJVMThread.class); - - private final int jvm; - private final ConfigrationProvider configrationProvider; - private Process process = null; - - public ConcurrentContextJVMThread(final int jvm, final ConfigrationProvider configrationProvider) { - this.jvm = jvm; - this.configrationProvider = configrationProvider; - } - - /* - * (non-Javadoc) - * - * @see java.lang.Runnable#run() - */ - @Override - public void run() { - final List commandList = new ArrayList<>(); - commandList.add(System.getProperty("java.home") + System.getProperty("file.separator") + "bin" - + System.getProperty("file.separator") + "java"); - commandList.add("-Xms512m"); - commandList.add("-Xmx512m"); - commandList.add("-cp"); - commandList.add(System.getProperty("java.class.path")); - commandList.add(ConcurrentContextJVM.class.getCanonicalName()); - commandList.add(configrationProvider.getTestName()); - commandList.add(new Integer(jvm).toString()); - 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 parameterServiceEntry : ParameterService.getAll()) { - commandList.add(parameterServiceEntry.getValue().getClass().getCanonicalName()); - commandList.add(new Gson().toJson(parameterServiceEntry.getValue())); - } - - LOGGER.info("starting JVM " + jvm); - - // Run the JVM - final ProcessBuilder processBuilder = new ProcessBuilder(commandList); - processBuilder.redirectErrorStream(true); - - try { - process = processBuilder.start(); - - final InputStream is = process.getInputStream(); - final InputStreamReader isr = new InputStreamReader(is); - final BufferedReader br = new BufferedReader(isr); - String line; - LOGGER.info("JVM Output for command " + commandList + "\n"); - while ((line = br.readLine()) != null) { - LOGGER.info(line); - } - - // Wait to get exit value - try { - final int exitValue = process.waitFor(); - LOGGER.info("\n\nJVM " + jvm + " finished, exit value is " + exitValue); - } catch (final InterruptedException e) { - LOGGER.warn("Thread was interrupted"); - Thread.currentThread().interrupt(); - } - } catch (final Exception ioException) { - LOGGER.error("Error occured while writing JVM Output for command ", ioException); - } - } - - - @Override - public void close() { - LOGGER.info("Shutting down {} thread ...", Thread.currentThread().getName()); - if (process != null) { - LOGGER.info("Destroying process ..."); - process.destroy(); - } - } -} 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 new file mode 100644 index 000000000..7d7548569 --- /dev/null +++ b/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/locking/ConcurrentContextJvm.java @@ -0,0 +1,226 @@ +/*- + * ============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 com.google.gson.Gson; + +import java.net.InetAddress; +import java.net.NetworkInterface; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Enumeration; +import java.util.List; +import java.util.Map.Entry; +import java.util.TreeSet; +import java.util.concurrent.ExecutorService; +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.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.common.parameters.ParameterGroup; +import org.onap.policy.common.parameters.ParameterService; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + + +/** + * The Class ConcurrentContextJVM tests concurrent use of context in a single JVM. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public final class ConcurrentContextJvm { + // Logger for this class + private static final XLogger LOGGER = XLoggerFactory.getXLogger(ConcurrentContextJvm.class); + + private static final int IPV4_ADDRESS_LENGTH = 4; + + private final int jvmNo; + + private final ExecutorService executorService; + + private final ConfigrationProvider configrationProvider; + + private ConcurrentContextJvm(final int jvmNo, final ConfigrationProvider configrationProvider) { + this.jvmNo = jvmNo; + this.configrationProvider = configrationProvider; + final String name = configrationProvider.getTestName() + ":ConcurrentContextThread_" + jvmNo; + this.executorService = configrationProvider.getExecutorService(name, configrationProvider.getThreadCount()); + } + + /** + * This method executes the test of concurrent use of context in a single JVM. + * @throws ApexException the Apex exception occurs while running the test + */ + public void execute() throws ApexException { + LOGGER.debug("starting JVMs and threads . . ."); + + final AxArtifactKey distributorKey = new AxArtifactKey("ApexDistributor" + jvmNo, "0.0.1"); + final Distributor distributor = configrationProvider.getDistributor(distributorKey); + final ContextAlbum contextAlbum = configrationProvider.getContextAlbum(distributor); + assert (contextAlbum != null); + + final List> tasks = new ArrayList<>(configrationProvider.getThreadCount()); + + for (int t = 0; t < configrationProvider.getThreadCount(); t++) { + tasks.add(executorService.submit(new ConcurrentContextThread(jvmNo, t, configrationProvider))); + } + + try { + executorService.shutdown(); + // wait for threads to finish, if not Timeout + executorService.awaitTermination(10, TimeUnit.MINUTES); + } catch (final InterruptedException interruptedException) { + LOGGER.error("Exception while waiting for threads to finish", interruptedException); + // restore the interrupt status + Thread.currentThread().interrupt(); + } + + LOGGER.debug("threads finished, end value is {}", contextAlbum.get(Constants.TEST_VALUE)); + distributor.clear(); + LOGGER.info("Shutting down now ... "); + executorService.shutdownNow(); + } + + + + /** + * The main method. + * + * @param args the args + * @throws Exception Any exception thrown by the test code + */ + @SuppressWarnings("unchecked") + public static void main(final String[] args) throws Exception { + configure(); + + System.out.println("JVM Arguments: " + Arrays.toString(args)); + // CHECKSTYLE:OFF: checkstyle:magicNumber + + // An even number of arguments greater than 3 + if (args.length < 9) { + LOGGER.error("invalid arguments: " + Arrays.toString(args)); + LOGGER.error("usage: TestConcurrentContextJVM testType jvmNo threadCount threadLoops albumSize " + + "lockType [parameterKey parameterJson].... "); + 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 = 7; p < args.length - 1; p += 2) { + @SuppressWarnings("rawtypes") + final Class parametersClass = Class.forName(args[p]); + final ParameterGroup parameters = + (ParameterGroup) new Gson().fromJson(args[p + 1], parametersClass); + ParameterService.register(parameters); + } + + for (final Entry parameterEntry : ParameterService.getAll()) { + LOGGER.info("Parameter class " + parameterEntry.getKey() + "=" + + parameterEntry.getValue().toString()); + } + + try { + final ConfigrationProvider configrationProvider = + new ConfigrationProviderImpl(testName, 1, threadCount, threadLoops, albumSize, lockType); + final ConcurrentContextJvm concurrentContextJvm = new ConcurrentContextJvm(jvmNo, configrationProvider); + concurrentContextJvm.execute(); + + } catch (final Exception e) { + LOGGER.error("error running test in JVM", e); + return; + } + // 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 sets up any static configuration required by the JVM. + * + * @throws Exception on configuration errors + */ + public static void configure() throws Exception { + System.setProperty("java.net.preferIPv4Stack", "true"); + // 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 te address to be the + // first non-loopback + // IPv4 address + // on a host + final TreeSet ipAddressSet = new TreeSet(); + + final Enumeration nets = NetworkInterface.getNetworkInterfaces(); + for (final NetworkInterface netint : Collections.list(nets)) { + final Enumeration inetAddresses = netint.getInetAddresses(); + for (final InetAddress inetAddress : Collections.list(inetAddresses)) { + // Look for real IPv4 Internet addresses + if (!inetAddress.isLoopbackAddress() && inetAddress.getAddress().length == IPV4_ADDRESS_LENGTH) { + ipAddressSet.add(inetAddress.getHostAddress()); + } + } + } + + if (ipAddressSet.size() == 0) { + throw new Exception("cound not find real IP address for test"); + } + System.out.println("Setting jgroups.tcp.address to: " + ipAddressSet.first()); + System.setProperty("jgroups.tcp.address", ipAddressSet.first()); + } +} 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 new file mode 100644 index 000000000..339c95d26 --- /dev/null +++ b/context/context-test-utils/src/main/java/org/onap/policy/apex/context/test/locking/ConcurrentContextJvmThread.java @@ -0,0 +1,127 @@ +/*- + * ============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 com.google.gson.Gson; + +import java.io.BufferedReader; +import java.io.Closeable; +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.context.test.utils.ConfigrationProvider; +import org.onap.policy.common.parameters.ParameterGroup; +import org.onap.policy.common.parameters.ParameterService; +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 ConcurrentContextJvmThread implements Runnable, Closeable { + // Logger for this class + private static final XLogger LOGGER = XLoggerFactory.getXLogger(ConcurrentContextJvmThread.class); + + private final int jvm; + private final ConfigrationProvider configrationProvider; + private Process process = null; + + public ConcurrentContextJvmThread(final int jvm, final ConfigrationProvider configrationProvider) { + this.jvm = jvm; + this.configrationProvider = configrationProvider; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Runnable#run() + */ + @Override + public void run() { + final List commandList = new ArrayList<>(); + commandList.add(System.getProperty("java.home") + System.getProperty("file.separator") + "bin" + + System.getProperty("file.separator") + "java"); + commandList.add("-Xms512m"); + commandList.add("-Xmx512m"); + commandList.add("-cp"); + commandList.add(System.getProperty("java.class.path")); + commandList.add(ConcurrentContextJvm.class.getCanonicalName()); + commandList.add(configrationProvider.getTestName()); + commandList.add(new Integer(jvm).toString()); + 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 parameterServiceEntry : ParameterService.getAll()) { + commandList.add(parameterServiceEntry.getValue().getClass().getCanonicalName()); + commandList.add(new Gson().toJson(parameterServiceEntry.getValue())); + } + + LOGGER.info("starting JVM " + jvm); + + // Run the JVM + final ProcessBuilder processBuilder = new ProcessBuilder(commandList); + processBuilder.redirectErrorStream(true); + + try { + process = processBuilder.start(); + + final InputStream is = process.getInputStream(); + final InputStreamReader isr = new InputStreamReader(is); + final BufferedReader br = new BufferedReader(isr); + String line; + LOGGER.info("JVM Output for command " + commandList + "\n"); + while ((line = br.readLine()) != null) { + LOGGER.info(line); + } + + // Wait to get exit value + try { + final int exitValue = process.waitFor(); + LOGGER.info("\n\nJVM " + jvm + " finished, exit value is " + exitValue); + } catch (final InterruptedException e) { + LOGGER.warn("Thread was interrupted"); + Thread.currentThread().interrupt(); + } + } catch (final Exception ioException) { + LOGGER.error("Error occured while writing JVM Output for command ", ioException); + } + } + + + @Override + public void close() { + LOGGER.info("Shutting down {} thread ...", Thread.currentThread().getName()); + if (process != null) { + LOGGER.info("Destroying process ..."); + process.destroy(); + } + } +} -- cgit 1.2.3-korg