-
Notifications
You must be signed in to change notification settings - Fork 918
/
NetworkHealthTest.java
339 lines (263 loc) · 9.91 KB
/
NetworkHealthTest.java
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.activemq.artemis.utils;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.URL;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import org.apache.activemq.artemis.core.server.ActiveMQComponent;
import org.apache.activemq.artemis.core.server.NetworkHealthCheck;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assume.assumeTrue;
public class NetworkHealthTest {
private static final InetAddress INVALID_ADDRESS;
private static String IPV6_LOCAL = "::1";
static {
InetAddress address = null;
try {
address = InetAddress.getByName("203.0.113.1");
} catch (Exception e) {
e.printStackTrace();
}
INVALID_ADDRESS = address;
}
Set<NetworkHealthCheck> list = new HashSet<>();
NetworkHealthCheck addCheck(NetworkHealthCheck check) {
list.add(check);
return check.setIgnoreLoopback(true);
}
HttpServer httpServer;
final ReusableLatch latch = new ReusableLatch(1);
ActiveMQComponent component = new ActiveMQComponent() {
boolean started = true;
@Override
public void start() throws Exception {
started = true;
latch.countDown();
}
@Override
public void stop() throws Exception {
started = false;
latch.countDown();
}
@Override
public boolean isStarted() {
return started;
}
};
@Before
public void before() throws Exception {
latch.setCount(1);
}
private void startHTTPServer() throws IOException {
Assert.assertNull(httpServer);
InetSocketAddress address = new InetSocketAddress("127.0.0.1", 8787);
httpServer = HttpServer.create(address, 100);
httpServer.start();
httpServer.createContext("/", new HttpHandler() {
@Override
public void handle(HttpExchange t) throws IOException {
String response = "<html><body><b>This is a unit test</b></body></html>";
t.sendResponseHeaders(200, response.length());
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
}
});
}
private void stopHTTPServer() {
if (httpServer != null) {
try {
httpServer.stop(0);
} catch (Throwable ignored) {
}
httpServer = null;
}
}
@After
public void after() {
stopHTTPServer();
for (NetworkHealthCheck check : this.list) {
check.stop();
}
}
@Test
public void testCheck6() throws Exception {
assumeTrue(purePingWorks(IPV6_LOCAL));
NetworkHealthCheck check = addCheck(new NetworkHealthCheck(null, 100, 100));
check.addComponent(component);
InetAddress address = InetAddress.getByName(IPV6_LOCAL);
Assert.assertTrue(address instanceof Inet6Address);
Assert.assertTrue(check.purePing(address));
Assert.assertTrue(check.check(address));
}
@Test
public void testAlreadyShutdown() throws Exception {
assumeTrue(purePingWorks(IPV6_LOCAL));
ReusableLatch latch = new ReusableLatch(0);
NetworkHealthCheck check = addCheck(new NetworkHealthCheck(null, 100, 100) {
@Override
public void run() {
super.run();
latch.countDown();
}
});
check.addComponent(component);
InetAddress address = InetAddress.getByName("127.0.0.1");
check.addAddress(address);
component.stop();
latch.setCount(1);
Assert.assertTrue(latch.await(1, TimeUnit.MINUTES));
Assert.assertFalse("NetworkHealthCheck should have no business on restarting the component, the network was never down, hence no check needed!", component.isStarted());
}
@Test
public void testParseSpaces() throws Exception {
NetworkHealthCheck check = addCheck(new NetworkHealthCheck(null, 100, 100));
// using two addresses for URI and localhost
check.parseAddressList("localhost, , 127.0.0.2");
Assert.assertEquals(2, check.getAddresses().size());
}
@Test
public void testParseLogger() throws Exception {
NetworkHealthCheck check = addCheck(new NetworkHealthCheck(null, 100, 100));
// using two addresses for URI and localhost
check.parseAddressList("localhost, , 127.0.0.2");
Assert.assertEquals(2, check.getAddresses().size());
Assert.assertEquals(0, check.getUrls().size());
}
@Test
public void testPings() throws Exception {
assumeTrue(purePingWorks("127.0.0.1"));
doCheck("127.0.0.1");
}
private void doCheck(String localaddress) throws Exception {
NetworkHealthCheck check = addCheck(new NetworkHealthCheck(null, 100, 100));
check.addComponent(component);
// Any external IP, to make sure we would use a PING
InetAddress address = InetAddress.getByName(localaddress);
Assert.assertTrue(check.check(address));
Assert.assertTrue(check.purePing(address));
Assert.assertFalse(check.purePing(INVALID_ADDRESS));
}
@Test
public void testPingsIPV6() throws Exception {
assumeTrue(purePingWorks(IPV6_LOCAL));
doCheck(IPV6_LOCAL);
}
private boolean purePingWorks(String localaddress) throws Exception {
try {
return addCheck(new NetworkHealthCheck(null, 100, 100)).purePing(InetAddress.getByName(localaddress));
} catch (Exception e) {
return false;
}
}
@Test
public void testCheckNoNodes() throws Exception {
NetworkHealthCheck check = addCheck(new NetworkHealthCheck());
Assert.assertTrue(check.check());
}
@Test
public void testCheckUsingHTTP() throws Exception {
startHTTPServer();
NetworkHealthCheck check = addCheck(new NetworkHealthCheck(null, 100, 1000));
Assert.assertTrue(check.check(new URL("http://localhost:8787")));
stopHTTPServer();
Assert.assertFalse(check.check(new URL("http://localhost:8787")));
check.addComponent(component);
URL url = new URL("http://localhost:8787");
Assert.assertFalse(check.check(url));
startHTTPServer();
Assert.assertTrue(check.check(url));
check.addURL(url);
Assert.assertFalse(latch.await(500, TimeUnit.MILLISECONDS));
Assert.assertTrue(component.isStarted());
// stopping the web server should stop the component
stopHTTPServer();
Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));
Assert.assertFalse(component.isStarted());
latch.setCount(1);
startHTTPServer();
Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));
Assert.assertTrue(component.isStarted());
}
@Test
public void testValidateCommand() throws Throwable {
AtomicInteger purePing = new AtomicInteger(0);
AtomicInteger isReacheable = new AtomicInteger(0);
NetworkHealthCheck myCheck =
new NetworkHealthCheck() {
@Override
protected boolean isReachable(InetAddress address) throws IOException {
isReacheable.incrementAndGet();
return false;
}
@Override
public boolean purePing(InetAddress address) throws IOException, InterruptedException {
purePing.incrementAndGet();
return false;
}
};
myCheck.setIpv4Command("whatever");
myCheck.setIpv6Command("whatever");
myCheck.check(InetAddress.getByName("127.0.0.1"));
Assert.assertEquals(0, isReacheable.get());
Assert.assertEquals(1, purePing.get());
}
@Test
public void testValidateIsReachable() throws Throwable {
AtomicInteger purePing = new AtomicInteger(0);
AtomicInteger isReacheable = new AtomicInteger(0);
NetworkHealthCheck myCheck =
new NetworkHealthCheck() {
@Override
protected boolean isReachable(InetAddress address) throws IOException {
isReacheable.incrementAndGet();
return true;
}
@Override
public boolean purePing(InetAddress address) throws IOException, InterruptedException {
purePing.incrementAndGet();
return false;
}
};
//myCheck.setIpv4Command("whatever");
//myCheck.setIpv6Command("whatever");
myCheck.check(InetAddress.getByName("127.0.0.1"));
Assert.assertEquals(1, isReacheable.get());
Assert.assertEquals(0, purePing.get());
}
@Test(timeout = 30_000)
public void testPurePingTimeout() throws Exception {
NetworkHealthCheck check = new NetworkHealthCheck(null, 100, 2000);
long time = System.currentTimeMillis();
//[RFC1166] reserves the address block 192.0.2.0/24 for test.
Assert.assertFalse(check.purePing(InetAddress.getByName("192.0.2.0")));
Assert.assertTrue(System.currentTimeMillis() - time >= 2000);
}
}