From 3ed1b2b94a3f2253351362b45a10a64974659d41 Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Mon, 17 Oct 2022 15:10:18 -0500 Subject: [PATCH] Minor IOTest cleanup --- .../java/org/eclipse/jetty/io/IOTest.java | 123 +++++++++--------- 1 file changed, 62 insertions(+), 61 deletions(-) diff --git a/jetty-core/jetty-io/src/test/java/org/eclipse/jetty/io/IOTest.java b/jetty-core/jetty-io/src/test/java/org/eclipse/jetty/io/IOTest.java index 46821034fa6..cad6f1c6b9a 100644 --- a/jetty-core/jetty-io/src/test/java/org/eclipse/jetty/io/IOTest.java +++ b/jetty-core/jetty-io/src/test/java/org/eclipse/jetty/io/IOTest.java @@ -38,7 +38,6 @@ import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import org.eclipse.jetty.toolchain.test.FS; -import org.eclipse.jetty.toolchain.test.MavenTestingUtils; import org.eclipse.jetty.toolchain.test.jupiter.WorkDir; import org.eclipse.jetty.toolchain.test.jupiter.WorkDirExtension; import org.eclipse.jetty.util.BufferUtil; @@ -47,7 +46,6 @@ import org.eclipse.jetty.util.resource.Resource; import org.eclipse.jetty.util.resource.ResourceFactory; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; import org.junit.jupiter.api.condition.OS; import org.junit.jupiter.api.extension.ExtendWith; @@ -159,7 +157,9 @@ public class IOTest try (ServerSocketChannel connector = ServerSocketChannel.open()) { connector.socket().bind(null); - try (Socket client = SocketChannel.open(connector.socket().getLocalSocketAddress()).socket()) + try (ServerSocket serverSocket = connector.socket(); + SocketChannel socketChannel = SocketChannel.open(serverSocket.getLocalSocketAddress()); + Socket client = socketChannel.socket()) { client.setSoTimeout(1000); try (Socket server = connector.accept().socket()) @@ -219,7 +219,9 @@ public class IOTest { connector.socket().bind(null); - try (Socket client = SocketChannel.open(connector.socket().getLocalSocketAddress()).socket()) + try (ServerSocket serverSocket = connector.socket(); + SocketChannel socketChannel = SocketChannel.open(serverSocket.getLocalSocketAddress()); + Socket client = socketChannel.socket()) { client.setSoTimeout(1000); try (Socket server = connector.accept().socket()) @@ -269,8 +271,7 @@ public class IOTest // Client eventually sees Broken Pipe assertThrows(IOException.class, () -> { - int i = 0; - for (i = 0; i < 100000; i++) + for (int i = 0; i < 100000; i++) { client.getOutputStream().write(1); } @@ -287,7 +288,9 @@ public class IOTest { connector.configureBlocking(true); connector.socket().bind(null); - try (Socket client = SocketChannel.open(connector.socket().getLocalSocketAddress()).socket()) + try (ServerSocket serverSocket = connector.socket(); + SocketChannel socketChannel = SocketChannel.open(serverSocket.getLocalSocketAddress()); + Socket client = socketChannel.socket()) { client.setSoTimeout(2000); try (Socket server = connector.accept().socket()) @@ -396,29 +399,32 @@ public class IOTest @Test public void testAsyncSocketChannel() throws Exception { - AsynchronousServerSocketChannel connector = AsynchronousServerSocketChannel.open(); - connector.bind(null); - InetSocketAddress addr = (InetSocketAddress)connector.getLocalAddress(); - Future acceptor = connector.accept(); + try (AsynchronousServerSocketChannel connector = AsynchronousServerSocketChannel.open()) + { + connector.bind(null); + InetSocketAddress addr = (InetSocketAddress)connector.getLocalAddress(); + Future acceptor = connector.accept(); - AsynchronousSocketChannel client = AsynchronousSocketChannel.open(); + try (AsynchronousSocketChannel client = AsynchronousSocketChannel.open()) + { + client.connect(new InetSocketAddress("127.0.0.1", addr.getPort())).get(5, TimeUnit.SECONDS); - client.connect(new InetSocketAddress("127.0.0.1", addr.getPort())).get(5, TimeUnit.SECONDS); + AsynchronousSocketChannel server = acceptor.get(5, TimeUnit.SECONDS); - AsynchronousSocketChannel server = acceptor.get(5, TimeUnit.SECONDS); + ByteBuffer read = ByteBuffer.allocate(1024); + Future reading = server.read(read); - ByteBuffer read = ByteBuffer.allocate(1024); - Future reading = server.read(read); + byte[] data = "Testing 1 2 3".getBytes(StandardCharsets.UTF_8); + ByteBuffer write = BufferUtil.toBuffer(data); + Future writing = client.write(write); - byte[] data = "Testing 1 2 3".getBytes(StandardCharsets.UTF_8); - ByteBuffer write = BufferUtil.toBuffer(data); - Future writing = client.write(write); + writing.get(5, TimeUnit.SECONDS); + reading.get(5, TimeUnit.SECONDS); + read.flip(); - writing.get(5, TimeUnit.SECONDS); - reading.get(5, TimeUnit.SECONDS); - read.flip(); - - assertEquals(ByteBuffer.wrap(data), read); + assertEquals(ByteBuffer.wrap(data), read); + } + } } @Test @@ -458,12 +464,13 @@ public class IOTest } @Test - public void testDeleteNonExistentFile(TestInfo testInfo) + public void testDeleteNonExistentFile(WorkDir workDir) { - File dir = MavenTestingUtils.getTargetTestingDir(testInfo.getDisplayName()); + Path dir = workDir.getEmptyPathDir(); FS.ensureEmpty(dir); - File noFile = new File(dir, "nada"); + Path noFile = dir.resolve("nada"); assertFalse(IO.delete(noFile)); + assertFalse(IO.delete(noFile.toFile())); } @Test @@ -473,40 +480,40 @@ public class IOTest } @Test - public void testIsEmptyDoesNotExist(TestInfo testInfo) + public void testIsEmptyDoesNotExist(WorkDir workDir) { - File dir = MavenTestingUtils.getTargetTestingDir(testInfo.getDisplayName()); + Path dir = workDir.getEmptyPathDir(); FS.ensureEmpty(dir); - File noFile = new File(dir, "nada"); - assertTrue(IO.isEmptyDir(noFile)); + Path noFile = dir.resolve("nada"); + assertTrue(IO.isEmptyDir(noFile.toFile())); } @Test - public void testIsEmptyExistButAsFile(TestInfo testInfo) throws IOException + public void testIsEmptyExistButAsFile(WorkDir workDir) throws IOException { - File dir = MavenTestingUtils.getTargetTestingDir(testInfo.getDisplayName()); + Path dir = workDir.getEmptyPathDir(); FS.ensureEmpty(dir); - File file = new File(dir, "nada"); + Path file = dir.resolve("nada"); FS.touch(file); - assertFalse(IO.isEmptyDir(file)); + assertFalse(IO.isEmptyDir(file.toFile())); } @Test - public void testIsEmptyExistAndIsEmpty(TestInfo testInfo) + public void testIsEmptyExistAndIsEmpty(WorkDir workDir) { - File dir = MavenTestingUtils.getTargetTestingDir(testInfo.getDisplayName()); + Path dir = workDir.getEmptyPathDir(); FS.ensureEmpty(dir); - assertTrue(IO.isEmptyDir(dir)); + assertTrue(IO.isEmptyDir(dir.toFile())); } @Test - public void testIsEmptyExistAndHasContent(TestInfo testInfo) throws IOException + public void testIsEmptyExistAndHasContent(WorkDir workDir) throws IOException { - File dir = MavenTestingUtils.getTargetTestingDir(testInfo.getDisplayName()); + Path dir = workDir.getEmptyPathDir(); FS.ensureEmpty(dir); - File file = new File(dir, "nada"); + Path file = dir.resolve("nada"); FS.touch(file); - assertFalse(IO.isEmptyDir(dir)); + assertFalse(IO.isEmptyDir(dir.toFile())); } @Test @@ -550,16 +557,14 @@ public class IOTest } @Test - public void testSymbolicLink(TestInfo testInfo) throws Exception + public void testSymbolicLink(WorkDir workDir) throws Exception { - File dir = MavenTestingUtils.getTargetTestingDir(testInfo.getDisplayName()); + Path dir = workDir.getEmptyPathDir(); FS.ensureEmpty(dir); - File realFile = new File(dir, "real"); - Path realPath = realFile.toPath(); - FS.touch(realFile); + Path realPath = dir.resolve("real"); + FS.touch(realPath); - File linkFile = new File(dir, "link"); - Path linkPath = linkFile.toPath(); + Path linkPath = dir.resolve("link"); Files.createSymbolicLink(linkPath, realPath); Path targPath = linkPath.toRealPath(); @@ -570,30 +575,26 @@ public class IOTest assertFalse(Files.isSymbolicLink(realPath)); assertTrue(Files.isSymbolicLink(linkPath)); - Resource link = ResourceFactory.root().newResource(dir.toPath()).resolve("link"); + Resource link = ResourceFactory.root().newResource(dir.resolve("link")); assertThat(link.isAlias(), is(true)); } @Test - public void testSymbolicLinkDir(TestInfo testInfo) throws Exception + public void testSymbolicLinkDir(WorkDir workDir) throws Exception { - File dir = MavenTestingUtils.getTargetTestingDir(testInfo.getDisplayName()); + Path dir = workDir.getEmptyPathDir(); FS.ensureEmpty(dir); - File realDirFile = new File(dir, "real"); - Path realDirPath = realDirFile.toPath(); + Path realDirPath = dir.resolve("real"); Files.createDirectories(realDirPath); - File linkDirFile = new File(dir, "link"); - Path linkDirPath = linkDirFile.toPath(); + Path linkDirPath = dir.resolve("link"); Files.createSymbolicLink(linkDirPath, realDirPath); - File realFile = new File(realDirFile, "file"); - Path realPath = realFile.toPath(); - FS.touch(realFile); + Path realPath = realDirPath.resolve("file"); + FS.touch(realPath); - File linkFile = new File(linkDirFile, "file"); - Path linkPath = linkFile.toPath(); + Path linkPath = linkDirPath.resolve("file"); Path targPath = linkPath.toRealPath(); System.err.printf("realPath = %s%n", realPath);