use of io.aeron.Image in project aeron by real-logic.
the class ClusterNodeRestartTest method launchService.
private void launchService(final boolean initialLaunch, final AtomicLong msgCounter) {
final ClusteredService service = new StubClusteredService() {
private int counterValue = 0;
public void onSessionMessage(final long clusterSessionId, final long correlationId, final long timestampMs, final DirectBuffer buffer, final int offset, final int length, final Header header) {
final int sentValue = buffer.getInt(offset);
assertThat(sentValue, is(counterValue));
counterValue++;
serviceState.set(Integer.toString(counterValue));
msgCounter.getAndIncrement();
}
public void onTakeSnapshot(final Publication snapshotPublication) {
final ExpandableArrayBuffer buffer = new ExpandableArrayBuffer();
int length = 0;
buffer.putInt(length, counterValue);
length += SIZE_OF_INT;
length += buffer.putIntAscii(length, counterValue);
snapshotPublication.offer(buffer, 0, length);
}
public void onLoadSnapshot(final Image snapshotImage) {
while (true) {
final int fragments = snapshotImage.poll((buffer, offset, length, header) -> {
counterValue = buffer.getInt(offset);
final String s = buffer.getStringWithoutLengthAscii(offset + SIZE_OF_INT, length - SIZE_OF_INT);
serviceState.set(s);
}, 1);
if (fragments == 1) {
break;
}
TestUtil.checkInterruptedStatus();
Thread.yield();
}
}
};
container = null;
container = ClusteredServiceContainer.launch(new ClusteredServiceContainer.Context().clusteredService(service).terminationHook(() -> {
}).errorHandler(Throwable::printStackTrace).deleteDirOnStart(initialLaunch));
}
use of io.aeron.Image in project Aeron by real-logic.
the class RecordingSessionTest method mockImage.
private static Image mockImage(final Subscription subscription) {
final Image image = mock(Image.class);
when(image.sessionId()).thenReturn(SESSION_ID);
when(image.initialTermId()).thenReturn(INITIAL_TERM_ID);
when(image.sourceIdentity()).thenReturn(SOURCE_IDENTITY);
when(image.termBufferLength()).thenReturn(TERM_BUFFER_LENGTH);
when(image.mtuLength()).thenReturn(MTU_LENGTH);
when(image.joinPosition()).thenReturn(START_POSITION);
when(image.subscription()).thenReturn(subscription);
return image;
}
use of io.aeron.Image in project Aeron by real-logic.
the class RecordingWriterTest method onBlockShouldNotComputeCrcForThePaddingFrame.
@Test
void onBlockShouldNotComputeCrcForThePaddingFrame() throws IOException {
final Image image = mockImage(0L);
final Context ctx = new Context().archiveDir(archiveDir).recordChecksumBuffer(new UnsafeBuffer(allocateDirectAligned(512, 64))).recordChecksum(crc32());
final RecordingWriter recordingWriter = new RecordingWriter(1, 0, SEGMENT_LENGTH, image, ctx);
recordingWriter.init();
final UnsafeBuffer termBuffer = new UnsafeBuffer(allocate(512));
final int length = 128;
final byte[] data = new byte[length - HEADER_LENGTH];
final int sessionId = 5;
final int termId = 18;
fill(data, (byte) 99);
frameType(termBuffer, 0, HDR_TYPE_PAD);
frameTermId(termBuffer, 0, termId);
frameLengthOrdered(termBuffer, 0, length);
frameSessionId(termBuffer, 0, sessionId);
termBuffer.putBytes(HEADER_LENGTH, data);
recordingWriter.onBlock(termBuffer, 0, HEADER_LENGTH, -1, -1);
recordingWriter.close();
final File segmentFile = segmentFile(1, 0);
assertTrue(segmentFile.exists());
assertEquals(SEGMENT_LENGTH, segmentFile.length());
final UnsafeBuffer fileBuffer = new UnsafeBuffer();
fileBuffer.wrap(readAllBytes(segmentFile.toPath()));
assertEquals(HDR_TYPE_PAD, frameType(fileBuffer, 0));
assertEquals(termId, frameTermId(fileBuffer, 0));
assertEquals(length, frameLength(fileBuffer, 0));
assertEquals(sessionId, frameSessionId(fileBuffer, 0));
}
use of io.aeron.Image in project Aeron by real-logic.
the class RecordingWriterTest method onBlockThrowsNullPointerExceptionIfInitWasNotCalled.
@Test
void onBlockThrowsNullPointerExceptionIfInitWasNotCalled() {
final Image image = mockImage(0L);
final RecordingWriter recordingWriter = new RecordingWriter(1, 0, SEGMENT_LENGTH, image, new Context().archiveDir(archiveDir));
assertThrows(NullPointerException.class, () -> recordingWriter.onBlock(new UnsafeBuffer(allocate(32)), 0, 10, 5, 8));
}
use of io.aeron.Image in project Aeron by real-logic.
the class RecordingWriterTest method initThrowsIOExceptionIfItCannotOpenAFileChannel.
@Test
void initThrowsIOExceptionIfItCannotOpenAFileChannel() throws IOException {
final File notADirectory = new File(archiveDir, "dummy.txt");
assertTrue(notADirectory.createNewFile());
final Image image = mockImage(0L);
final RecordingWriter recordingWriter = new RecordingWriter(1, 0, SEGMENT_LENGTH, image, new Context().archiveDir(notADirectory));
assertThrows(IOException.class, recordingWriter::init);
}
Aggregations