Path: blob/master/test/jdk/java/nio/channels/AsynchronousFileChannel/LotsOfWrites.java
41153 views
/*1* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 691387725* @summary Stress AsynchronousFileChannel.write26* @key randomness27*/2829import java.io.*;30import java.nio.ByteBuffer;31import static java.nio.file.StandardOpenOption.*;32import java.nio.channels.*;33import java.util.Random;34import java.util.concurrent.CountDownLatch;3536public class LotsOfWrites {37static final Random rand = new Random();3839/**40* Asynchronously writes a known pattern to a file up to a given size,41* counting down a latch to release waiters when done.42*/43static class Writer implements CompletionHandler<Integer,ByteBuffer> {44private final File file;45private final long size;46private final CountDownLatch latch;47private final AsynchronousFileChannel channel;4849private volatile long position;50private volatile byte nextByte;5152private long updatePosition(long nwrote) {53position += nwrote;54return position;55}5657private ByteBuffer genNextBuffer() {58int n = Math.min(8192 + rand.nextInt(8192), (int)(size - position));59ByteBuffer buf = ByteBuffer.allocate(n);60for (int i=0; i<n; i++) {61buf.put(nextByte++);62}63buf.flip();64return buf;65}6667// close channel and release any waiters68private void done() {69try {70channel.close();71} catch (IOException ignore) { }72latch.countDown();73}7475Writer(File file, long size, CountDownLatch latch) throws IOException {76this.file = file;77this.size = size;78this.latch = latch;79this.channel = AsynchronousFileChannel.open(file.toPath(), WRITE);80}8182File file() {83return file;84}8586long size() {87return size;88}8990// initiate first write91void start() {92ByteBuffer buf = genNextBuffer();93channel.write(buf, 0L, buf, this);94}9596@Override97public void completed(Integer nwrote, ByteBuffer buf) {98long pos = updatePosition(nwrote);99if (!buf.hasRemaining()) {100// buffer has been completely written; decide if we need to101// write more102if (position >= size) {103done();104return;105}106buf = genNextBuffer();107}108channel.write(buf, pos, buf, this);109}110111@Override112public void failed(Throwable exc, ByteBuffer buf) {113exc.printStackTrace();114done();115}116}117118public static void main(String[] args) throws Exception {119// random number of writers120int count = 20 + rand.nextInt(16);121Writer[] writers = new Writer[count];122CountDownLatch latch = new CountDownLatch(count);123124// initiate writing to each file125for (int i=0; i<count; i++) {126long size = 512*1024 + rand.nextInt(512*1024);127File blah = File.createTempFile("blah", null);128blah.deleteOnExit();129Writer writer = new Writer(blah, size, latch);130writers[i] = writer;131writer.start();132}133134// wait for writing to complete135latch.await();136137// verify content of each file138boolean failed = false;139byte[] buf = new byte[8192];140for (int i=0; i<count ;i++) {141Writer writer = writers[i];142FileInputStream in = new FileInputStream(writer.file());143try {144long size = 0L;145byte expected = 0;146int nread = in.read(buf);147while (nread > 0) {148for (int j=0; j<nread; j++) {149if (buf[j] != expected) {150System.err.println("Unexpected contents");151failed = true;152break;153}154expected++;155}156if (failed)157break;158size += nread;159nread = in.read(buf);160}161if (!failed && size != writer.size()) {162System.err.println("Unexpected size");163failed = true;164}165if (failed)166break;167} finally {168in.close();169}170}171172// clean-up173for (int i=0; i<count; i++) {174writers[i].file().delete();175}176177if (failed)178throw new RuntimeException("Test failed");179}180}181182183