2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Modification Copyright (C) 2018 IBM
10 * =============================================================================
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
23 * ============LICENSE_END=========================================================
26 package org.onap.appc.concurrent;
28 import static org.junit.Assert.assertFalse;
29 import static org.junit.Assert.assertTrue;
30 import static org.junit.Assert.assertEquals;
31 import java.text.DateFormat;
32 import java.text.SimpleDateFormat;
33 import java.util.Date;
34 import java.util.concurrent.TimeoutException;
36 import org.junit.Test;
37 import org.onap.appc.concurrent.Signal;
41 public class TestSignal {
43 private static final DateFormat formatter = new SimpleDateFormat("HH:mm:ss.SSS");
44 public static final String SIGNAL_READY = "READY";
45 public static final String SIGNAL_SHUTDOWN = "SHUTDOWN";
48 public void TestSimpleSignal() throws TimeoutException {
50 Signal mySignal = new Signal(Thread.currentThread());
51 mySignal.setTimeout(5000L);
52 Fred fred = new Fred(mySignal);
53 Thread t1 = new Thread(fred);
56 * Verify that fred is dead, then start him and wait for him to signal us he is ready to proceed
58 assertFalse(t1.isAlive());
60 System.out.println(formatter.format(new Date()) + " MAIN: Waiting for Ready...");
61 mySignal.waitFor(SIGNAL_READY);
62 System.out.println(formatter.format(new Date()) + " MAIN: Signal Ready received");
65 * Verify that fred is still alive and we will sleep for a while (simulate doing things)
67 assertTrue(t1.isAlive());
70 } catch (InterruptedException e) {
75 * Verify that fred is still alive and signal him to shutdown
77 assertTrue(t1.isAlive());
78 System.out.println(formatter.format(new Date()) + " MAIN: Signaling shutdown");
79 fred.getSignal().signal(SIGNAL_SHUTDOWN);
86 } catch (InterruptedException e) {
91 * Verify that fred is dead now and that he completed normally
93 System.out.println(formatter.format(new Date()) + " MAIN: Shutting down...");
94 assertFalse(t1.isAlive());
95 assertTrue(fred.isCompleted());
98 public class Fred implements Runnable {
99 private Signal signal;
100 private Signal parentSignal;
101 private boolean completed = false;
103 public Fred(Signal parentSignal) {
104 this.parentSignal = parentSignal;
109 signal = new Signal(Thread.currentThread());
110 signal.setTimeout(5000L);
113 } catch (InterruptedException e) {
117 System.out.println(formatter.format(new Date()) + " FRED: Signaling ready...");
118 parentSignal.signal(SIGNAL_READY);
121 System.out.println(formatter.format(new Date()) + " FRED: Waiting for shutdown...");
122 signal.waitFor(SIGNAL_SHUTDOWN);
123 System.out.println(formatter.format(new Date()) + " FRED: Received shutdown");
125 } catch (TimeoutException e) {
130 public boolean isCompleted() {
134 public Signal getSignal() {
140 public void testWaitForAny() throws Exception
142 Signal mySignal = new Signal(Thread.currentThread());
143 mySignal.setTimeout(50L);
144 String receivedSignal= mySignal.waitForAny(SIGNAL_READY);
145 assertEquals("READY", receivedSignal);
148 @Test(expected=TimeoutException.class)
149 public void testWaitForAnyForEmptySignal() throws TimeoutException
151 Signal mySignal = new Signal(Thread.currentThread());
152 mySignal.setTimeout(50L);
153 mySignal.waitForAny();
157 public void testIsSignaled() throws TimeoutException
159 Signal mySignal = new Signal(Thread.currentThread());
160 assertFalse(mySignal.isSignaled(SIGNAL_READY));