Improve robustness of unit testing 06/109006/4
authorhuaxing <huaxing.jin@est.tech>
Wed, 10 Jun 2020 06:58:34 +0000 (14:58 +0800)
committerhuaxing <huaxing.jin@est.tech>
Fri, 12 Jun 2020 01:59:42 +0000 (09:59 +0800)
Issue-ID: POLICY-2630
Signed-off-by: huaxing <huaxing.jin@est.tech>
Change-Id: I6475f9272c1a770836af537c13b23e486b66ac3e

core/core-infrastructure/src/test/java/org/onap/policy/apex/core/infrastructure/threading/ThreadingTest.java
core/core-infrastructure/src/test/java/org/onap/policy/apex/core/infrastructure/threading/ThreadingTestThread.java [deleted file]
examples/examples-aadm/src/test/java/org/onap/policy/apex/examples/aadm/TestApexActionListener.java
services/services-engine/src/main/java/org/onap/policy/apex/service/engine/engdep/EngDepMessageListener.java
services/services-engine/src/test/java/org/onap/policy/apex/service/engine/engdep/EngDepMessageListenerTest.java
testsuites/performance/performance-benchmark-test/src/main/java/org/onap/policy/apex/testsuites/performance/benchmark/eventgenerator/EventGenerator.java
testsuites/performance/performance-benchmark-test/src/test/java/org/onap/policy/apex/testsuites/performance/benchmark/eventgenerator/EventGeneratorTest.java

index 23f458a..ba6d4c6 100644 (file)
@@ -71,30 +71,19 @@ public class ThreadingTest {
      * @param threadFactory the thread factory
      */
     private void testThreadFactory(final ApplicationThreadFactory threadFactory) {
-        final List<ThreadingTestThread> threadList = new ArrayList<>();
+        final List<Thread> threadList = new ArrayList<>();
 
         for (int i = 0; i < 5; i++) {
-            final ThreadingTestThread runnable = new ThreadingTestThread();
-            threadList.add(runnable);
-
-            final Thread thread = threadFactory.newThread(runnable);
+            final Thread thread = threadFactory.newThread(() -> {
+            });
+            threadList.add(thread);
             thread.start();
-
-            if (i == 4) {
-                await().atLeast(100, TimeUnit.MILLISECONDS).until(() -> thread.isAlive());
-            }
-
-        }
-
-        for (int i = 0; i < 5; i++) {
-            threadList.get(i).interrupt();
         }
 
         for (int i = 0; i < 5; i++) {
-            ThreadingTestThread thread = threadList.get(i);
+            Thread thread = threadList.get(i);
             assertTrue(thread.getName().startsWith("Apex-" + LOCAL_NAME));
             assertTrue(thread.getName().contains(":" + i));
-            assertTrue("Thread (" + i + ") count should be greater than 0 ", thread.getCounter() > 0);
         }
     }
 }
