Skip to content

Commit

Permalink
File systems close tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MishaDemianenko committed Nov 21, 2016
1 parent ba5524b commit bb2b341
Show file tree
Hide file tree
Showing 5 changed files with 232 additions and 9 deletions.
Expand Up @@ -135,14 +135,7 @@ public Path getPath( String first, String... more )
public PathMatcher getPathMatcher( String syntaxAndPattern )
{
final PathMatcher matcher = delegate.getPathMatcher( syntaxAndPattern );
return new PathMatcher()
{
@Override
public boolean matches( Path path )
{
return matcher.matches( DelegatingPath.getDelegate( path ) );
}
};
return path -> matcher.matches( DelegatingPath.getDelegate( path ) );
}

@Override
Expand Down
@@ -0,0 +1,46 @@
/*
* Copyright (c) 2002-2016 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.graphdb.mockfs;

import java.io.IOException;
import java.util.zip.ZipOutputStream;

import org.neo4j.io.fs.FileSystemAbstraction;

public class CloseTrackingFileSystem implements FileSystemAbstraction.ThirdPartyFileSystem
{
private boolean closed = false;

@Override
public void close()
{
closed = true;
}

@Override
public void dumpToZip( ZipOutputStream zip, byte[] scratchPad ) throws IOException
{
}

public boolean isClosed()
{
return closed;
}
}
Expand Up @@ -39,9 +39,11 @@

import static java.nio.ByteBuffer.allocateDirect;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

public class EphemeralFileSystemAbstractionCrashTest
public class EphemeralFileSystemAbstractionTest
{

private EphemeralFileSystemAbstraction fs;
Expand Down Expand Up @@ -221,6 +223,31 @@ public void shouldBeConsistentAfterConcurrentWritesAndForces() throws Exception
}
}

@Test
public void releaseResourcesOnClose() throws IOException
{
try ( EphemeralFileSystemAbstraction fileSystemAbstraction = new EphemeralFileSystemAbstraction() )
{
CloseTrackingFileSystem closeTrackingFileSystem = new CloseTrackingFileSystem();
fileSystemAbstraction.getOrCreateThirdPartyFileSystem( CloseTrackingFileSystem.class,
closeTrackingFileSystemClass -> closeTrackingFileSystem );
File testDir = new File( "testDir" );
File testFile = new File( "testFile" );
fileSystemAbstraction.mkdir( testDir );
fileSystemAbstraction.create( testFile );

assertTrue( fileSystemAbstraction.fileExists( testFile ) );
assertTrue( fileSystemAbstraction.fileExists( testFile ) );
assertFalse( closeTrackingFileSystem.isClosed() );

fileSystemAbstraction.close();

assertTrue( closeTrackingFileSystem.isClosed() );
assertFalse( fileSystemAbstraction.fileExists( testFile ) );
assertFalse( fileSystemAbstraction.fileExists( testFile ) );
}
}

private void verifyFileIsFullOfLongIntegerOnes( StoreChannel channel )
{
try
Expand Down
Expand Up @@ -27,7 +27,11 @@
import java.io.IOException;
import java.util.UUID;

import org.neo4j.graphdb.mockfs.CloseTrackingFileSystem;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
Expand Down Expand Up @@ -115,4 +119,22 @@ public boolean mkdirs()
.UNABLE_TO_CREATE_DIRECTORY_FORMAT, path ) ) );
}
}

@Test
public void closeThirdPartyFileSystemsOnClose() throws IOException
{
CloseTrackingFileSystem closeTrackingFileSystem = new CloseTrackingFileSystem();

CloseTrackingFileSystem fileSystem = defaultFileSystemAbstraction
.getOrCreateThirdPartyFileSystem( CloseTrackingFileSystem.class,
thirdPartyFileSystemClass -> closeTrackingFileSystem );

assertSame( closeTrackingFileSystem, fileSystem );
assertFalse( closeTrackingFileSystem.isClosed() );

defaultFileSystemAbstraction.close();

assertTrue( closeTrackingFileSystem.isClosed() );
}

}
@@ -0,0 +1,135 @@
/*
* Copyright (c) 2002-2016 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.io.fs;

import org.junit.Test;

import java.io.IOException;
import java.nio.file.FileStore;
import java.nio.file.FileSystem;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.WatchService;
import java.nio.file.attribute.UserPrincipalLookupService;
import java.nio.file.spi.FileSystemProvider;
import java.util.Set;

import org.neo4j.graphdb.mockfs.CloseTrackingFileSystem;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class DelegateFileSystemAbstractionTest
{

@Test
public void closeAllResourcesOnClose() throws Exception
{
TrackableFileSystem fileSystem = new TrackableFileSystem();
CloseTrackingFileSystem closeTrackingFileSystem = new CloseTrackingFileSystem();

DelegateFileSystemAbstraction fileSystemAbstraction = new DelegateFileSystemAbstraction( fileSystem );
fileSystemAbstraction.getOrCreateThirdPartyFileSystem( CloseTrackingFileSystem.class,
closeTrackingFileSystemClass -> closeTrackingFileSystem );

fileSystemAbstraction.close();

assertFalse( fileSystem.isOpen() );
assertTrue( closeTrackingFileSystem.isClosed() );
}

private class TrackableFileSystem extends FileSystem
{

private boolean closed;

@Override
public FileSystemProvider provider()
{
return null;
}

@Override
public void close() throws IOException
{
closed = true;
}

@Override
public boolean isOpen()
{
return !closed;
}

@Override
public boolean isReadOnly()
{
return false;
}

@Override
public String getSeparator()
{
return null;
}

@Override
public Iterable<Path> getRootDirectories()
{
return null;
}

@Override
public Iterable<FileStore> getFileStores()
{
return null;
}

@Override
public Set<String> supportedFileAttributeViews()
{
return null;
}

@Override
public Path getPath( String first, String... more )
{
return null;
}

@Override
public PathMatcher getPathMatcher( String syntaxAndPattern )
{
return null;
}

@Override
public UserPrincipalLookupService getUserPrincipalLookupService()
{
return null;
}

@Override
public WatchService newWatchService() throws IOException
{
return null;
}
}
}

0 comments on commit bb2b341

Please sign in to comment.