< prev index next >

test/java/nio/channels/AsynchronousSocketChannel/Basic.java

Print this page

        

*** 1,7 **** /* ! * Copyright (c) 2008, 2016, 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. --- 1,7 ---- /* ! * Copyright (c) 2008, 2017, 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.
*** 26,45 **** * @summary Unit test for AsynchronousSocketChannel * @run main Basic -skipSlowConnectTest * @key randomness intermittent */ import java.nio.ByteBuffer; import java.nio.channels.*; - import static java.net.StandardSocketOptions.*; - import java.net.*; import java.util.Random; import java.util.concurrent.*; import java.util.concurrent.atomic.*; - import java.io.Closeable; - import java.io.IOException; - import java.util.Set; public class Basic { static final Random rand = new Random(); static boolean skipSlowConnectTest = false; --- 26,45 ---- * @summary Unit test for AsynchronousSocketChannel * @run main Basic -skipSlowConnectTest * @key randomness intermittent */ + import java.io.Closeable; + import java.io.IOException; + import java.net.*; + import static java.net.StandardSocketOptions.*; import java.nio.ByteBuffer; import java.nio.channels.*; import java.util.Random; + import java.util.Set; import java.util.concurrent.*; import java.util.concurrent.atomic.*; public class Basic { static final Random rand = new Random(); static boolean skipSlowConnectTest = false;
*** 325,345 **** final AtomicReference<Throwable> writeException = new AtomicReference<Throwable>(); // write bytes to fill socket buffer ch.write(genBuffer(), ch, new CompletionHandler<Integer,AsynchronousSocketChannel>() { public void completed(Integer result, AsynchronousSocketChannel ch) { ch.write(genBuffer(), ch, this); } public void failed(Throwable x, AsynchronousSocketChannel ch) { writeException.set(x); } }); ! // give time for socket buffer to fill up. ! Thread.sleep(5*1000); // attempt a concurrent write - should fail with WritePendingException try { ch.write(genBuffer()); throw new RuntimeException("WritePendingException expected"); --- 325,358 ---- final AtomicReference<Throwable> writeException = new AtomicReference<Throwable>(); // write bytes to fill socket buffer + final AtomicLong completedTime = new AtomicLong(); ch.write(genBuffer(), ch, new CompletionHandler<Integer,AsynchronousSocketChannel>() { public void completed(Integer result, AsynchronousSocketChannel ch) { + completedTime.set(System.nanoTime()); ch.write(genBuffer(), ch, this); } public void failed(Throwable x, AsynchronousSocketChannel ch) { writeException.set(x); } }); ! // give time for socket buffer to fill up - ! // take pauses until the handler is no longer being invoked ! // because all writes are being pended which guarantees that ! // the internal channel state indicates it is writing ! long t0 = System.nanoTime(); ! long previousCompletedTime = completedTime.get(); ! do { ! Thread.sleep(1000); ! if (completedTime.get() == previousCompletedTime) { ! break; ! } ! previousCompletedTime = completedTime.get(); ! } while (true); // attempt a concurrent write - should fail with WritePendingException try { ch.write(genBuffer()); throw new RuntimeException("WritePendingException expected");
< prev index next >