diff --git a/core/core-infrastructure/src/test/java/org/onap/policy/apex/core/infrastructure/threading/ThreadingTestThread.java b/core/core-infrastructure/src/test/java/org/onap/policy/apex/core/infrastructure/threading/ThreadingTestThread.java
deleted file mode 100644 (file)
index ee22121..0000000
+++ /dev/null
@@ -1,92 +0,0 @@
-/*-
- * ============LICENSE_START=======================================================
- *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
- *  Modifications Copyright (C) 2020 Nordix Foundation.
- * ================================================================================
- * 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.core.infrastructure.threading;
-
-import org.slf4j.ext.XLogger;
-import org.slf4j.ext.XLoggerFactory;
-
-/**
- * The Class ThreadingTestThread.
- *
- * @author Liam Fallon (liam.fallon@ericsson.com)
- */
-public class ThreadingTestThread implements Runnable {
-
-    // Logger for this class
-    private static final XLogger logger = XLoggerFactory.getXLogger(ThreadingTestThread.class);
-
-    private boolean interrupted = false;
-
-    private long counter = -1;
-
-    private String threadName;
-
-    /**
-     * {@inheritDoc}.
-     */
-    @Override
-    public void run() {
-        this.threadName = Thread.currentThread().getName();
-        if (logger.isDebugEnabled()) {
-            logger.debug("starting threading test thread \"" + threadName + "\" . . .");
-        }
-
-        while (!interrupted) {
-            counter++;
-            if (logger.isDebugEnabled()) {
-                logger.debug("in threading test thread \"" + threadName + "\", counter=" + counter + " . . .");
-            }
-            if (!ThreadUtilities.sleep(50)) {
-                interrupted = true;
-            }
-        }
-
-        if (logger.isDebugEnabled()) {
-            logger.debug("stopped threading test thread \"" + threadName + "\"");
-        }
-    }
-
-    /**
-     * Gets the name.
-     *
-     * @return the name
-     */
-    public String getName() {
-        return threadName;
-    }
-
-    /**
-     * Interrupt.
-     */
-    public void interrupt() {
-        interrupted = true;
-    }
-
-    /**
-     * Gets the counter.
-     *
-     * @return the counter
-     */
-    public Long getCounter() {
-        return counter;
-    }
-}
index 11a7556..00cf98a 100644 (file)
 
 package org.onap.policy.apex.examples.aadm;
 
-import static org.awaitility.Awaitility.await;
-
 import java.util.ArrayList;
 import java.util.List;
-import java.util.concurrent.TimeUnit;
-
 import org.onap.policy.apex.core.engine.engine.EnEventListener;
 import org.onap.policy.apex.core.engine.event.EnEvent;
 
