Add junit coverage to TimerServiceImpl class. 57/40457/4
authorArnel Pajarillo <ap1541@att.com>
Sat, 31 Mar 2018 15:12:28 +0000 (11:12 -0400)
committerTakamune Cho <tc012c@att.com>
Tue, 3 Apr 2018 15:28:29 +0000 (15:28 +0000)
Introduce junit test for client-lib's TimerService.java.

Change-Id: I3f8947cd526dc6a62ddcd2ced189565a401c6353
Issue-ID: APPC-815
Signed-off-by: Arnel Pajarillo <ap1541@att.com>
appc-client/client-lib/src/test/java/org/onap/appc/client/impl/core/TimerServiceImplTest.java [new file with mode: 0644]

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 (file)
index 0000000..b8ee11d
--- /dev/null
@@ -0,0 +1,94 @@
+/*\r
+ * ============LICENSE_START=======================================================\r
+ * ONAP : APPC\r
+ * ================================================================================\r
+ * Copyright 2018 AT&T\r
+ * =================================================================================\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ * ============LICENSE_END=========================================================\r
+ */\r
+\r
+package org.onap.appc.client.impl.core;\r
+\r
+import static org.junit.Assert.assertTrue;\r
+\r
+import java.util.concurrent.CountDownLatch;\r
+import java.util.concurrent.TimeUnit;\r
+\r
+import org.junit.AfterClass;\r
+import org.junit.BeforeClass;\r
+import org.junit.Test;\r
+\r
+public class TimerServiceImplTest {\r
+\r
+    private static TimerServiceImpl ts = new TimerServiceImpl(100);\r
+    private static TimeoutHandlerImpl handler1;\r
+    private static TimeoutHandlerImpl handler2;\r
+    private static TimeoutHandlerImpl handler3;\r
+    \r
+    @BeforeClass\r
+    public static void setUpBeforeClass() throws Exception {\r
+        handler1 = new TimeoutHandlerImpl();\r
+        ts.add("1", handler1);\r
+        ts.cancel("1");\r
+\r
+        handler2 = new TimeoutHandlerImpl();\r
+        ts.add("2", handler2);\r
+\r
+        handler3 = new TimeoutHandlerImpl();\r
+        ts.add("3", handler3);\r
+    }\r
+    \r
+    @AfterClass\r
+    public static void cleanupAfterClass() throws Exception {\r
+        ts.shutdown();\r
+    }\r
+\r
+    @Test\r
+    public void testCancelledTimer() {\r
+        assertTrue("TimerServiceImpl cancel failed!", handler1.getLatch().getCount() == 1);\r
+    }\r
+\r
+    @Test\r
+    public void testTimeoutActionPerformed() throws InterruptedException {\r
+        handler2.getLatch().await(2, TimeUnit.SECONDS);\r
+        assertTrue("TimerServiceImpl timeout action not performed!", handler2.getLatch().getCount() == 0);\r
+    }\r
+\r
+    @Test\r
+    public void testLateCancel() throws InterruptedException {\r
+        TimeUnit.MILLISECONDS.sleep(300);\r
+        ts.cancel("3");\r
+        assertTrue("TimerServiceImpl late cancel - action unexpectedly performed!", handler3.getLatch().getCount() == 0);\r
+    }\r
+    \r
+    private static class TimeoutHandlerImpl implements ITimeoutHandler {\r
+\r
+        private CountDownLatch latch = new CountDownLatch(1);\r
+\r
+        public CountDownLatch getLatch() {\r
+            return latch;\r
+        }\r
+\r
+        /**\r
+         * When a timeout event is occurring, the new Timeout task will be assigned into a queue,\r
+         * this queue is shared between both timeout and handlers which belong to same correlation ID.\r
+         */\r
+        @Override\r
+        public void onTimeout() {\r
+            latch.countDown();\r
+        }\r
+    }\r
+\r
+\r
+}\r