use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.
the class PhysicalFlushableChannelTest method shouldThrowIllegalStateExceptionAfterClosed.
@Test
public void shouldThrowIllegalStateExceptionAfterClosed() throws Exception {
// GIVEN
final File file = new File(directory.directory(), "file");
StoreChannel storeChannel = fileSystemRule.get().open(file, "rw");
PhysicalLogVersionedStoreChannel versionedStoreChannel = new PhysicalLogVersionedStoreChannel(storeChannel, 1, (byte) -1);
PhysicalFlushableChannel channel = new PhysicalFlushableChannel(versionedStoreChannel);
// closing the WritableLogChannel, then the underlying channel is what PhysicalLogFile does
channel.close();
storeChannel.close();
// WHEN just appending something to the buffer
channel.put((byte) 0);
// and wanting to empty that into the channel
try {
channel.prepareForFlush();
fail("Should have thrown exception");
} catch (IllegalStateException e) {
// THEN we should get an IllegalStateException, not a ClosedChannelException
}
}
use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.
the class PhysicalFlushableChannelTest method shouldBeAbleToWriteSmallNumberOfBytes.
@Test
public void shouldBeAbleToWriteSmallNumberOfBytes() throws IOException {
final File firstFile = new File(directory.directory(), "file1");
StoreChannel storeChannel = fileSystemRule.get().open(firstFile, "rw");
PhysicalLogVersionedStoreChannel versionedStoreChannel = new PhysicalLogVersionedStoreChannel(storeChannel, 1, (byte) -1);
PhysicalFlushableChannel channel = new PhysicalFlushableChannel(versionedStoreChannel);
int length = 26_145;
byte[] bytes = generateBytes(length);
channel.put(bytes, length);
channel.close();
byte[] writtenBytes = new byte[length];
try (InputStream in = new FileInputStream(firstFile)) {
in.read(writtenBytes);
}
assertArrayEquals(bytes, writtenBytes);
}
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 LogHeaderWriterTest method shouldWriteALogHeaderInAStoreChannel.
@Test
public void shouldWriteALogHeaderInAStoreChannel() throws IOException {
// given
final File file = File.createTempFile("WriteLogHeader", getClass().getSimpleName());
final StoreChannel channel = fileSystemRule.get().open(file, "rw");
// when
writeLogHeader(channel, expectedLogVersion, expectedTxId);
channel.close();
// then
final byte[] array = new byte[LOG_HEADER_SIZE];
try (InputStream stream = fileSystemRule.get().openAsInputStream(file)) {
int read = stream.read(array);
assertEquals(LOG_HEADER_SIZE, read);
}
final ByteBuffer result = ByteBuffer.wrap(array);
long encodedLogVersions = result.getLong();
assertEquals(encodeLogVersion(expectedLogVersion), encodedLogVersions);
byte logFormatVersion = decodeLogFormatVersion(encodedLogVersions);
assertEquals(CURRENT_LOG_VERSION, logFormatVersion);
long logVersion = decodeLogVersion(encodedLogVersions);
assertEquals(expectedLogVersion, logVersion);
long txId = result.getLong();
assertEquals(expectedTxId, txId);
}
Aggregations