Java Code Examples for org.agrona.collections.MutableInteger#set()

The following examples show how to use org.agrona.collections.MutableInteger#set() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SamplesUtil.java    From aeron with Apache License 2.0 6 votes vote down vote up
/**
 * Map an existing CnC file.
 *
 * @param cncFileVersion to set as value of file.
 * @return the {@link CountersReader} over the CnC file.
 */
public static CountersReader mapCounters(final MutableInteger cncFileVersion)
{
    final File cncFile = CommonContext.newDefaultCncFile();
    System.out.println("Command `n Control file " + cncFile);

    final MappedByteBuffer cncByteBuffer = mapExistingFileReadOnly(cncFile);
    final DirectBuffer cncMetaData = createMetaDataBuffer(cncByteBuffer);
    final int cncVersion = cncMetaData.getInt(cncVersionOffset(0));

    cncFileVersion.set(cncVersion);
    checkVersion(cncVersion);

    return new CountersReader(
        createCountersMetaDataBuffer(cncByteBuffer, cncMetaData),
        createCountersValuesBuffer(cncByteBuffer, cncMetaData));
}
 
Example 2
Source File: PongTest.java    From aeron with Apache License 2.0 5 votes vote down vote up
@Test
public void playPingPong()
{
    buffer.putInt(0, 1);

    while (pingPublication.offer(buffer, 0, BitUtil.SIZE_OF_INT) < 0L)
    {
        Tests.yield();
    }

    final MutableInteger fragmentsRead = new MutableInteger();

    Tests.executeUntil(
        () -> fragmentsRead.get() > 0,
        (i) ->
        {
            fragmentsRead.value += pingSubscription.poll(this::echoPingHandler, 10);
            Thread.yield();
        },
        Integer.MAX_VALUE,
        TimeUnit.MILLISECONDS.toNanos(5900));

    fragmentsRead.set(0);

    Tests.executeUntil(
        () -> fragmentsRead.get() > 0,
        (i) ->
        {
            fragmentsRead.value += pongSubscription.poll(pongHandler, 10);
            Thread.yield();
        },
        Integer.MAX_VALUE,
        TimeUnit.MILLISECONDS.toNanos(5900));

    verify(pongHandler).onFragment(
        any(DirectBuffer.class),
        eq(DataHeaderFlyweight.HEADER_LENGTH),
        eq(BitUtil.SIZE_OF_INT),
        any(Header.class));
}
 
Example 3
Source File: PongTest.java    From aeron with Apache License 2.0 4 votes vote down vote up
@SlowTest
@Test
public void playPingPongWithRestart()
{
    buffer.putInt(0, 1);

    while (pingPublication.offer(buffer, 0, BitUtil.SIZE_OF_INT) < 0L)
    {
        Tests.yield();
    }

    final MutableInteger fragmentsRead = new MutableInteger();

    Tests.executeUntil(
        () -> fragmentsRead.get() > 0,
        (i) ->
        {
            fragmentsRead.value += pingSubscription.poll(this::echoPingHandler, 1);
            Thread.yield();
        },
        Integer.MAX_VALUE,
        TimeUnit.MILLISECONDS.toNanos(5900));

    fragmentsRead.set(0);

    Tests.executeUntil(
        () -> fragmentsRead.get() > 0,
        (i) ->
        {
            fragmentsRead.value += pongSubscription.poll(pongHandler, 1);
            Thread.yield();
        },
        Integer.MAX_VALUE,
        TimeUnit.MILLISECONDS.toNanos(5900));

    // close Pong side
    pongPublication.close();
    pingSubscription.close();

    // wait for disconnect to ensure we stay in lock step
    while (pingPublication.isConnected())
    {
        Tests.sleep(10);
    }

    // restart Pong side
    pingSubscription = pingClient.addSubscription(PING_URI, PING_STREAM_ID);
    pongPublication = pongClient.addPublication(PONG_URI, PONG_STREAM_ID);

    fragmentsRead.set(0);

    while (pingPublication.offer(buffer, 0, BitUtil.SIZE_OF_INT) < 0L)
    {
        Tests.yield();
    }

    Tests.executeUntil(
        () -> fragmentsRead.get() > 0,
        (i) ->
        {
            fragmentsRead.value += pingSubscription.poll(this::echoPingHandler, 10);
            Thread.yield();
        },
        Integer.MAX_VALUE,
        TimeUnit.MILLISECONDS.toNanos(5900));

    fragmentsRead.set(0);

    Tests.executeUntil(
        () -> fragmentsRead.get() > 0,
        (i) ->
        {
            fragmentsRead.value += pongSubscription.poll(pongHandler, 10);
            Thread.yield();
        },
        Integer.MAX_VALUE,
        TimeUnit.MILLISECONDS.toNanos(5900));

    verify(pongHandler, times(2)).onFragment(
        any(DirectBuffer.class),
        eq(DataHeaderFlyweight.HEADER_LENGTH),
        eq(BitUtil.SIZE_OF_INT),
        any(Header.class));
}
 
