Skip to content

Commit

Permalink
Close default file system in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MishaDemianenko committed Nov 21, 2016
1 parent add362d commit bb578e3
Show file tree
Hide file tree
Showing 91 changed files with 915 additions and 663 deletions.
Expand Up @@ -208,6 +208,9 @@ private void runConsistencyCheckToolWith( FileSystemAbstraction fileSystem, Stri
private void runConsistencyCheckToolWith( ConsistencyCheckService
consistencyCheckService, PrintStream systemError, String... args ) throws ToolFailureException, IOException
{
new ConsistencyCheckTool( consistencyCheckService, new DefaultFileSystemAbstraction(), systemError ).run( args );
try ( FileSystemAbstraction fileSystemAbstraction = new DefaultFileSystemAbstraction() )
{
new ConsistencyCheckTool( consistencyCheckService, fileSystemAbstraction, systemError ).run( args );
}
}
}
Expand Up @@ -54,6 +54,7 @@
import org.neo4j.logging.NullLogProvider;
import org.neo4j.test.rule.RandomRule;
import org.neo4j.test.rule.TestDirectory;
import org.neo4j.test.rule.fs.DefaultFileSystemRule;

import static org.junit.Assert.assertTrue;
import static org.neo4j.graphdb.Label.label;
Expand All @@ -63,10 +64,9 @@ public class DetectAllRelationshipInconsistenciesIT
{
private final TestDirectory directory = TestDirectory.testDirectory();
private final RandomRule random = new RandomRule();
private final DefaultFileSystemAbstraction fileSystem = new DefaultFileSystemAbstraction();

private final DefaultFileSystemRule fileSystemRule = new DefaultFileSystemRule();
@Rule
public final RuleChain rules = RuleChain.outerRule( random ).around( directory );
public final RuleChain rules = RuleChain.outerRule( random ).around( directory ).around( fileSystemRule );

@Test
public void shouldDetectSabotagedRelationshipWhereEverItIs() throws Exception
Expand Down Expand Up @@ -146,6 +146,7 @@ public void shouldDetectSabotagedRelationshipWhereEverItIs() throws Exception

private StoreFactory newStoreFactory( PageCache pageCache )
{
DefaultFileSystemAbstraction fileSystem = fileSystemRule.get();
return new StoreFactory( directory.directory(), getTuningConfiguration(),
new DefaultIdGeneratorFactory( fileSystem ), pageCache, fileSystem, NullLogProvider.getInstance() );
}
Expand Down
Expand Up @@ -25,6 +25,7 @@
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.RuleChain;

import java.io.File;

Expand All @@ -33,7 +34,6 @@
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseSettings;
import org.neo4j.io.fs.DefaultFileSystemAbstraction;
import org.neo4j.io.pagecache.PageCache;
import org.neo4j.kernel.configuration.Config;
import org.neo4j.kernel.impl.store.RecordStore;
Expand All @@ -42,6 +42,7 @@
import org.neo4j.test.TestGraphDatabaseFactory;
import org.neo4j.test.rule.PageCacheRule;
import org.neo4j.test.rule.TestDirectory;
import org.neo4j.test.rule.fs.DefaultFileSystemRule;

import static org.junit.Assert.assertEquals;
import static org.neo4j.kernel.impl.store.record.RecordLoad.NORMAL;
Expand All @@ -50,10 +51,14 @@ public class RelationshipChainExplorerTest
{
private static final int NDegreeTwoNodes = 10;

private final TestDirectory storeLocation = TestDirectory.testDirectory();
private final DefaultFileSystemRule fileSystemRule = new DefaultFileSystemRule();

@ClassRule
public static PageCacheRule pageCacheRule = new PageCacheRule();
@Rule
public TestDirectory storeLocation = TestDirectory.testDirectory();
public RuleChain ruleChain = RuleChain.outerRule( storeLocation ).around( fileSystemRule );

private StoreAccess store;

@Before
Expand Down Expand Up @@ -141,8 +146,8 @@ private StoreAccess createStoreWithOneHighDegreeNodeAndSeveralDegreeTwoNodes( in
transaction.success();
}
database.shutdown();
PageCache pageCache = pageCacheRule.getPageCache( new DefaultFileSystemAbstraction() );
StoreAccess storeAccess = new StoreAccess( new DefaultFileSystemAbstraction(), pageCache, storeDirectory,
PageCache pageCache = pageCacheRule.getPageCache( fileSystemRule.get() );
StoreAccess storeAccess = new StoreAccess( fileSystemRule.get(), pageCache, storeDirectory,
Config.empty() );
return storeAccess.initialize();
}
Expand Down
Expand Up @@ -22,6 +22,7 @@
import org.apache.commons.lang3.mutable.MutableLong;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.RuleChain;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

Expand Down Expand Up @@ -53,7 +54,6 @@
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseSettings;
import org.neo4j.helpers.progress.ProgressMonitorFactory;
import org.neo4j.io.fs.DefaultFileSystemAbstraction;
import org.neo4j.kernel.configuration.Config;
import org.neo4j.kernel.impl.logging.NullLogService;
import org.neo4j.kernel.impl.store.format.RecordFormats;
Expand All @@ -63,6 +63,7 @@
import org.neo4j.test.TestGraphDatabaseFactory;
import org.neo4j.test.rule.RandomRule;
import org.neo4j.test.rule.TestDirectory;
import org.neo4j.test.rule.fs.DefaultFileSystemRule;
import org.neo4j.unsafe.impl.batchimport.cache.idmapping.IdGenerator;
import org.neo4j.unsafe.impl.batchimport.cache.idmapping.IdMapper;
import org.neo4j.unsafe.impl.batchimport.input.Group;
Expand Down Expand Up @@ -92,11 +93,12 @@
@RunWith( Parameterized.class )
public class ParallelBatchImporterTest
{
@Rule
public final TestDirectory directory = TestDirectory.testDirectory();
private final TestDirectory directory = TestDirectory.testDirectory();
private final RandomRule random = new RandomRule();
private final DefaultFileSystemRule fileSystemRule = new DefaultFileSystemRule();

@Rule
public final RandomRule random = new RandomRule();
public RuleChain ruleChain = RuleChain.outerRule( directory ).around( random ).around( fileSystemRule );

private static final int NODE_COUNT = 10_000;
private static final int RELATIONSHIPS_PER_NODE = 5;
Expand Down Expand Up @@ -161,7 +163,7 @@ public void shouldImportCsvData() throws Exception
// GIVEN
ExecutionMonitor processorAssigner = eagerRandomSaturation( config.maxNumberOfProcessors() );
final BatchImporter inserter = new ParallelBatchImporter( directory.graphDbDir(),
new DefaultFileSystemAbstraction(), config, NullLogService.getInstance(),
fileSystemRule.get(), config, NullLogService.getInstance(),
processorAssigner, EMPTY, Config.empty(), getFormat() );

boolean successful = false;
Expand Down
Expand Up @@ -44,6 +44,7 @@
import org.neo4j.commandline.admin.Usage;
import org.neo4j.dbms.archive.Dumper;
import org.neo4j.io.fs.DefaultFileSystemAbstraction;
import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.kernel.impl.store.MetaDataStore;
import org.neo4j.kernel.impl.storemigration.StoreFileType;
import org.neo4j.kernel.internal.StoreLocker;
Expand Down Expand Up @@ -164,7 +165,8 @@ public void shouldRespectTheStoreLock() throws Exception
{
Path databaseDirectory = homeDir.resolve( "data/databases/foo.db" );

try ( StoreLocker storeLocker = new StoreLocker( new DefaultFileSystemAbstraction() ) )
try ( FileSystemAbstraction fileSystem = new DefaultFileSystemAbstraction();
StoreLocker storeLocker = new StoreLocker( fileSystem ) )
{
storeLocker.checkLock( databaseDirectory.toFile() );

Expand Down Expand Up @@ -220,7 +222,8 @@ public void shouldReportAHelpfulErrorIfWeDontHaveWritePermissionsForLock() throw
{
assumeFalse( "We haven't found a way to reliably tests permissions on Windows", SystemUtils.IS_OS_WINDOWS );

try ( StoreLocker storeLocker = new StoreLocker( new DefaultFileSystemAbstraction() ) )
try ( FileSystemAbstraction fileSystem = new DefaultFileSystemAbstraction();
StoreLocker storeLocker = new StoreLocker( fileSystem ) )
{
storeLocker.checkLock( databaseDirectory.toFile() );

Expand Down Expand Up @@ -374,7 +377,8 @@ private void execute( final String database ) throws IncorrectUsage, CommandFail

private void assertCanLockStore( Path databaseDirectory ) throws IOException
{
try ( StoreLocker storeLocker = new StoreLocker( new DefaultFileSystemAbstraction() ) )
try ( FileSystemAbstraction fileSystem = new DefaultFileSystemAbstraction();
StoreLocker storeLocker = new StoreLocker( fileSystem ) )
{
storeLocker.checkLock( databaseDirectory.toFile() );
}
Expand Down
Expand Up @@ -44,6 +44,7 @@
import org.neo4j.dbms.archive.Loader;
import org.neo4j.helpers.ArrayUtil;
import org.neo4j.io.fs.DefaultFileSystemAbstraction;
import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.kernel.internal.StoreLocker;
import org.neo4j.test.rule.TestDirectory;

Expand Down Expand Up @@ -178,7 +179,8 @@ public void shouldRespectTheStoreLock() throws IOException, IncorrectUsage
Path databaseDirectory = homeDir.resolve( "data/databases/foo.db" );
Files.createDirectories( databaseDirectory );

try ( StoreLocker locker = new StoreLocker( new DefaultFileSystemAbstraction() ) )
try ( FileSystemAbstraction fileSystem = new DefaultFileSystemAbstraction();
StoreLocker locker = new StoreLocker( fileSystem ) )
{
locker.checkLock( databaseDirectory.toFile() );
execute( "foo.db", "--force" );
Expand Down
Expand Up @@ -19,26 +19,35 @@
*/
package org.neo4j.io.fs;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

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

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.*;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

public class DefaultFileSystemAbstractionTest
{
private final DefaultFileSystemAbstraction defaultFileSystemAbstraction = new DefaultFileSystemAbstraction();

private DefaultFileSystemAbstraction defaultFileSystemAbstraction;
private File path;

@Before
public void before() throws Exception
{
path = new File( "target/" + UUID.randomUUID() );
defaultFileSystemAbstraction = new DefaultFileSystemAbstraction();
}

@After
public void tearDown() throws IOException
{
defaultFileSystemAbstraction.close();
}

@Test
Expand Down
Expand Up @@ -41,6 +41,7 @@
import org.neo4j.graphdb.mockfs.DelegatingFileSystemAbstraction;
import org.neo4j.graphdb.mockfs.DelegatingStoreChannel;
import org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction;
import org.neo4j.io.IOUtils;
import org.neo4j.io.fs.DefaultFileSystemAbstraction;
import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.io.fs.StoreChannel;
Expand All @@ -59,19 +60,22 @@

public class SingleFilePageSwapperTest extends PageSwapperTest
{
private EphemeralFileSystemAbstraction fs = new EphemeralFileSystemAbstraction();
private EphemeralFileSystemAbstraction ephemeralFileSystem;
private DefaultFileSystemAbstraction fileSystem;
private File file;

@Before
public void setUp() throws IOException
{
file = new File( "file" ).getCanonicalFile();
ephemeralFileSystem = new EphemeralFileSystemAbstraction();
fileSystem = new DefaultFileSystemAbstraction();
}

@After
public void tearDown() throws Exception
{
fs.close();
IOUtils.closeAll( ephemeralFileSystem, fileSystem );
}

protected PageSwapperFactory swapperFactory()
Expand Down Expand Up @@ -100,7 +104,7 @@ protected File getFile()

protected FileSystemAbstraction getFs()
{
return fs;
return ephemeralFileSystem;
}

protected void assumeFalse( String message, boolean test )
Expand Down Expand Up @@ -215,16 +219,15 @@ public void creatingSwapperForFileMustTakeLockOnFile() throws Exception
assumeFalse( "No file locking on Windows", SystemUtils.IS_OS_WINDOWS );

PageSwapperFactory factory = createSwapperFactory();
DefaultFileSystemAbstraction fs = new DefaultFileSystemAbstraction();
factory.setFileSystemAbstraction( fs );
factory.setFileSystemAbstraction( fileSystem );
File file = testDir.file( "file" );
fs.create( file ).close();
fileSystem.create( file ).close();

PageSwapper pageSwapper = createSwapper( factory, file, 4, NO_CALLBACK, false );

try
{
StoreChannel channel = fs.open( file, "rw" );
StoreChannel channel = fileSystem.open( file, "rw" );
expectedException.expect( OverlappingFileLockException.class );
channel.tryLock();
}
Expand All @@ -240,11 +243,10 @@ public void creatingSwapperForInternallyLockedFileMustThrow() throws Exception
assumeFalse( "No file locking on Windows", SystemUtils.IS_OS_WINDOWS ); // no file locking on Windows.

PageSwapperFactory factory = createSwapperFactory();
DefaultFileSystemAbstraction fs = new DefaultFileSystemAbstraction();
factory.setFileSystemAbstraction( fs );
factory.setFileSystemAbstraction( fileSystem );
File file = testDir.file( "file" );

StoreFileChannel channel = fs.create( file );
StoreFileChannel channel = fileSystem.create( file );

try ( FileLock fileLock = channel.tryLock() )
{
Expand All @@ -260,11 +262,10 @@ public void creatingSwapperForExternallyLockedFileMustThrow() throws Exception
assumeFalse( "No file locking on Windows", SystemUtils.IS_OS_WINDOWS ); // no file locking on Windows.

PageSwapperFactory factory = createSwapperFactory();
DefaultFileSystemAbstraction fs = new DefaultFileSystemAbstraction();
factory.setFileSystemAbstraction( fs );
factory.setFileSystemAbstraction( fileSystem );
File file = testDir.file( "file" );

fs.create( file ).close();
fileSystem.create( file ).close();

ProcessBuilder pb = new ProcessBuilder(
ProcessUtil.getJavaExecutable().toString(),
Expand Down Expand Up @@ -295,14 +296,13 @@ public void mustUnlockFileWhenThePageSwapperIsClosed() throws Exception
assumeFalse( "No file locking on Windows", SystemUtils.IS_OS_WINDOWS ); // no file locking on Windows.

PageSwapperFactory factory = createSwapperFactory();
DefaultFileSystemAbstraction fs = new DefaultFileSystemAbstraction();
factory.setFileSystemAbstraction( fs );
factory.setFileSystemAbstraction( fileSystem );
File file = testDir.file( "file" );
fs.create( file ).close();
fileSystem.create( file ).close();

createSwapper( factory, file, 4, NO_CALLBACK, false ).close();

try ( StoreFileChannel channel = fs.open( file, "rw" );
try ( StoreFileChannel channel = fileSystem.open( file, "rw" );
FileLock fileLock = channel.tryLock() )
{
assertThat( fileLock, is( not( nullValue() ) ) );
Expand All @@ -315,16 +315,15 @@ public void fileMustRemainLockedEvenIfChannelIsClosedByStrayInterrupt() throws E
assumeFalse( "No file locking on Windows", SystemUtils.IS_OS_WINDOWS ); // no file locking on Windows.

PageSwapperFactory factory = createSwapperFactory();
DefaultFileSystemAbstraction fs = new DefaultFileSystemAbstraction();
factory.setFileSystemAbstraction( fs );
factory.setFileSystemAbstraction( fileSystem );
File file = testDir.file( "file" );
fs.create( file ).close();
fileSystem.create( file ).close();

PageSwapper pageSwapper = createSwapper( factory, file, 4, NO_CALLBACK, false );

try
{
StoreChannel channel = fs.open( file, "rw" );
StoreChannel channel = fileSystem.open( file, "rw" );

Thread.currentThread().interrupt();
pageSwapper.force();
Expand All @@ -345,8 +344,7 @@ public void mustCloseFilesIfTakingFileLockThrows() throws Exception

final AtomicInteger openFilesCounter = new AtomicInteger();
PageSwapperFactory factory = createSwapperFactory();
DefaultFileSystemAbstraction fs = new DefaultFileSystemAbstraction();
factory.setFileSystemAbstraction( new DelegatingFileSystemAbstraction( fs )
factory.setFileSystemAbstraction( new DelegatingFileSystemAbstraction( fileSystem )
{
@Override
public StoreChannel open( File fileName, String mode ) throws IOException
Expand All @@ -364,8 +362,8 @@ public void close() throws IOException
}
} );
File file = testDir.file( "file" );
try ( StoreChannel ch = fs.create( file );
FileLock ignore = ch.tryLock() )
try ( StoreChannel ch = fileSystem.create( file );
FileLock ignore = ch.tryLock() )
{
createSwapper( factory, file, 4, NO_CALLBACK, false ).close();
fail( "Creating a page swapper for a locked channel should have thrown" );
Expand Down

0 comments on commit bb578e3

Please sign in to comment.