diff --git a/community/io/src/test/java/org/neo4j/io/fs/FileSystemAbstractionInterruptionTest.java b/community/io/src/test/java/org/neo4j/io/fs/FileSystemAbstractionInterruptionTest.java index 02b45b00bdd31..3ebfeaf30c8d7 100644 --- a/community/io/src/test/java/org/neo4j/io/fs/FileSystemAbstractionInterruptionTest.java +++ b/community/io/src/test/java/org/neo4j/io/fs/FileSystemAbstractionInterruptionTest.java @@ -101,6 +101,7 @@ public void verifyInterruptionAndChannelState() throws IOException // This is good. What we expect to see. } } + channel.close(); fs.close(); } diff --git a/community/io/src/test/java/org/neo4j/io/pagecache/PageCacheTest.java b/community/io/src/test/java/org/neo4j/io/pagecache/PageCacheTest.java index 29ec6eeed0404..1e978ddbb14ec 100644 --- a/community/io/src/test/java/org/neo4j/io/pagecache/PageCacheTest.java +++ b/community/io/src/test/java/org/neo4j/io/pagecache/PageCacheTest.java @@ -1739,51 +1739,52 @@ public void pagesAddedWithNextWithPageIdMustBeAccessibleWithNoGrowSpecified() th public void writesOfDifferentUnitsMustHaveCorrectEndianess() throws Exception { configureStandardPageCache(); - PagedFile pagedFile = pageCache.map( file( "a" ), 20 ); - - try ( PageCursor cursor = pagedFile.io( 0, PF_SHARED_WRITE_LOCK ) ) + try ( PagedFile pagedFile = pageCache.map( file( "a" ), 20 ) ) { - assertTrue( cursor.next() ); - byte[] data = { 42, 43, 44, 45, 46 }; - cursor.putLong( 41 ); // 0+8 = 8 - cursor.putInt( 41 ); // 8+4 = 12 - cursor.putShort( (short) 41 ); // 12+2 = 14 - cursor.putByte( (byte) 41 ); // 14+1 = 15 - cursor.putBytes( data ); // 15+5 = 20 - } + try ( PageCursor cursor = pagedFile.io( 0, PF_SHARED_WRITE_LOCK ) ) + { + assertTrue( cursor.next() ); + byte[] data = {42, 43, 44, 45, 46}; - try ( PageCursor cursor = pagedFile.io( 0, PF_SHARED_WRITE_LOCK ) ) - { - assertTrue( cursor.next() ); + cursor.putLong( 41 ); // 0+8 = 8 + cursor.putInt( 41 ); // 8+4 = 12 + cursor.putShort( (short) 41 ); // 12+2 = 14 + cursor.putByte( (byte) 41 ); // 14+1 = 15 + cursor.putBytes( data ); // 15+5 = 20 + } - long a = cursor.getLong(); // 8 - int b = cursor.getInt(); // 12 - short c = cursor.getShort();// 14 - byte[] data = new byte[] { - cursor.getByte(), // 15 - cursor.getByte(), // 16 - cursor.getByte(), // 17 - cursor.getByte(), // 18 - cursor.getByte(), // 19 - cursor.getByte() // 20 - }; - cursor.setOffset( 0 ); - cursor.putLong( 1 + a ); - cursor.putInt( 1 + b ); - cursor.putShort( (short) (1 + c) ); - for ( byte d : data ) + try ( PageCursor cursor = pagedFile.io( 0, PF_SHARED_WRITE_LOCK ) ) { - d++; - cursor.putByte( d ); + assertTrue( cursor.next() ); + + long a = cursor.getLong(); // 8 + int b = cursor.getInt(); // 12 + short c = cursor.getShort();// 14 + byte[] data = new byte[]{cursor.getByte(), // 15 + cursor.getByte(), // 16 + cursor.getByte(), // 17 + cursor.getByte(), // 18 + cursor.getByte(), // 19 + cursor.getByte() // 20 + }; + cursor.setOffset( 0 ); + cursor.putLong( 1 + a ); + cursor.putInt( 1 + b ); + cursor.putShort( (short) (1 + c) ); + for ( byte d : data ) + { + d++; + cursor.putByte( d ); + } } } - pagedFile.close(); - - StoreChannel channel = fs.open( file( "a" ), "r" ); ByteBuffer buf = ByteBuffer.allocate( 20 ); - channel.read( buf ); + try ( StoreChannel channel = fs.open( file( "a" ), "r" ) ) + { + channel.read( buf ); + } buf.flip(); assertThat( buf.getLong(), is( 42L ) ); @@ -2067,81 +2068,87 @@ public void evictionMustFlushPagesToTheRightFiles() throws IOException int filePageSize2 = filePageSize - 3; // diff. page size just to be difficult long maxPageIdCursor1 = recordCount / recordsPerFilePage; File file2 = file( "b" ); - OutputStream outputStream = fs.openAsOutputStream( file2, false ); long file2sizeBytes = (maxPageIdCursor1 + 17) * filePageSize2; - for ( int i = 0; i < file2sizeBytes; i++ ) + try ( OutputStream outputStream = fs.openAsOutputStream( file2, false ) ) { - // We will ues the page cache to change these 'a's into 'b's. - outputStream.write( 'a' ); + for ( int i = 0; i < file2sizeBytes; i++ ) + { + // We will ues the page cache to change these 'a's into 'b's. + outputStream.write( 'a' ); + } + outputStream.flush(); } - outputStream.flush(); - outputStream.close(); configureStandardPageCache(); - PagedFile pagedFile1 = pageCache.map( file( "a" ), filePageSize ); - PagedFile pagedFile2 = pageCache.map( file2, filePageSize2 ); - - long pageId1 = 0; - long pageId2 = 0; - boolean moreWorkToDo; - do + try ( PagedFile pagedFile1 = pageCache.map( file( "a" ), filePageSize ) ) { - boolean cursorReady1; - boolean cursorReady2; - - try ( PageCursor cursor = pagedFile1.io( pageId1, PF_SHARED_WRITE_LOCK ) ) + try ( PagedFile pagedFile2 = pageCache.map( file2, filePageSize2 ) ) { - cursorReady1 = cursor.next() && cursor.getCurrentPageId() < maxPageIdCursor1; - if ( cursorReady1 ) - { - writeRecords( cursor ); - pageId1++; - } - } - try ( PageCursor cursor = pagedFile2.io( pageId2, PF_SHARED_WRITE_LOCK | PF_NO_GROW ) ) - { - cursorReady2 = cursor.next(); - if ( cursorReady2 ) + long pageId1 = 0; + long pageId2 = 0; + boolean moreWorkToDo; + do { - for ( int i = 0; i < filePageSize2; i++ ) + boolean cursorReady1; + boolean cursorReady2; + + try ( PageCursor cursor = pagedFile1.io( pageId1, PF_SHARED_WRITE_LOCK ) ) { - cursor.putByte( (byte) 'b' ); + cursorReady1 = cursor.next() && cursor.getCurrentPageId() < maxPageIdCursor1; + if ( cursorReady1 ) + { + writeRecords( cursor ); + pageId1++; + } } - assertFalse( cursor.shouldRetry() ); + + try ( PageCursor cursor = pagedFile2.io( pageId2, PF_SHARED_WRITE_LOCK | PF_NO_GROW ) ) + { + cursorReady2 = cursor.next(); + if ( cursorReady2 ) + { + for ( int i = 0; i < filePageSize2; i++ ) + { + cursor.putByte( (byte) 'b' ); + } + assertFalse( cursor.shouldRetry() ); + } + pageId2++; + } + + moreWorkToDo = cursorReady1 || cursorReady2; } - pageId2++; - } + while ( moreWorkToDo ); - moreWorkToDo = cursorReady1 || cursorReady2; + } } - while ( moreWorkToDo ); - - pagedFile1.close(); - pagedFile2.close(); // Verify the file contents assertThat( fs.getFileSize( file2 ), is( file2sizeBytes ) ); - InputStream inputStream = fs.openAsInputStream( file2 ); - for ( int i = 0; i < file2sizeBytes; i++ ) + try ( InputStream inputStream = fs.openAsInputStream( file2 ) ) { - int b = inputStream.read(); - assertThat( b, is( (int) 'b' ) ); + for ( int i = 0; i < file2sizeBytes; i++ ) + { + int b = inputStream.read(); + assertThat( b, is( (int) 'b' ) ); + } + assertThat( inputStream.read(), is( -1 ) ); } - assertThat( inputStream.read(), is( -1 ) ); - inputStream.close(); - StoreChannel channel = fs.open( file( "a" ), "r" ); - ByteBuffer bufB = ByteBuffer.allocate( recordSize ); - for ( int i = 0; i < recordCount; i++ ) + try ( StoreChannel channel = fs.open( file( "a" ), "r" ) ) { - bufA.clear(); - channel.read( bufA ); - bufA.flip(); - bufB.clear(); - generateRecordForId( i, bufB ); - assertThat( bufB.array(), byteArray( bufA.array() ) ); + ByteBuffer bufB = ByteBuffer.allocate( recordSize ); + for ( int i = 0; i < recordCount; i++ ) + { + bufA.clear(); + channel.read( bufA ); + bufA.flip(); + bufB.clear(); + generateRecordForId( i, bufB ); + assertThat( bufB.array(), byteArray( bufA.array() ) ); + } } } diff --git a/community/io/src/test/java/org/neo4j/io/pagecache/impl/muninn/MuninnPageCacheTest.java b/community/io/src/test/java/org/neo4j/io/pagecache/impl/muninn/MuninnPageCacheTest.java index 5a50ac1b5349a..4665983d1ea24 100644 --- a/community/io/src/test/java/org/neo4j/io/pagecache/impl/muninn/MuninnPageCacheTest.java +++ b/community/io/src/test/java/org/neo4j/io/pagecache/impl/muninn/MuninnPageCacheTest.java @@ -19,6 +19,7 @@ */ package org.neo4j.io.pagecache.impl.muninn; +import org.apache.commons.lang3.mutable.MutableBoolean; import org.junit.Test; import java.io.File; @@ -96,32 +97,25 @@ public void mustEvictCleanPageWithoutFlushing() throws Exception RecordingPageCursorTracer cursorTracer = new RecordingPageCursorTracer(); ConfigurablePageCursorTracerSupplier cursorTracerSupplier = new ConfigurablePageCursorTracerSupplier( cursorTracer ); - MuninnPageCache pageCache = createPageCache( fs, 2, 8, blockCacheFlush( tracer ), cursorTracerSupplier ); - PagedFile pagedFile = pageCache.map( file( "a" ), 8 ); - - try ( PageCursor cursor = pagedFile.io( 0, PF_SHARED_READ_LOCK ) ) + try ( MuninnPageCache pageCache = createPageCache( fs, 2, 8, blockCacheFlush( tracer ), cursorTracerSupplier ) ) { - assertTrue( cursor.next() ); - } - cursorTracer.reportEvents(); - assertNotNull( cursorTracer.observe( Fault.class ) ); - assertEquals( 1, cursorTracer.faults() ); - assertEquals( 1, tracer.faults() ); - - int clockArm = pageCache.evictPages( 1, 0, tracer.beginPageEvictions( 1 ) ); - assertThat( clockArm, is( 1 ) ); - assertNotNull( tracer.observe( Evict.class ) ); - } + try ( PagedFile pagedFile = pageCache.map( file( "a" ), 8 ) ) + { - private void writeInitialDataTo( File file ) throws IOException - { - StoreChannel channel = fs.create( file ); - ByteBuffer buf = ByteBuffer.allocate( 16 ); - buf.putLong( x ); - buf.putLong( y ); - buf.flip(); - channel.writeAll( buf ); - channel.close(); + try ( PageCursor cursor = pagedFile.io( 0, PF_SHARED_READ_LOCK ) ) + { + assertTrue( cursor.next() ); + } + cursorTracer.reportEvents(); + assertNotNull( cursorTracer.observe( Fault.class ) ); + assertEquals( 1, cursorTracer.faults() ); + assertEquals( 1, tracer.faults() ); + + int clockArm = pageCache.evictPages( 1, 0, tracer.beginPageEvictions( 1 ) ); + assertThat( clockArm, is( 1 ) ); + assertNotNull( tracer.observe( Evict.class ) ); + } + } } @Test @@ -132,30 +126,30 @@ public void mustFlushDirtyPagesOnEvictingFirstPage() throws Exception RecordingPageCursorTracer cursorTracer = new RecordingPageCursorTracer(); ConfigurablePageCursorTracerSupplier cursorTracerSupplier = new ConfigurablePageCursorTracerSupplier( cursorTracer ); - MuninnPageCache pageCache = createPageCache( fs, 2, 8, blockCacheFlush( tracer ), - cursorTracerSupplier ); - PagedFile pagedFile = pageCache.map( file( "a" ), 8 ); - - try ( PageCursor cursor = pagedFile.io( 0, PF_SHARED_WRITE_LOCK ) ) + try ( MuninnPageCache pageCache = createPageCache( fs, 2, 8, blockCacheFlush( tracer ), cursorTracerSupplier ) ) { - assertTrue( cursor.next() ); - cursor.putLong( 0L ); + try ( PagedFile pagedFile = pageCache.map( file( "a" ), 8 ) ) + { + + try ( PageCursor cursor = pagedFile.io( 0, PF_SHARED_WRITE_LOCK ) ) + { + assertTrue( cursor.next() ); + cursor.putLong( 0L ); + } + cursorTracer.reportEvents(); + assertNotNull( cursorTracer.observe( Fault.class ) ); + assertEquals( 1, cursorTracer.faults() ); + assertEquals( 1, tracer.faults() ); + + int clockArm = pageCache.evictPages( 1, 0, tracer.beginPageEvictions( 1 ) ); + assertThat( clockArm, is( 1 ) ); + assertNotNull( tracer.observe( Evict.class ) ); + + ByteBuffer buf = readIntoBuffer( "a" ); + assertThat( buf.getLong(), is( 0L ) ); + assertThat( buf.getLong(), is( y ) ); + } } - cursorTracer.reportEvents(); - assertNotNull( cursorTracer.observe( Fault.class ) ); - assertEquals( 1, cursorTracer.faults() ); - assertEquals( 1, tracer.faults() ); - - int clockArm = pageCache.evictPages( 1, 0, tracer.beginPageEvictions( 1 ) ); - assertThat( clockArm, is( 1 ) ); - assertNotNull( tracer.observe( Evict.class ) ); - - ByteBuffer buf = ByteBuffer.allocate( 16 ); - StoreChannel channel = fs.open( file( "a" ), "r" ); - channel.read( buf ); - buf.flip(); - assertThat( buf.getLong(), is( 0L ) ); - assertThat( buf.getLong(), is( y ) ); } @Test @@ -166,30 +160,30 @@ public void mustFlushDirtyPagesOnEvictingLastPage() throws Exception RecordingPageCursorTracer cursorTracer = new RecordingPageCursorTracer(); ConfigurablePageCursorTracerSupplier cursorTracerSupplier = new ConfigurablePageCursorTracerSupplier( cursorTracer ); - MuninnPageCache pageCache = createPageCache( fs, 2, 8, blockCacheFlush( tracer ), - cursorTracerSupplier ); - PagedFile pagedFile = pageCache.map( file( "a" ), 8 ); - - try ( PageCursor cursor = pagedFile.io( 1, PF_SHARED_WRITE_LOCK ) ) + try ( MuninnPageCache pageCache = createPageCache( fs, 2, 8, blockCacheFlush( tracer ), cursorTracerSupplier ) ) { - assertTrue( cursor.next() ); - cursor.putLong( 0L ); + try ( PagedFile pagedFile = pageCache.map( file( "a" ), 8 ) ) + { + + try ( PageCursor cursor = pagedFile.io( 1, PF_SHARED_WRITE_LOCK ) ) + { + assertTrue( cursor.next() ); + cursor.putLong( 0L ); + } + cursorTracer.reportEvents(); + assertNotNull( cursorTracer.observe( Fault.class ) ); + assertEquals( 1, cursorTracer.faults() ); + assertEquals( 1, tracer.faults() ); + + int clockArm = pageCache.evictPages( 1, 0, tracer.beginPageEvictions( 1 ) ); + assertThat( clockArm, is( 1 ) ); + assertNotNull( tracer.observe( Evict.class ) ); + + ByteBuffer buf = readIntoBuffer( "a" ); + assertThat( buf.getLong(), is( x ) ); + assertThat( buf.getLong(), is( 0L ) ); + } } - cursorTracer.reportEvents(); - assertNotNull( cursorTracer.observe( Fault.class ) ); - assertEquals( 1, cursorTracer.faults() ); - assertEquals( 1, tracer.faults() ); - - int clockArm = pageCache.evictPages( 1, 0, tracer.beginPageEvictions( 1 ) ); - assertThat( clockArm, is( 1 ) ); - assertNotNull( tracer.observe( Evict.class ) ); - - ByteBuffer buf = ByteBuffer.allocate( 16 ); - StoreChannel channel = fs.open( file( "a" ), "r" ); - channel.read( buf ); - buf.flip(); - assertThat( buf.getLong(), is( x ) ); - assertThat( buf.getLong(), is( 0L ) ); } @Test @@ -200,35 +194,35 @@ public void mustFlushDirtyPagesOnEvictingAllPages() throws Exception RecordingPageCursorTracer cursorTracer = new RecordingPageCursorTracer( Fault.class ); ConfigurablePageCursorTracerSupplier cursorTracerSupplier = new ConfigurablePageCursorTracerSupplier( cursorTracer ); - MuninnPageCache pageCache = createPageCache( fs, 4, 8, blockCacheFlush( tracer ), - cursorTracerSupplier ); - PagedFile pagedFile = pageCache.map( file( "a" ), 8 ); - - try ( PageCursor cursor = pagedFile.io( 0, PF_SHARED_WRITE_LOCK | PF_NO_GROW ) ) + try ( MuninnPageCache pageCache = createPageCache( fs, 4, 8, blockCacheFlush( tracer ), cursorTracerSupplier ) ) { - assertTrue( cursor.next() ); - cursor.putLong( 0L ); - assertTrue( cursor.next() ); - cursor.putLong( 0L ); - assertFalse( cursor.next() ); + try ( PagedFile pagedFile = pageCache.map( file( "a" ), 8 ) ) + { + + try ( PageCursor cursor = pagedFile.io( 0, PF_SHARED_WRITE_LOCK | PF_NO_GROW ) ) + { + assertTrue( cursor.next() ); + cursor.putLong( 0L ); + assertTrue( cursor.next() ); + cursor.putLong( 0L ); + assertFalse( cursor.next() ); + } + cursorTracer.reportEvents(); + assertNotNull( cursorTracer.observe( Fault.class ) ); + assertNotNull( cursorTracer.observe( Fault.class ) ); + assertEquals( 2, cursorTracer.faults() ); + assertEquals( 2, tracer.faults() ); + + int clockArm = pageCache.evictPages( 2, 0, tracer.beginPageEvictions( 2 ) ); + assertThat( clockArm, is( 2 ) ); + assertNotNull( tracer.observe( Evict.class ) ); + assertNotNull( tracer.observe( Evict.class ) ); + + ByteBuffer buf = readIntoBuffer( "a" ); + assertThat( buf.getLong(), is( 0L ) ); + assertThat( buf.getLong(), is( 0L ) ); + } } - cursorTracer.reportEvents(); - assertNotNull( cursorTracer.observe( Fault.class ) ); - assertNotNull( cursorTracer.observe( Fault.class ) ); - assertEquals( 2, cursorTracer.faults() ); - assertEquals( 2, tracer.faults() ); - - int clockArm = pageCache.evictPages( 2, 0, tracer.beginPageEvictions( 2 ) ); - assertThat( clockArm, is( 2 ) ); - assertNotNull( tracer.observe( Evict.class ) ); - assertNotNull( tracer.observe( Evict.class ) ); - - ByteBuffer buf = ByteBuffer.allocate( 16 ); - StoreChannel channel = fs.open( file( "a" ), "r" ); - channel.read( buf ); - buf.flip(); - assertThat( buf.getLong(), is( 0L ) ); - assertThat( buf.getLong(), is( 0L ) ); } @Test @@ -236,41 +230,42 @@ public void closingTheCursorMustUnlockModifiedPage() throws Exception { writeInitialDataTo( file( "a" ) ); - final MuninnPageCache pageCache = createPageCache( fs, 2, 8, PageCacheTracer.NULL, - DefaultPageCursorTracerSupplier.INSTANCE ); - final PagedFile pagedFile = pageCache.map( file( "a" ), 8 ); - - Future task = executor.submit( () -> + try ( MuninnPageCache pageCache = createPageCache( fs, 2, 8, PageCacheTracer.NULL, + DefaultPageCursorTracerSupplier.INSTANCE ) ) { - try ( PageCursor cursor = pagedFile.io( 0, PF_SHARED_WRITE_LOCK ) ) - { - assertTrue( cursor.next() ); - cursor.putLong( 41 ); - } - catch ( IOException e ) + try ( PagedFile pagedFile = pageCache.map( file( "a" ), 8 ) ) { - throw new RuntimeException( e ); - } - } ); - task.get(); - try ( PageCursor cursor = pagedFile.io( 0, PF_SHARED_WRITE_LOCK ) ) - { - assertTrue( cursor.next() ); - long value = cursor.getLong(); - cursor.setOffset( 0 ); - cursor.putLong( value + 1 ); - } + Future task = executor.submit( () -> + { + try ( PageCursor cursor = pagedFile.io( 0, PF_SHARED_WRITE_LOCK ) ) + { + assertTrue( cursor.next() ); + cursor.putLong( 41 ); + } + catch ( IOException e ) + { + throw new RuntimeException( e ); + } + } ); + task.get(); - int clockArm = pageCache.evictPages( 1, 0, EvictionRunEvent.NULL ); - assertThat( clockArm, is( 1 ) ); + try ( PageCursor cursor = pagedFile.io( 0, PF_SHARED_WRITE_LOCK ) ) + { + assertTrue( cursor.next() ); + long value = cursor.getLong(); + cursor.setOffset( 0 ); + cursor.putLong( value + 1 ); + } + + int clockArm = pageCache.evictPages( 1, 0, EvictionRunEvent.NULL ); + assertThat( clockArm, is( 1 ) ); - ByteBuffer buf = ByteBuffer.allocate( 16 ); - StoreChannel channel = fs.open( file( "a" ), "r" ); - channel.read( buf ); - buf.flip(); - assertThat( buf.getLong(), is( 42L ) ); - assertThat( buf.getLong(), is( y ) ); + ByteBuffer buf = readIntoBuffer( "a" ); + assertThat( buf.getLong(), is( 42L ) ); + assertThat( buf.getLong(), is( y ) ); + } + } } @Test( timeout = SEMI_LONG_TIMEOUT_MILLIS ) @@ -278,6 +273,7 @@ public void mustUnblockPageFaultersWhenEvictionGetsException() throws Exception { writeInitialDataTo( file( "a" ) ); + MutableBoolean throwException = new MutableBoolean( true ); FileSystemAbstraction fs = new DelegatingFileSystemAbstraction( this.fs ) { @Override @@ -288,30 +284,43 @@ public StoreChannel open( File fileName, String mode ) throws IOException @Override public void writeAll( ByteBuffer src, long position ) throws IOException { - throw new IOException( "uh-oh..." ); + if ( throwException.booleanValue() ) + { + throw new IOException( "uh-oh..." ); + } + else + { + super.writeAll( src, position ); + } } }; } }; - MuninnPageCache pageCache = createPageCache( fs, 2, 8, PageCacheTracer.NULL, - DefaultPageCursorTracerSupplier.INSTANCE ); - final PagedFile pagedFile = pageCache.map( file( "a" ), 8 ); - - // The basic idea is that this loop, which will encounter a lot of page faults, must not block forever even - // though the eviction thread is unable to flush any dirty pages because the file system throws exceptions on - // all writes. - try ( PageCursor cursor = pagedFile.io( 0, PF_SHARED_WRITE_LOCK ) ) + try ( MuninnPageCache pageCache = createPageCache( fs, 2, 8, PageCacheTracer.NULL, + DefaultPageCursorTracerSupplier.INSTANCE ) ) { - for ( int i = 0; i < 1000; i++ ) + try ( PagedFile pagedFile = pageCache.map( file( "a" ), 8 ) ) { - assertTrue( cursor.next() ); + + // The basic idea is that this loop, which will encounter a lot of page faults, must not block forever even + // though the eviction thread is unable to flush any dirty pages because the file system throws exceptions on + // all writes. + try ( PageCursor cursor = pagedFile.io( 0, PF_SHARED_WRITE_LOCK ) ) + { + for ( int i = 0; i < 1000; i++ ) + { + assertTrue( cursor.next() ); + } + fail( "Expected an exception at this point" ); + } + catch ( IOException ignore ) + { + // Good. + } + + throwException.setFalse(); } - fail( "Expected an exception at this point" ); - } - catch ( IOException ignore ) - { - // Good. } } @@ -320,33 +329,58 @@ public void mustThrowIfMappingFileWouldOverflowReferenceCount() throws Exception { File file = file( "a" ); writeInitialDataTo( file ); - MuninnPageCache pageCache = createPageCache( fs, 30, pageCachePageSize, PageCacheTracer.NULL, - DefaultPageCursorTracerSupplier.NULL ); - PagedFile pf = null; - int i = 0; - - try - { - expectedException.expect( IllegalStateException.class ); - for ( ; i < Integer.MAX_VALUE; i++ ) - { - pf = pageCache.map( file, filePageSize ); - } - } - finally + try ( MuninnPageCache pageCache = createPageCache( fs, 30, pageCachePageSize, PageCacheTracer.NULL, + DefaultPageCursorTracerSupplier.NULL ) ) { - for ( int j = 0; j < i; j++ ) + PagedFile pf = null; + int i = 0; + + try { - try + expectedException.expect( IllegalStateException.class ); + for ( ; i < Integer.MAX_VALUE; i++ ) { - pf.close(); + pf = pageCache.map( file, filePageSize ); } - catch ( Exception e ) + } + finally + { + for ( int j = 0; j < i; j++ ) { - //noinspection ThrowFromFinallyBlock - throw new AssertionError( "Did not expect pf.close() to throw", e ); + try + { + pf.close(); + } + catch ( Exception e ) + { + //noinspection ThrowFromFinallyBlock + throw new AssertionError( "Did not expect pf.close() to throw", e ); + } } } } } + + private void writeInitialDataTo( File file ) throws IOException + { + try (StoreChannel channel = fs.create( file )) + { + ByteBuffer buf = ByteBuffer.allocate( 16 ); + buf.putLong( x ); + buf.putLong( y ); + buf.flip(); + channel.writeAll( buf ); + } + } + + private ByteBuffer readIntoBuffer( String fileName ) throws IOException + { + ByteBuffer buffer = ByteBuffer.allocate( 16 ); + try ( StoreChannel channel = fs.open( file( fileName ), "r" ) ) + { + channel.read( buffer ); + } + buffer.flip(); + return buffer; + } }