use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.
the class StateRecoveryManagerTest method shouldReturnPreviouslyInactiveWhenOneFileFullAndOneEmpty.
@Test
public void shouldReturnPreviouslyInactiveWhenOneFileFullAndOneEmpty() throws Exception {
// given
EphemeralFileSystemAbstraction fsa = fileSystemRule.get();
fsa.mkdir(testDir.directory());
File fileA = fileA();
StoreChannel channel = fsa.create(fileA);
fillUpAndForce(channel);
File fileB = fileB();
fsa.create(fileB);
StateRecoveryManager<Long> manager = new StateRecoveryManager<>(fsa, new LongMarshal());
// when
final StateRecoveryManager.RecoveryStatus recoveryStatus = manager.recover(fileA, fileB);
// then
assertEquals(fileB, recoveryStatus.activeFile());
}
use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.
the class PhysicalLogFileTest method shouldCloseChannelInFailedAttemptToReadHeaderAfterOpen.
@Test
public void shouldCloseChannelInFailedAttemptToReadHeaderAfterOpen() throws Exception {
// GIVEN a file which returns 1/2 log header size worth of bytes
File directory = new File("/dir");
FileSystemAbstraction fs = mock(FileSystemAbstraction.class);
PhysicalLogFiles logFiles = new PhysicalLogFiles(directory, fs);
int logVersion = 0;
File logFile = logFiles.getLogFileForVersion(logVersion);
StoreChannel channel = mock(StoreChannel.class);
when(channel.read(any(ByteBuffer.class))).thenReturn(LogHeader.LOG_HEADER_SIZE / 2);
when(fs.fileExists(logFile)).thenReturn(true);
when(fs.open(eq(logFile), anyString())).thenReturn(channel);
// WHEN
try {
PhysicalLogFile.openForVersion(logFiles, fs, logVersion, false);
fail("Should have failed");
} catch (IncompleteLogHeaderException e) {
// THEN good
verify(channel).close();
}
}
use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.
the class PhysicalLogFileTest method shouldSuppressFailueToCloseChannelInFailedAttemptToReadHeaderAfterOpen.
@Test
public void shouldSuppressFailueToCloseChannelInFailedAttemptToReadHeaderAfterOpen() throws Exception {
// GIVEN a file which returns 1/2 log header size worth of bytes
File directory = new File("/dir");
FileSystemAbstraction fs = mock(FileSystemAbstraction.class);
PhysicalLogFiles logFiles = new PhysicalLogFiles(directory, fs);
int logVersion = 0;
File logFile = logFiles.getLogFileForVersion(logVersion);
StoreChannel channel = mock(StoreChannel.class);
when(channel.read(any(ByteBuffer.class))).thenReturn(LogHeader.LOG_HEADER_SIZE / 2);
when(fs.fileExists(logFile)).thenReturn(true);
when(fs.open(eq(logFile), anyString())).thenReturn(channel);
doThrow(IOException.class).when(channel).close();
// WHEN
try {
PhysicalLogFile.openForVersion(logFiles, fs, logVersion, false);
fail("Should have failed");
} catch (IncompleteLogHeaderException e) {
// THEN good
verify(channel).close();
assertEquals(1, e.getSuppressed().length);
assertTrue(e.getSuppressed()[0] instanceof IOException);
}
}
use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.
the class FailureStorage method readFailure.
private String readFailure(File failureFile) throws IOException {
try (StoreChannel channel = fs.open(failureFile, "r")) {
byte[] data = new byte[(int) channel.size()];
int readData = channel.read(ByteBuffer.wrap(data));
return readData <= 0 ? "" : UTF8.decode(withoutZeros(data));
}
}
use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.
the class FileSenderTest method sendLargeFile.
@Test
public void sendLargeFile() throws Exception {
// given
int dataSize = MAX_SIZE + (MAX_SIZE / 2);
byte[] bytes = new byte[dataSize];
random.nextBytes(bytes);
File smallFile = testDirectory.file("smallFile");
try (StoreChannel storeChannel = fs.create(smallFile)) {
storeChannel.write(ByteBuffer.wrap(bytes));
}
FileSender fileSender = new FileSender(fs.open(smallFile, "r"));
// when + then
assertFalse(fileSender.isEndOfInput());
assertEquals(FileChunk.create(copyOfRange(bytes, 0, MAX_SIZE), false), fileSender.readChunk(allocator));
assertEquals(FileChunk.create(copyOfRange(bytes, MAX_SIZE, bytes.length), true), fileSender.readChunk(allocator));
assertNull(fileSender.readChunk(allocator));
assertTrue(fileSender.isEndOfInput());
}
Aggregations