diff options
author | Jim Hahn <jrh3@att.com> | 2018-04-21 00:01:49 -0400 |
---|---|---|
committer | Jim Hahn <jrh3@att.com> | 2018-04-21 00:14:14 -0400 |
commit | aa6aea30f8d356ccbe9b649eb2416f5955e25534 (patch) | |
tree | 2273ccbc17b547e43c6c68ecfc2c0e694f6685cc /feature-pooling-dmaap/src/test/java | |
parent | 4bc349ba8c4df586b285f2b7f140643617366e10 (diff) |
Sonar fixes for latest pooling code
Made changes to pooling code to address new sonar issues.
Add comments to awaitActive() methods.
Change-Id: I390173de00135a0a5fe50af82ed4ba780df9df80
Issue-ID: POLICY-728
Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'feature-pooling-dmaap/src/test/java')
5 files changed, 60 insertions, 12 deletions
diff --git a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/DmaapManagerTest.java b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/DmaapManagerTest.java index 29dc15e4..a5688df6 100644 --- a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/DmaapManagerTest.java +++ b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/DmaapManagerTest.java @@ -34,6 +34,7 @@ import static org.mockito.Mockito.when; import java.util.Arrays; import java.util.LinkedList; import java.util.Properties; +import java.util.concurrent.CountDownLatch; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; @@ -264,11 +265,15 @@ public class DmaapManagerTest { long minms = 2000L; // tell the publisher to stop in minms + additional time - Thread thread = new Thread(() -> mgr.stopPublisher(minms + 3000L)); + CountDownLatch latch = new CountDownLatch(1); + Thread thread = new Thread(() -> { + latch.countDown(); + mgr.stopPublisher(minms + 3000L); + }); thread.start(); - // give the thread a chance to start - Thread.sleep(50L); + // wait for the thread to start + latch.await(); // interrupt it - it should immediately finish its work thread.interrupt(); diff --git a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/FeatureTest2.java b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/FeatureTest2.java index 84449e75..6884bec8 100644 --- a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/FeatureTest2.java +++ b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/FeatureTest2.java @@ -189,8 +189,7 @@ public class FeatureTest2 { } ctx.startHosts(); - - ctx.awaitEvents(STD_IDENTIFICATION_MS * 2, TimeUnit.MILLISECONDS); + ctx.awaitAllActive(STD_IDENTIFICATION_MS * 2); for (int x = 0; x < nmessages; ++x) { ctx.offerExternal(makeMessage(x)); @@ -414,6 +413,21 @@ public class FeatureTest2 { public boolean awaitEvents(long time, TimeUnit units) throws InterruptedException { return eventCounter.await(time, units); } + + /** + * Waits, for a period of time, for all hosts to enter the Active state. + * + * @param timeMs maximum time to wait, in milliseconds + * @throws InterruptedException + */ + public void awaitAllActive(long timeMs) throws InterruptedException { + long tend = timeMs + System.currentTimeMillis(); + + for (Host host : hosts) { + long tremain = Math.max(0, tend - System.currentTimeMillis()); + assertTrue(host.awaitActive(tremain)); + } + } } /** @@ -459,6 +473,18 @@ public class FeatureTest2 { } /** + * Waits, for a period of time, for the host to enter the Active state. + * + * @param timeMs time to wait, in milliseconds + * @return {@code true} if the host entered the Active state within the given + * amount of time, {@code false} otherwise + * @throws InterruptedException + */ + public boolean awaitActive(long timeMs) throws InterruptedException { + return feature.getActiveLatch().await(timeMs, TimeUnit.MILLISECONDS); + } + + /** * Starts threads for the host so that it begins consuming from both the external * "DMaaP" topic and its own internal "DMaaP" topic. */ diff --git a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/PoolingFeatureTest.java b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/PoolingFeatureTest.java index 9ee2d976..f8f37559 100644 --- a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/PoolingFeatureTest.java +++ b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/PoolingFeatureTest.java @@ -123,7 +123,7 @@ public class PoolingFeatureTest { when(factory.getController(drools2)).thenReturn(controller2); when(factory.getController(droolsDisabled)).thenReturn(controllerDisabled); - when(factory.makeManager(any(), any(), any())).thenAnswer(args -> { + when(factory.makeManager(any(), any(), any(), any())).thenAnswer(args -> { PoolingProperties props = args.getArgument(2); PoolingManagerImpl mgr = mock(PoolingManagerImpl.class); diff --git a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/PoolingManagerImplTest.java b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/PoolingManagerImplTest.java index 693cb6de..e0024b79 100644 --- a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/PoolingManagerImplTest.java +++ b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/PoolingManagerImplTest.java @@ -113,6 +113,7 @@ public class PoolingManagerImplTest { private DroolsController drools; private Serializer ser; private Factory factory; + private CountDownLatch active; private PoolingManagerImpl mgr; @@ -142,6 +143,7 @@ public class PoolingManagerImplTest { futures = new LinkedList<>(); ser = new Serializer(); + active = new CountDownLatch(1); factory = mock(Factory.class); eventQueue = mock(EventQueue.class); @@ -181,7 +183,7 @@ public class PoolingManagerImplTest { PoolingManagerImpl.setFactory(factory); - mgr = new PoolingManagerImpl(MY_HOST, controller, poolProps); + mgr = new PoolingManagerImpl(MY_HOST, controller, poolProps, active); } @Test @@ -204,7 +206,7 @@ public class PoolingManagerImplTest { PolicyController ctlr = mock(PolicyController.class); PoolingFeatureRtException ex = expectException(PoolingFeatureRtException.class, - () -> new PoolingManagerImpl(MY_HOST, ctlr, poolProps)); + () -> new PoolingManagerImpl(MY_HOST, ctlr, poolProps, active)); assertNotNull(ex.getCause()); assertTrue(ex.getCause() instanceof ClassCastException); } @@ -216,7 +218,7 @@ public class PoolingManagerImplTest { when(factory.makeDmaapManager(any(), any())).thenThrow(ex); PoolingFeatureRtException ex2 = expectException(PoolingFeatureRtException.class, - () -> new PoolingManagerImpl(MY_HOST, controller, poolProps)); + () -> new PoolingManagerImpl(MY_HOST, controller, poolProps, active)); assertEquals(ex, ex2.getCause()); } @@ -233,7 +235,7 @@ public class PoolingManagerImplTest { public void testGetHost() { assertEquals(MY_HOST, mgr.getHost()); - mgr = new PoolingManagerImpl(HOST2, controller, poolProps); + mgr = new PoolingManagerImpl(HOST2, controller, poolProps, active); assertEquals(HOST2, mgr.getHost()); } @@ -1102,7 +1104,6 @@ public class PoolingManagerImplTest { // route the messages to this host CountDownLatch latch = mgr.startDistributing(makeAssignments(true)); - assertNotNull(latch); assertTrue(latch.await(2, TimeUnit.SECONDS)); // all of the events should have been processed locally @@ -1123,7 +1124,8 @@ public class PoolingManagerImplTest { when(eventQueue.poll()).thenAnswer(args -> lst.poll()); // route the messages to the OTHER host - assertTrue(mgr.startDistributing(makeAssignments(false)).await(2, TimeUnit.SECONDS)); + CountDownLatch latch = mgr.startDistributing(makeAssignments(false)); + assertTrue(latch.await(2, TimeUnit.SECONDS)); // all of the events should have been forwarded verify(dmaap, times(4)).publish(any()); @@ -1159,6 +1161,7 @@ public class PoolingManagerImplTest { assertTrue(st instanceof ActiveState); assertEquals(mgr.getHost(), st.getHost()); assertEquals(asgn, mgr.getAssignments()); + assertEquals(0, active.getCount()); } @Test @@ -1166,6 +1169,7 @@ public class PoolingManagerImplTest { State st = mgr.goInactive(); assertTrue(st instanceof InactiveState); assertEquals(mgr.getHost(), st.getHost()); + assertEquals(1, active.getCount()); } @Test diff --git a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/state/StateTest.java b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/state/StateTest.java index 08b55c6b..47624aa0 100644 --- a/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/state/StateTest.java +++ b/feature-pooling-dmaap/src/test/java/org/onap/policy/drools/pooling/state/StateTest.java @@ -186,6 +186,19 @@ public class StateTest extends BasicStateTester { } @Test + public void testGoActive_NullAssignment() { + State act = mock(State.class); + State inact = mock(State.class); + + when(mgr.goActive()).thenReturn(act); + when(mgr.goInactive()).thenReturn(inact); + + assertEquals(inact, state.goActive(null)); + + verify(mgr, never()).startDistributing(any()); + } + + @Test public void testGoInactive() { State next = mock(State.class); when(mgr.goInactive()).thenReturn(next); |