From bb2b3412983b528da12e6661e88b14d888091993 Mon Sep 17 00:00:00 2001 From: MishaDemianenko Date: Thu, 3 Nov 2016 01:14:53 +0100 Subject: [PATCH] File systems close tests --- .../org/neo4j/io/fs/DelegatingFileSystem.java | 9 +- .../mockfs/CloseTrackingFileSystem.java | 46 ++++++ ...> EphemeralFileSystemAbstractionTest.java} | 29 +++- .../fs/DefaultFileSystemAbstractionTest.java | 22 +++ .../fs/DelegateFileSystemAbstractionTest.java | 135 ++++++++++++++++++ 5 files changed, 232 insertions(+), 9 deletions(-) create mode 100644 community/io/src/test/java/org/neo4j/graphdb/mockfs/CloseTrackingFileSystem.java rename community/io/src/test/java/org/neo4j/graphdb/mockfs/{EphemeralFileSystemAbstractionCrashTest.java => EphemeralFileSystemAbstractionTest.java} (87%) create mode 100644 community/io/src/test/java/org/neo4j/io/fs/DelegateFileSystemAbstractionTest.java diff --git a/community/io/src/main/java/org/neo4j/io/fs/DelegatingFileSystem.java b/community/io/src/main/java/org/neo4j/io/fs/DelegatingFileSystem.java index 2d2ec9fd27598..5fe7739496e75 100644 --- a/community/io/src/main/java/org/neo4j/io/fs/DelegatingFileSystem.java +++ b/community/io/src/main/java/org/neo4j/io/fs/DelegatingFileSystem.java @@ -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 diff --git a/community/io/src/test/java/org/neo4j/graphdb/mockfs/CloseTrackingFileSystem.java b/community/io/src/test/java/org/neo4j/graphdb/mockfs/CloseTrackingFileSystem.java new file mode 100644 index 0000000000000..c405eac778261 --- /dev/null +++ b/community/io/src/test/java/org/neo4j/graphdb/mockfs/CloseTrackingFileSystem.java @@ -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 . + */ +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; + } +} diff --git a/community/io/src/test/java/org/neo4j/graphdb/mockfs/EphemeralFileSystemAbstractionCrashTest.java b/community/io/src/test/java/org/neo4j/graphdb/mockfs/EphemeralFileSystemAbstractionTest.java similarity index 87% rename from community/io/src/test/java/org/neo4j/graphdb/mockfs/EphemeralFileSystemAbstractionCrashTest.java rename to community/io/src/test/java/org/neo4j/graphdb/mockfs/EphemeralFileSystemAbstractionTest.java index 43dcd2723f266..3ece62e86bb7e 100644 --- a/community/io/src/test/java/org/neo4j/graphdb/mockfs/EphemeralFileSystemAbstractionCrashTest.java +++ b/community/io/src/test/java/org/neo4j/graphdb/mockfs/EphemeralFileSystemAbstractionTest.java @@ -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; @@ -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 diff --git a/community/io/src/test/java/org/neo4j/io/fs/DefaultFileSystemAbstractionTest.java b/community/io/src/test/java/org/neo4j/io/fs/DefaultFileSystemAbstractionTest.java index afdbb2218f1e2..bb0d5b4fea37d 100644 --- a/community/io/src/test/java/org/neo4j/io/fs/DefaultFileSystemAbstractionTest.java +++ b/community/io/src/test/java/org/neo4j/io/fs/DefaultFileSystemAbstractionTest.java @@ -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; @@ -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() ); + } + } diff --git a/community/io/src/test/java/org/neo4j/io/fs/DelegateFileSystemAbstractionTest.java b/community/io/src/test/java/org/neo4j/io/fs/DelegateFileSystemAbstractionTest.java new file mode 100644 index 0000000000000..702daa679672a --- /dev/null +++ b/community/io/src/test/java/org/neo4j/io/fs/DelegateFileSystemAbstractionTest.java @@ -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 . + */ +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 getRootDirectories() + { + return null; + } + + @Override + public Iterable getFileStores() + { + return null; + } + + @Override + public Set 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; + } + } +}