2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.openecomp.appc.concurrent;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
27 import java.text.DateFormat;
28 import java.text.SimpleDateFormat;
29 import java.util.Date;
30 import java.util.concurrent.TimeoutException;
32 import org.junit.Test;
33 import org.openecomp.appc.concurrent.Signal;
35 public class TestSignal {
37 private static final DateFormat formatter = new SimpleDateFormat("HH:mm:ss.SSS");
38 public static final String SIGNAL_READY = "READY";
39 public static final String SIGNAL_SHUTDOWN = "SHUTDOWN";
42 public void TestSimpleSignal() throws TimeoutException {
44 Signal mySignal = new Signal(Thread.currentThread());
45 mySignal.setTimeout(5000L);
46 Fred fred = new Fred(mySignal);
47 Thread t1 = new Thread(fred);
50 * Verify that fred is dead, then start him and wait for him to signal us he is ready to proceed
52 assertFalse(t1.isAlive());
54 System.out.println(formatter.format(new Date()) + " MAIN: Waiting for Ready...");
55 mySignal.waitFor(SIGNAL_READY);
56 System.out.println(formatter.format(new Date()) + " MAIN: Signal Ready received");
59 * Verify that fred is still alive and we will sleep for a while (simulate doing things)
61 assertTrue(t1.isAlive());
64 } catch (InterruptedException e) {
69 * Verify that fred is still alive and signal him to shutdown
71 assertTrue(t1.isAlive());
72 System.out.println(formatter.format(new Date()) + " MAIN: Signaling shutdown");
73 fred.getSignal().signal(SIGNAL_SHUTDOWN);
80 } catch (InterruptedException e) {
85 * Verify that fred is dead now and that he completed normally
87 System.out.println(formatter.format(new Date()) + " MAIN: Shutting down...");
88 assertFalse(t1.isAlive());
89 assertTrue(fred.isCompleted());
92 public class Fred implements Runnable {
93 private Signal signal;
94 private Signal parentSignal;
95 private boolean completed = false;
97 public Fred(Signal parentSignal) {
98 this.parentSignal = parentSignal;
103 signal = new Signal(Thread.currentThread());
104 signal.setTimeout(5000L);
107 } catch (InterruptedException e) {
111 System.out.println(formatter.format(new Date()) + " FRED: Signaling ready...");
112 parentSignal.signal(SIGNAL_READY);
115 System.out.println(formatter.format(new Date()) + " FRED: Waiting for shutdown...");
116 signal.waitFor(SIGNAL_SHUTDOWN);
117 System.out.println(formatter.format(new Date()) + " FRED: Received shutdown");
119 } catch (TimeoutException e) {
124 public boolean isCompleted() {
128 public Signal getSignal() {