2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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 * ============LICENSE_END=========================================================
24 package org.onap.appc.concurrent;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertTrue;
29 import java.text.DateFormat;
30 import java.text.SimpleDateFormat;
31 import java.util.Date;
32 import java.util.concurrent.TimeoutException;
34 import org.junit.Test;
35 import org.onap.appc.concurrent.Signal;
37 public class TestSignal {
39 private static final DateFormat formatter = new SimpleDateFormat("HH:mm:ss.SSS");
40 public static final String SIGNAL_READY = "READY";
41 public static final String SIGNAL_SHUTDOWN = "SHUTDOWN";
44 public void TestSimpleSignal() throws TimeoutException {
46 Signal mySignal = new Signal(Thread.currentThread());
47 mySignal.setTimeout(5000L);
48 Fred fred = new Fred(mySignal);
49 Thread t1 = new Thread(fred);
52 * Verify that fred is dead, then start him and wait for him to signal us he is ready to proceed
54 assertFalse(t1.isAlive());
56 System.out.println(formatter.format(new Date()) + " MAIN: Waiting for Ready...");
57 mySignal.waitFor(SIGNAL_READY);
58 System.out.println(formatter.format(new Date()) + " MAIN: Signal Ready received");
61 * Verify that fred is still alive and we will sleep for a while (simulate doing things)
63 assertTrue(t1.isAlive());
66 } catch (InterruptedException e) {
71 * Verify that fred is still alive and signal him to shutdown
73 assertTrue(t1.isAlive());
74 System.out.println(formatter.format(new Date()) + " MAIN: Signaling shutdown");
75 fred.getSignal().signal(SIGNAL_SHUTDOWN);
82 } catch (InterruptedException e) {
87 * Verify that fred is dead now and that he completed normally
89 System.out.println(formatter.format(new Date()) + " MAIN: Shutting down...");
90 assertFalse(t1.isAlive());
91 assertTrue(fred.isCompleted());
94 public class Fred implements Runnable {
95 private Signal signal;
96 private Signal parentSignal;
97 private boolean completed = false;
99 public Fred(Signal parentSignal) {
100 this.parentSignal = parentSignal;
105 signal = new Signal(Thread.currentThread());
106 signal.setTimeout(5000L);
109 } catch (InterruptedException e) {
113 System.out.println(formatter.format(new Date()) + " FRED: Signaling ready...");
114 parentSignal.signal(SIGNAL_READY);
117 System.out.println(formatter.format(new Date()) + " FRED: Waiting for shutdown...");
118 signal.waitFor(SIGNAL_SHUTDOWN);
119 System.out.println(formatter.format(new Date()) + " FRED: Received shutdown");
121 } catch (TimeoutException e) {
126 public boolean isCompleted() {
130 public Signal getSignal() {