|
27 | 27 | import java.net.UnixDomainSocketAddress; |
28 | 28 | import java.nio.channels.ServerSocketChannel; |
29 | 29 | import java.nio.channels.SocketChannel; |
30 | | -import java.nio.file.*; |
| 30 | +import java.nio.file.Files; |
| 31 | +import java.nio.file.Path; |
31 | 32 | import java.util.concurrent.TimeUnit; |
32 | | -import java.util.concurrent.atomic.AtomicInteger; |
33 | 33 |
|
34 | 34 | import org.openjdk.jmh.annotations.*; |
35 | 35 | import org.openjdk.jmh.runner.Runner; |
|
49 | 49 | public class SocketChannelConnectionSetup { |
50 | 50 |
|
51 | 51 | private ServerSocketChannel ssc; |
52 | | - private SocketChannel s1, s2; |
53 | | - |
54 | | - private static volatile String tempDir; |
55 | | - private static final AtomicInteger count = new AtomicInteger(0); |
56 | | - private volatile Path socket; |
57 | | - |
58 | | - @Param({"inet", "unix"}) |
59 | | - private volatile String family; |
60 | 52 |
|
61 | | - static { |
62 | | - try { |
63 | | - Path p = Files.createTempDirectory("readWriteTest"); |
64 | | - tempDir = p.toString(); |
65 | | - } catch (IOException e) { |
66 | | - tempDir = null; |
67 | | - } |
68 | | - } |
| 53 | + private Path sscFilePath; |
69 | 54 |
|
70 | | - private ServerSocketChannel getServerSocketChannel() throws IOException { |
71 | | - if (family.equals("inet")) |
72 | | - return getInetServerSocketChannel(); |
73 | | - else if (family.equals("unix")) |
74 | | - return getUnixServerSocketChannel(); |
75 | | - throw new InternalError(); |
76 | | - } |
77 | | - |
78 | | - |
79 | | - private ServerSocketChannel getInetServerSocketChannel() throws IOException { |
80 | | - return ServerSocketChannel.open().bind(null); |
81 | | - } |
| 55 | + private SocketChannel s1, s2; |
82 | 56 |
|
83 | | - private ServerSocketChannel getUnixServerSocketChannel() throws IOException { |
84 | | - int next = count.incrementAndGet(); |
85 | | - socket = Paths.get(tempDir, Integer.toString(next)); |
86 | | - UnixDomainSocketAddress addr = UnixDomainSocketAddress.of(socket); |
87 | | - return ServerSocketChannel.open(StandardProtocolFamily.UNIX).bind(addr); |
88 | | - } |
| 57 | + @Param({"INET", "UNIX"}) |
| 58 | + private String family; |
89 | 59 |
|
90 | 60 | @Setup(Level.Trial) |
91 | 61 | public void beforeRun() throws IOException { |
92 | | - ssc = getServerSocketChannel(); |
| 62 | + StandardProtocolFamily typedFamily = StandardProtocolFamily.valueOf(family); |
| 63 | + ssc = ServerSocketChannel.open(typedFamily).bind(null); |
| 64 | + // Record the UDS file path right after binding, as the socket may be |
| 65 | + // closed later due to a failure, and subsequent calls to `getPath()` |
| 66 | + // will throw. |
| 67 | + sscFilePath = ssc.getLocalAddress() instanceof UnixDomainSocketAddress udsChannel |
| 68 | + ? udsChannel.getPath() |
| 69 | + : null; |
93 | 70 | } |
94 | 71 |
|
95 | 72 | @TearDown(Level.Trial) |
96 | | - public void afterRun() throws IOException { |
| 73 | + public void afterRun() throws Exception { |
97 | 74 | ssc.close(); |
98 | | - if (family.equals("unix")) { |
99 | | - Files.deleteIfExists(socket); |
100 | | - Files.deleteIfExists(Path.of(tempDir)); |
| 75 | + if (sscFilePath != null) { |
| 76 | + Files.delete(sscFilePath); |
101 | 77 | } |
102 | 78 | } |
103 | 79 |
|
|
0 commit comments