1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty5.channel.unix.tests;
17
18
19 import java.lang.reflect.Method;
20 import java.net.InetAddress;
21 import java.net.InetSocketAddress;
22 import java.net.SocketAddress;
23
24 import static org.junit.jupiter.api.Assumptions.assumeTrue;
25
26 public final class UnixTestUtils {
27 private static final Object INET_LOOPBACK_UNAVAILABLE = new Object();
28 private static volatile Object inetLoopbackCache;
29
30
31
32
33
34
35 public static SocketAddress newInetLoopbackSocketAddress() {
36 Object loopback = inetLoopbackCache;
37
38 if (loopback == null) {
39 inetLoopbackCache = loopback = getLoopbackAddress();
40 }
41
42 assumeTrue(loopback != INET_LOOPBACK_UNAVAILABLE, "InetAddress.getLoopbackAddress() is not available");
43 return new InetSocketAddress((InetAddress) loopback, 0);
44 }
45
46 private static Object getLoopbackAddress() {
47 try {
48 Method method = InetAddress.class.getMethod("getLoopbackAddress");
49 return method.invoke(null);
50 } catch (Exception ignore) {
51 return INET_LOOPBACK_UNAVAILABLE;
52 }
53 }
54
55 private UnixTestUtils() { }
56 }