summaryrefslogtreecommitdiffstats
path: root/appc-client/client-lib
diff options
context:
space:
mode:
authorArnel Pajarillo <ap1541@att.com>2018-03-31 11:12:28 -0400
committerTakamune Cho <tc012c@att.com>2018-04-03 15:28:29 +0000
commit81ad515289250e8e9d14989c844363c76edbe27b (patch)
tree59c1c8298895c1675ab6b6499cb75349a0ef674a /appc-client/client-lib
parent8224dc829b8975c25d5e85e0d72898b20182e815 (diff)
Add junit coverage to TimerServiceImpl class.
Introduce junit test for client-lib's TimerService.java. Change-Id: I3f8947cd526dc6a62ddcd2ced189565a401c6353 Issue-ID: APPC-815 Signed-off-by: Arnel Pajarillo <ap1541@att.com>
Diffstat (limited to 'appc-client/client-lib')
-rw-r--r--appc-client/client-lib/src/test/java/org/onap/appc/client/impl/core/TimerServiceImplTest.java94
1 files changed, 94 insertions, 0 deletions
diff --git a/appc-client/client-lib/src/test/java/org/onap/appc/client/impl/core/TimerServiceImplTest.java b/appc-client/client-lib/src/test/java/org/onap/appc/client/impl/core/TimerServiceImplTest.java
new file mode 100644
index 000000000..b8ee11d78
--- /dev/null
+++ b/appc-client/client-lib/src/test/java/org/onap/appc/client/impl/core/TimerServiceImplTest.java
@@ -0,0 +1,94 @@
+/*
+ * ============LICENSE_START=======================================================
+ * ONAP : APPC
+ * ================================================================================
+ * Copyright 2018 AT&T
+ * =================================================================================
+ * 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.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.appc.client.impl.core;
+
+import static org.junit.Assert.assertTrue;
+
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class TimerServiceImplTest {
+
+ private static TimerServiceImpl ts = new TimerServiceImpl(100);
+ private static TimeoutHandlerImpl handler1;
+ private static TimeoutHandlerImpl handler2;
+ private static TimeoutHandlerImpl handler3;
+
+ @BeforeClass
+ public static void setUpBeforeClass() throws Exception {
+ handler1 = new TimeoutHandlerImpl();
+ ts.add("1", handler1);
+ ts.cancel("1");
+
+ handler2 = new TimeoutHandlerImpl();
+ ts.add("2", handler2);
+
+ handler3 = new TimeoutHandlerImpl();
+ ts.add("3", handler3);
+ }
+
+ @AfterClass
+ public static void cleanupAfterClass() throws Exception {
+ ts.shutdown();
+ }
+
+ @Test
+ public void testCancelledTimer() {
+ assertTrue("TimerServiceImpl cancel failed!", handler1.getLatch().getCount() == 1);
+ }
+
+ @Test
+ public void testTimeoutActionPerformed() throws InterruptedException {
+ handler2.getLatch().await(2, TimeUnit.SECONDS);
+ assertTrue("TimerServiceImpl timeout action not performed!", handler2.getLatch().getCount() == 0);
+ }
+
+ @Test
+ public void testLateCancel() throws InterruptedException {
+ TimeUnit.MILLISECONDS.sleep(300);
+ ts.cancel("3");
+ assertTrue("TimerServiceImpl late cancel - action unexpectedly performed!", handler3.getLatch().getCount() == 0);
+ }
+
+ private static class TimeoutHandlerImpl implements ITimeoutHandler {
+
+ private CountDownLatch latch = new CountDownLatch(1);
+
+ public CountDownLatch getLatch() {
+ return latch;
+ }
+
+ /**
+ * When a timeout event is occurring, the new Timeout task will be assigned into a queue,
+ * this queue is shared between both timeout and handlers which belong to same correlation ID.
+ */
+ @Override
+ public void onTimeout() {
+ latch.countDown();
+ }
+ }
+
+
+}