Fix request handler class error
[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  * Modification Copyright (C) 2018 IBM
10  * =============================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  * 
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  * 
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  * 
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.appc.concurrent;
27
28 import static org.junit.Assert.assertFalse;
29 import static org.junit.Assert.assertTrue;
30 import static org.junit.Assert.assertEquals;
31 import java.text.DateFormat;
32 import java.text.SimpleDateFormat;
33 import java.util.Date;
34 import java.util.concurrent.TimeoutException;
35
36 import org.junit.Test;
37 import org.onap.appc.concurrent.Signal;
38
39
40
41 public class TestSignal {
42
43     private static final DateFormat formatter = new SimpleDateFormat("HH:mm:ss.SSS");
44     public static final String SIGNAL_READY = "READY";
45     public static final String SIGNAL_SHUTDOWN = "SHUTDOWN";
46
47     @Test
48     public void TestSimpleSignal() throws TimeoutException {
49
50         Signal mySignal = new Signal(Thread.currentThread());
51         mySignal.setTimeout(5000L);
52         Fred fred = new Fred(mySignal);
53         Thread t1 = new Thread(fred);
54
55         /*
56          * Verify that fred is dead, then start him and wait for him to signal us he is ready to proceed
57          */
58         assertFalse(t1.isAlive());
59         t1.start();
60         System.out.println(formatter.format(new Date()) + " MAIN: Waiting for Ready...");
61         mySignal.waitFor(SIGNAL_READY);
62         System.out.println(formatter.format(new Date()) + " MAIN: Signal Ready received");
63
64         /*
65          * Verify that fred is still alive and we will sleep for a while (simulate doing things)
66          */
67         assertTrue(t1.isAlive());
68         try {
69             Thread.sleep(250L);
70         } catch (InterruptedException e) {
71             // ignored
72         }
73
74         /*
75          * Verify that fred is still alive and signal him to shutdown
76          */
77         assertTrue(t1.isAlive());
78         System.out.println(formatter.format(new Date()) + " MAIN: Signaling shutdown");
79         fred.getSignal().signal(SIGNAL_SHUTDOWN);
80
81         /*
82          * Wait a little bit
83          */
84         try {
85             Thread.sleep(250L);
86         } catch (InterruptedException e) {
87             // ignored
88         }
89
90         /*
91          * Verify that fred is dead now and that he completed normally
92          */
93         System.out.println(formatter.format(new Date()) + " MAIN: Shutting down...");
94         assertFalse(t1.isAlive());
95         assertTrue(fred.isCompleted());
96     }
97
98     public class Fred implements Runnable {
99         private Signal signal;
100         private Signal parentSignal;
101         private boolean completed = false;
102
103         public Fred(Signal parentSignal) {
104             this.parentSignal = parentSignal;
105         }
106
107         @Override
108         public void run() {
109             signal = new Signal(Thread.currentThread());
110             signal.setTimeout(5000L);
111             try {
112                 Thread.sleep(250L);
113             } catch (InterruptedException e) {
114                 // Ignore
115             }
116
117             System.out.println(formatter.format(new Date()) + " FRED: Signaling ready...");
118             parentSignal.signal(SIGNAL_READY);
119
120             try {
121                 System.out.println(formatter.format(new Date()) + " FRED: Waiting for shutdown...");
122                 signal.waitFor(SIGNAL_SHUTDOWN);
123                 System.out.println(formatter.format(new Date()) + " FRED: Received shutdown");
124                 completed = true;
125             } catch (TimeoutException e) {
126                 
127             }
128         }
129
130         public boolean isCompleted() {
131             return completed;
132         }
133
134         public Signal getSignal() {
135             return signal;
136         }
137     }
138     
139     @Test
140     public void testWaitForAny() throws Exception
141     {
142         Signal mySignal = new Signal(Thread.currentThread());
143         mySignal.setTimeout(50L);
144         String receivedSignal= mySignal.waitForAny(SIGNAL_READY);
145         assertEquals("READY", receivedSignal);
146     }
147     
148     @Test(expected=TimeoutException.class)
149     public void testWaitForAnyForEmptySignal() throws TimeoutException
150     {
151         Signal mySignal = new Signal(Thread.currentThread());
152         mySignal.setTimeout(50L);
153         mySignal.waitForAny();
154     }
155     
156     @Test
157     public void testIsSignaled() throws TimeoutException
158     {
159         Signal mySignal = new Signal(Thread.currentThread());
160         assertFalse(mySignal.isSignaled(SIGNAL_READY));
161         
162     }
163 }