jdk/test/java/nio/channels/AsynchronousFileChannel/Basic.java
changeset 3632 399359a027de
parent 3327 82e069ae54ab
child 5506 202f599c92aa
--- a/jdk/test/java/nio/channels/AsynchronousFileChannel/Basic.java	Sat Aug 22 17:40:18 2009 +0100
+++ b/jdk/test/java/nio/channels/AsynchronousFileChannel/Basic.java	Sun Aug 23 12:53:45 2009 +0100
@@ -22,7 +22,7 @@
  */
 
 /* @test
- * @bug 4607272 6822643 6830721
+ * @bug 4607272 6822643 6830721 6842687
  * @summary Unit test for AsynchronousFileChannel
  */
 
@@ -195,8 +195,6 @@
                     }
                     public void failed(Throwable exc, Void att) {
                     }
-                    public void cancelled(Void att) {
-                    }
                 });
                 throw new RuntimeException("OverlappingFileLockException expected");
             } catch (OverlappingFileLockException x) {
@@ -229,8 +227,6 @@
             }
             public void failed(Throwable exc, Void att) {
             }
-            public void cancelled(Void att) {
-            }
         });
 
         // wait for handler to complete
@@ -318,8 +314,6 @@
                         }
                         public void failed(Throwable exc, Void att) {
                         }
-                        public void cancelled(Void att) {
-                        }
                     });
                     await(latch);
 
@@ -338,8 +332,41 @@
                 }
             } finally {
                 ch.close();
+                executor.shutdown();
             }
         }
+
+
+        // test sharing a thread pool between many channels
+        ExecutorService executor = Executors
+            .newFixedThreadPool(1+rand.nextInt(10), threadFactory);
+        final int n = 50 + rand.nextInt(50);
+        AsynchronousFileChannel[] channels = new AsynchronousFileChannel[n];
+        try {
+            for (int i=0; i<n; i++) {
+                Set<StandardOpenOption> opts = EnumSet.of(WRITE);
+                channels[i] = AsynchronousFileChannel.open(file, opts, executor);
+                final CountDownLatch latch = new CountDownLatch(1);
+                channels[i].write(genBuffer(), 0L, (Void)null, new CompletionHandler<Integer,Void>() {
+                    public void completed(Integer result, Void att) {
+                        latch.countDown();
+                    }
+                    public void failed(Throwable exc, Void att) {
+                    }
+                });
+                await(latch);
+
+                // close ~half the channels
+                if (rand.nextBoolean())
+                    channels[i].close();
+            }
+        } finally {
+            // close remaining channels
+            for (int i=0; i<n; i++) {
+                if (channels[i] != null) channels[i].close();
+            }
+            executor.shutdown();
+        }
     }
 
     // exercise asynchronous close
@@ -409,17 +436,7 @@
                 .open(file, WRITE, SYNC);
 
             // start write operation
-            final CountDownLatch latch = new CountDownLatch(1);
-            Future<Integer> res = ch.write(genBuffer(), 0L, (Void)null,
-                new CompletionHandler<Integer,Void>() {
-                    public void completed(Integer result, Void att) {
-                    }
-                    public void failed(Throwable exc, Void att) {
-                    }
-                    public void cancelled(Void att) {
-                        latch.countDown();
-                    }
-            });
+            Future<Integer> res = ch.write(genBuffer(), 0L);
 
             // cancel operation
             boolean cancelled = res.cancel(mayInterruptIfRunning);
@@ -456,10 +473,6 @@
                 throw new RuntimeException(x);
             }
 
-            // check that cancelled method is invoked
-            if (cancelled)
-                await(latch);
-
             ch.close();
         }
     }
@@ -547,8 +560,6 @@
             }
             public void failed(Throwable exc, Long position) {
             }
-            public void cancelled(Long position) {
-            }
         });
 
         // wait for writes to complete
@@ -574,8 +585,6 @@
             }
             public void failed(Throwable exc, Long position) {
             }
-            public void cancelled(Long position) {
-            }
         });
 
         // wait for reads to complete