6
6
import java .net .InetSocketAddress ;
7
7
import java .net .StandardProtocolFamily ;
8
8
import java .net .UnixDomainSocketAddress ;
9
- import java .nio .ByteBuffer ;
10
9
import java .nio .channels .ServerSocketChannel ;
11
10
import java .nio .channels .SocketChannel ;
12
11
import java .nio .file .Files ;
18
17
public class TunnelingJdkSocketTest {
19
18
20
19
private static final AtomicBoolean is_server_running = new AtomicBoolean (false );
20
+ private final int client_timeout = 1000 ;
21
+ private final int test_timeout = 3000 ;
21
22
22
23
@ Test
23
24
public void testTimeout () throws Exception {
24
- // set socket path and address
25
25
Path socketPath = getSocketPath ();
26
26
UnixDomainSocketAddress socketAddress = UnixDomainSocketAddress .of (socketPath );
27
-
28
- // start server in a separate thread
29
- startServer (socketAddress , false );
30
-
31
- // create client
27
+ startServer (socketAddress );
32
28
TunnelingJdkSocket clientSocket = createClient (socketPath );
33
29
34
- // will fail after three seconds if timeout (set to one second) is not supported
35
- assertTimeoutPreemptively ( Duration .ofMillis (3000 ), () -> clientSocket .getInputStream ().read ());
30
+ assertTimeoutPreemptively (
31
+ Duration .ofMillis (test_timeout ), () -> clientSocket .getInputStream ().read ());
36
32
37
- // clean up
38
33
clientSocket .close ();
39
34
is_server_running .set (false );
40
35
}
@@ -46,7 +41,7 @@ private Path getSocketPath() throws IOException {
46
41
return socketPath ;
47
42
}
48
43
49
- private static void startServer (UnixDomainSocketAddress socketAddress , boolean sendMessage ) {
44
+ private static void startServer (UnixDomainSocketAddress socketAddress ) {
50
45
Thread serverThread =
51
46
new Thread (
52
47
() -> {
@@ -55,12 +50,8 @@ private static void startServer(UnixDomainSocketAddress socketAddress, boolean s
55
50
serverChannel .bind (socketAddress );
56
51
is_server_running .set (true );
57
52
58
- // wait for client connection
59
53
while (is_server_running .get ()) {
60
54
SocketChannel clientChannel = serverChannel .accept ();
61
- if (sendMessage ) {
62
- clientChannel .write (ByteBuffer .wrap ("Hello!" .getBytes ()));
63
- }
64
55
}
65
56
} catch (IOException e ) {
66
57
throw new RuntimeException (e );
@@ -72,7 +63,7 @@ private static void startServer(UnixDomainSocketAddress socketAddress, boolean s
72
63
private TunnelingJdkSocket createClient (Path socketPath ) throws IOException {
73
64
TunnelingJdkSocket clientSocket = new TunnelingJdkSocket (socketPath );
74
65
clientSocket .connect (new InetSocketAddress ("localhost" , 0 ));
75
- clientSocket .setSoTimeout (1000 );
66
+ clientSocket .setSoTimeout (client_timeout );
76
67
return clientSocket ;
77
68
}
78
69
}
0 commit comments