3
3
import static org .junit .jupiter .api .Assertions .assertTimeoutPreemptively ;
4
4
5
5
import java .io .IOException ;
6
+ import java .net .UnixDomainSocketAddress ;
6
7
import java .nio .channels .ServerSocketChannel ;
7
8
import java .nio .file .Files ;
8
9
import java .nio .file .Path ;
9
10
import java .time .Duration ;
10
11
import java .util .concurrent .atomic .AtomicBoolean ;
11
- import jnr .unixsocket .UnixSocketAddress ;
12
12
import org .junit .jupiter .api .Assertions ;
13
13
import org .junit .jupiter .api .Test ;
14
14
@@ -18,22 +18,43 @@ public class TunnelingJdkSocketTest {
18
18
19
19
@ Test
20
20
public void testTimeout () throws Exception {
21
- Assertions .assertEquals (1 + 1 , 3 ); // should fail
21
+ Assertions .assertEquals (1 + 1 , 2 );
22
22
23
- // create test path
24
- Path socketPath = Files . createTempFile ( "testSocket" , null );
23
+ // set test socket path
24
+ Path socketPath = getSocketPath ( );
25
25
// start server
26
26
startServer (socketPath );
27
+
28
+ // timeout after two seconds if server doesn't start
29
+ long startTime = System .currentTimeMillis ();
30
+ long timeout = 2000 ;
31
+ while (!running .get ()) {
32
+ Thread .sleep (100 );
33
+ if (System .currentTimeMillis () - startTime > timeout ) {
34
+ System .out .println ("Timeout waiting for server to start." );
35
+ break ;
36
+ }
37
+ }
38
+
27
39
// create client socket
28
40
TunnelingJdkSocket clientSocket = createClient (socketPath );
29
41
30
42
// attempt to read from empty socket (read should block indefinitely)
43
+ System .out .println ("Test is starting..." );
31
44
assertTimeoutPreemptively (Duration .ofSeconds (5 ), () -> clientSocket .getInputStream ().read ());
32
45
33
46
// clean up client, server, and path
34
47
clientSocket .close ();
35
48
running .set (false );
36
49
Files .deleteIfExists (socketPath );
50
+ System .out .println ("Client, server, and path cleaned." );
51
+ }
52
+
53
+ private Path getSocketPath () throws IOException {
54
+ Path socketPath = Files .createTempFile ("testSocket" , ".sock" );
55
+ Files .delete (socketPath );
56
+ socketPath .toFile ().deleteOnExit ();
57
+ return socketPath ;
37
58
}
38
59
39
60
private void startServer (Path socketPath ) {
@@ -42,13 +63,26 @@ private void startServer(Path socketPath) {
42
63
() -> {
43
64
// open and bind server to socketPath
44
65
try (ServerSocketChannel serverChannel = ServerSocketChannel .open ()) {
45
- serverChannel .socket ().bind (new UnixSocketAddress (socketPath .toFile ()));
66
+ System .out .println ("serverChannel is open." );
67
+ serverChannel .configureBlocking (false );
68
+ System .out .println ("serverChannel is not blocking." );
69
+ serverChannel .socket ().bind (UnixDomainSocketAddress .of (socketPath ));
46
70
// accept connections made to the server
47
71
running .set (true );
72
+ System .out .println ("Server is running and ready to accept connections." );
48
73
while (running .get ()) {
49
74
serverChannel .accept ();
75
+ System .out .println ("Server is accepting connections." );
50
76
}
51
77
} catch (IOException e ) {
78
+ System .out .println ("Server encountered error with accepting a connection." );
79
+ // clean up server and path
80
+ running .set (false );
81
+ try {
82
+ Files .deleteIfExists (socketPath );
83
+ } catch (IOException ex ) {
84
+ throw new RuntimeException (ex );
85
+ }
52
86
throw new RuntimeException (e );
53
87
}
54
88
});
@@ -62,6 +96,14 @@ private TunnelingJdkSocket createClient(Path socketPath) throws IOException {
62
96
TunnelingJdkSocket clientSocket = new TunnelingJdkSocket (socketPath );
63
97
// set timeout to one second
64
98
clientSocket .setSoTimeout (1000 );
99
+ System .out .println ("Client set timeout." );
100
+
101
+ if (clientSocket .isConnected ()) {
102
+ System .out .println ("Client connected successfully." );
103
+ } else {
104
+ System .out .println ("Client failed to connect." );
105
+ }
106
+
65
107
return clientSocket ;
66
108
}
67
109
}
0 commit comments