From 718a1611a1e580ed7874088e1a0e013416334989 Mon Sep 17 00:00:00 2001 From: huaxing Date: Wed, 10 Jun 2020 14:58:34 +0800 Subject: Improve robustness of unit testing Issue-ID: POLICY-2630 Signed-off-by: huaxing Change-Id: I6475f9272c1a770836af537c13b23e486b66ac3e --- .../engine/engdep/EngDepMessageListenerTest.java | 43 ++++++++++++---------- 1 file changed, 23 insertions(+), 20 deletions(-) (limited to 'services/services-engine/src/test/java/org') diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/engdep/EngDepMessageListenerTest.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/engdep/EngDepMessageListenerTest.java index f755a669c..9f5329e55 100644 --- a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/engdep/EngDepMessageListenerTest.java +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/engdep/EngDepMessageListenerTest.java @@ -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 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); } } -- cgit 1.2.3-korg