aboutsummaryrefslogtreecommitdiff
path: root/test/java/nio
diff options
context:
space:
mode:
Diffstat (limited to 'test/java/nio')
-rw-r--r--test/java/nio/Buffer/Chars.java4
-rw-r--r--test/java/nio/channels/AsynchronousChannelGroup/Basic.java34
-rw-r--r--test/java/nio/channels/AsynchronousChannelGroup/GroupOfOne.java2
-rw-r--r--test/java/nio/channels/AsynchronousChannelGroup/Restart.java2
-rw-r--r--test/java/nio/channels/AsynchronousSocketChannel/CompletionHandlerRelease.java211
-rw-r--r--test/java/nio/channels/Selector/ChangingInterests.java2
-rw-r--r--test/java/nio/file/FileStore/Basic.java51
-rw-r--r--test/java/nio/file/FileSystem/Basic.java47
8 files changed, 302 insertions, 51 deletions
diff --git a/test/java/nio/Buffer/Chars.java b/test/java/nio/Buffer/Chars.java
index e91b6a7388..ef2df66ac1 100644
--- a/test/java/nio/Buffer/Chars.java
+++ b/test/java/nio/Buffer/Chars.java
@@ -51,8 +51,8 @@ public class Chars {
*/
static CharBuffer randomizeRange(CharBuffer cb) {
int mid = cb.capacity() >>> 1;
- int start = RAND.nextInt(mid);
- int end = mid + RAND.nextInt(mid);
+ int start = RAND.nextInt(mid + 1); // from 0 to mid
+ int end = mid + RAND.nextInt(cb.capacity() - mid + 1); // from mid to capacity
cb.position(start);
cb.limit(end);
return cb;
diff --git a/test/java/nio/channels/AsynchronousChannelGroup/Basic.java b/test/java/nio/channels/AsynchronousChannelGroup/Basic.java
index 8c6b5cb702..1c5da1309b 100644
--- a/test/java/nio/channels/AsynchronousChannelGroup/Basic.java
+++ b/test/java/nio/channels/AsynchronousChannelGroup/Basic.java
@@ -24,8 +24,6 @@
/* @test
* @bug 4607272
* @summary Unit test for AsynchronousChannelGroup
- * @build Basic
- * @run main/othervm -XX:-UseVMInterruptibleIO Basic
*/
import java.nio.ByteBuffer;
@@ -37,12 +35,9 @@ import java.io.IOException;
public class Basic {
static final Random rand = new Random();
- static final ThreadFactory threadFactory = new ThreadFactory() {
- @Override
- public Thread newThread(final Runnable r) {
- return new Thread(r);
- }};
-
+ static final ThreadFactory threadFactory = (Runnable r) -> {
+ return new Thread(r);
+ };
public static void main(String[] args) throws Exception {
shutdownTests();
@@ -51,6 +46,12 @@ public class Basic {
miscTests();
}
+ static void awaitTermination(AsynchronousChannelGroup group) throws InterruptedException {
+ boolean terminated = group.awaitTermination(20, TimeUnit.SECONDS);
+ if (!terminated)
+ throw new RuntimeException("Group should have terminated");
+ }
+
static void testShutdownWithNoChannels(ExecutorService pool,
AsynchronousChannelGroup group)
throws Exception
@@ -59,9 +60,7 @@ public class Basic {
if (!group.isShutdown())
throw new RuntimeException("Group should be shutdown");
// group should terminate quickly
- boolean terminated = group.awaitTermination(3, TimeUnit.SECONDS);
- if (!terminated)
- throw new RuntimeException("Group should have terminated");
+ awaitTermination(group);
if (pool != null && !pool.isTerminated())
throw new RuntimeException("Executor should have terminated");
}
@@ -86,9 +85,7 @@ public class Basic {
ch.close();
// group should terminate quickly
- boolean terminated = group.awaitTermination(3, TimeUnit.SECONDS);
- if (!terminated)
- throw new RuntimeException("Group should have terminated");
+ awaitTermination(group);
if (pool != null && !pool.isTerminated())
throw new RuntimeException("Executor should have terminated");
}
@@ -153,9 +150,8 @@ public class Basic {
if (ch.isOpen())
throw new RuntimeException("Channel should be closed");
- boolean terminated = group.awaitTermination(3, TimeUnit.SECONDS);
- if (!terminated)
- throw new RuntimeException("Group should have terminated");
+ awaitTermination(group);
+
if (pool != null && !pool.isTerminated())
throw new RuntimeException("Executor should have terminated");
}
@@ -260,9 +256,7 @@ public class Basic {
// close channel; group should terminate quickly
ch.close();
listener.close();
- terminated = group.awaitTermination(3, TimeUnit.SECONDS);
- if (!terminated)
- throw new RuntimeException("Group should have terminated");
+ awaitTermination(group);
}
static void miscTests() throws Exception {
diff --git a/test/java/nio/channels/AsynchronousChannelGroup/GroupOfOne.java b/test/java/nio/channels/AsynchronousChannelGroup/GroupOfOne.java
index 2945343bfe..ce7d3f6355 100644
--- a/test/java/nio/channels/AsynchronousChannelGroup/GroupOfOne.java
+++ b/test/java/nio/channels/AsynchronousChannelGroup/GroupOfOne.java
@@ -136,7 +136,7 @@ public class GroupOfOne {
// clean-up
group.shutdown();
- boolean terminated = group.awaitTermination(5, TimeUnit.SECONDS);
+ boolean terminated = group.awaitTermination(20, TimeUnit.SECONDS);
if (!terminated)
throw new RuntimeException("Group did not terminate");
diff --git a/test/java/nio/channels/AsynchronousChannelGroup/Restart.java b/test/java/nio/channels/AsynchronousChannelGroup/Restart.java
index 5fb2f123b0..456f66a2ee 100644
--- a/test/java/nio/channels/AsynchronousChannelGroup/Restart.java
+++ b/test/java/nio/channels/AsynchronousChannelGroup/Restart.java
@@ -24,8 +24,6 @@
/* @test
* @bug 4607272 6842687
* @summary Unit test for AsynchronousChannelGroup
- * @build Restart
- * @run main/othervm -XX:-UseVMInterruptibleIO Restart
*/
import java.nio.channels.*;
diff --git a/test/java/nio/channels/AsynchronousSocketChannel/CompletionHandlerRelease.java b/test/java/nio/channels/AsynchronousSocketChannel/CompletionHandlerRelease.java
new file mode 100644
index 0000000000..7abbf064b4
--- /dev/null
+++ b/test/java/nio/channels/AsynchronousSocketChannel/CompletionHandlerRelease.java
@@ -0,0 +1,211 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/* @test
+ * @bug 8202252
+ * @run testng CompletionHandlerRelease
+ * @summary Verify that reference to CompletionHandler is cleared after use
+ */
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.lang.ref.Reference;
+import java.lang.ref.ReferenceQueue;
+import java.lang.ref.WeakReference;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import static java.net.StandardSocketOptions.*;
+import java.nio.ByteBuffer;
+import java.nio.channels.AsynchronousChannelGroup;
+import java.nio.channels.AsynchronousServerSocketChannel;
+import java.nio.channels.AsynchronousSocketChannel;
+import java.nio.channels.CompletionHandler;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+
+import org.testng.annotations.AfterTest;
+import org.testng.annotations.BeforeTest;
+import org.testng.annotations.Test;
+import static org.testng.Assert.*;
+
+public class CompletionHandlerRelease {
+ @Test
+ public void testConnect() throws Exception {
+ try (Server server = new Server()) {
+ try (AsynchronousSocketChannel ch =
+ AsynchronousSocketChannel.open(GROUP)) {
+ CountDownLatch latch = new CountDownLatch(1);
+ Handler<Void,Object> handler =
+ new Handler<Void,Object>("connect", latch);
+ ReferenceQueue queue = new ReferenceQueue<WeakReference>();
+ WeakReference<Object> ref =
+ new WeakReference<Object>(handler, queue);
+
+ ch.connect(server.address(), null, handler);
+
+ try { latch.await(); } catch (InterruptedException ignore) { }
+
+ handler = null;
+ waitForRefToClear(ref, queue);
+
+ server.accept().get().close();
+ }
+ }
+ }
+
+ @Test
+ public void testWrite() throws Exception {
+ try (Server server = new Server();
+ AsynchronousSocketChannel ch =
+ AsynchronousSocketChannel.open(GROUP)) {
+ ch.connect(server.address()).get();
+
+ try (AsynchronousSocketChannel sc = server.accept().get()) {
+ ByteBuffer src = ByteBuffer.wrap("hello".getBytes("UTF-8"));
+ sc.setOption(SO_SNDBUF, src.remaining());
+
+ CountDownLatch latch = new CountDownLatch(1);
+ Handler<Integer,Object> handler =
+ new Handler<Integer,Object>("write", latch);
+ ReferenceQueue queue = new ReferenceQueue<WeakReference>();
+ WeakReference<Object> ref =
+ new WeakReference<Object>(handler, queue);
+
+ sc.write(src, null, handler);
+
+ try { latch.await(); } catch (InterruptedException ignore) { }
+
+ handler = null;
+ waitForRefToClear(ref, queue);
+ }
+ }
+ }
+
+ @Test
+ public void testRead() throws Exception {
+ try (Server server = new Server();
+ AsynchronousSocketChannel ch =
+ AsynchronousSocketChannel.open(GROUP)) {
+ ch.connect(server.address()).get();
+
+ try (AsynchronousSocketChannel sc = server.accept().get()) {
+ ByteBuffer src = ByteBuffer.wrap("hello".getBytes("UTF-8"));
+ sc.setOption(SO_SNDBUF, src.remaining());
+ sc.write(src).get();
+
+ CountDownLatch latch = new CountDownLatch(1);
+ Handler<Integer,Object> handler =
+ new Handler<Integer,Object>("read", latch);
+ ReferenceQueue queue = new ReferenceQueue<WeakReference>();
+ WeakReference<Object> ref =
+ new WeakReference<Object>(handler, queue);
+
+ ByteBuffer dst = ByteBuffer.allocate(64);
+ ch.read(dst, null, handler);
+
+ try { latch.await(); } catch (InterruptedException ignore) { }
+
+ handler = null;
+ waitForRefToClear(ref, queue);
+ }
+ }
+ }
+
+ private AsynchronousChannelGroup GROUP;
+
+ @BeforeTest
+ void setup() throws IOException {
+ GROUP = AsynchronousChannelGroup.withFixedThreadPool(2,
+ Executors.defaultThreadFactory());
+ }
+
+ @AfterTest
+ void cleanup() throws IOException {
+ GROUP.shutdownNow();
+ }
+
+ class Server implements Closeable {
+ private final AsynchronousServerSocketChannel ssc;
+ private final InetSocketAddress address;
+
+ Server() throws IOException {
+ this(0);
+ }
+
+ Server(int recvBufSize) throws IOException {
+ ssc = AsynchronousServerSocketChannel.open(GROUP);
+ if (recvBufSize > 0) {
+ ssc.setOption(SO_RCVBUF, recvBufSize);
+ }
+ ssc.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(),
+ 0));
+ address = (InetSocketAddress)ssc.getLocalAddress();
+ }
+
+ InetSocketAddress address() {
+ return address;
+ }
+
+ Future<AsynchronousSocketChannel> accept() throws IOException {
+ return ssc.accept();
+ }
+
+ public void close() throws IOException {
+ ssc.close();
+ }
+ }
+
+ static class Handler<V,A> implements CompletionHandler<V,A> {
+ private final String name;
+ private final CountDownLatch latch;
+
+ Handler(String name, CountDownLatch latch) {
+ this.name = name;
+ this.latch = latch;
+ }
+
+ public void completed(V result, A attachment) {
+ System.out.format("%s completed(%s, %s)%n",
+ name, result, attachment);
+ latch.countDown();
+ }
+
+ public void failed(Throwable exc, A attachment) {
+ System.out.format("%s failed(%s, %s)%n",
+ name, exc, attachment);
+ exc.printStackTrace();
+ latch.countDown();
+ }
+ }
+
+ private void waitForRefToClear(Reference ref, ReferenceQueue queue)
+ throws InterruptedException {
+ Reference r;
+ while ((r = queue.remove(20)) == null) {
+ System.gc();
+ }
+ assertEquals(r, ref);
+ assertNull(r.get());
+ }
+}
diff --git a/test/java/nio/channels/Selector/ChangingInterests.java b/test/java/nio/channels/Selector/ChangingInterests.java
index 59f6210067..c3e1dbd096 100644
--- a/test/java/nio/channels/Selector/ChangingInterests.java
+++ b/test/java/nio/channels/Selector/ChangingInterests.java
@@ -138,8 +138,10 @@ public class ChangingInterests {
ServerSocketChannel.open().bind(new InetSocketAddress(0));
final SocketChannel sc = SocketChannel.open();
+ sc.setOption(StandardSocketOptions.TCP_NODELAY, true);
sc.connect(new InetSocketAddress(lh, ssc.socket().getLocalPort()));
SocketChannel peer = ssc.accept();
+ peer.setOption(StandardSocketOptions.TCP_NODELAY, true);
sc.configureBlocking(false);
diff --git a/test/java/nio/file/FileStore/Basic.java b/test/java/nio/file/FileStore/Basic.java
index 176efef87a..5c8e53f5d0 100644
--- a/test/java/nio/file/FileStore/Basic.java
+++ b/test/java/nio/file/FileStore/Basic.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2008, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -24,7 +24,10 @@
/* @test
* @bug 4313887 6873621 6979526 7006126 7020517
* @summary Unit test for java.nio.file.FileStore
- * @library ..
+ * @key intermittent
+ * @library .. /lib/testlibrary
+ * @build jdk.testlibrary.FileUtils
+ * @run main Basic
*/
import java.nio.file.*;
@@ -32,6 +35,7 @@ import java.nio.file.attribute.*;
import java.io.File;
import java.io.IOException;
import java.util.*;
+import jdk.testlibrary.FileUtils;
public class Basic {
@@ -109,19 +113,36 @@ public class Basic {
/**
* Test: Enumerate all FileStores
*/
- FileStore prev = null;
- for (FileStore store: FileSystems.getDefault().getFileStores()) {
- System.out.format("%s (name=%s type=%s)\n", store, store.name(),
- store.type());
-
- // check space attributes are accessible
- store.getTotalSpace();
- store.getUnallocatedSpace();
- store.getUsableSpace();
-
- // two distinct FileStores should not be equal
- assertTrue(!store.equals(prev));
- prev = store;
+ if (FileUtils.areFileSystemsAccessible()) {
+ FileStore prev = null;
+ for (FileStore store: FileSystems.getDefault().getFileStores()) {
+ System.out.format("%s (name=%s type=%s)\n", store, store.name(),
+ store.type());
+
+ // check space attributes are accessible
+ try {
+ store.getTotalSpace();
+ store.getUnallocatedSpace();
+ store.getUsableSpace();
+ } catch (NoSuchFileException nsfe) {
+ // ignore exception as the store could have been
+ // deleted since the iterator was instantiated
+ System.err.format("%s was not found\n", store);
+ } catch (AccessDeniedException ade) {
+ // ignore exception as the lack of ability to access the
+ // store due to lack of file permission or similar does not
+ // reflect whether the space attributes would be accessible
+ // were access to be permitted
+ System.err.format("%s is inaccessible\n", store);
+ }
+
+ // two distinct FileStores should not be equal
+ assertTrue(!store.equals(prev));
+ prev = store;
+ }
+ } else {
+ System.err.println
+ ("Skipping FileStore check due to file system access failure");
}
}
}
diff --git a/test/java/nio/file/FileSystem/Basic.java b/test/java/nio/file/FileSystem/Basic.java
index 89903611b4..18f788ea1f 100644
--- a/test/java/nio/file/FileSystem/Basic.java
+++ b/test/java/nio/file/FileSystem/Basic.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2008, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -24,12 +24,25 @@
/* @test
* @bug 4313887 6838333
* @summary Unit test for java.nio.file.FileSystem
- * @library ..
+ * @library .. /lib/testlibrary
+ * @build jdk.testlibrary.FileUtils
+ * @run main Basic
*/
-import java.nio.file.*;
-import java.nio.file.attribute.*;
+import java.io.File;
import java.io.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.nio.file.Files;
+import java.nio.file.FileStore;
+import java.nio.file.FileSystem;
+import java.nio.file.FileSystems;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.ProviderNotFoundException;
+import java.util.HashMap;
+import java.util.concurrent.TimeUnit;
+import jdk.testlibrary.FileUtils;
/**
* Simple santity checks for java.nio.file.FileSystem
@@ -41,6 +54,20 @@ public class Basic {
throw new RuntimeException(msg);
}
+ static void checkFileStores(FileSystem fs) throws IOException {
+ // sanity check method
+ if (FileUtils.areFileSystemsAccessible()) {
+ System.out.println("\n--- Begin FileStores ---");
+ for (FileStore store: fs.getFileStores()) {
+ System.out.println(store);
+ }
+ System.out.println("--- EndFileStores ---\n");
+ } else {
+ System.err.println
+ ("Skipping FileStore check due to file system access failure");
+ }
+ }
+
static void checkSupported(FileSystem fs, String... views) {
for (String view: views) {
check(fs.supportedFileAttributeViews().contains(view),
@@ -48,7 +75,9 @@ public class Basic {
}
}
- public static void main(String[] args) throws IOException {
+ public static void main(String[] args)
+ throws IOException, URISyntaxException {
+ String os = System.getProperty("os.name");
FileSystem fs = FileSystems.getDefault();
// close should throw UOE
@@ -63,15 +92,11 @@ public class Basic {
check(fs.provider().getScheme().equals("file"),
"should use 'file' scheme");
- // santity check method - need to re-visit this in future as I/O errors
- // are possible
- for (FileStore store: fs.getFileStores()) {
- System.out.println(store);
- }
+ // sanity check FileStores
+ checkFileStores(fs);
// sanity check supportedFileAttributeViews
checkSupported(fs, "basic");
- String os = System.getProperty("os.name");
if (os.equals("SunOS"))
checkSupported(fs, "posix", "unix", "owner", "acl", "user");
if (os.equals("Linux"))