Skip to content

Commit

Permalink
test: Fix FileDescriptorCastTest
Browse files Browse the repository at this point in the history
On Linux, for testCastGenericDuplicating, we may get a connection
refused if we premature close the original socket.

Don't close the original socket.
  • Loading branch information
kohlschuetter committed Feb 4, 2024
1 parent 10e014b commit 3a29f00
Showing 1 changed file with 2 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"THROWS_METHOD_THROWS_CLAUSE_THROWABLE", "THROWS_METHOD_THROWS_CLAUSE_BASIC_EXCEPTION"})
public class FileDescriptorCastTest {
// CPD-OFF

@Test
public void testInvalidFileDescriptor() throws IOException {
assertThrows(IOException.class, () -> FileDescriptorCast.using(new FileDescriptor()));
Expand Down Expand Up @@ -206,8 +206,6 @@ public void testCastGeneric() throws Exception {
try (AFUNIXServerSocketChannel ussc = AFUNIXServerSocketChannel.open()) {
ussc.bind(AFUNIXSocketAddress.ofNewTempFile());

// NOTE: We're using .duplicating instead of .using, which allows us to not keep a reference
// to "ussc" until the end of this method (e.g., by wrapping a try-with-resources).
AFGenericServerSocketChannel gssc = FileDescriptorCast.using(ussc.getFileDescriptor()).as(
AFGenericServerSocketChannel.class);

Expand Down Expand Up @@ -264,17 +262,14 @@ public void testCastGeneric() throws Exception {
public void testCastGenericDuplicating() throws Exception {
AFUNIXSocketAddress addr = AFUNIXSocketAddress.ofNewTempFile();
Path p = addr.getFile().toPath();
try {
AFUNIXServerSocketChannel ussc = AFUNIXServerSocketChannel.open();
try (AFUNIXServerSocketChannel ussc = AFUNIXServerSocketChannel.open()) {
ussc.bind(addr);
ussc.setDeleteOnClose(false);

FileDescriptorCast fdc = FileDescriptorCast.duplicating(ussc.getFileDescriptor());
if (fdc == null) {
throw new TestAbortedNotAnIssueException("FileDescriptCast.duplicating not supported");
}

ussc.close(); // closes the original file descriptor but not the duplicate
// also won't delete the file because we told to it not delete above, otherwise
// we would be able to bind but not connect (this is an AF_UNIX-specific issue)

Expand Down

0 comments on commit 3a29f00

Please sign in to comment.