2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.concurrent;
27 import static org.junit.Assert.assertFalse;
28 import static org.junit.Assert.assertTrue;
30 import java.text.DateFormat;
31 import java.text.SimpleDateFormat;
32 import java.util.Date;
33 import java.util.concurrent.TimeoutException;
35 import org.junit.Test;
36 import org.onap.appc.concurrent.Signal;
38 public class TestSignal {
40 private static final DateFormat formatter = new SimpleDateFormat("HH:mm:ss.SSS");
41 public static final String SIGNAL_READY = "READY";
42 public static final String SIGNAL_SHUTDOWN = "SHUTDOWN";
45 public void TestSimpleSignal() throws TimeoutException {
47 Signal mySignal = new Signal(Thread.currentThread());
48 mySignal.setTimeout(5000L);
49 Fred fred = new Fred(mySignal);
50 Thread t1 = new Thread(fred);
53 * Verify that fred is dead, then start him and wait for him to signal us he is ready to proceed
55 assertFalse(t1.isAlive());
57 System.out.println(formatter.format(new Date()) + " MAIN: Waiting for Ready...");
58 mySignal.waitFor(SIGNAL_READY);
59 System.out.println(formatter.format(new Date()) + " MAIN: Signal Ready received");
62 * Verify that fred is still alive and we will sleep for a while (simulate doing things)
64 assertTrue(t1.isAlive());
67 } catch (InterruptedException e) {
72 * Verify that fred is still alive and signal him to shutdown
74 assertTrue(t1.isAlive());
75 System.out.println(formatter.format(new Date()) + " MAIN: Signaling shutdown");
76 fred.getSignal().signal(SIGNAL_SHUTDOWN);
83 } catch (InterruptedException e) {
88 * Verify that fred is dead now and that he completed normally
90 System.out.println(formatter.format(new Date()) + " MAIN: Shutting down...");
91 assertFalse(t1.isAlive());
92 assertTrue(fred.isCompleted());
95 public class Fred implements Runnable {
96 private Signal signal;
97 private Signal parentSignal;
98 private boolean completed = false;
100 public Fred(Signal parentSignal) {
101 this.parentSignal = parentSignal;
106 signal = new Signal(Thread.currentThread());
107 signal.setTimeout(5000L);
110 } catch (InterruptedException e) {
114 System.out.println(formatter.format(new Date()) + " FRED: Signaling ready...");
115 parentSignal.signal(SIGNAL_READY);
118 System.out.println(formatter.format(new Date()) + " FRED: Waiting for shutdown...");
119 signal.waitFor(SIGNAL_SHUTDOWN);
120 System.out.println(formatter.format(new Date()) + " FRED: Received shutdown");
122 } catch (TimeoutException e) {
127 public boolean isCompleted() {
131 public Signal getSignal() {