# HG changeset patch # User bpb # Date 1488315298 28800 # Tue Feb 28 12:54:58 2017 -0800 # Node ID ba55a1e0fc49b158d48b5d637df218334104e076 # Parent c3039b7b89f0991e5e3f8666c8c681988741e362 8175209: Account for race condition in java/nio/channels/AsynchronousSocketChannel/Basic.java Summary: Pause until the channel reaches a pended state instead of for a fixed time. Reviewed-by: XXX diff --git a/test/java/nio/channels/AsynchronousSocketChannel/Basic.java b/test/java/nio/channels/AsynchronousSocketChannel/Basic.java --- a/test/java/nio/channels/AsynchronousSocketChannel/Basic.java +++ b/test/java/nio/channels/AsynchronousSocketChannel/Basic.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved. + * 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 @@ -28,16 +28,16 @@ * @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 static java.net.StandardSocketOptions.*; -import java.net.*; import java.util.Random; +import java.util.Set; 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(); @@ -327,8 +327,10 @@ new AtomicReference(); // write bytes to fill socket buffer + final AtomicLong completedTime = new AtomicLong(); ch.write(genBuffer(), ch, new CompletionHandler() { public void completed(Integer result, AsynchronousSocketChannel ch) { + completedTime.set(System.nanoTime()); ch.write(genBuffer(), ch, this); } public void failed(Throwable x, AsynchronousSocketChannel ch) { @@ -336,10 +338,21 @@ } }); - // give time for socket buffer to fill up. - Thread.sleep(5*1000); + // 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 + // attempt a concurrent write - should fail with WritePendingException try { ch.write(genBuffer()); throw new RuntimeException("WritePendingException expected");