changeset 3652:f34dcfeecc8d

7012823: TEST_BUG: java/nio/MappedByteBuffer tests leave file mappingsthat prevent clean-up (win) Reviewed-by: forax
author alanb
date Mon, 07 Feb 2011 13:53:36 +0000
parents b0bd638036bd
children 32f1c5f67aae
files test/java/nio/MappedByteBuffer/Force.java test/java/nio/MappedByteBuffer/ZeroMap.java
diffstat 2 files changed, 26 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/test/java/nio/MappedByteBuffer/Force.java	Mon Feb 07 09:11:26 2011 +0000
+++ b/test/java/nio/MappedByteBuffer/Force.java	Mon Feb 07 13:53:36 2011 +0000
@@ -37,14 +37,17 @@
         Random random = new Random();
         long filesize = random.nextInt(3*1024*1024);
         int cut = random.nextInt((int)filesize);
-        File file = new File("Blah");
-        RandomAccessFile raf = new RandomAccessFile(file, "rw");
-        raf.setLength(filesize);
-        FileChannel fc = raf.getChannel();
-        MappedByteBuffer buf1 = fc.map(
-                        FileChannel.MapMode.READ_WRITE, cut, filesize-cut);
-        buf1.force();
-        fc.close();
-        raf.close();
+        File file = File.createTempFile("Blah", null);
+        file.deleteOnExit();
+        try (RandomAccessFile raf = new RandomAccessFile(file, "rw")) {
+            raf.setLength(filesize);
+            FileChannel fc = raf.getChannel();
+            MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_WRITE, cut, filesize-cut);
+            mbb.force();
+        }
+
+        // improve chance that mapped buffer will be unmapped
+        System.gc();
+        Thread.sleep(500);
     }
 }
--- a/test/java/nio/MappedByteBuffer/ZeroMap.java	Mon Feb 07 09:11:26 2011 +0000
+++ b/test/java/nio/MappedByteBuffer/ZeroMap.java	Mon Feb 07 13:53:36 2011 +0000
@@ -37,16 +37,19 @@
         Random random = new Random();
         long filesize = random.nextInt(1024*1024);
         int cut = random.nextInt((int)filesize);
-        File file = new File("Blah");
-        RandomAccessFile raf = new RandomAccessFile(file, "rw");
-        raf.setLength(filesize);
-        FileChannel fc = raf.getChannel();
-        MappedByteBuffer buf1 = fc.map(
-                        FileChannel.MapMode.READ_WRITE, cut, 0);
-        buf1.force();
-        buf1.load();
-        buf1.isLoaded();
-        fc.close();
-        raf.close();
+        File file = File.createTempFile("Blah", null);
+        file.deleteOnExit();
+        try (RandomAccessFile raf = new RandomAccessFile(file, "rw")) {
+            raf.setLength(filesize);
+            FileChannel fc = raf.getChannel();
+            MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_WRITE, cut, 0);
+            mbb.force();
+            mbb.load();
+            mbb.isLoaded();
+       }
+
+        // improve chance that mapped buffer will be unmapped
+        System.gc();
+        Thread.sleep(500);
     }
 }