Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
import java.net.UnixDomainSocketAddress;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.file.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
Expand All @@ -49,55 +49,31 @@
public class SocketChannelConnectionSetup {

private ServerSocketChannel ssc;
private SocketChannel s1, s2;

private static volatile String tempDir;
private static final AtomicInteger count = new AtomicInteger(0);
private volatile Path socket;

@Param({"inet", "unix"})
private volatile String family;

static {
try {
Path p = Files.createTempDirectory("readWriteTest");
tempDir = p.toString();
} catch (IOException e) {
tempDir = null;
}
}
private Path sscFilePath;

private ServerSocketChannel getServerSocketChannel() throws IOException {
if (family.equals("inet"))
return getInetServerSocketChannel();
else if (family.equals("unix"))
return getUnixServerSocketChannel();
throw new InternalError();
}


private ServerSocketChannel getInetServerSocketChannel() throws IOException {
return ServerSocketChannel.open().bind(null);
}
private SocketChannel s1, s2;

private ServerSocketChannel getUnixServerSocketChannel() throws IOException {
int next = count.incrementAndGet();
socket = Paths.get(tempDir, Integer.toString(next));
UnixDomainSocketAddress addr = UnixDomainSocketAddress.of(socket);
return ServerSocketChannel.open(StandardProtocolFamily.UNIX).bind(addr);
}
@Param({"INET", "UNIX"})
private String family;

@Setup(Level.Trial)
public void beforeRun() throws IOException {
ssc = getServerSocketChannel();
StandardProtocolFamily typedFamily = StandardProtocolFamily.valueOf(family);
ssc = ServerSocketChannel.open(typedFamily).bind(null);
// Record the UDS file path right after binding, as the socket may be
// closed later due to a failure, and subsequent calls to `getPath()`
// will throw.
sscFilePath = ssc.getLocalAddress() instanceof UnixDomainSocketAddress udsChannel
? udsChannel.getPath()
: null;
}

@TearDown(Level.Trial)
public void afterRun() throws IOException {
public void afterRun() throws Exception {
ssc.close();
if (family.equals("unix")) {
Files.deleteIfExists(socket);
Files.deleteIfExists(Path.of(tempDir));
if (sscFilePath != null) {
Files.delete(sscFilePath);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,18 @@
public class UnixSocketChannelReadWrite {

private ServerSocketChannel ssc;
private Path sscFilePath;
private SocketChannel s1, s2;
private ReadThread rt;
private ByteBuffer bb = ByteBuffer.allocate(1);

private volatile Path socket;

private ServerSocketChannel getServerSocketChannel() throws IOException {
socket = Files.createTempDirectory(UnixSocketChannelReadWrite.class.getSimpleName()).resolve("sock");
UnixDomainSocketAddress addr = UnixDomainSocketAddress.of(socket);
ServerSocketChannel c = ServerSocketChannel.open(StandardProtocolFamily.UNIX);
c.bind(addr);
return c;
}

@Setup(Level.Trial)
public void beforeRun() throws IOException {
ssc = getServerSocketChannel();
ssc = ServerSocketChannel.open(StandardProtocolFamily.UNIX).bind(null);
// Record the UDS file path right after binding, as the socket may be
// closed later due to a failure, and subsequent calls to `getPath()`
// will throw.
sscFilePath = ((UnixDomainSocketAddress) ssc.getLocalAddress()).getPath();
s1 = SocketChannel.open(ssc.getLocalAddress());
s2 = ssc.accept();

Expand All @@ -79,8 +74,7 @@ public void afterRun() throws IOException, InterruptedException {
s1.close();
s2.close();
ssc.close();
Files.delete(socket);
Files.delete(socket.getParent());
Files.delete(sscFilePath);
rt.join();
}

Expand Down