Repository: netty/netty
File: transport-native-kqueue/src/test/java/io/netty/channel/kqueue/KqueueRegistrationTest.java (Java)

Code before:
import io.netty.util.concurrent.ImmediateEventExecutor;
import io.netty.util.concurrent.Promise;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.net.ConnectException;
import java.net.InetSocketAddress;

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

public class KqueueRegistrationTest {

    @BeforeEach
    public void setUp() {
        KQueue.ensureAvailability();
    }

    @Test
    void testFdReuse() throws Exception {
        Bootstrap bootstrap = new Bootstrap();
        EventLoopGroup group = new MultiThreadIoEventLoopGroup(1, KQueueIoHandler.newFactory());
        Promise<Throwable> promise = ImmediateEventExecutor.INSTANCE.newPromise();
        try {
            bootstrap.group(group)
                    .channel(KQueueSocketChannel.class)
                    //.channel(NioSocketChannel.class)
                    .handler(new LoggingHandler());
            bootstrap.connect(new InetSocketAddress(NetUtil.LOCALHOST, SocketTestPermutation.UNASSIGNED_PORT))
                    .addListener((ChannelFutureListener) f1 -> {
                        if (f1.cause() instanceof ConnectException) {
                            f1.channel().close();
                            bootstrap.connect(new InetSocketAddress(NetUtil.LOCALHOST,
                                            SocketTestPermutation.UNASSIGNED_PORT))
                                    .addListener((ChannelFutureListener) f2 -> {
                                        f2.channel().close();
                                        promise.setSuccess(f2.cause());
                                    });
                        } else {
                            promise.setFailure(new IllegalStateException());
                        }
                    });
            Throwable cause = promise.sync().getNow();
            assertThat(cause, is(instanceOf(ConnectException.class)));
        } finally {
            group.shutdownGracefully();
        }
    }
}


Review comment (bug):
```
//.channel(NioSocketChannel.class)
.handler(new LoggingHandler());
```

Debug leftover?

Code after:
import io.netty.util.concurrent.ImmediateEventExecutor;
import io.netty.util.concurrent.Promise;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.net.ConnectException;
import java.net.InetSocketAddress;

import static org.junit.jupiter.api.Assertions.assertInstanceOf;

public class KqueueRegistrationTest {

    @BeforeEach
    public void setUp() {
        KQueue.ensureAvailability();
    }

    @Test
    void testFdReuse() throws Exception {
        Bootstrap bootstrap = new Bootstrap();
        EventLoopGroup group = new MultiThreadIoEventLoopGroup(1, KQueueIoHandler.newFactory());
        Promise<Throwable> promise = ImmediateEventExecutor.INSTANCE.newPromise();
        try {
            bootstrap.group(group)
                    .channel(KQueueSocketChannel.class)
                    .handler(new LoggingHandler());
            bootstrap.connect(new InetSocketAddress(NetUtil.LOCALHOST, SocketTestPermutation.UNASSIGNED_PORT))
                    .addListener((ChannelFutureListener) f1 -> {
                        if (f1.cause() instanceof ConnectException) {
                            f1.channel().close();
                            bootstrap.connect(new InetSocketAddress(NetUtil.LOCALHOST,
                                            SocketTestPermutation.UNASSIGNED_PORT))
                                    .addListener((ChannelFutureListener) f2 -> {
                                        f2.channel().close();
                                        promise.setSuccess(f2.cause());
                                    });
                        } else {
                            promise.setFailure(new IllegalStateException());
                        }
                    });
            Throwable cause = promise.sync().getNow();
            assertInstanceOf(ConnectException.class, cause);
        } finally {
            group.shutdownGracefully();
        }
    }
}