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 --- .../infrastructure/messaging/EndToEndStringMessagingTest.java | 10 ++++++---- .../apex/core/infrastructure/messaging/StringTestServer.java | 6 +++--- .../apex/core/infrastructure/threading/ThreadingTest.java | 10 +++++++--- .../core/infrastructure/threading/ThreadingTestThread.java | 11 ++++++++--- 4 files changed, 24 insertions(+), 13 deletions(-) (limited to 'core/core-infrastructure/src') 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