Skip to content

Commit

Permalink
Close selective fs;
Browse files Browse the repository at this point in the history
update Ephemeral fs rule to be only a wrapper to FS and not to be a FS.
  • Loading branch information
MishaDemianenko committed Nov 21, 2016
1 parent 3ecf81b commit fb4cf4c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 195 deletions.
Expand Up @@ -19,10 +19,10 @@
*/ */
package org.neo4j.io.fs; package org.neo4j.io.fs;


import java.io.File;

import org.junit.Test; import org.junit.Test;


import java.io.File;

import org.neo4j.graphdb.mockfs.SelectiveFileSystemAbstraction; import org.neo4j.graphdb.mockfs.SelectiveFileSystemAbstraction;


import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
Expand All @@ -40,12 +40,16 @@ public void shouldUseCorrectFileSystemForChosenFile() throws Exception
FileSystemAbstraction special = mock( FileSystemAbstraction.class ); FileSystemAbstraction special = mock( FileSystemAbstraction.class );


// when // when
new SelectiveFileSystemAbstraction( specialFile, special, normal ).open( specialFile, "r" ); try ( SelectiveFileSystemAbstraction systemAbstraction = new SelectiveFileSystemAbstraction( specialFile,
special, normal ) )
{
systemAbstraction.open( specialFile, "r" );


// then // then
verify( special).open( specialFile, "r" ) ; verify( special ).open( specialFile, "r" );
verifyNoMoreInteractions( special ); verifyNoMoreInteractions( special );
verifyNoMoreInteractions( normal ); verifyNoMoreInteractions( normal );
}
} }


@Test @Test
Expand All @@ -59,14 +63,16 @@ public void shouldUseDefaultFileSystemForOtherFiles() throws Exception
FileSystemAbstraction special = mock( FileSystemAbstraction.class ); FileSystemAbstraction special = mock( FileSystemAbstraction.class );


