|
1 | 1 | package zmq.io.net.tcp; |
2 | 2 |
|
3 | 3 | import java.io.IOException; |
4 | | -import java.lang.reflect.InvocationTargetException; |
5 | | -import java.lang.reflect.Method; |
6 | | -import java.net.ServerSocket; |
7 | | -import java.net.Socket; |
8 | | -import java.net.SocketOption; |
9 | 4 | import java.nio.channels.ServerSocketChannel; |
10 | 5 | import java.nio.channels.SocketChannel; |
11 | 6 |
|
12 | 7 | import org.junit.Assert; |
13 | 8 | import org.junit.Test; |
14 | 9 |
|
15 | | -import zmq.Options; |
| 10 | +import jdk.net.ExtendedSocketOptions; |
16 | 11 |
|
17 | 12 | public class KeepAliveTest |
18 | 13 | { |
19 | 14 | @Test |
20 | 15 | public void testConsistent() throws IOException |
21 | 16 | { |
22 | | - try { |
23 | | - int javaspec = Integer.parseInt(System.getProperty("java.specification.version")); |
24 | | - // Test fails for jvm lest that 13 |
25 | | - if (javaspec >= 13) { |
26 | | - Class<?> eso = Options.class.getClassLoader().loadClass("jdk.net.ExtendedSocketOptions"); |
27 | | - SocketOption<Integer> count = (SocketOption<Integer>) eso.getField("TCP_KEEPCOUNT").get(null); |
28 | | - SocketOption<Integer> idle = (SocketOption<Integer>) eso.getField("TCP_KEEPIDLE").get(null); |
29 | | - SocketOption<Integer> interval = (SocketOption<Integer>) eso.getField("TCP_KEEPINTERVAL").get(null); |
30 | | - Method socketgetOption = Socket.class.getMethod("getOption", SocketOption.class); |
31 | | - Method serversocketgetOption = ServerSocket.class.getMethod("getOption", SocketOption.class); |
32 | | - Assert.assertTrue(TcpUtils.WITH_EXTENDED_KEEPALIVE); |
| 17 | + int javaspec = Integer.parseInt(System.getProperty("java.specification.version")); |
| 18 | + // Test fails for jvm less than 13 |
| 19 | + if (javaspec >= 13) { |
| 20 | + SocketChannel sc = SocketChannel.open(); |
| 21 | + TcpUtils.tuneTcpKeepalives(sc, 1, 2, 3, 4); |
| 22 | + Assert.assertEquals(2, (int) sc.socket().getOption(ExtendedSocketOptions.TCP_KEEPCOUNT)); |
| 23 | + Assert.assertEquals(3, (int) sc.socket().getOption(ExtendedSocketOptions.TCP_KEEPIDLE)); |
| 24 | + Assert.assertEquals(4, (int) sc.socket().getOption(ExtendedSocketOptions.TCP_KEEPINTERVAL)); |
33 | 25 |
|
34 | | - SocketChannel sc = SocketChannel.open(); |
35 | | - TcpUtils.tuneTcpKeepalives(sc, 1, 2, 3, 4); |
36 | | - Assert.assertEquals(2, socketgetOption.invoke(sc.socket(), count)); |
37 | | - Assert.assertEquals(3, socketgetOption.invoke(sc.socket(), idle)); |
38 | | - Assert.assertEquals(4, socketgetOption.invoke(sc.socket(), interval)); |
39 | | - |
40 | | - ServerSocketChannel ssc = ServerSocketChannel.open(); |
41 | | - TcpUtils.tuneTcpKeepalives(ssc, 1, 2, 3, 4); |
42 | | - Assert.assertEquals(2, serversocketgetOption.invoke(ssc.socket(), count)); |
43 | | - Assert.assertEquals(3, serversocketgetOption.invoke(ssc.socket(), idle)); |
44 | | - Assert.assertEquals(4, serversocketgetOption.invoke(ssc.socket(), interval)); |
45 | | - } |
46 | | - } |
47 | | - catch (NumberFormatException ex) { |
48 | | - // java 1.8, not a problem as keepalive is not handled |
| 26 | + ServerSocketChannel ssc = ServerSocketChannel.open(); |
| 27 | + TcpUtils.tuneTcpKeepalives(ssc, 1, 2, 3, 4); |
| 28 | + Assert.assertEquals(2, (int) sc.socket().getOption(ExtendedSocketOptions.TCP_KEEPCOUNT)); |
| 29 | + Assert.assertEquals(3, (int) sc.socket().getOption(ExtendedSocketOptions.TCP_KEEPIDLE)); |
| 30 | + Assert.assertEquals(4, (int) sc.socket().getOption(ExtendedSocketOptions.TCP_KEEPINTERVAL)); |
49 | 31 | } |
50 | | - catch (NoSuchMethodException | ClassNotFoundException | NoSuchFieldException | IllegalAccessException | |
51 | | - InvocationTargetException e) { |
52 | | - // Not defined, just skip the test |
53 | | - } |
54 | | - |
55 | 32 | } |
56 | 33 | } |
0 commit comments