diff options
author | Sven Gothel <[email protected]> | 2014-10-03 03:12:42 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2014-10-03 03:12:42 +0200 |
commit | a7a3d5ab98ee0ad33fdef50bf081afeb8295ebe4 (patch) | |
tree | 8316bb60b02dfa3d1a61aed6640afeaaeab99c13 /src/junit/com/jogamp/common/nio/TestByteBufferOutputStream.java | |
parent | 00a9ee70054872712017b5a14b19aa92068c8420 (diff) |
MappedByteBuffer*Stream:
- Validate active and GC'ed mapped-buffer count
in cleanAllSlices() via close() ..
- Fix missing unmapping last buffer in notifyLengthChangeImpl(),
branch criteria was off by one.
- cleanSlice(..) now also issues cleanBuffer(..) on the GC'ed entry,
hence if WeakReference is still alive, enforce it's release.
- cleanBuffer(..) reverts FLUSH_PRE_HARD -> FLUSH_PRE_SOFT
in case of an error.
- flush() -> flush(boolean metaData) to expose FileChannel.force(metaData).
- Add synchronous mode, flushing/syncing the mapped buffers when
in READ_WRITE mapping mode and issue FileChannel.force() if not READ_ONLY.
Above is implemented via flush()/flushImpl(..) for buffers and FileChannel,
as well as in syncSlice(..) for buffers only.
flush*()/syncSlice() is covered by:
- setLength()
- notifyLengthChange*(..)
- nextSlice()
Always issue flushImpl() in close().
- Windows: Clean all buffers in setLength(),
otherwise Windows will report:
- Windows: Catch MappedByteBuffer.force() IOException
- Optimization of position(..)
position(..) is now standalone to allow issuing flushSlice(..)
before gathering the new mapped buffer.
This shall avoid one extra cache miss.
Hence rename positionImpl(..) -> position2(..).
- All MappedByteBufferOutputStream.write(..) methods
issue syncSlice(..) on the last written current slice
to ensure new 'synchronous' mode is honored.
+++
Unit tests:
- Ensure test files are being deleted
- TestByteBufferCopyStream: Reduced test file size to more sensible values.
-
Diffstat (limited to 'src/junit/com/jogamp/common/nio/TestByteBufferOutputStream.java')
-rw-r--r-- | src/junit/com/jogamp/common/nio/TestByteBufferOutputStream.java | 298 |
1 files changed, 157 insertions, 141 deletions
diff --git a/src/junit/com/jogamp/common/nio/TestByteBufferOutputStream.java b/src/junit/com/jogamp/common/nio/TestByteBufferOutputStream.java index c06dbe8..f57101e 100644 --- a/src/junit/com/jogamp/common/nio/TestByteBufferOutputStream.java +++ b/src/junit/com/jogamp/common/nio/TestByteBufferOutputStream.java @@ -52,180 +52,196 @@ public class TestByteBufferOutputStream extends JunitTracer { final int sliceShift) throws IOException { + testImpl(fname, payLoad, payLoadOffset, postPayLoadFiller, endBytes, sliceShift, false); + testImpl(fname, payLoad, payLoadOffset, postPayLoadFiller, endBytes, sliceShift, true); + } + static void testImpl(final String fname, + final byte[] payLoad, final long payLoadOffset, final long postPayLoadFiller, + final byte[] endBytes, + final int sliceShift, final boolean synchronous) + throws IOException + { final File file = new File(fname); file.delete(); file.createNewFile(); file.deleteOnExit(); - final RandomAccessFile out = new RandomAccessFile(file, "rw"); - final MappedByteBufferInputStream.FileResizeOp szOp = new MappedByteBufferInputStream.FileResizeOp() { - @Override - public void setLength(final long newSize) throws IOException { - out.setLength(newSize); - } - }; - final MappedByteBufferInputStream mis = new MappedByteBufferInputStream(out.getChannel(), - FileChannel.MapMode.READ_WRITE, - MappedByteBufferInputStream.CacheMode.FLUSH_PRE_SOFT, - sliceShift); - final MappedByteBufferOutputStream mos = mis.getOutputStream(szOp); - // resize to payLoad start and position to it - mos.setLength(payLoadOffset); - Assert.assertEquals(payLoadOffset, out.length()); - Assert.assertEquals(payLoadOffset, mos.length()); - Assert.assertEquals(0, mos.position()); // no change - mos.position(payLoadOffset); - Assert.assertEquals(payLoadOffset, mos.position()); + try { + final RandomAccessFile out = new RandomAccessFile(file, "rw"); + final MappedByteBufferInputStream.FileResizeOp szOp = new MappedByteBufferInputStream.FileResizeOp() { + @Override + public void setLength(final long newSize) throws IOException { + out.setLength(newSize); + } + }; + final MappedByteBufferInputStream mis = new MappedByteBufferInputStream(out.getChannel(), + FileChannel.MapMode.READ_WRITE, + MappedByteBufferInputStream.CacheMode.FLUSH_PRE_SOFT, + sliceShift); + final MappedByteBufferOutputStream mos = mis.getOutputStream(szOp); + mos.setSynchronous(synchronous); - // mark, write-expand payLoad - mis.mark(1); - mos.write(payLoad); - Assert.assertEquals(payLoadOffset+payLoad.length, out.length()); - Assert.assertEquals(payLoadOffset+payLoad.length, mos.length()); - Assert.assertEquals(payLoadOffset+payLoad.length, mos.position()); + try { + // resize to payLoad start and position to it + mos.setLength(payLoadOffset); + Assert.assertEquals(payLoadOffset, out.length()); + Assert.assertEquals(payLoadOffset, mos.length()); + Assert.assertEquals(0, mos.position()); // no change + mos.position(payLoadOffset); + Assert.assertEquals(payLoadOffset, mos.position()); - // expand + 1 - mos.setLength(payLoadOffset+payLoad.length+1); - Assert.assertEquals(payLoadOffset+payLoad.length+1, out.length()); - Assert.assertEquals(payLoadOffset+payLoad.length+1, mos.length()); - Assert.assertEquals(payLoadOffset+payLoad.length, mos.position()); // no change + // mark, write-expand payLoad + mis.mark(1); + mos.write(payLoad); + Assert.assertEquals(payLoadOffset+payLoad.length, out.length()); + Assert.assertEquals(payLoadOffset+payLoad.length, mos.length()); + Assert.assertEquals(payLoadOffset+payLoad.length, mos.position()); - // expand up-to very end, ahead of write - position to endBytes start - mos.setLength(payLoadOffset+payLoad.length+postPayLoadFiller+endBytes.length); - Assert.assertEquals(payLoadOffset+payLoad.length+postPayLoadFiller+endBytes.length, out.length()); - Assert.assertEquals(payLoadOffset+payLoad.length+postPayLoadFiller+endBytes.length, mos.length()); - Assert.assertEquals(payLoadOffset+payLoad.length, mos.position()); // no change - mos.skip(postPayLoadFiller); - Assert.assertEquals(payLoadOffset+payLoad.length+postPayLoadFiller, mos.position()); + // expand + 1 + mos.setLength(payLoadOffset+payLoad.length+1); + Assert.assertEquals(payLoadOffset+payLoad.length+1, out.length()); + Assert.assertEquals(payLoadOffset+payLoad.length+1, mos.length()); + Assert.assertEquals(payLoadOffset+payLoad.length, mos.position()); // no change - // write endBytes (no resize) - mos.write(endBytes); - Assert.assertEquals(payLoadOffset+payLoad.length+postPayLoadFiller+endBytes.length, mos.position()); + // expand up-to very end, ahead of write - position to endBytes start + mos.setLength(payLoadOffset+payLoad.length+postPayLoadFiller+endBytes.length); + Assert.assertEquals(payLoadOffset+payLoad.length+postPayLoadFiller+endBytes.length, out.length()); + Assert.assertEquals(payLoadOffset+payLoad.length+postPayLoadFiller+endBytes.length, mos.length()); + Assert.assertEquals(payLoadOffset+payLoad.length, mos.position()); // no change + mos.skip(postPayLoadFiller); + Assert.assertEquals(payLoadOffset+payLoad.length+postPayLoadFiller, mos.position()); - // Reset to payLoad, read it and verify - mis.reset(); - Assert.assertEquals(payLoadOffset, mos.position()); - Assert.assertEquals(payLoadOffset, mis.position()); - final byte[] tmp = new byte[payLoad.length]; - Assert.assertEquals(payLoad.length, mis.read(tmp)); - Assert.assertEquals(payLoadOffset+payLoad.length, mos.position()); - Assert.assertEquals(payLoadOffset+payLoad.length, mis.position()); - Assert.assertArrayEquals(payLoad, tmp); + // write endBytes (no resize) + mos.write(endBytes); + Assert.assertEquals(payLoadOffset+payLoad.length+postPayLoadFiller+endBytes.length, mos.position()); - // Shrink to end of payLoad, mark, read >= 0, reset .. redo - Assert.assertEquals(payLoadOffset+payLoad.length+postPayLoadFiller+endBytes.length, out.length()); - Assert.assertEquals(payLoadOffset+payLoad.length+postPayLoadFiller+endBytes.length, mos.length()); - mos.setLength(payLoadOffset+payLoad.length+1); - Assert.assertEquals(payLoadOffset+payLoad.length+1, out.length()); - Assert.assertEquals(payLoadOffset+payLoad.length+1, mos.length()); - Assert.assertEquals(payLoadOffset+payLoad.length, mos.position()); - mis.mark(1); - Assert.assertTrue(mis.read()>=0); - Assert.assertEquals(payLoadOffset+payLoad.length+1, mos.position()); - mis.reset(); - Assert.assertEquals(payLoadOffset+payLoad.length, mos.position()); - Assert.assertTrue(mis.read()>=0); - Assert.assertEquals(payLoadOffset+payLoad.length+1, mos.position()); + // Reset to payLoad, read it and verify + mis.reset(); + Assert.assertEquals(payLoadOffset, mos.position()); + Assert.assertEquals(payLoadOffset, mis.position()); + final byte[] tmp = new byte[payLoad.length]; + Assert.assertEquals(payLoad.length, mis.read(tmp)); + Assert.assertEquals(payLoadOffset+payLoad.length, mos.position()); + Assert.assertEquals(payLoadOffset+payLoad.length, mis.position()); + Assert.assertArrayEquals(payLoad, tmp); - // Shrink -1, read EOS - mos.setLength(payLoadOffset+payLoad.length); - Assert.assertEquals(payLoadOffset+payLoad.length, out.length()); - Assert.assertEquals(payLoadOffset+payLoad.length, mos.length()); - Assert.assertEquals(payLoadOffset+payLoad.length, mos.position()); - Assert.assertEquals(-1, mis.read()); + // Shrink to end of payLoad, mark, read >= 0, reset .. redo + Assert.assertEquals(payLoadOffset+payLoad.length+postPayLoadFiller+endBytes.length, out.length()); + Assert.assertEquals(payLoadOffset+payLoad.length+postPayLoadFiller+endBytes.length, mos.length()); + mos.setLength(payLoadOffset+payLoad.length+1); + Assert.assertEquals(payLoadOffset+payLoad.length+1, out.length()); + Assert.assertEquals(payLoadOffset+payLoad.length+1, mos.length()); + Assert.assertEquals(payLoadOffset+payLoad.length, mos.position()); + mis.mark(1); + Assert.assertTrue(mis.read()>=0); + Assert.assertEquals(payLoadOffset+payLoad.length+1, mos.position()); + mis.reset(); + Assert.assertEquals(payLoadOffset+payLoad.length, mos.position()); + Assert.assertTrue(mis.read()>=0); + Assert.assertEquals(payLoadOffset+payLoad.length+1, mos.position()); - // Expand + 1, mark, read >= 0, reset .. redo - mos.setLength(payLoadOffset+payLoad.length+1); - Assert.assertEquals(payLoadOffset+payLoad.length+1, out.length()); - Assert.assertEquals(payLoadOffset+payLoad.length+1, mos.length()); - Assert.assertEquals(payLoadOffset+payLoad.length, mos.position()); - mis.mark(1); - Assert.assertTrue(mis.read()>=0); - Assert.assertEquals(payLoadOffset+payLoad.length+1, mos.position()); - mis.reset(); - Assert.assertEquals(payLoadOffset+payLoad.length, mos.position()); - Assert.assertTrue(mis.read()>=0); - Assert.assertEquals(payLoadOffset+payLoad.length+1, mos.position()); + // Shrink -1, read EOS + mos.setLength(payLoadOffset+payLoad.length); + Assert.assertEquals(payLoadOffset+payLoad.length, out.length()); + Assert.assertEquals(payLoadOffset+payLoad.length, mos.length()); + Assert.assertEquals(payLoadOffset+payLoad.length, mos.position()); + Assert.assertEquals(-1, mis.read()); - // Shrink -1, read EOS, write-expand, reset and verify - mos.setLength(payLoadOffset+payLoad.length); - Assert.assertEquals(payLoadOffset+payLoad.length, out.length()); - Assert.assertEquals(payLoadOffset+payLoad.length, mos.length()); - Assert.assertEquals(payLoadOffset+payLoad.length, mos.position()); - Assert.assertEquals(-1, mis.read()); - mos.write('Z'); // expand while writing .. - Assert.assertEquals(payLoadOffset+payLoad.length+1, out.length()); - Assert.assertEquals(payLoadOffset+payLoad.length+1, mos.length()); - Assert.assertEquals(payLoadOffset+payLoad.length+1, mos.position()); - mis.reset(); - Assert.assertEquals(payLoadOffset+payLoad.length, mos.position()); - Assert.assertEquals(payLoadOffset+payLoad.length, mis.position()); - Assert.assertEquals('Z', mis.read()); - Assert.assertEquals(payLoadOffset+payLoad.length+1, mos.position()); - Assert.assertEquals(payLoadOffset+payLoad.length+1, mis.position()); + // Expand + 1, mark, read >= 0, reset .. redo + mos.setLength(payLoadOffset+payLoad.length+1); + Assert.assertEquals(payLoadOffset+payLoad.length+1, out.length()); + Assert.assertEquals(payLoadOffset+payLoad.length+1, mos.length()); + Assert.assertEquals(payLoadOffset+payLoad.length, mos.position()); + mis.mark(1); + Assert.assertTrue(mis.read()>=0); + Assert.assertEquals(payLoadOffset+payLoad.length+1, mos.position()); + mis.reset(); + Assert.assertEquals(payLoadOffset+payLoad.length, mos.position()); + Assert.assertTrue(mis.read()>=0); + Assert.assertEquals(payLoadOffset+payLoad.length+1, mos.position()); - // Shrink -2, shall clear mark, test reset failure - mos.setLength(payLoadOffset+payLoad.length-1); - Assert.assertEquals(payLoadOffset+payLoad.length-1, out.length()); - Assert.assertEquals(payLoadOffset+payLoad.length-1, mos.length()); - Assert.assertEquals(payLoadOffset+payLoad.length-1, mos.position()); - try { - mis.reset(); - Assert.assertTrue(false); // shall not reach - } catch( final IOException ioe ) { - Assert.assertNotNull(ioe); - } - mis.mark(1); + // Shrink -1, read EOS, write-expand, reset and verify + mos.setLength(payLoadOffset+payLoad.length); + Assert.assertEquals(payLoadOffset+payLoad.length, out.length()); + Assert.assertEquals(payLoadOffset+payLoad.length, mos.length()); + Assert.assertEquals(payLoadOffset+payLoad.length, mos.position()); + Assert.assertEquals(-1, mis.read()); + mos.write('Z'); // expand while writing .. + Assert.assertEquals(payLoadOffset+payLoad.length+1, out.length()); + Assert.assertEquals(payLoadOffset+payLoad.length+1, mos.length()); + Assert.assertEquals(payLoadOffset+payLoad.length+1, mos.position()); + mis.reset(); + Assert.assertEquals(payLoadOffset+payLoad.length, mos.position()); + Assert.assertEquals(payLoadOffset+payLoad.length, mis.position()); + Assert.assertEquals('Z', mis.read()); + Assert.assertEquals(payLoadOffset+payLoad.length+1, mos.position()); + Assert.assertEquals(payLoadOffset+payLoad.length+1, mis.position()); - // ZERO file, test reset failure, read EOS, write-expand - mos.setLength(0); - Assert.assertEquals(0, out.length()); - Assert.assertEquals(0, mos.length()); - Assert.assertEquals(0, mos.position()); - try { - mis.reset(); - Assert.assertTrue(false); // shall not reach - } catch( final IOException ioe ) { - Assert.assertNotNull(ioe); - } - Assert.assertEquals(-1, mis.read()); - mos.write('Z'); // expand while writing .. - Assert.assertEquals(1, out.length()); - Assert.assertEquals(1, mos.length()); - Assert.assertEquals(1, mos.position()); - mis.position(0); - Assert.assertEquals(0, mos.position()); - Assert.assertEquals(0, mis.position()); - Assert.assertEquals('Z', mis.read()); + // Shrink -2, shall clear mark, test reset failure + mos.setLength(payLoadOffset+payLoad.length-1); + Assert.assertEquals(payLoadOffset+payLoad.length-1, out.length()); + Assert.assertEquals(payLoadOffset+payLoad.length-1, mos.length()); + Assert.assertEquals(payLoadOffset+payLoad.length-1, mos.position()); + try { + mis.reset(); + Assert.assertTrue(false); // shall not reach + } catch( final IOException ioe ) { + Assert.assertNotNull(ioe); + } + mis.mark(1); - mos.close(); - mis.close(); - out.close(); - file.delete(); + // ZERO file, test reset failure, read EOS, write-expand + mos.setLength(0); + Assert.assertEquals(0, out.length()); + Assert.assertEquals(0, mos.length()); + Assert.assertEquals(0, mos.position()); + try { + mis.reset(); + Assert.assertTrue(false); // shall not reach + } catch( final IOException ioe ) { + Assert.assertNotNull(ioe); + } + Assert.assertEquals(-1, mis.read()); + mos.write('Z'); // expand while writing .. + Assert.assertEquals(1, out.length()); + Assert.assertEquals(1, mos.length()); + Assert.assertEquals(1, mos.position()); + mis.position(0); + Assert.assertEquals(0, mos.position()); + Assert.assertEquals(0, mis.position()); + Assert.assertEquals('Z', mis.read()); + } finally { + mos.close(); + mis.close(); + out.close(); + } + } finally { + file.delete(); + } } @Test public void test00() throws IOException { final int sliceShift = 13; // 8192 bytes per slice - testImpl("./test01.bin", "123456789AB".getBytes(), 0L, 0L, "EOF".getBytes(), sliceShift); + testImpl(getSimpleTestName(".")+".bin", "123456789AB".getBytes(), 0L, 0L, "EOF".getBytes(), sliceShift); } @Test public void test01() throws IOException { final int sliceShift = 13; // 8192 bytes per slice - testImpl("./test01.bin", "123456789AB".getBytes(), 9000L, 100L, "EOF".getBytes(), sliceShift); + testImpl(getSimpleTestName(".")+".bin", "123456789AB".getBytes(), 9000L, 100L, "EOF".getBytes(), sliceShift); } @Test public void test02() throws IOException { final int sliceShift = 13; // 8192 bytes per slice - testImpl("./test01.bin", "123456789AB".getBytes(), 8189L, 9001L, "EOF".getBytes(), sliceShift); + testImpl(getSimpleTestName(".")+".bin", "123456789AB".getBytes(), 8189L, 9001L, "EOF".getBytes(), sliceShift); } @Test public void test03() throws IOException { final int sliceShift = 13; // 8192 bytes per slice - testImpl("./test01.bin", "123456789AB".getBytes(), 58189L, 109001L, "EOF".getBytes(), sliceShift); + testImpl(getSimpleTestName(".")+".bin", "123456789AB".getBytes(), 58189L, 109001L, "EOF".getBytes(), sliceShift); } @Test @@ -235,7 +251,7 @@ public class TestByteBufferOutputStream extends JunitTracer { for(int i=0; i<payLoad.length; i++) { payLoad[i] = (byte)('A' + i%26); } - testImpl("./test01.bin", payLoad, 0L, 0L, "EOF".getBytes(), sliceShift); + testImpl(getSimpleTestName(".")+".bin", payLoad, 0L, 0L, "EOF".getBytes(), sliceShift); } @Test @@ -245,7 +261,7 @@ public class TestByteBufferOutputStream extends JunitTracer { for(int i=0; i<payLoad.length; i++) { payLoad[i] = (byte)('A' + i%26); } - testImpl("./test01.bin", payLoad, 1030L, 99L, "EOF".getBytes(), sliceShift); + testImpl(getSimpleTestName(".")+".bin", payLoad, 1030L, 99L, "EOF".getBytes(), sliceShift); } @Test @@ -255,7 +271,7 @@ public class TestByteBufferOutputStream extends JunitTracer { for(int i=0; i<payLoad.length; i++) { payLoad[i] = (byte)('A' + i%26); } - testImpl("./test01.bin", payLoad, 1021L, 1301L, "EOF".getBytes(), sliceShift); + testImpl(getSimpleTestName(".")+".bin", payLoad, 1021L, 1301L, "EOF".getBytes(), sliceShift); } @Test @@ -265,7 +281,7 @@ public class TestByteBufferOutputStream extends JunitTracer { for(int i=0; i<payLoad.length; i++) { payLoad[i] = (byte)('A' + i%26); } - testImpl("./test01.bin", payLoad, 3021L, 6301L, "EOF".getBytes(), sliceShift); + testImpl(getSimpleTestName(".")+".bin", payLoad, 3021L, 6301L, "EOF".getBytes(), sliceShift); } public static void main(final String args[]) throws IOException { |