Moving all files to root directory
[appc.git] / appc-common / src / test / java / org / openecomp / appc / concurrent / TestSignal.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
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
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
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=========================================================
20  */
21
22 package org.openecomp.appc.concurrent;
23
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
26
27 import java.text.DateFormat;
28 import java.text.SimpleDateFormat;
29 import java.util.Date;
30 import java.util.concurrent.TimeoutException;
31
32 import org.junit.Test;
33 import org.openecomp.appc.concurrent.Signal;
34
35 public class TestSignal {
36
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";
40
41     @Test
42     public void TestSimpleSignal() throws TimeoutException {
43
44         Signal mySignal = new Signal(Thread.currentThread());
45         mySignal.setTimeout(5000L);
46         Fred fred = new Fred(mySignal);
47         Thread t1 = new Thread(fred);
48
49         /*
50          * Verify that fred is dead, then start him and wait for him to signal us he is ready to proceed
51          */
52         assertFalse(t1.isAlive());
53         t1.start();
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");
57
58         /*
59          * Verify that fred is still alive and we will sleep for a while (simulate doing things)
60          */
61         assertTrue(t1.isAlive());
62         try {
63             Thread.sleep(250L);
64         } catch (InterruptedException e) {
65             // ignored
66         }
67
68         /*
69          * Verify that fred is still alive and signal him to shutdown
70          */
71         assertTrue(t1.isAlive());
72         System.out.println(formatter.format(new Date()) + " MAIN: Signaling shutdown");
73         fred.getSignal().signal(SIGNAL_SHUTDOWN);
74
75         /*
76          * Wait a little bit
77          */
78         try {
79             Thread.sleep(250L);
80         } catch (InterruptedException e) {
81             // ignored
82         }
83
84         /*
85          * Verify that fred is dead now and that he completed normally
86          */
87         System.out.println(formatter.format(new Date()) + " MAIN: Shutting down...");
88         assertFalse(t1.isAlive());
89         assertTrue(fred.isCompleted());
90     }
91
92     public class Fred implements Runnable {
93         private Signal signal;
94         private Signal parentSignal;
95         private boolean completed = false;
96
97         public Fred(Signal parentSignal) {
98             this.parentSignal = parentSignal;
99         }
100
101         @Override
102         public void run() {
103             signal = new Signal(Thread.currentThread());
104             signal.setTimeout(5000L);
105             try {
106                 Thread.sleep(250L);
107             } catch (InterruptedException e) {
108                 // Ignore
109             }
110
111             System.out.println(formatter.format(new Date()) + " FRED: Signaling ready...");
112             parentSignal.signal(SIGNAL_READY);
113
114             try {
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");
118                 completed = true;
119             } catch (TimeoutException e) {
120                 e.printStackTrace();
121             }
122         }
123
124         public boolean isCompleted() {
125             return completed;
126         }
127
128         public Signal getSignal() {
129             return signal;
130         }
131     }
132 }