8207833: java/nio/channels/Selector/SelectWithConsumer.java testCancel() fails intermittently
authormli
Fri, 20 Jul 2018 15:24:42 +0800
changeset 51211 6ed2290ba410
parent 51210 e750c1a054fa
child 51212 a138b5fe288d
8207833: java/nio/channels/Selector/SelectWithConsumer.java testCancel() fails intermittently Reviewed-by: alanb
test/jdk/java/nio/channels/Selector/SelectWithConsumer.java
--- a/test/jdk/java/nio/channels/Selector/SelectWithConsumer.java	Thu Jul 19 18:21:24 2018 -0700
+++ b/test/jdk/java/nio/channels/Selector/SelectWithConsumer.java	Fri Jul 20 15:24:42 2018 +0800
@@ -548,12 +548,18 @@
             // write to sink to ensure that the source is readable
             sink.write(messageBuffer());
 
+            source.configureBlocking(false);
+            SelectionKey key1 = source.register(sel, SelectionKey.OP_READ);
+            // make sure pipe source is readable before we do following checks.
+            // this is sometime necessary on windows where pipe is implemented
+            // as a pair of connected socket, so there is no guarantee that written
+            // bytes on sink side is immediately available on source side.
+            sel.select();
+
             sink.configureBlocking(false);
-            source.configureBlocking(false);
-            SelectionKey key1 = sink.register(sel, SelectionKey.OP_WRITE);
-            SelectionKey key2 = source.register(sel, SelectionKey.OP_READ);
+            SelectionKey key2 = sink.register(sel, SelectionKey.OP_WRITE);
+            sel.selectNow();
 
-            sel.selectNow();
             assertTrue(sel.keys().contains(key1));
             assertTrue(sel.keys().contains(key2));
             assertTrue(sel.selectedKeys().contains(key1));