|
3 | 3 | import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
|
4 | 4 |
|
5 | 5 | import java.io.IOException;
|
| 6 | +import java.net.InetSocketAddress; |
| 7 | +import java.net.StandardProtocolFamily; |
6 | 8 | import java.net.UnixDomainSocketAddress;
|
| 9 | +import java.nio.ByteBuffer; |
7 | 10 | import java.nio.channels.ServerSocketChannel;
|
| 11 | +import java.nio.channels.SocketChannel; |
8 | 12 | import java.nio.file.Files;
|
9 | 13 | import java.nio.file.Path;
|
10 | 14 | import java.time.Duration;
|
11 | 15 | import java.util.concurrent.atomic.AtomicBoolean;
|
12 |
| -import org.junit.jupiter.api.Assertions; |
13 | 16 | import org.junit.jupiter.api.Test;
|
14 | 17 |
|
15 | 18 | public class TunnelingJdkSocketTest {
|
16 | 19 |
|
17 |
| - private final AtomicBoolean running = new AtomicBoolean(false); |
| 20 | + private static final AtomicBoolean is_server_running = new AtomicBoolean(false); |
18 | 21 |
|
19 | 22 | @Test
|
20 | 23 | public void testTimeout() throws Exception {
|
21 |
| - Assertions.assertEquals(1 + 1, 2); |
22 |
| - |
23 |
| - // set test socket path |
| 24 | + // set socket path and address |
24 | 25 | Path socketPath = getSocketPath();
|
25 |
| - // start server |
26 |
| - startServer(socketPath); |
| 26 | + UnixDomainSocketAddress socketAddress = UnixDomainSocketAddress.of(socketPath); |
27 | 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 |
| - } |
| 28 | + // start server in a separate thread |
| 29 | + startServer(socketAddress, false); |
38 | 30 |
|
39 |
| - // create client socket |
| 31 | + // create client |
40 | 32 | TunnelingJdkSocket clientSocket = createClient(socketPath);
|
41 | 33 |
|
42 |
| - // attempt to read from empty socket (read should block indefinitely) |
43 |
| - System.out.println("Test is starting..."); |
44 |
| - assertTimeoutPreemptively(Duration.ofSeconds(5), () -> clientSocket.getInputStream().read()); |
| 34 | + // expect a failure after three seconds because timeout is not supported yet |
| 35 | + assertTimeoutPreemptively(Duration.ofMillis(3000), () -> clientSocket.getInputStream().read()); |
45 | 36 |
|
46 |
| - // clean up client, server, and path |
| 37 | + // clean up |
47 | 38 | clientSocket.close();
|
48 |
| - running.set(false); |
49 |
| - Files.deleteIfExists(socketPath); |
50 |
| - System.out.println("Client, server, and path cleaned."); |
| 39 | + is_server_running.set(false); |
51 | 40 | }
|
52 | 41 |
|
53 | 42 | private Path getSocketPath() throws IOException {
|
54 |
| - Path socketPath = Files.createTempFile("testSocket", ".sock"); |
| 43 | + Path socketPath = Files.createTempFile("testSocket", null); |
55 | 44 | Files.delete(socketPath);
|
56 | 45 | socketPath.toFile().deleteOnExit();
|
57 | 46 | return socketPath;
|
58 | 47 | }
|
59 | 48 |
|
60 |
| - private void startServer(Path socketPath) { |
| 49 | + private static void startServer(UnixDomainSocketAddress socketAddress, boolean sendMessage) { |
61 | 50 | Thread serverThread =
|
62 | 51 | new Thread(
|
63 | 52 | () -> {
|
64 |
| - // open and bind server to socketPath |
65 |
| - try (ServerSocketChannel serverChannel = ServerSocketChannel.open()) { |
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)); |
70 |
| - // accept connections made to the server |
71 |
| - running.set(true); |
72 |
| - System.out.println("Server is running and ready to accept connections."); |
73 |
| - while (running.get()) { |
74 |
| - serverChannel.accept(); |
75 |
| - System.out.println("Server is accepting connections."); |
| 53 | + try (ServerSocketChannel serverChannel = |
| 54 | + ServerSocketChannel.open(StandardProtocolFamily.UNIX)) { |
| 55 | + serverChannel.bind(socketAddress); |
| 56 | + is_server_running.set(true); |
| 57 | + |
| 58 | + // wait for client connection |
| 59 | + while (is_server_running.get()) { |
| 60 | + SocketChannel clientChannel = serverChannel.accept(); |
| 61 | + if (sendMessage) { |
| 62 | + clientChannel.write(ByteBuffer.wrap("Hello!".getBytes())); |
| 63 | + } |
76 | 64 | }
|
77 | 65 | } 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 |
| - } |
86 | 66 | throw new RuntimeException(e);
|
87 | 67 | }
|
88 | 68 | });
|
89 |
| - |
90 |
| - // start server in separate thread |
91 | 69 | serverThread.start();
|
92 | 70 | }
|
93 | 71 |
|
94 | 72 | private TunnelingJdkSocket createClient(Path socketPath) throws IOException {
|
95 |
| - // create client socket |
96 | 73 | TunnelingJdkSocket clientSocket = new TunnelingJdkSocket(socketPath);
|
97 |
| - // set timeout to one second |
| 74 | + clientSocket.connect(new InetSocketAddress("localhost", 0)); |
98 | 75 | 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 |
| - |
107 | 76 | return clientSocket;
|
108 | 77 | }
|
109 | 78 | }
|
0 commit comments