Search in sources :

Example 1 with RawBlockHandler

use of io.aeron.logbuffer.RawBlockHandler in project Aeron by real-logic.

the class ImageArchivingSessionTest method shouldRecordFragmentsFromImage.

@Test
public void shouldRecordFragmentsFromImage() throws IOException {
    final EpochClock epochClock = Mockito.mock(EpochClock.class);
    when(epochClock.time()).thenReturn(42L);
    final ImageArchivingSession session = new ImageArchivingSession(proxy, index, tempFolderForTest, image, epochClock);
    Assert.assertEquals(streamInstanceId, session.streamInstanceId());
    // setup the mock image to pass on the mock log buffer
    when(image.rawPoll(any(), anyInt())).thenAnswer(invocation -> {
        final RawBlockHandler handle = invocation.getArgument(0);
        if (handle == null) {
            return 0;
        }
        handle.onBlock(mockLogBufferChannel, 0, mockLogBufferMapped, termOffset, 100, sessionId, 0);
        return 100;
    });
    // expecting session to proxy the available data from the image
    Assert.assertNotEquals("Expect some work", 0, session.doWork());
    // We now evaluate the output of the archiver...
    // meta data exists and is as expected
    final File archiveMetaFile = new File(tempFolderForTest, archiveMetaFileName(session.streamInstanceId()));
    Assert.assertTrue(archiveMetaFile.exists());
    final ArchiveDescriptorDecoder metaData = ArchiveFileUtil.archiveMetaFileFormatDecoder(archiveMetaFile);
    Assert.assertEquals(streamInstanceId, metaData.streamInstanceId());
    Assert.assertEquals(termBufferLength, metaData.termBufferLength());
    Assert.assertEquals(initialTermId, metaData.initialTermId());
    Assert.assertEquals(termOffset, metaData.initialTermOffset());
    Assert.assertEquals(initialTermId, metaData.lastTermId());
    Assert.assertEquals(streamId, metaData.streamId());
    Assert.assertEquals(termOffset + 100, metaData.lastTermOffset());
    Assert.assertEquals(42L, metaData.startTime());
    Assert.assertEquals(-1L, metaData.endTime());
    Assert.assertEquals(source, metaData.source());
    Assert.assertEquals(channel, metaData.channel());
    // data exists and is as expected
    final File archiveDataFile = new File(tempFolderForTest, archiveDataFileName(session.streamInstanceId(), 0));
    Assert.assertTrue(archiveDataFile.exists());
    final StreamInstanceArchiveFragmentReader reader = new StreamInstanceArchiveFragmentReader(session.streamInstanceId(), tempFolderForTest);
    final int polled = reader.poll((buffer, offset, length, header) -> {
        Assert.assertEquals(100, header.frameLength());
        Assert.assertEquals(termOffset + DataHeaderFlyweight.HEADER_LENGTH, offset);
        Assert.assertEquals(100 - DataHeaderFlyweight.HEADER_LENGTH, length);
    });
    Assert.assertEquals(1, polled);
    // next poll has no data
    when(image.rawPoll(any(), anyInt())).thenReturn(0);
    Assert.assertEquals("Expect no work", 0, session.doWork());
    // image is closed
    when(image.isClosed()).thenReturn(true);
    when(epochClock.time()).thenReturn(128L);
    Assert.assertNotEquals("Expect some work", 0, session.doWork());
    Assert.assertTrue(session.isDone());
    Assert.assertEquals(128L, metaData.endTime());
    IoUtil.unmap(metaData.buffer().byteBuffer());
}
Also used : ArchiveDescriptorDecoder(io.aeron.archiver.messages.ArchiveDescriptorDecoder) RawBlockHandler(io.aeron.logbuffer.RawBlockHandler)

Aggregations

ArchiveDescriptorDecoder (io.aeron.archiver.messages.ArchiveDescriptorDecoder)1 RawBlockHandler (io.aeron.logbuffer.RawBlockHandler)1