use of org.agrona.ExpandableArrayBuffer in project aeron by real-logic.
the class ClusterNodeTest method launchTimedService.
private ClusteredServiceContainer launchTimedService() {
final ClusteredService timedService = new StubClusteredService() {
long clusterSessionId;
long correlationId;
String msg;
public void onSessionMessage(final long clusterSessionId, final long correlationId, final long timestampMs, final DirectBuffer buffer, final int offset, final int length, final Header header) {
this.clusterSessionId = clusterSessionId;
this.correlationId = correlationId;
this.msg = buffer.getStringWithoutLengthAscii(offset, length);
cluster.scheduleTimer(correlationId, timestampMs + 100);
}
public void onTimerEvent(final long correlationId, final long timestampMs) {
final String responseMsg = msg + "-scheduled";
final ExpandableArrayBuffer buffer = new ExpandableArrayBuffer();
buffer.putStringWithoutLengthAscii(0, responseMsg);
final ClientSession clientSession = cluster.getClientSession(clusterSessionId);
while (clientSession.offer(correlationId, buffer, 0, responseMsg.length()) < 0) {
TestUtil.checkInterruptedStatus();
Thread.yield();
}
}
};
return ClusteredServiceContainer.launch(new ClusteredServiceContainer.Context().clusteredService(timedService).errorHandler(Throwable::printStackTrace).deleteDirOnStart(true));
}
use of org.agrona.ExpandableArrayBuffer in project aeron by real-logic.
the class ClusterTest method shouldEchoMessagesViaService.
@Test(timeout = 10_000)
public void shouldEchoMessagesViaService() throws InterruptedException {
final Aeron aeron = client.context().aeron();
final SessionDecorator sessionDecorator = new SessionDecorator(client.clusterSessionId());
final Publication publication = client.ingressPublication();
final ExpandableArrayBuffer msgBuffer = new ExpandableArrayBuffer();
final long msgCorrelationId = aeron.nextCorrelationId();
msgBuffer.putStringWithoutLengthAscii(0, MSG);
final EchoConsumer consumer = new EchoConsumer(client.egressSubscription());
final Thread thread = new Thread(consumer);
thread.setName("consumer");
thread.setDaemon(true);
thread.start();
for (int i = 0; i < MESSAGE_COUNT; i++) {
while (sessionDecorator.offer(publication, msgCorrelationId, msgBuffer, 0, MSG.length()) < 0) {
TestUtil.checkInterruptedStatus();
Thread.yield();
}
}
latch.await();
for (final EchoService service : echoServices) {
assertThat(service.messageCount(), is(MESSAGE_COUNT));
}
}
use of org.agrona.ExpandableArrayBuffer in project Aeron by real-logic.
the class ClusterNodeRestartTest method launchService.
private void launchService(final AtomicLong msgCounter) {
final ClusteredService service = new StubClusteredService() {
private int nextCorrelationId = 0;
private int counterValue = 0;
public void onStart(final Cluster cluster, final Image snapshotImage) {
super.onStart(cluster, snapshotImage);
if (null != snapshotImage) {
final FragmentHandler fragmentHandler = (buffer, offset, length, header) -> {
nextCorrelationId = buffer.getInt(offset);
offset += SIZE_OF_INT;
counterValue = buffer.getInt(offset);
offset += SIZE_OF_INT;
serviceState.set(buffer.getStringAscii(offset));
};
while (true) {
final int fragments = snapshotImage.poll(fragmentHandler, 1);
if (fragments == 1 || snapshotImage.isEndOfStream()) {
break;
}
idleStrategy.idle();
}
}
}
public void onSessionMessage(final ClientSession session, final long timestamp, final DirectBuffer buffer, final int offset, final int length, final Header header) {
final int sentValue = buffer.getInt(offset + MESSAGE_VALUE_OFFSET);
assertEquals(counterValue, sentValue);
counterValue++;
serviceState.set(Integer.toString(counterValue));
msgCounter.getAndIncrement();
if (TIMER_MESSAGE_LENGTH == length) {
final long correlationId = serviceCorrelationId(nextCorrelationId++);
final long deadlineMs = timestamp + buffer.getLong(offset + TIMER_MESSAGE_DELAY_OFFSET);
while (!cluster.scheduleTimer(correlationId, deadlineMs)) {
idleStrategy.idle();
}
}
}
public void onTakeSnapshot(final ExclusivePublication snapshotPublication) {
final ExpandableArrayBuffer buffer = new ExpandableArrayBuffer();
int length = 0;
buffer.putInt(length, nextCorrelationId);
length += SIZE_OF_INT;
buffer.putInt(length, counterValue);
length += SIZE_OF_INT;
length += buffer.putStringAscii(length, Integer.toString(counterValue));
snapshotPublication.offer(buffer, 0, length);
}
};
container = ClusteredServiceContainer.launch(new ClusteredServiceContainer.Context().clusteredService(service).terminationHook(ClusterTests.NOOP_TERMINATION_HOOK).errorHandler(ClusterTests.errorHandler(0)));
}
use of org.agrona.ExpandableArrayBuffer in project Aeron by real-logic.
the class ClusterNodeTest method shouldScheduleEventInService.
@Test
@InterruptAfter(10)
public void shouldScheduleEventInService() {
final ExpandableArrayBuffer msgBuffer = new ExpandableArrayBuffer();
final String msg = "Hello World!";
msgBuffer.putStringWithoutLengthAscii(0, msg);
final MutableInteger messageCount = new MutableInteger();
final EgressListener listener = (clusterSessionId, timestamp, buffer, offset, length, header) -> {
final String expected = msg + "-scheduled";
assertEquals(expected, buffer.getStringWithoutLengthAscii(offset, length));
messageCount.value += 1;
};
container = launchTimedService();
aeronCluster = connectToCluster(listener);
offerMessage(msgBuffer, msg);
awaitResponse(messageCount);
ClusterTests.failOnClusterError();
}
use of org.agrona.ExpandableArrayBuffer in project Aeron by real-logic.
the class ClusterNodeTest method launchTimedService.
private ClusteredServiceContainer launchTimedService() {
final ClusteredService clusteredService = new StubClusteredService() {
private final ExpandableArrayBuffer buffer = new ExpandableArrayBuffer();
private long clusterSessionId;
private int nextCorrelationId;
private String msg;
public void onSessionMessage(final ClientSession session, final long timestamp, final DirectBuffer buffer, final int offset, final int length, final Header header) {
clusterSessionId = session.id();
msg = buffer.getStringWithoutLengthAscii(offset, length);
final long correlationId = serviceCorrelationId(nextCorrelationId++);
idleStrategy.reset();
while (!cluster.scheduleTimer(correlationId, timestamp + 100)) {
idleStrategy.idle();
}
}
public void onTimerEvent(final long correlationId, final long timestamp) {
final String responseMsg = msg + "-scheduled";
buffer.putStringWithoutLengthAscii(0, responseMsg);
final ClientSession clientSession = cluster.getClientSession(clusterSessionId);
idleStrategy.reset();
while (clientSession.offer(buffer, 0, responseMsg.length()) < 0) {
idleStrategy.idle();
}
}
};
return ClusteredServiceContainer.launch(new ClusteredServiceContainer.Context().clusteredService(clusteredService).terminationHook(ClusterTests.NOOP_TERMINATION_HOOK).errorHandler(ClusterTests.errorHandler(0)));
}
Aggregations