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