Add junit coverage to TimerServiceImpl class.
[appc.git] / appc-client / client-lib / src / test / java / org / onap / appc / client / impl / core / TimerServiceImplTest.java
1 /*\r
2  * ============LICENSE_START=======================================================\r
3  * ONAP : APPC\r
4  * ================================================================================\r
5  * Copyright 2018 AT&T\r
6  * =================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  *\r
11  * http://www.apache.org/licenses/LICENSE-2.0\r
12  *\r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ============LICENSE_END=========================================================\r
19  */\r
20 \r
21 package org.onap.appc.client.impl.core;\r
22 \r
23 import static org.junit.Assert.assertTrue;\r
24 \r
25 import java.util.concurrent.CountDownLatch;\r
26 import java.util.concurrent.TimeUnit;\r
27 \r
28 import org.junit.AfterClass;\r
29 import org.junit.BeforeClass;\r
30 import org.junit.Test;\r
31 \r
32 public class TimerServiceImplTest {\r
33 \r
34     private static TimerServiceImpl ts = new TimerServiceImpl(100);\r
35     private static TimeoutHandlerImpl handler1;\r
36     private static TimeoutHandlerImpl handler2;\r
37     private static TimeoutHandlerImpl handler3;\r
38     \r
39     @BeforeClass\r
40     public static void setUpBeforeClass() throws Exception {\r
41         handler1 = new TimeoutHandlerImpl();\r
42         ts.add("1", handler1);\r
43         ts.cancel("1");\r
44 \r
45         handler2 = new TimeoutHandlerImpl();\r
46         ts.add("2", handler2);\r
47 \r
48         handler3 = new TimeoutHandlerImpl();\r
49         ts.add("3", handler3);\r
50     }\r
51     \r
52     @AfterClass\r
53     public static void cleanupAfterClass() throws Exception {\r
54         ts.shutdown();\r
55     }\r
56 \r
57     @Test\r
58     public void testCancelledTimer() {\r
59         assertTrue("TimerServiceImpl cancel failed!", handler1.getLatch().getCount() == 1);\r
60     }\r
61 \r
62     @Test\r
63     public void testTimeoutActionPerformed() throws InterruptedException {\r
64         handler2.getLatch().await(2, TimeUnit.SECONDS);\r
65         assertTrue("TimerServiceImpl timeout action not performed!", handler2.getLatch().getCount() == 0);\r
66     }\r
67 \r
68     @Test\r
69     public void testLateCancel() throws InterruptedException {\r
70         TimeUnit.MILLISECONDS.sleep(300);\r
71         ts.cancel("3");\r
72         assertTrue("TimerServiceImpl late cancel - action unexpectedly performed!", handler3.getLatch().getCount() == 0);\r
73     }\r
74     \r
75     private static class TimeoutHandlerImpl implements ITimeoutHandler {\r
76 \r
77         private CountDownLatch latch = new CountDownLatch(1);\r
78 \r
79         public CountDownLatch getLatch() {\r
80             return latch;\r
81         }\r
82 \r
83         /**\r
84          * When a timeout event is occurring, the new Timeout task will be assigned into a queue,\r
85          * this queue is shared between both timeout and handlers which belong to same correlation ID.\r
86          */\r
87         @Override\r
88         public void onTimeout() {\r
89             latch.countDown();\r
90         }\r
91     }\r
92 \r
93 \r
94 }\r