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