Skip to content

Commit

Permalink
Close all test allocated resources so files can be cleaned up on wind…
Browse files Browse the repository at this point in the history
…ows without any warnings.
  • Loading branch information
MishaDemianenko committed May 15, 2017
1 parent a1cd9a6 commit c8e228a
Show file tree
Hide file tree
Showing 3 changed files with 292 additions and 250 deletions.
Expand Up @@ -101,6 +101,7 @@ public void verifyInterruptionAndChannelState() throws IOException
// This is good. What we expect to see. // This is good. What we expect to see.
} }
} }
channel.close();
fs.close(); fs.close();
} }


Expand Down
185 changes: 96 additions & 89 deletions community/io/src/test/java/org/neo4j/io/pagecache/PageCacheTest.java
Expand Up @@ -1739,51 +1739,52 @@ public void pagesAddedWithNextWithPageIdMustBeAccessibleWithNoGrowSpecified() th
public void writesOfDifferentUnitsMustHaveCorrectEndianess() throws Exception public void writesOfDifferentUnitsMustHaveCorrectEndianess() throws Exception
{ {
configureStandardPageCache(); configureStandardPageCache();
PagedFile pagedFile = pageCache.map( file( "a" ), 20 ); try ( PagedFile pagedFile = pageCache.map( file( "a" ), 20 ) )

try ( PageCursor cursor = pagedFile.io( 0, PF_SHARED_WRITE_LOCK ) )
{ {
assertTrue( cursor.next() );
byte[] data = { 42, 43, 44, 45, 46 };


cursor.putLong( 41 ); // 0+8 = 8 try ( PageCursor cursor = pagedFile.io( 0, PF_SHARED_WRITE_LOCK ) )
cursor.putInt( 41 ); // 8+4 = 12 {
cursor.putShort( (short) 41 ); // 12+2 = 14 assertTrue( cursor.next() );
cursor.putByte( (byte) 41 ); // 14+1 = 15 byte[] data = {42, 43, 44, 45, 46};
cursor.putBytes( data ); // 15+5 = 20
}


try ( PageCursor cursor = pagedFile.io( 0, PF_SHARED_WRITE_LOCK ) ) cursor.putLong( 41 ); // 0+8 = 8
{ cursor.putInt( 41 ); // 8+4 = 12
assertTrue( cursor.next() ); 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 try ( PageCursor cursor = pagedFile.io( 0, PF_SHARED_WRITE_LOCK ) )
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++; assertTrue( cursor.next() );
cursor.putByte( d );
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 ); ByteBuffer buf = ByteBuffer.allocate( 20 );
channel.read( buf ); try ( StoreChannel channel = fs.open( file( "a" ), "r" ) )
{
channel.read( buf );
}
buf.flip(); buf.flip();


assertThat( buf.getLong(), is( 42L ) ); assertThat( buf.getLong(), is( 42L ) );
Expand Down Expand Up @@ -2067,81 +2068,87 @@ public void evictionMustFlushPagesToTheRightFiles() throws IOException
int filePageSize2 = filePageSize - 3; // diff. page size just to be difficult int filePageSize2 = filePageSize - 3; // diff. page size just to be difficult
long maxPageIdCursor1 = recordCount / recordsPerFilePage; long maxPageIdCursor1 = recordCount / recordsPerFilePage;
File file2 = file( "b" ); File file2 = file( "b" );
OutputStream outputStream = fs.openAsOutputStream( file2, false );
long file2sizeBytes = (maxPageIdCursor1 + 17) * filePageSize2; 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. for ( int i = 0; i < file2sizeBytes; i++ )
outputStream.write( 'a' ); {
// We will ues the page cache to change these 'a's into 'b's.
outputStream.write( 'a' );
}
outputStream.flush();
} }
outputStream.flush();
outputStream.close();


configureStandardPageCache(); configureStandardPageCache();


PagedFile pagedFile1 = pageCache.map( file( "a" ), filePageSize ); try ( PagedFile pagedFile1 = pageCache.map( file( "a" ), filePageSize ) )
PagedFile pagedFile2 = pageCache.map( file2, filePageSize2 );

long pageId1 = 0;
long pageId2 = 0;
boolean moreWorkToDo;
do
{ {
boolean cursorReady1; try ( PagedFile pagedFile2 = pageCache.map( file2, filePageSize2 ) )
boolean cursorReady2;

try ( PageCursor cursor = pagedFile1.io( pageId1, PF_SHARED_WRITE_LOCK ) )
{ {
cursorReady1 = cursor.next() && cursor.getCurrentPageId() < maxPageIdCursor1;
if ( cursorReady1 )
{
writeRecords( cursor );
pageId1++;
}
}


try ( PageCursor cursor = pagedFile2.io( pageId2, PF_SHARED_WRITE_LOCK | PF_NO_GROW ) ) long pageId1 = 0;
{ long pageId2 = 0;
cursorReady2 = cursor.next(); boolean moreWorkToDo;
if ( cursorReady2 ) 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 // Verify the file contents
assertThat( fs.getFileSize( file2 ), is( file2sizeBytes ) ); assertThat( fs.getFileSize( file2 ), is( file2sizeBytes ) );
InputStream inputStream = fs.openAsInputStream( file2 ); try ( InputStream inputStream = fs.openAsInputStream( file2 ) )
for ( int i = 0; i < file2sizeBytes; i++ )
{ {
int b = inputStream.read(); for ( int i = 0; i < file2sizeBytes; i++ )
assertThat( b, is( (int) 'b' ) ); {
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" ); try ( StoreChannel channel = fs.open( file( "a" ), "r" ) )
ByteBuffer bufB = ByteBuffer.allocate( recordSize );
for ( int i = 0; i < recordCount; i++ )
{ {
bufA.clear(); ByteBuffer bufB = ByteBuffer.allocate( recordSize );
channel.read( bufA ); for ( int i = 0; i < recordCount; i++ )
bufA.flip(); {
bufB.clear(); bufA.clear();
generateRecordForId( i, bufB ); channel.read( bufA );
assertThat( bufB.array(), byteArray( bufA.array() ) ); bufA.flip();
bufB.clear();
generateRecordForId( i, bufB );
assertThat( bufB.array(), byteArray( bufA.array() ) );
}
} }
} }


Expand Down

0 comments on commit c8e228a

Please sign in to comment.