Skip to content

Commit

Permalink
test: Improve SelectorProvider for JEP380 tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kohlschuetter committed Jul 2, 2024
1 parent 2f72a23 commit c532e3a
Showing 1 changed file with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@
import java.net.SocketException;
import java.net.StandardProtocolFamily;
import java.nio.channels.DatagramChannel;
import java.nio.channels.Pipe;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.channels.spi.AbstractSelector;
import java.nio.channels.spi.SelectorProvider;
import java.nio.file.Path;

Expand All @@ -48,6 +50,8 @@ public final class JEP380AddressSpecifics implements AddressSpecifics<SocketAddr
private final Method serverSocketChannelOpenMethod;
private final Method datagramChannelOpenMethod;

private final SelectorProvider sp;

private JEP380AddressSpecifics() {
this.pf = unixProtocolFamilyIfAvailable();
this.addressOfMethod = tryResolve("java.net.UnixDomainSocketAddress", "of", Path.class);
Expand All @@ -57,6 +61,40 @@ private JEP380AddressSpecifics() {
ProtocolFamily.class);
this.datagramChannelOpenMethod = tryResolve("java.nio.channels.DatagramChannel", "open",
ProtocolFamily.class);

this.sp = new SelectorProvider() {
private final SelectorProvider upstream = SelectorProvider.provider();

@Override
public SocketChannel openSocketChannel() throws IOException {
return newSocketChannel();
}

@Override
public ServerSocketChannel openServerSocketChannel() throws IOException {
return newServerSocketChannel();
}

@Override
public AbstractSelector openSelector() throws IOException {
return upstream.openSelector();
}

@Override
public Pipe openPipe() throws IOException {
return upstream.openPipe();
}

@Override
public DatagramChannel openDatagramChannel(ProtocolFamily family) throws IOException {
return upstream.openDatagramChannel(family);
}

@Override
public DatagramChannel openDatagramChannel() throws IOException {
return newDatagramChannel();
}
};
}

private static Method tryResolve(String className, String methodName, Class<?>... argType) {
Expand Down Expand Up @@ -137,7 +175,7 @@ public SocketAddress unwrap(InetAddress addr, int port) throws SocketException {

@Override
public SelectorProvider selectorProvider() {
return SelectorProvider.provider();
return sp;
}

@Override
Expand Down

0 comments on commit c532e3a

Please sign in to comment.