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