From 153ad0056c3928116d28eb7e1bb14c4a04a76fc2 Mon Sep 17 00:00:00 2001 From: "Henry.Sun" Date: Mon, 2 Mar 2020 15:47:09 +0800 Subject: replace test sleep() with awaitality package Signed-off-by: Henry.Sun Change-Id: I305771ddef42bd3032ad52f4c5ecd55b01ed5a1a Issue-ID: POLICY-1914 Signed-off-by: Henry.Sun --- .../policy/apex/core/deployment/DeploymentClientTest.java | 14 ++++++-------- .../policy/apex/core/deployment/DummyDeploymentClient.java | 9 ++++----- .../apex/core/engine/engine/impl/ApexEngineImplTest.java | 10 ++++++---- .../core/engine/engine/impl/DummySlowEnEventListener.java | 3 ++- .../messaging/EndToEndStringMessagingTest.java | 10 ++++++---- .../core/infrastructure/messaging/StringTestServer.java | 6 +++--- .../apex/core/infrastructure/threading/ThreadingTest.java | 10 +++++++--- .../core/infrastructure/threading/ThreadingTestThread.java | 11 ++++++++--- 8 files changed, 42 insertions(+), 31 deletions(-) (limited to 'core') diff --git a/core/core-deployment/src/test/java/org/onap/policy/apex/core/deployment/DeploymentClientTest.java b/core/core-deployment/src/test/java/org/onap/policy/apex/core/deployment/DeploymentClientTest.java index d5b34a054..f51b2337c 100644 --- a/core/core-deployment/src/test/java/org/onap/policy/apex/core/deployment/DeploymentClientTest.java +++ b/core/core-deployment/src/test/java/org/onap/policy/apex/core/deployment/DeploymentClientTest.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 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. @@ -20,6 +21,7 @@ package org.onap.policy.apex.core.deployment; +import static org.awaitility.Awaitility.await; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; @@ -29,6 +31,7 @@ import static org.mockito.Matchers.anyObject; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.TimeUnit; import org.junit.Test; import org.junit.runner.RunWith; @@ -41,7 +44,6 @@ import org.onap.policy.apex.core.infrastructure.messaging.MessageListener; import org.onap.policy.apex.core.infrastructure.messaging.MessagingService; import org.onap.policy.apex.core.infrastructure.messaging.MessagingServiceFactory; 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.GetEngineStatus; import org.onap.policy.apex.core.protocols.engdep.messages.Response; @@ -81,7 +83,7 @@ public class DeploymentClientTest { Thread clientThread = new Thread(deploymentClient); clientThread.start(); - ThreadUtilities.sleep(100); + await().atMost(200, TimeUnit.MILLISECONDS).until(() -> deploymentClient.isStarted()); assertTrue(deploymentClient.isStarted()); assertTrue(clientThread.isAlive()); @@ -90,7 +92,6 @@ public class DeploymentClientTest { GetEngineStatus getEngineStatus = new GetEngineStatus(engineKey); deploymentClient.sendMessage(new GetEngineStatus(engineKey)); - ThreadUtilities.sleep(20); Response response = new Response(engineKey, true, getEngineStatus); List messageList = new ArrayList<>(); messageList.add(response); @@ -105,8 +106,7 @@ public class DeploymentClientTest { assertEquals("String mesages are not supported on the EngDep protocol", use.getMessage()); } - ThreadUtilities.sleep(300); - assertEquals(1, deploymentClient.getMessagesSent()); + await().atMost(300, TimeUnit.MILLISECONDS).until(() -> deploymentClient.getMessagesReceived() == 2); assertEquals(2, deploymentClient.getMessagesReceived()); deploymentClient.stopClient(); @@ -128,14 +128,12 @@ public class DeploymentClientTest { Thread clientThread = new Thread(deploymentClient); clientThread.start(); - ThreadUtilities.sleep(50); + await().atLeast(50, TimeUnit.MILLISECONDS).until(() -> !deploymentClient.isStarted()); assertFalse(deploymentClient.isStarted()); assertFalse(clientThread.isAlive()); assertEquals(0, deploymentClient.getReceiveQueue().size()); - ThreadUtilities.sleep(100); - deploymentClient.stopClient(); } } diff --git a/core/core-deployment/src/test/java/org/onap/policy/apex/core/deployment/DummyDeploymentClient.java b/core/core-deployment/src/test/java/org/onap/policy/apex/core/deployment/DummyDeploymentClient.java index 5bbe1812d..965013acb 100644 --- a/core/core-deployment/src/test/java/org/onap/policy/apex/core/deployment/DummyDeploymentClient.java +++ b/core/core-deployment/src/test/java/org/onap/policy/apex/core/deployment/DummyDeploymentClient.java @@ -26,8 +26,8 @@ import java.util.ArrayList; import java.util.List; import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.TimeUnit; -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.EngineServiceInfoResponse; import org.onap.policy.apex.core.protocols.engdep.messages.GetEngineInfo; @@ -42,6 +42,8 @@ import org.onap.policy.apex.core.protocols.engdep.messages.UpdateModel; import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; import org.onap.policy.common.utils.resources.TextFileUtils; +import static org.awaitility.Awaitility.await; + /** * Dummy deployment client. */ @@ -81,10 +83,7 @@ public class DummyDeploymentClient extends DeploymentClient implements Runnable started = true; // Loop forever, sending messages as they appear on the queue - while (started && !thisThread.isInterrupted()) { - ThreadUtilities.sleep(50); - } - + await().atLeast(50, TimeUnit.MILLISECONDS).until(() -> !(started && !thisThread.isInterrupted())); // Thread has been interrupted thisThread = null; started = false; diff --git a/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/engine/impl/ApexEngineImplTest.java b/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/engine/impl/ApexEngineImplTest.java index 2663dfc0d..f91b58bb9 100644 --- a/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/engine/impl/ApexEngineImplTest.java +++ b/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/engine/impl/ApexEngineImplTest.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2019-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. @@ -22,6 +22,7 @@ package org.onap.policy.apex.core.engine.engine.impl; import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.awaitility.Awaitility.await; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; @@ -32,6 +33,8 @@ import static org.junit.Assert.fail; import java.io.IOException; import java.lang.reflect.Field; import java.util.HashMap; +import java.util.concurrent.TimeUnit; + import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; @@ -312,8 +315,7 @@ public class ApexEngineImplTest { assertEquals(AxEngineState.STOPPED, engine.getState()); } }).start(); - - Thread.sleep(50); + await().atLeast(50, TimeUnit.MILLISECONDS).until(() -> engine.getState().equals(AxEngineState.EXECUTING)); assertEquals(AxEngineState.EXECUTING, engine.getState()); assertFalse(engine.handleEvent(event)); @@ -343,7 +345,7 @@ public class ApexEngineImplTest { } }).start(); - Thread.sleep(50); + await().atLeast(50, TimeUnit.MILLISECONDS).until(() -> engine.getState().equals(AxEngineState.EXECUTING)); assertEquals(AxEngineState.EXECUTING, engine.getState()); try { engine.stop(); diff --git a/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/engine/impl/DummySlowEnEventListener.java b/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/engine/impl/DummySlowEnEventListener.java index 9b6fc398c..178e16a43 100644 --- a/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/engine/impl/DummySlowEnEventListener.java +++ b/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/engine/impl/DummySlowEnEventListener.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. + * Modifications Copyright (C) 2019-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. @@ -41,7 +42,7 @@ public class DummySlowEnEventListener implements EnEventListener { Thread.sleep(waitTime); } catch (InterruptedException ie) { - // Do nothing + //Do nothing } } diff --git a/core/core-infrastructure/src/test/java/org/onap/policy/apex/core/infrastructure/messaging/EndToEndStringMessagingTest.java b/core/core-infrastructure/src/test/java/org/onap/policy/apex/core/infrastructure/messaging/EndToEndStringMessagingTest.java index c9d56ef2c..d9691f17e 100644 --- a/core/core-infrastructure/src/test/java/org/onap/policy/apex/core/infrastructure/messaging/EndToEndStringMessagingTest.java +++ b/core/core-infrastructure/src/test/java/org/onap/policy/apex/core/infrastructure/messaging/EndToEndStringMessagingTest.java @@ -1,6 +1,7 @@ /*- * ============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. @@ -20,6 +21,7 @@ package org.onap.policy.apex.core.infrastructure.messaging; +import static org.awaitility.Awaitility.await; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @@ -27,10 +29,11 @@ import org.junit.Test; import org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WsStringMessageClient; import org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WsStringMessageListener; import org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WsStringMessageServer; -import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities; import org.slf4j.ext.XLogger; import org.slf4j.ext.XLoggerFactory; +import java.util.concurrent.TimeUnit; + /** * The Class EndToEndMessagingTest. * @@ -59,9 +62,8 @@ public class EndToEndStringMessagingTest { client.sendString("Hello, client here"); - while (!finished) { - ThreadUtilities.sleep(50); - } + await().atLeast(50, TimeUnit.MILLISECONDS).until(() -> finished); + } finally { if (client != null) { client.stop(); diff --git a/core/core-infrastructure/src/test/java/org/onap/policy/apex/core/infrastructure/messaging/StringTestServer.java b/core/core-infrastructure/src/test/java/org/onap/policy/apex/core/infrastructure/messaging/StringTestServer.java index 1471bf331..9cf99feb3 100644 --- a/core/core-infrastructure/src/test/java/org/onap/policy/apex/core/infrastructure/messaging/StringTestServer.java +++ b/core/core-infrastructure/src/test/java/org/onap/policy/apex/core/infrastructure/messaging/StringTestServer.java @@ -1,6 +1,7 @@ /*- * ============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. @@ -47,9 +48,8 @@ public class StringTestServer { System.out.println("StringTestServer started on port " + port + " for " + timeToLive + " seconds"); - for (; timeToLive > 0; timeToLive--) { - ThreadUtilities.sleep(1000); - } + // convert to milliSeconds + ThreadUtilities.sleep(1000 * timeToLive); server.stop(); System.out.println("StringTestServer completed"); diff --git a/core/core-infrastructure/src/test/java/org/onap/policy/apex/core/infrastructure/threading/ThreadingTest.java b/core/core-infrastructure/src/test/java/org/onap/policy/apex/core/infrastructure/threading/ThreadingTest.java index 055a76fc5..23f458a67 100644 --- a/core/core-infrastructure/src/test/java/org/onap/policy/apex/core/infrastructure/threading/ThreadingTest.java +++ b/core/core-infrastructure/src/test/java/org/onap/policy/apex/core/infrastructure/threading/ThreadingTest.java @@ -1,6 +1,7 @@ /*- * ============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. @@ -20,11 +21,13 @@ package org.onap.policy.apex.core.infrastructure.threading; +import static org.awaitility.Awaitility.await; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.TimeUnit; import org.junit.Test; import org.slf4j.ext.XLogger; @@ -77,10 +80,11 @@ public class ThreadingTest { final Thread thread = threadFactory.newThread(runnable); thread.start(); - } + if (i == 4) { + await().atLeast(100, TimeUnit.MILLISECONDS).until(() -> thread.isAlive()); + } - // Threads should need a little more than 300ms to count to 3 - ThreadUtilities.sleep(380); + } for (int i = 0; i < 5; i++) { threadList.get(i).interrupt(); 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 index b504780b7..a2c51539f 100644 --- 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 @@ -1,6 +1,7 @@ /*- * ============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. @@ -23,6 +24,8 @@ package org.onap.policy.apex.core.infrastructure.threading; import org.slf4j.ext.XLogger; import org.slf4j.ext.XLoggerFactory; +import java.util.concurrent.CountDownLatch; + /** * The Class ThreadingTestThread. * @@ -39,7 +42,9 @@ public class ThreadingTestThread implements Runnable { private String threadName; - /** + private CountDownLatch latch = new CountDownLatch(1); + + /** * {@inheritDoc}. */ @Override @@ -54,11 +59,11 @@ public class ThreadingTestThread implements Runnable { if (logger.isDebugEnabled()) { logger.debug("in threading test thread \"" + threadName + "\", counter=" + counter + " . . ."); } - - if (!ThreadUtilities.sleep(50)) { + if(!ThreadUtilities.sleep(50)) { interrupted = true; } } + if (logger.isDebugEnabled()) { logger.debug("stopped threading test thread \"" + threadName + "\""); } -- cgit 1.2.3-korg