Example 4
Source File: StopStartSecondSubscriberTest.java    From aeron with Apache License 2.0 4 votes vote down vote up
private void shouldReceiveMessagesAfterStopStart(
    final String channelOne, final int streamOne, final String channelTwo, final int streamTwo)
{
    final ExecutorService executor = Executors.newFixedThreadPool(2);
    final int numMessages = 1;
    final MutableInteger subscriber2AfterRestartCount = new MutableInteger();
    final AtomicBoolean running = new AtomicBoolean(true);

    final FragmentHandler fragmentHandler2b =
        (buffer, offset, length, header) -> subscriber2AfterRestartCount.value++;

    launch(channelOne, streamOne, channelTwo, streamTwo);

    buffer.putInt(0, 1);

    executor.execute(() -> doPublisherWork(publicationOne, running));
    executor.execute(() -> doPublisherWork(publicationTwo, running));

    final MutableInteger fragmentsReadOne = new MutableInteger();
    final MutableInteger fragmentsReadTwo = new MutableInteger();
    final BooleanSupplier fragmentsReadCondition =
        () -> fragmentsReadOne.get() >= numMessages && fragmentsReadTwo.get() >= numMessages;

    Tests.executeUntil(
        fragmentsReadCondition,
        (i) ->
        {
            fragmentsReadOne.value += subscriptionOne.poll(fragmentHandlerOne, 1);
            fragmentsReadTwo.value += subscriptionTwo.poll(fragmentHandlerTwo, 1);
            Thread.yield();
        },
        Integer.MAX_VALUE,
        TimeUnit.MILLISECONDS.toNanos(4900));

    assertTrue(subOneCount.get() >= numMessages);
    assertTrue(subTwoCount.get() >= numMessages);

    subscriptionTwo.close();

    fragmentsReadOne.set(0);
    fragmentsReadTwo.set(0);

    subscriptionTwo = subscriberTwo.addSubscription(channelTwo, streamTwo);

    Tests.executeUntil(
        fragmentsReadCondition,
        (i) ->
        {
            fragmentsReadOne.value += subscriptionOne.poll(fragmentHandlerOne, 1);
            fragmentsReadTwo.value += subscriptionTwo.poll(fragmentHandler2b, 1);
            Thread.yield();
        },
        Integer.MAX_VALUE,
        TimeUnit.MILLISECONDS.toNanos(4900));

    running.set(false);

    assertTrue(subOneCount.get() >= numMessages * 2,
        "Expecting subscriberOne to receive messages the entire time");
    assertTrue(subTwoCount.get() >= numMessages,
        "Expecting subscriberTwo to receive messages before being stopped and started");
    assertTrue(subscriber2AfterRestartCount.get() >= numMessages,
        "Expecting subscriberTwo to receive messages after being stopped and started");

    executor.shutdown();

    try
    {
        while (!executor.awaitTermination(1, TimeUnit.SECONDS))
        {
            System.err.println("awaiting termination");
        }
    }
    catch (final InterruptedException ex)
    {
        LangUtil.rethrowUnchecked(ex);
    }
}
 
