--- /dev/null
+/*\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