use of org.neo4j.graphdb.mockfs.DelegatingFileSystemAbstraction in project neo4j by neo4j.
the class PageCacheTest method flushingDuringPagedFileCloseMustRetryUntilItSucceeds.
@RepeatRule.Repeat(times = 100)
@Test(timeout = SHORT_TIMEOUT_MILLIS)
public void flushingDuringPagedFileCloseMustRetryUntilItSucceeds() throws IOException {
FileSystemAbstraction fs = new DelegatingFileSystemAbstraction(this.fs) {
@Override
public StoreChannel open(File fileName, String mode) throws IOException {
return new DelegatingStoreChannel(super.open(fileName, mode)) {
private int writeCount = 0;
@Override
public void writeAll(ByteBuffer src, long position) throws IOException {
if (writeCount++ < 10) {
throw new IOException("This is a benign exception that we expect to be thrown " + "during a flush of a PagedFile.");
}
super.writeAll(src, position);
}
};
}
};
getPageCache(fs, maxPages, pageCachePageSize, PageCacheTracer.NULL, PageCursorTracerSupplier.NULL);
PrintStream oldSystemErr = System.err;
try (PagedFile pf = pageCache.map(file("a"), filePageSize);
PageCursor cursor = pf.io(0, PF_SHARED_WRITE_LOCK)) {
assertTrue(cursor.next());
writeRecords(cursor);
// Silence any stack traces the failed flushes might print.
System.setErr(new PrintStream(new ByteArrayOutputStream()));
} finally {
System.setErr(oldSystemErr);
}
verifyRecordsInFile(file("a"), recordsPerFilePage);
}
use of org.neo4j.graphdb.mockfs.DelegatingFileSystemAbstraction in project neo4j by neo4j.
the class SingleFilePageSwapperTest method mustCloseFilesIfTakingFileLockThrows.
@Test
public void mustCloseFilesIfTakingFileLockThrows() throws Exception {
// no file locking on Windows.
assumeFalse("No file locking on Windows", SystemUtils.IS_OS_WINDOWS);
final AtomicInteger openFilesCounter = new AtomicInteger();
PageSwapperFactory factory = createSwapperFactory();
factory.setFileSystemAbstraction(new DelegatingFileSystemAbstraction(fileSystem) {
@Override
public StoreChannel open(File fileName, String mode) throws IOException {
openFilesCounter.getAndIncrement();
return new DelegatingStoreChannel(super.open(fileName, mode)) {
@Override
public void close() throws IOException {
openFilesCounter.getAndDecrement();
super.close();
}
};
}
});
File file = testDir.file("file");
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");
} catch (FileLockException e) {
// As expected.
}
assertThat(openFilesCounter.get(), is(0));
}
use of org.neo4j.graphdb.mockfs.DelegatingFileSystemAbstraction in project neo4j by neo4j.
the class StoreLockerTest method shouldNotObtainLockWhenStoreAlreadyInUse.
@Test
public void shouldNotObtainLockWhenStoreAlreadyInUse() throws Exception {
FileSystemAbstraction fileSystemAbstraction = new DelegatingFileSystemAbstraction(fileSystemRule.get()) {
@Override
public boolean fileExists(File fileName) {
return false;
}
@Override
public StoreChannel open(File fileName, String mode) throws IOException {
return new DelegatingStoreChannel(super.open(fileName, mode)) {
@Override
public FileLock tryLock() throws IOException {
// 'null' implies that the file has been externally locked
return null;
}
};
}
};
try (StoreLocker storeLocker = new StoreLocker(fileSystemAbstraction)) {
storeLocker.checkLock(target.directory("unused"));
fail();
} catch (StoreLockException e) {
assertThat(e.getMessage(), containsString("Store and its lock file has been locked by another process"));
}
}
use of org.neo4j.graphdb.mockfs.DelegatingFileSystemAbstraction in project neo4j by neo4j.
the class RotatingFileOutputStreamSupplierTest method shouldNotUpdateOutputStreamWhenClosedDuringRotation.
@Test
public void shouldNotUpdateOutputStreamWhenClosedDuringRotation() throws Exception {
final CountDownLatch allowRotationComplete = new CountDownLatch(1);
RotationListener rotationListener = spy(new RotationListener() {
@Override
public void outputFileCreated(OutputStream out) {
try {
allowRotationComplete.await();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
});
final List<OutputStream> mockStreams = new ArrayList<>();
FileSystemAbstraction fs = new DelegatingFileSystemAbstraction(fileSystem) {
@Override
public OutputStream openAsOutputStream(File fileName, boolean append) throws IOException {
final OutputStream stream = spy(super.openAsOutputStream(fileName, append));
mockStreams.add(stream);
return stream;
}
};
RotatingFileOutputStreamSupplier supplier = new RotatingFileOutputStreamSupplier(fs, logFile, 10, 0, 10, Executors.newSingleThreadExecutor(), rotationListener);
OutputStream outputStream = supplier.get();
write(supplier, "A string longer than 10 bytes");
assertThat(supplier.get(), is(outputStream));
allowRotationComplete.countDown();
supplier.close();
assertStreamClosed(mockStreams.get(0));
}
use of org.neo4j.graphdb.mockfs.DelegatingFileSystemAbstraction in project neo4j by neo4j.
the class RotatingFileOutputStreamSupplierTest method shouldCloseAllStreamsDespiteError.
@Test
public void shouldCloseAllStreamsDespiteError() throws Exception {
final List<OutputStream> mockStreams = new ArrayList<>();
FileSystemAbstraction fs = new DelegatingFileSystemAbstraction(fileSystem) {
@Override
public OutputStream openAsOutputStream(File fileName, boolean append) throws IOException {
final OutputStream stream = spy(super.openAsOutputStream(fileName, append));
mockStreams.add(stream);
return stream;
}
};
RotatingFileOutputStreamSupplier supplier = new RotatingFileOutputStreamSupplier(fs, logFile, 10, 0, 10, DIRECT_EXECUTOR);
write(supplier, "A string longer than 10 bytes");
write(supplier, "A string longer than 10 bytes");
IOException exception = new IOException("test exception");
OutputStream mockStream = mockStreams.get(1);
doThrow(exception).when(mockStream).close();
try {
supplier.close();
} catch (IOException e) {
assertThat(e, sameInstance(exception));
}
verify(mockStream).close();
}
Aggregations