// when // when
SelectiveFileSystemAbstraction fs = new SelectiveFileSystemAbstraction( specialFile, special, normal ); try ( SelectiveFileSystemAbstraction fs = new SelectiveFileSystemAbstraction( specialFile, special, normal ) )
fs.create( otherFile ); {
fs.open( otherFile, "r" ); fs.create( otherFile );
fs.open( otherFile, "r" );


// then // then
verify( normal ).create( otherFile ) ; verify( normal ).create( otherFile );
verify( normal ).open( otherFile, "r" ) ; verify( normal ).open( otherFile, "r" );
verifyNoMoreInteractions( special ); verifyNoMoreInteractions( special );
verifyNoMoreInteractions( normal ); verifyNoMoreInteractions( normal );
}
} }
} }
Expand Up @@ -103,7 +103,7 @@ public void shouldReturnMinusOneWhenRunningOutOfIds() throws Exception
public void shouldOnlyOverflowWhenThresholdIsReached() throws Exception public void shouldOnlyOverflowWhenThresholdIsReached() throws Exception
{ {
// Given // Given
StoreChannel channel = spy( fs.open( new File( "id.file" ), "rw" ) ); StoreChannel channel = spy( fs.get().open( new File( "id.file" ), "rw" ) );


int threshold = 10; int threshold = 10;
FreeIdKeeper keeper = new FreeIdKeeper( channel, threshold, true ); FreeIdKeeper keeper = new FreeIdKeeper( channel, threshold, true );
Expand All @@ -130,7 +130,7 @@ public void shouldOnlyOverflowWhenThresholdIsReached() throws Exception
public void shouldReadBackPersistedIdsWhenAggressiveReuseIsSet() throws Exception public void shouldReadBackPersistedIdsWhenAggressiveReuseIsSet() throws Exception
{ {
// given // given
StoreChannel channel = fs.open( new File( "id.file" ), "rw" ); StoreChannel channel = fs.get().open( new File( "id.file" ), "rw" );


int threshold = 10; int threshold = 10;
FreeIdKeeper keeper = new FreeIdKeeper( channel, threshold, true ); FreeIdKeeper keeper = new FreeIdKeeper( channel, threshold, true );
Expand All @@ -154,7 +154,7 @@ public void shouldReadBackPersistedIdsWhenAggressiveReuseIsSet() throws Exceptio
public void shouldReadBackManyPersistedIdBatchesWhenAggressiveReuseIsSet() throws Exception public void shouldReadBackManyPersistedIdBatchesWhenAggressiveReuseIsSet() throws Exception
{ {
// given // given
StoreChannel channel = fs.open( new File( "id.file" ), "rw" ); StoreChannel channel = fs.get().open( new File( "id.file" ), "rw" );


int threshold = 10; int threshold = 10;
FreeIdKeeper keeper = new FreeIdKeeper( channel, threshold, true ); FreeIdKeeper keeper = new FreeIdKeeper( channel, threshold, true );
Expand Down Expand Up @@ -182,7 +182,7 @@ public void shouldFirstReturnNonPersistedIdsAndThenPersistedOnesWhenAggressiveRe
{ {
// this is testing the stack property, but from the viewpoint of avoiding unnecessary disk reads // this is testing the stack property, but from the viewpoint of avoiding unnecessary disk reads
// given // given
StoreChannel channel = fs.open( new File( "id.file" ), "rw" ); StoreChannel channel = fs.get().open( new File( "id.file" ), "rw" );


int threshold = 10; int threshold = 10;
FreeIdKeeper keeper = new FreeIdKeeper( channel, threshold, true ); FreeIdKeeper keeper = new FreeIdKeeper( channel, threshold, true );
Expand Down Expand Up @@ -218,7 +218,7 @@ public void shouldFirstReturnNonPersistedIdsAndThenPersistedOnesWhenAggressiveRe
public void persistedIdsShouldStillBeCounted() throws Exception public void persistedIdsShouldStillBeCounted() throws Exception
{ {
// given // given
StoreChannel channel = fs.open( new File( "id.file" ), "rw" ); StoreChannel channel = fs.get().open( new File( "id.file" ), "rw" );


int threshold = 10; int threshold = 10;
FreeIdKeeper keeper = new FreeIdKeeper( channel, threshold, true ); FreeIdKeeper keeper = new FreeIdKeeper( channel, threshold, true );
Expand All @@ -245,7 +245,7 @@ public void persistedIdsShouldStillBeCounted() throws Exception
public void shouldStoreAndRestoreIds() throws Exception public void shouldStoreAndRestoreIds() throws Exception
{ {
// given // given
StoreChannel channel = fs.open( new File( "id.file" ), "rw" ); StoreChannel channel = fs.get().open( new File( "id.file" ), "rw" );


int threshold = 10; int threshold = 10;
FreeIdKeeper keeper = new FreeIdKeeper( channel, threshold, true ); FreeIdKeeper keeper = new FreeIdKeeper( channel, threshold, true );
Expand All @@ -269,7 +269,7 @@ public void shouldStoreAndRestoreIds() throws Exception
keeper.close(); keeper.close();
channel.close(); channel.close();
// and then we open a new one over the same file // and then we open a new one over the same file
channel = fs.open( new File( "id.file" ), "rw" ); channel = fs.get().open( new File( "id.file" ), "rw" );
keeper = new FreeIdKeeper( channel, threshold, true ); keeper = new FreeIdKeeper( channel, threshold, true );


// then // then
Expand All @@ -288,7 +288,7 @@ public void shouldStoreAndRestoreIds() throws Exception
public void shouldNotReturnNewlyReleasedIdsIfAggressiveIsFalse() throws Exception public void shouldNotReturnNewlyReleasedIdsIfAggressiveIsFalse() throws Exception
{ {
// given // given
StoreChannel channel = fs.open( new File( "id.file" ), "rw" ); StoreChannel channel = fs.get().open( new File( "id.file" ), "rw" );


int threshold = 10; int threshold = 10;
FreeIdKeeper keeper = new FreeIdKeeper( channel, threshold, false ); FreeIdKeeper keeper = new FreeIdKeeper( channel, threshold, false );
Expand All @@ -305,7 +305,7 @@ public void shouldNotReturnNewlyReleasedIdsIfAggressiveIsFalse() throws Exceptio
public void shouldNotReturnIdsPersistedDuringThisRunIfAggressiveIsFalse() throws Exception public void shouldNotReturnIdsPersistedDuringThisRunIfAggressiveIsFalse() throws Exception
{ {
// given // given
StoreChannel channel = spy( fs.open( new File( "id.file" ), "rw" ) ); StoreChannel channel = spy( fs.get().open( new File( "id.file" ), "rw" ) );


int threshold = 10; int threshold = 10;
FreeIdKeeper keeper = new FreeIdKeeper( channel, threshold, false ); FreeIdKeeper keeper = new FreeIdKeeper( channel, threshold, false );
Expand All @@ -328,7 +328,7 @@ public void shouldNotReturnIdsPersistedDuringThisRunIfAggressiveIsFalse() throws
public void shouldReturnIdsRestoredAndIgnoreNewlyReleasedIfAggressiveReuseIsFalse() throws Exception public void shouldReturnIdsRestoredAndIgnoreNewlyReleasedIfAggressiveReuseIsFalse() throws Exception
{ {
// given // given
StoreChannel channel = fs.open( new File("id.file" ), "rw" ); StoreChannel channel = fs.get().open( new File("id.file" ), "rw" );


int threshold = 10; int threshold = 10;
FreeIdKeeper keeper = new FreeIdKeeper( channel, threshold, false ); FreeIdKeeper keeper = new FreeIdKeeper( channel, threshold, false );
Expand All @@ -341,7 +341,7 @@ public void shouldReturnIdsRestoredAndIgnoreNewlyReleasedIfAggressiveReuseIsFals
keeper.close(); keeper.close();
channel.close(); channel.close();
// and then we open a new one over the same file // and then we open a new one over the same file
channel = fs.open( new File( "id.file" ), "rw" ); channel = fs.get().open( new File( "id.file" ), "rw" );
keeper = new FreeIdKeeper( channel, threshold, false ); keeper = new FreeIdKeeper( channel, threshold, false );


// when // when
Expand All @@ -368,7 +368,7 @@ public void shouldReturnNoResultIfIdsAreRestoredAndExhaustedAndThereAreFreeIdsFr
throws Exception throws Exception
{ {
// given // given
StoreChannel channel = fs.open( new File("id.file" ), "rw" ); StoreChannel channel = fs.get().open( new File("id.file" ), "rw" );


int threshold = 10; int threshold = 10;
FreeIdKeeper keeper = new FreeIdKeeper( channel, threshold, false ); FreeIdKeeper keeper = new FreeIdKeeper( channel, threshold, false );
Expand All @@ -381,7 +381,7 @@ public void shouldReturnNoResultIfIdsAreRestoredAndExhaustedAndThereAreFreeIdsFr
keeper.close(); keeper.close();
channel.close(); channel.close();
// and then we open a new one over the same file // and then we open a new one over the same file
channel = fs.open( new File( "id.file" ), "rw" ); channel = fs.get().open( new File( "id.file" ), "rw" );
keeper = new FreeIdKeeper( channel, threshold, false ); keeper = new FreeIdKeeper( channel, threshold, false );


// when - then // when - then
Expand Down
Expand Up @@ -22,24 +22,14 @@
import org.junit.rules.ExternalResource; import org.junit.rules.ExternalResource;


import java.io.File; import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.file.CopyOption;
import java.util.function.Function;
import java.util.function.Supplier; import java.util.function.Supplier;


import org.neo4j.graphdb.GraphDatabaseService; import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction; import org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction;
import org.neo4j.io.fs.FileSystemAbstraction; import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.io.fs.StoreChannel;


public class EphemeralFileSystemRule extends ExternalResource implements Supplier<FileSystemAbstraction>, public class EphemeralFileSystemRule extends ExternalResource implements Supplier<FileSystemAbstraction>
FileSystemAbstraction
{ {
private EphemeralFileSystemAbstraction fs = new EphemeralFileSystemAbstraction(); private EphemeralFileSystemAbstraction fs = new EphemeralFileSystemAbstraction();


Expand Down Expand Up @@ -105,136 +95,12 @@ public void crash()
fs.crash(); fs.crash();
} }


public void assertNoOpenFiles() throws Exception
{
fs.assertNoOpenFiles();
}

public void dumpZip( OutputStream output ) throws IOException
{
fs.dumpZip( output );
}

@Override
public StoreChannel open( File fileName, String mode ) throws IOException
{
return fs.open( fileName, mode );
}

@Override
public OutputStream openAsOutputStream( File fileName, boolean append ) throws IOException
{
return fs.openAsOutputStream( fileName, append );
}

@Override
public InputStream openAsInputStream( File fileName ) throws IOException
{
return fs.openAsInputStream( fileName );
}

@Override
public Reader openAsReader( File fileName, Charset charset ) throws IOException
{
return fs.openAsReader( fileName, charset );
}

@Override @Override
public String toString() public String toString()
{ {
return fs.toString(); return fs.toString();
} }


@Override
public Writer openAsWriter( File fileName, Charset charset, boolean append ) throws IOException
{
return fs.openAsWriter( fileName, charset, append );
}

@Override
public StoreChannel create( File fileName ) throws IOException
{
return fs.create( fileName );
}

@Override
public long getFileSize( File fileName )
{
return fs.getFileSize( fileName );
}

@Override
public boolean fileExists( File file )
{
return fs.fileExists( file );
}

@Override
public boolean isDirectory( File file )
{
return fs.isDirectory( file );
}

@Override
public boolean mkdir( File directory )
{
return fs.mkdir( directory );
}

@Override
public void mkdirs( File directory )
{
fs.mkdirs( directory );
}

@Override
public boolean deleteFile( File fileName )
{
return fs.deleteFile( fileName );
}

@Override
public void deleteRecursively( File directory ) throws IOException
{
fs.deleteRecursively( directory );
}

@Override
public void renameFile( File from, File to, CopyOption... copyOptions ) throws IOException
{
fs.renameFile( from, to, copyOptions );
}

@Override
public File[] listFiles( File directory )
{
return fs.listFiles( directory );
}

@Override
public File[] listFiles( File directory, FilenameFilter filter )
{
return fs.listFiles( directory, filter );
}

@Override
public void moveToDirectory( File file, File toDirectory ) throws IOException
{
fs.moveToDirectory( file, toDirectory );
}

@Override
public void copyFile( File from, File to ) throws IOException
{
fs.copyFile( from, to );
}

@Override
public void copyRecursively( File fromDirectory, File toDirectory ) throws IOException
{
fs.copyRecursively( fromDirectory, toDirectory );
}

public EphemeralFileSystemAbstraction snapshot() public EphemeralFileSystemAbstraction snapshot()
{ {
return fs.snapshot(); return fs.snapshot();
Expand All @@ -249,35 +115,4 @@ public long checksum()
{ {
return fs.checksum(); return fs.checksum();
} }

@Override
public <K extends ThirdPartyFileSystem> K getOrCreateThirdPartyFileSystem( Class<K> clazz,
Function<Class<K>,K> creator )
{
return fs.getOrCreateThirdPartyFileSystem( clazz, creator );
}

@Override
public void truncate( File file, long size ) throws IOException
{
fs.truncate( file, size );
}

@Override
public long lastModifiedTime( File file ) throws IOException
{
return fs.lastModifiedTime( file );
}

@Override
public void deleteFileOrThrow( File file ) throws IOException
{
fs.deleteFileOrThrow( file );
}

@Override
public void close() throws IOException
{
fs.close();
}
} }
Expand Up @@ -91,9 +91,10 @@ public void setup() throws Throwable
} }


@After @After
public void teardown() throws Throwable public void tearDown() throws Throwable
{ {
realm.shutdown(); realm.shutdown();
fs.close();
} }


@Test @Test
Expand Down

0 comments on commit fb4cf4c

Please sign in to comment.