Example 5
Source File: MultiDriverTest.java    From aeron with Apache License 2.0 4 votes vote down vote up
@Test
@Timeout(10)
public void shouldJoinExistingIdleStreamWithLockStepSendingReceiving() throws InterruptedException
{
    final int numMessagesToSendPreJoin = 0;
    final int numMessagesToSendPostJoin = NUM_MESSAGES_PER_TERM;

    launch();

    subscriptionA = clientA.addSubscription(MULTICAST_URI, STREAM_ID);
    publication = clientA.addPublication(MULTICAST_URI, STREAM_ID);

    while (!publication.isConnected() && !subscriptionA.isConnected())
    {
        Tests.yield();
    }

    final CountDownLatch newImageLatch = new CountDownLatch(1);
    subscriptionB = clientB.addSubscription(MULTICAST_URI, STREAM_ID, (image) -> newImageLatch
        .countDown(), null);

    newImageLatch.await();

    for (int i = 0; i < numMessagesToSendPostJoin; i++)
    {
        while (publication.offer(buffer, 0, buffer.capacity()) < 0L)
        {
            Tests.yield();
        }

        final MutableInteger fragmentsRead = new MutableInteger();
        Tests.executeUntil(
            () -> fragmentsRead.get() > 0,
            (j) ->
            {
                fragmentsRead.value += subscriptionA.poll(fragmentHandlerA, 10);
                Thread.yield();
            },
            Integer.MAX_VALUE,
            TimeUnit.MILLISECONDS.toNanos(500));

        fragmentsRead.set(0);
        Tests.executeUntil(
            () -> fragmentsRead.get() > 0,
            (j) ->
            {
                fragmentsRead.value += subscriptionB.poll(fragmentHandlerB, 10);
                Thread.yield();
            },
            Integer.MAX_VALUE,
            TimeUnit.MILLISECONDS.toNanos(500));
    }

    assertEquals(numMessagesToSendPreJoin + numMessagesToSendPostJoin, fragmentCountA.value);
    assertEquals(numMessagesToSendPostJoin, fragmentCountB.value);
}
 
Example 6
Source File: MaxFlowControlStrategySystemTest.java    From aeron with Apache License 2.0 4 votes vote down vote up
@Test
@Timeout(10)
public void shouldTimeoutImageWhenBehindForTooLongWithMaxMulticastFlowControlStrategy()
{
    final int numMessagesToSend = NUM_MESSAGES_PER_TERM * 3;

    driverBContext.imageLivenessTimeoutNs(TimeUnit.MILLISECONDS.toNanos(500));
    driverAContext.multicastFlowControlSupplier(new MaxMulticastFlowControlSupplier());

    launch();

    subscriptionA = clientA.addSubscription(MULTICAST_URI, STREAM_ID);
    subscriptionB = clientB.addSubscription(MULTICAST_URI, STREAM_ID);
    publication = clientA.addPublication(MULTICAST_URI, STREAM_ID);

    while (!subscriptionA.isConnected() || !subscriptionB.isConnected() || !publication.isConnected())
    {
        Tests.yield();
    }

    final MutableInteger fragmentsRead = new MutableInteger();

    for (int i = 0; i < numMessagesToSend; i++)
    {
        while (publication.offer(buffer, 0, buffer.capacity()) < 0L)
        {
            Tests.yield();
        }

        fragmentsRead.set(0);

        // A keeps up
        Tests.executeUntil(
            () -> fragmentsRead.get() > 0,
            (j) ->
            {
                fragmentsRead.value += subscriptionA.poll(fragmentHandlerA, 10);
                Thread.yield();
            },
            Integer.MAX_VALUE,
            TimeUnit.MILLISECONDS.toNanos(500));

        fragmentsRead.set(0);

        // B receives slowly and eventually can't keep up
        if (i % 10 == 0)
        {
            Tests.executeUntil(
                () -> fragmentsRead.get() > 0,
                (j) ->
                {
                    fragmentsRead.value += subscriptionB.poll(fragmentHandlerB, 1);
                    Thread.yield();
                },
                Integer.MAX_VALUE,
                TimeUnit.MILLISECONDS.toNanos(500));
        }
    }

    verify(fragmentHandlerA, times(numMessagesToSend)).onFragment(
        any(DirectBuffer.class),
        anyInt(),
        eq(MESSAGE_LENGTH),
        any(Header.class));

    verify(fragmentHandlerB, atMost(numMessagesToSend - 1)).onFragment(
        any(DirectBuffer.class),
        anyInt(),
        eq(MESSAGE_LENGTH),
        any(Header.class));
}