use of io.aeron.logbuffer.Header in project aeron by real-logic.
the class EgressAdapterTest method onFragmentShouldInvokeOnSessionEventCallbackIfSessionIdMatches.
@Test
void onFragmentShouldInvokeOnSessionEventCallbackIfSessionIdMatches() {
final int offset = 8;
final long clusterSessionId = 42;
final long correlationId = 777;
final long leadershipTermId = 6;
final int leaderMemberId = 3;
final EventCode eventCode = EventCode.REDIRECT;
final int version = 18;
final String eventDetail = "Event details";
sessionEventEncoder.wrapAndApplyHeader(buffer, offset, messageHeaderEncoder).clusterSessionId(clusterSessionId).correlationId(correlationId).leadershipTermId(leadershipTermId).leaderMemberId(leaderMemberId).code(eventCode).version(version).detail(eventDetail);
final EgressListener egressListener = mock(EgressListener.class);
final Header header = new Header(1, 3);
final EgressAdapter adapter = new EgressAdapter(egressListener, clusterSessionId, mock(Subscription.class), 10);
adapter.onFragment(buffer, offset, sessionEventEncoder.encodedLength(), header);
verify(egressListener).onSessionEvent(correlationId, clusterSessionId, leadershipTermId, leaderMemberId, eventCode, eventDetail);
verifyNoMoreInteractions(egressListener);
}
use of io.aeron.logbuffer.Header in project aeron by real-logic.
the class EgressAdapterTest method onFragmentIsANoOpIfSessionIdDoesNotMatchOnAdminResponse.
@Test
void onFragmentIsANoOpIfSessionIdDoesNotMatchOnAdminResponse() {
final int offset = 24;
final long clusterSessionId = 18;
final long correlationId = 3274239749237498239L;
final AdminRequestType type = AdminRequestType.SNAPSHOT;
final AdminResponseCode responseCode = AdminResponseCode.OK;
final String message = "Unauthorised access detected!";
final byte[] payload = new byte[] { 0x1, 0x2, 0x3 };
adminResponseEncoder.wrapAndApplyHeader(buffer, offset, messageHeaderEncoder).clusterSessionId(clusterSessionId).correlationId(correlationId).requestType(type).responseCode(responseCode).message(message);
adminResponseEncoder.putPayload(payload, 0, payload.length);
final EgressListener egressListener = mock(EgressListener.class);
final Header header = new Header(1, 3);
final EgressAdapter adapter = new EgressAdapter(egressListener, -clusterSessionId, mock(Subscription.class), 10);
adapter.onFragment(buffer, offset, adminResponseEncoder.encodedLength(), header);
verifyNoInteractions(egressListener);
}
use of io.aeron.logbuffer.Header in project aeron by real-logic.
the class FragmentAssemblerTest method shouldAssembleFourPartMessage.
@Test
public void shouldAssembleFourPartMessage() {
when(header.flags()).thenReturn(FrameDescriptor.BEGIN_FRAG_FLAG).thenReturn((byte) 0).thenReturn((byte) 0).thenReturn(FrameDescriptor.END_FRAG_FLAG);
final UnsafeBuffer srcBuffer = new UnsafeBuffer(new byte[1024]);
final int offset = 0;
final int length = srcBuffer.capacity() / 4;
for (int i = 0; i < 4; i++) {
srcBuffer.setMemory(i * length, length, (byte) (65 + i));
}
adapter.onFragment(srcBuffer, offset, length, header);
adapter.onFragment(srcBuffer, offset + length, length, header);
adapter.onFragment(srcBuffer, offset + (length * 2), length, header);
adapter.onFragment(srcBuffer, offset + (length * 3), length, header);
final ArgumentCaptor<UnsafeBuffer> bufferArg = ArgumentCaptor.forClass(UnsafeBuffer.class);
final ArgumentCaptor<Header> headerArg = ArgumentCaptor.forClass(Header.class);
verify(delegateFragmentHandler, times(1)).onFragment(bufferArg.capture(), eq(offset), eq(length * 4), headerArg.capture());
final UnsafeBuffer capturedBuffer = bufferArg.getValue();
for (int i = 0; i < srcBuffer.capacity(); i++) {
assertEquals(srcBuffer.getByte(i), capturedBuffer.getByte(i), "same at i=" + i);
}
final Header capturedHeader = headerArg.getValue();
assertEquals(SESSION_ID, capturedHeader.sessionId());
assertEquals(FrameDescriptor.END_FRAG_FLAG, capturedHeader.flags());
}
use of io.aeron.logbuffer.Header in project aeron by real-logic.
the class ReplaySessionTest method shouldReplayFromActiveRecording.
@Test
public void shouldReplayFromActiveRecording() throws IOException {
final UnsafeBuffer termBuffer = new UnsafeBuffer(allocateDirectAligned(4096, 64));
final int recordingId = RECORDING_ID + 1;
recordingSummary.recordingId = recordingId;
recordingSummary.stopPosition = NULL_POSITION;
when(mockCatalog.stopPosition(recordingId)).thenReturn(START_POSITION + FRAME_LENGTH * 4);
recordingPosition = START_POSITION;
context.recordChecksumBuffer(new UnsafeBuffer(ByteBuffer.allocateDirect(TERM_BUFFER_LENGTH)));
final RecordingWriter writer = new RecordingWriter(recordingId, START_POSITION, SEGMENT_LENGTH, mockImage, context);
writer.init();
final UnsafeBuffer buffer = new UnsafeBuffer(allocateDirectAligned(TERM_BUFFER_LENGTH, 64));
final DataHeaderFlyweight headerFwt = new DataHeaderFlyweight();
final Header header = new Header(INITIAL_TERM_ID, Integer.numberOfLeadingZeros(TERM_BUFFER_LENGTH));
header.buffer(buffer);
recordFragment(writer, buffer, headerFwt, header, 0, FRAME_LENGTH, 0, UNFRAGMENTED, HDR_TYPE_DATA, SESSION_ID);
recordFragment(writer, buffer, headerFwt, header, FRAME_LENGTH, FRAME_LENGTH, 1, BEGIN_FRAG_FLAG, HDR_TYPE_DATA, SESSION_ID);
final long length = 5 * FRAME_LENGTH;
final long correlationId = 1L;
final int sessionId = 42;
final int streamId = 21;
try (ReplaySession replaySession = replaySession(RECORDING_POSITION, length, correlationId, mockReplayPub, mockControlSession, recordingPositionCounter, null)) {
when(mockReplayPub.isClosed()).thenReturn(false);
when(mockReplayPub.isConnected()).thenReturn(false);
when(mockReplayPub.sessionId()).thenReturn(sessionId);
when(mockReplayPub.streamId()).thenReturn(streamId);
replaySession.doWork();
assertEquals(replaySession.state(), ReplaySession.State.INIT);
when(mockReplayPub.isConnected()).thenReturn(true);
mockPublication(mockReplayPub, termBuffer);
assertNotEquals(0, replaySession.doWork());
assertThat(messageCounter, is(1));
validateFrame(termBuffer, 0, FRAME_LENGTH, 0, UNFRAGMENTED, sessionId, streamId);
validateFrame(termBuffer, FRAME_LENGTH, FRAME_LENGTH, 1, BEGIN_FRAG_FLAG, sessionId, streamId);
assertEquals(0, replaySession.doWork());
recordFragment(writer, buffer, headerFwt, header, FRAME_LENGTH * 2, FRAME_LENGTH, 2, END_FRAG_FLAG, HDR_TYPE_DATA, SESSION_ID);
recordFragment(writer, buffer, headerFwt, header, FRAME_LENGTH * 3, FRAME_LENGTH, 3, UNFRAGMENTED, HDR_TYPE_PAD, SESSION_ID);
writer.close();
when(recordingPositionCounter.isClosed()).thenReturn(true);
when(mockCatalog.stopPosition(recordingId)).thenReturn(START_POSITION + FRAME_LENGTH * 4);
assertNotEquals(0, replaySession.doWork());
validateFrame(termBuffer, 2 * FRAME_LENGTH, FRAME_LENGTH, 2, END_FRAG_FLAG, sessionId, streamId);
verify(mockReplayPub).appendPadding(FRAME_LENGTH - HEADER_LENGTH);
assertTrue(replaySession.isDone());
}
}
use of io.aeron.logbuffer.Header in project aeron by real-logic.
the class ClusterTest method injectAdminResponseEgressListener.
private MutableBoolean injectAdminResponseEgressListener(final long expectedCorrelationId, final AdminRequestType expectedRequestType, final AdminResponseCode expectedResponseCode, final String expectedMessage) {
final MutableBoolean responseReceived = new MutableBoolean();
cluster.egressListener(new EgressListener() {
public void onMessage(final long clusterSessionId, final long timestamp, final DirectBuffer buffer, final int offset, final int length, final Header header) {
}
public void onAdminResponse(final long clusterSessionId, final long correlationId, final AdminRequestType requestType, final AdminResponseCode responseCode, final String message, final DirectBuffer payload, final int payloadOffset, final int payloadLength) {
responseReceived.set(true);
assertEquals(expectedCorrelationId, correlationId);
assertEquals(expectedRequestType, requestType);
assertEquals(expectedResponseCode, responseCode);
assertEquals(expectedMessage, message);
assertNotNull(payload);
final int minPayloadOffset = MessageHeaderEncoder.ENCODED_LENGTH + AdminResponseEncoder.BLOCK_LENGTH + AdminResponseEncoder.messageHeaderLength() + message.length() + AdminResponseEncoder.payloadHeaderLength();
assertTrue(payloadOffset > minPayloadOffset);
assertEquals(0, payloadLength);
}
});
return responseReceived;
}
Aggregations