33import static org .junit .jupiter .api .Assertions .assertTimeoutPreemptively ;
44
55import java .io .IOException ;
6+ import java .net .UnixDomainSocketAddress ;
67import java .nio .channels .ServerSocketChannel ;
78import java .nio .file .Files ;
89import java .nio .file .Path ;
910import java .time .Duration ;
1011import java .util .concurrent .atomic .AtomicBoolean ;
11- import jnr .unixsocket .UnixSocketAddress ;
1212import org .junit .jupiter .api .Assertions ;
1313import org .junit .jupiter .api .Test ;
1414
@@ -18,22 +18,43 @@ public class TunnelingJdkSocketTest {
1818
1919 @ Test
2020 public void testTimeout () throws Exception {
21- Assertions .assertEquals (1 + 1 , 3 ); // should fail
21+ Assertions .assertEquals (1 + 1 , 2 );
2222
23- // create test path
24- Path socketPath = Files . createTempFile ( "testSocket" , null );
23+ // set test socket path
24+ Path socketPath = getSocketPath ( );
2525 // start server
2626 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+
2739 // create client socket
2840 TunnelingJdkSocket clientSocket = createClient (socketPath );
2941
3042 // attempt to read from empty socket (read should block indefinitely)
43+ System .out .println ("Test is starting..." );
3144 assertTimeoutPreemptively (Duration .ofSeconds (5 ), () -> clientSocket .getInputStream ().read ());
3245
3346 // clean up client, server, and path
3447 clientSocket .close ();
3548 running .set (false );
3649 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 ;
3758 }
3859
3960 private void startServer (Path socketPath ) {
@@ -42,13 +63,26 @@ private void startServer(Path socketPath) {
4263 () -> {
4364 // open and bind server to socketPath
4465 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 ));
4670 // accept connections made to the server
4771 running .set (true );
72+ System .out .println ("Server is running and ready to accept connections." );
4873 while (running .get ()) {
4974 serverChannel .accept ();
75+ System .out .println ("Server is accepting connections." );
5076 }
5177 } 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+ }
5286 throw new RuntimeException (e );
5387 }
5488 });
@@ -62,6 +96,14 @@ private TunnelingJdkSocket createClient(Path socketPath) throws IOException {
6296 TunnelingJdkSocket clientSocket = new TunnelingJdkSocket (socketPath );
6397 // set timeout to one second
6498 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+
65107 return clientSocket ;
66108 }
67109}
0 commit comments