use of io.aeron.Publication in project aeron by real-logic.
the class ManageRecordingHistoryTest method shouldPurgeForLateJoinedStream.
@Test
@InterruptAfter(10)
void shouldPurgeForLateJoinedStream() throws IOException {
final String messagePrefix = "Message-Prefix-";
final int initialTermId = 7;
final long targetPosition = (SEGMENT_LENGTH * 13L) + 1;
final long startPosition = (SEGMENT_LENGTH * 10L) + 7 * FRAME_ALIGNMENT;
uriBuilder.initialPosition(startPosition, initialTermId, TERM_LENGTH);
try (Publication publication = aeronArchive.addRecordedExclusivePublication(uriBuilder.build(), STREAM_ID)) {
assertEquals(startPosition, publication.position());
final CountersReader counters = aeron.countersReader();
final int counterId = awaitRecordingCounterId(counters, publication.sessionId());
final long recordingId = RecordingPos.getRecordingId(counters, counterId);
offerToPosition(publication, messagePrefix, targetPosition);
awaitPosition(counters, counterId, publication.position());
final File archiveDir = archive.context().archiveDir();
long position = 0;
boolean deleted = false;
while (position < startPosition - SEGMENT_LENGTH) {
final String segmentFileName = Archive.segmentFileName(recordingId, position);
if (deleted) {
assertTrue(new File(archiveDir, segmentFileName + ".del").createNewFile());
} else {
assertTrue(new File(archiveDir, segmentFileName).createNewFile());
}
deleted = !deleted;
position += SEGMENT_LENGTH;
}
final String fileNamePrefix = recordingId + "-";
final String[] recordingFiles = archiveDir.list((dir, name) -> name.startsWith(fileNamePrefix));
assertThat(recordingFiles, arrayWithSize(14));
final long segmentFileBasePosition = AeronArchive.segmentFileBasePosition(startPosition, startPosition + SEGMENT_LENGTH + TERM_LENGTH + FRAME_ALIGNMENT * 5, TERM_LENGTH, SEGMENT_LENGTH);
final long purgeSegments = aeronArchive.purgeSegments(recordingId, segmentFileBasePosition);
assertEquals(11L, purgeSegments);
assertEquals(segmentFileBasePosition, aeronArchive.getStartPosition(recordingId));
aeronArchive.stopRecording(publication);
Tests.await(() -> {
final String[] files = archiveDir.list((dir, name) -> name.startsWith(fileNamePrefix));
if (null != files && 3 == files.length) {
assertThat(files, arrayContainingInAnyOrder(Archive.segmentFileName(recordingId, SEGMENT_LENGTH * 13L), Archive.segmentFileName(recordingId, SEGMENT_LENGTH * 12L), Archive.segmentFileName(recordingId, SEGMENT_LENGTH * 11L)));
return true;
}
return false;
});
}
}
Aggregations