2 * ============LICENSE_START=======================================================
\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
11 * http://www.apache.org/licenses/LICENSE-2.0
\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
21 package org.onap.appc.client.impl.core;
\r
23 import static org.junit.Assert.assertTrue;
\r
25 import java.util.concurrent.CountDownLatch;
\r
26 import java.util.concurrent.TimeUnit;
\r
28 import org.junit.AfterClass;
\r
29 import org.junit.BeforeClass;
\r
30 import org.junit.Test;
\r
32 public class TimerServiceImplTest {
\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
40 public static void setUpBeforeClass() throws Exception {
\r
41 handler1 = new TimeoutHandlerImpl();
\r
42 ts.add("1", handler1);
\r
45 handler2 = new TimeoutHandlerImpl();
\r
46 ts.add("2", handler2);
\r
48 handler3 = new TimeoutHandlerImpl();
\r
49 ts.add("3", handler3);
\r
53 public static void cleanupAfterClass() throws Exception {
\r
58 public void testCancelledTimer() {
\r
59 assertTrue("TimerServiceImpl cancel failed!", handler1.getLatch().getCount() == 1);
\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
69 public void testLateCancel() throws InterruptedException {
\r
70 TimeUnit.MILLISECONDS.sleep(300);
\r
72 assertTrue("TimerServiceImpl late cancel - action unexpectedly performed!", handler3.getLatch().getCount() == 0);
\r
75 private static class TimeoutHandlerImpl implements ITimeoutHandler {
\r
77 private CountDownLatch latch = new CountDownLatch(1);
\r
79 public CountDownLatch getLatch() {
\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
88 public void onTimeout() {
\r