@@ -58,7 +54,6 @@ public class TestApexActionListener implements EnEventListener {
      * @return the result
      */
     public EnEvent getResult() {
-        await().atLeast(100, TimeUnit.MILLISECONDS).until(() -> !resultEvents.isEmpty());
         return resultEvents.remove(0);
     }
 
@@ -67,8 +62,6 @@ public class TestApexActionListener implements EnEventListener {
      */
     @Override
     public void onEnEvent(final EnEvent actionEvent) {
-        await().atLeast(100, TimeUnit.MILLISECONDS).until(() -> actionEvent != null);
-        System.out.println("Action event from engine:" + actionEvent.getName());
         resultEvents.add(actionEvent);
     }
 
index 2e8218a..57c547b 100644 (file)
@@ -77,7 +77,7 @@ public class EngDepMessageListener implements MessageListener<Message>, Runnable
 
     // The message listener thread and stopping flag
     private Thread messageListenerThread;
-    private boolean stopOrderedFlag = false;
+    private volatile boolean stopOrderedFlag = false;
 
     // The message queue is used to hold messages prior to forwarding to Apex
     private final BlockingQueue<MessageBlock<Message>> messageQueue = new LinkedBlockingDeque<>();
@@ -149,7 +149,7 @@ public class EngDepMessageListener implements MessageListener<Message>, Runnable
     @Override
     public void run() {
         // Take messages off the queue and forward them to the Apex engine
-        while (messageListenerThread.isAlive() && !stopOrderedFlag) {
+        while (!stopOrderedFlag) {
             pollAndHandleMessage();
         }
     }
index f755a66..9f5329e 100644 (file)
@@ -5,36 +5,37 @@
  * 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.service.engine.engdep;
 
+import static org.awaitility.Awaitility.await;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.fail;
 
 import java.net.InetSocketAddress;
 import java.util.ArrayList;
 import java.util.List;
-
+import java.util.concurrent.BlockingQueue;
 import org.java_websocket.WebSocket;
 import org.junit.Before;
 import org.junit.Test;
 import org.mockito.Mock;
 import org.mockito.Mockito;
 import org.mockito.MockitoAnnotations;
+import org.mockito.internal.util.reflection.Whitebox;
 import org.onap.policy.apex.core.infrastructure.messaging.impl.ws.messageblock.MessageBlock;
-import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
 import org.onap.policy.apex.core.protocols.Message;
 import org.onap.policy.apex.core.protocols.engdep.messages.GetEngineInfo;
 import org.onap.policy.apex.core.protocols.engdep.messages.GetEngineServiceInfo;
@@ -57,7 +58,7 @@ public class EngDepMessageListenerTest {
 
     /**
      * Set up mocking of the engine service facade.
-     * 
+     *
      * @throws ApexException on engine service facade setup errors
      */
     @Before
@@ -72,6 +73,8 @@ public class EngDepMessageListenerTest {
     public void testMessageListener() throws ApexException {
         DummyEngineService dummyEngineService = new DummyEngineService();
         EngDepMessageListener listener = new EngDepMessageListener(dummyEngineService);
+        BlockingQueue<?> messageQueue
+            = (BlockingQueue<?>) Whitebox.getInternalState(listener, "messageQueue");
         listener.startProcessorThread();
 
         try {
@@ -84,49 +87,49 @@ public class EngDepMessageListenerTest {
         List<Message> messageList = new ArrayList<>();
         messageList.add(new StartEngine(new AxArtifactKey("Start:0.0.1")));
         listener.onMessage(new MessageBlock<>(messageList, webSocketMock));
-        ThreadUtilities.sleep(50);
+        await().until(messageQueue::isEmpty);
         assertEquals("Start:0.0.1", dummyEngineService.getStartEngineKey().getId());
 
         messageList.clear();
         messageList.add(new StopEngine(new AxArtifactKey("Stop:0.0.1")));
         listener.onMessage(new MessageBlock<>(messageList, webSocketMock));
-        ThreadUtilities.sleep(50);
+        await().until(messageQueue::isEmpty);
         assertEquals("Stop:0.0.1", dummyEngineService.getStopEngineKey().getId());
 
         messageList.clear();
         messageList.add(new StartPeriodicEvents(new AxArtifactKey("StartPeriodic:0.0.1"), "12345"));
         listener.onMessage(new MessageBlock<>(messageList, webSocketMock));
-        ThreadUtilities.sleep(50);
+        await().until(messageQueue::isEmpty);
         assertEquals(12345, dummyEngineService.getPeriodicPeriod());
 
         messageList.clear();
         messageList.add(new StopPeriodicEvents(new AxArtifactKey("StopPeriodic:0.0.1")));
         listener.onMessage(new MessageBlock<>(messageList, webSocketMock));
-        ThreadUtilities.sleep(50);
+        await().until(messageQueue::isEmpty);
         assertEquals(0, dummyEngineService.getPeriodicPeriod());
 
         messageList.clear();
         messageList.add(new GetEngineInfo(new AxArtifactKey("EngineInfo:0.0.1")));
         listener.onMessage(new MessageBlock<>(messageList, webSocketMock));
-        ThreadUtilities.sleep(50);
+        await().until(messageQueue::isEmpty);
         assertEquals("EngineInfo:0.0.1", dummyEngineService.getRuntimeInfoKey().getId());
 
         messageList.clear();
         messageList.add(new GetEngineStatus(new AxArtifactKey("EngineStatus:0.0.1")));
         listener.onMessage(new MessageBlock<>(messageList, webSocketMock));
-        ThreadUtilities.sleep(50);
+        await().until(messageQueue::isEmpty);
         assertEquals("EngineStatus:0.0.1", dummyEngineService.getStatusKey().getId());
 
         messageList.clear();
         messageList.add(new GetEngineServiceInfo(new AxArtifactKey("EngineServiceInfo:0.0.1")));
         listener.onMessage(new MessageBlock<>(messageList, webSocketMock));
-        ThreadUtilities.sleep(50);
+        await().until(messageQueue::isEmpty);
         assertEquals(1, dummyEngineService.getModelKeyGetCalled());
 
         messageList.clear();
         messageList.add(new UpdateModel(new AxArtifactKey("UpdateModel:0.0.1")));
         listener.onMessage(new MessageBlock<>(messageList, webSocketMock));
-        ThreadUtilities.sleep(50);
+        await().until(messageQueue::isEmpty);
         assertEquals("UpdateModel:0.0.1", dummyEngineService.getUpdateModelKey().getId());
 
         try {
@@ -134,35 +137,35 @@ public class EngDepMessageListenerTest {
             messageList.add(new Response(new AxArtifactKey("UpdateModel:0.0.1"), false,
                             new GetEngineInfo(new AxArtifactKey("EngineInfo:0.0.1"))));
             listener.onMessage(new MessageBlock<>(messageList, webSocketMock));
-            ThreadUtilities.sleep(50);
+            await().until(messageQueue::isEmpty);
             assertEquals("UpdateModel:0.0.1", dummyEngineService.getUpdateModelKey().getId());
 
             messageList.clear();
             Message badMessage0 = new DummyMessage(null, null);
             messageList.add(badMessage0);
             listener.onMessage(new MessageBlock<>(messageList, webSocketMock));
-            ThreadUtilities.sleep(50);
+            await().until(messageQueue::isEmpty);
 
             messageList.clear();
             Message badMessage1 = new DummyMessage(new DummyAction(null), null);
             messageList.add(badMessage1);
             listener.onMessage(new MessageBlock<>(messageList, webSocketMock));
-            ThreadUtilities.sleep(50);
+            await().until(messageQueue::isEmpty);
 
             messageList.clear();
             Message badMessage2 = new DummyMessage(new DummyAction("throw exception"), null);
             messageList.add(badMessage2);
             listener.onMessage(new MessageBlock<>(messageList, webSocketMock));
-            ThreadUtilities.sleep(50);
+            await().until(messageQueue::isEmpty);
 
+            messageList.clear();
             Mockito.doReturn(false).when(webSocketMock).isOpen();
             messageList.add(new StartEngine(new AxArtifactKey("Start:0.0.1")));
             listener.onMessage(new MessageBlock<>(messageList, webSocketMock));
-            ThreadUtilities.sleep(50);
+            await().until(messageQueue::isEmpty);
         } catch (Exception e) {
             fail("test should not throw exceptions on bad messages");
         }
         listener.stopProcessorThreads();
-        ThreadUtilities.sleep(50);
     }
 }
index 36d6add..3f6cad8 100644 (file)
@@ -23,6 +23,7 @@ package org.onap.policy.apex.testsuites.performance.benchmark.eventgenerator;
 
 import java.io.IOException;
 import java.net.URI;
+import java.nio.file.InvalidPathException;
 import java.util.Arrays;
 
 import org.apache.commons.cli.ParseException;
@@ -104,7 +105,7 @@ public class EventGenerator {
         if (parameters.getOutFile() != null) {
             try {
                 TextFileUtils.putStringAsTextFile(getEventGenerationStats(), parameters.getOutFile());
-            } catch (IOException ioe) {
+            } catch (IOException | InvalidPathException ioe) {
                 LOGGER.warn("could not output statistics to file \"" + parameters.getOutFile() + "\"", ioe);
             }
         }
index 3d37b3f..dcbafba 100644 (file)
@@ -140,7 +140,7 @@ public class EventGeneratorTest {
     @Test
     public void testEventGeneratorOutfileBad() {
         EventGeneratorParameters pars = new EventGeneratorParameters();
-        pars.setOutFile("/I/Dont/Exist");
+        pars.setOutFile("/I/Dont/Exist*");
 
         EventGenerator generator = new EventGenerator(pars);
         assertNotNull(generator);
@@ -151,7 +151,6 @@ public class EventGeneratorTest {
 
         final String outString = outContent.toString();
         System.setOut(stdout);
-
-        assertTrue(outString.contains("could not output statistics to file \"/I/Dont/Exist\""));
+        assertTrue(outString.contains("could not output statistics to file \"/I/Dont/Exist*\""));
     }
 }