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 --- .../client/editor/rest/ApexEditorStartupTest.java | 10 ++++----- .../apex/client/editor/rest/RestInterfaceTest.java | 26 ++++++++-------------- 2 files changed, 14 insertions(+), 22 deletions(-) (limited to 'client/client-editor') diff --git a/client/client-editor/src/test/java/org/onap/policy/apex/client/editor/rest/ApexEditorStartupTest.java b/client/client-editor/src/test/java/org/onap/policy/apex/client/editor/rest/ApexEditorStartupTest.java index 54e40ae77..ac1c60553 100644 --- a/client/client-editor/src/test/java/org/onap/policy/apex/client/editor/rest/ApexEditorStartupTest.java +++ b/client/client-editor/src/test/java/org/onap/policy/apex/client/editor/rest/ApexEditorStartupTest.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,12 +21,14 @@ package org.onap.policy.apex.client.editor.rest; +import static org.awaitility.Awaitility.await; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.PrintStream; +import java.util.concurrent.TimeUnit; import org.junit.Test; import org.onap.policy.apex.client.editor.rest.ApexEditorMain.EditorState; @@ -444,11 +447,8 @@ public class ApexEditorStartupTest { } }; new Thread(testThread).start(); - while (editorMain.getState().equals(EditorState.READY) - || editorMain.getState().equals(EditorState.INITIALIZING)) { - Thread.sleep(100); - } - + await().atMost(15000, TimeUnit.MILLISECONDS).until(() -> !(editorMain.getState().equals(EditorState.READY) + || editorMain.getState().equals(EditorState.INITIALIZING))); editorMain.shutdown(); final String outString = outBaStream.toString(); System.out.println(outString); diff --git a/client/client-editor/src/test/java/org/onap/policy/apex/client/editor/rest/RestInterfaceTest.java b/client/client-editor/src/test/java/org/onap/policy/apex/client/editor/rest/RestInterfaceTest.java index 260a898df..c95a04a76 100644 --- a/client/client-editor/src/test/java/org/onap/policy/apex/client/editor/rest/RestInterfaceTest.java +++ b/client/client-editor/src/test/java/org/onap/policy/apex/client/editor/rest/RestInterfaceTest.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.client.editor.rest; +import static org.awaitility.Awaitility.await; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; @@ -27,6 +29,7 @@ import static org.junit.Assert.assertTrue; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; +import java.util.concurrent.TimeUnit; import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; @@ -87,16 +90,11 @@ public class RestInterfaceTest { }; new Thread(testThread).start(); // wait until editorMain is in state RUNNING - final long startwait = System.currentTimeMillis(); - while (editorMain.getState().equals(EditorState.STOPPED) || editorMain.getState().equals(EditorState.READY) - || editorMain.getState().equals(EditorState.INITIALIZING)) { - if (editorMain.getState().equals(EditorState.STOPPED)) { - Assert.fail("Rest endpoint (" + editorMain + ") shut down before it could be used"); - } - if (System.currentTimeMillis() - startwait > MAX_WAIT) { - Assert.fail("Rest endpoint (" + editorMain + ") for test failed to start fast enough"); - } - Thread.sleep(100); + await().atMost(MAX_WAIT, TimeUnit.MILLISECONDS).until(() -> !(editorMain.getState().equals(EditorState.READY) + || editorMain.getState().equals(EditorState.INITIALIZING))); + + if (editorMain.getState().equals(EditorState.STOPPED)) { + Assert.fail("Rest endpoint (" + editorMain + ") shut down before it could be used"); } // create the client @@ -124,13 +122,7 @@ public class RestInterfaceTest { public static void cleanUpStreams() throws IOException, InterruptedException { editorMain.shutdown(); // wait until editorMain is in state STOPPED - final long startwait = System.currentTimeMillis(); - while (!editorMain.getState().equals(EditorState.STOPPED)) { - if (System.currentTimeMillis() - startwait > MAX_WAIT) { - Assert.fail("Rest endpoint (" + editorMain + ") for test failed to shutdown fast enough"); - } - Thread.sleep(50); - } + await().atMost(MAX_WAIT, TimeUnit.MILLISECONDS).until(() -> editorMain.getState().equals(EditorState.STOPPED)); System.setIn(SYSIN); } -- cgit 1.2.3-korg