org.agrona.collections.MutableInteger Java Examples

The following examples show how to use org.agrona.collections.MutableInteger. 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: ArchiveTool.java    From aeron with Apache License 2.0 6 votes vote down vote up
static boolean verify(
    final PrintStream out,
    final File archiveDir,
    final Set<VerifyOption> options,
    final Checksum checksum,
    final EpochClock epochClock,
    final ActionConfirmation<File> truncateFileOnPageStraddle)
{
    try (Catalog catalog = openCatalog(archiveDir, epochClock))
    {
        final MutableInteger errorCount = new MutableInteger();
        catalog.forEach(createVerifyEntryProcessor(
            out, archiveDir, options, checksum, epochClock, errorCount, truncateFileOnPageStraddle));

        return errorCount.get() == 0;
    }
}
 
Example #2
Source File: OneToOneRingBufferConcurrentTest.java    From agrona with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldExchangeMessagesViaTryClaimAbort()
{
    new ClaimAbort().start();

    final MutableInteger count = new MutableInteger();
    final MessageHandler handler =
        (msgTypeId, buffer, index, length) ->
        {
            final int iteration = buffer.getInt(index);

            assertEquals(count.get(), iteration);
            assertEquals(MSG_TYPE_ID, buffer.getInt(typeOffset(index - HEADER_LENGTH)));

            count.increment();
        };

    while (count.get() < REPETITIONS)
    {
        final int readCount = ringBuffer.read(handler);
        if (0 == readCount)
        {
            Thread.yield();
        }
    }
}
 
Example #3
Source File: OneToOneRingBufferConcurrentTest.java    From agrona with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldExchangeMessages()
{
    new Producer().start();

    final MutableInteger count = new MutableInteger();
    final MessageHandler handler =
        (msgTypeId, buffer, index, length) ->
        {
            final int iteration = buffer.getInt(index);

            assertEquals(count.get(), iteration);

            count.increment();
        };

    while (count.get() < REPETITIONS)
    {
        final int readCount = ringBuffer.read(handler);
        if (0 == readCount)
        {
            Thread.yield();
        }
    }
}
 
Example #4
Source File: StatusUtil.java    From aeron with Apache License 2.0 6 votes vote down vote up
/**
 * Return the read-only status indicator for the given receive channel URI.
 *
 * @param countersReader that holds the status indicator.
 * @param channel        for the receive channel.
 * @return read-only status indicator that can be used to query the status of the receive channel or null.
 * @see ChannelEndpointStatus for status values and indications.
 */
public static StatusIndicatorReader receiveChannelStatus(final CountersReader countersReader, final String channel)
{
    StatusIndicatorReader statusReader = null;
    final MutableInteger id = new MutableInteger(-1);

    countersReader.forEach(
        (counterId, typeId, keyBuffer, label) ->
        {
            if (typeId == ReceiveChannelStatus.RECEIVE_CHANNEL_STATUS_TYPE_ID)
            {
                if (channel.startsWith(keyBuffer.getStringAscii(ChannelEndpointStatus.CHANNEL_OFFSET)))
                {
                    id.value = counterId;
                }
            }
        });

    if (Aeron.NULL_VALUE != id.value)
    {
        statusReader = new UnsafeBufferStatusIndicator(countersReader.valuesBuffer(), id.value);
    }

    return statusReader;
}
 
Example #5
Source File: StatusUtil.java    From aeron with Apache License 2.0 6 votes vote down vote up
/**
 * Return the read-only status indicator for the given send channel URI.
 *
 * @param countersReader that holds the status indicator.
 * @param channel        for the send channel.
 * @return read-only status indicator that can be used to query the status of the send channel or null
 * @see ChannelEndpointStatus for status values and indications.
 */
public static StatusIndicatorReader sendChannelStatus(final CountersReader countersReader, final String channel)
{
    StatusIndicatorReader statusReader = null;
    final MutableInteger id = new MutableInteger(-1);

    countersReader.forEach(
        (counterId, typeId, keyBuffer, label) ->
        {
            if (typeId == SendChannelStatus.SEND_CHANNEL_STATUS_TYPE_ID)
            {
                if (channel.startsWith(keyBuffer.getStringAscii(ChannelEndpointStatus.CHANNEL_OFFSET)))
                {
                    id.value = counterId;
                }
            }
        });

    if (Aeron.NULL_VALUE != id.value)
    {
        statusReader = new UnsafeBufferStatusIndicator(countersReader.valuesBuffer(), id.value);
    }

    return statusReader;
}
 
Example #6
Source File: StatusUtil.java    From aeron with Apache License 2.0 6 votes vote down vote up
/**
 * Return the controllable idle strategy {@link StatusIndicator}.
 *
 * @param countersReader that holds the status indicator.
 * @return status indicator to use or null if not found.
 */
public static StatusIndicator controllableIdleStrategy(final CountersReader countersReader)
{
    StatusIndicator statusIndicator = null;
    final MutableInteger id = new MutableInteger(-1);

    countersReader.forEach(
        (counterId, label) ->
        {
            if (counterId == SystemCounterDescriptor.CONTROLLABLE_IDLE_STRATEGY.id() &&
                label.equals(SystemCounterDescriptor.CONTROLLABLE_IDLE_STRATEGY.label()))
            {
                id.value = counterId;
            }
        });

    if (Aeron.NULL_VALUE != id.value)
    {
        statusIndicator = new UnsafeBufferStatusIndicator(countersReader.valuesBuffer(), id.value);
    }

    return statusIndicator;
}
 
Example #7
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 #8
Source File: ClusterNodeTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
@Test
@Timeout(10)
public void shouldSendResponseAfterServiceMessage()
{
    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) ->
        {
            assertEquals(msg, buffer.getStringWithoutLengthAscii(offset, length));
            messageCount.value += 1;
        };

    container = launchServiceMessageIngressService();
    aeronCluster = connectToCluster(listener);

    offerMessage(msgBuffer, msg);
    awaitResponse(messageCount);

    ClusterTests.failOnClusterError();
}
 
Example #9
Source File: ClusterNodeTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
@Test
@Timeout(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();
}
 
Example #10
Source File: ClusterNodeTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
@Test
@Timeout(10)
public void shouldEchoMessageViaServiceUsingDirectOffer()
{
    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) ->
        {
            assertEquals(msg, buffer.getStringWithoutLengthAscii(offset, length));
            messageCount.value += 1;
        };

    container = launchEchoService();
    aeronCluster = connectToCluster(listener);

    offerMessage(msgBuffer, msg);
    awaitResponse(messageCount);

    ClusterTests.failOnClusterError();
}
 
Example #11
Source File: ArchiveTool.java    From aeron with Apache License 2.0 6 votes vote down vote up
static boolean verifyRecording(
    final PrintStream out,
    final File archiveDir,
    final long recordingId,
    final Set<VerifyOption> options,
    final Checksum checksum,
    final EpochClock epochClock,
    final ActionConfirmation<File> truncateFileOnPageStraddle)
{
    try (Catalog catalog = openCatalog(archiveDir, epochClock))
    {
        final MutableInteger errorCount = new MutableInteger();
        if (!catalog.forEntry(recordingId, createVerifyEntryProcessor(
            out, archiveDir, options, checksum, epochClock, errorCount, truncateFileOnPageStraddle)))
        {
            throw new AeronException("no recording found with recordingId: " + recordingId);
        }

        return errorCount.get() == 0;
    }
}
 
Example #12
Source File: ArchiveTool.java    From aeron with Apache License 2.0 6 votes vote down vote up
private static CatalogEntryProcessor createVerifyEntryProcessor(
    final PrintStream out,
    final File archiveDir,
    final Set<VerifyOption> options,
    final Checksum checksum,
    final EpochClock epochClock,
    final MutableInteger errorCount,
    final ActionConfirmation<File> truncateFileOnPageStraddle)
{
    final ByteBuffer buffer = BufferUtil.allocateDirectAligned(FILE_IO_MAX_LENGTH_DEFAULT, CACHE_LINE_LENGTH);
    buffer.order(LITTLE_ENDIAN);
    final DataHeaderFlyweight headerFlyweight = new DataHeaderFlyweight(buffer);

    return (headerEncoder, headerDecoder, descriptorEncoder, descriptorDecoder) -> verifyRecording(
        out,
        archiveDir,
        options,
        checksum,
        epochClock,
        errorCount,
        truncateFileOnPageStraddle,
        headerFlyweight,
        headerEncoder,
        descriptorEncoder,
        descriptorDecoder);
}
 
Example #13
Source File: ExtendRecordingTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
private static void consume(
    final Subscription subscription, final int startIndex, final int count, final String prefix)
{
    final MutableInteger received = new MutableInteger(startIndex);

    final FragmentHandler fragmentHandler = new FragmentAssembler(
        (buffer, offset, length, header) ->
        {
            final String expected = prefix + received.value;
            final String actual = buffer.getStringWithoutLengthAscii(offset, length);

            assertEquals(expected, actual);

            received.value++;
        });

    while (received.value < (startIndex + count))
    {
        if (0 == subscription.poll(fragmentHandler, FRAGMENT_LIMIT))
        {
            Tests.yield();
        }
    }

    assertEquals(startIndex + count, received.get());
}
 
Example #14
Source File: Common.java    From aeron with Apache License 2.0 6 votes vote down vote up
static void consume(final Subscription subscription, final int count, final String prefix)
{
    final MutableInteger received = new MutableInteger(0);

    final FragmentHandler fragmentHandler = new FragmentAssembler(
        (buffer, offset, length, header) ->
        {
            final String expected = prefix + received.value;
            final String actual = buffer.getStringWithoutLengthAscii(offset, length);

            assertEquals(expected, actual);

            received.value++;
        });

    while (received.value < count)
    {
        if (0 == subscription.poll(fragmentHandler, FRAGMENT_LIMIT))
        {
            Tests.yield();
        }
    }

    assertEquals(count, received.get());
}
 
Example #15
Source File: DriverNameResolverTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
private int neighborsCounterId(final String name)
{
    final CountersReader countersReader = clients.get(name).countersReader();
    final MutableInteger id = new MutableInteger(NULL_VALUE);

    countersReader.forEach(
        (counterId, typeId, keyBuffer, label) ->
        {
            if (label.startsWith("Resolver neighbors"))
            {
                id.value = counterId;
            }
        });

    return id.value;
}
 
Example #16
Source File: DriverNameResolverTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
private int cacheEntriesCounterId(final String name)
{
    final CountersReader countersReader = clients.get(name).countersReader();
    final MutableInteger id = new MutableInteger(NULL_VALUE);

    countersReader.forEach(
        (counterId, typeId, keyBuffer, label) ->
        {
            if (label.startsWith("Resolver cache entries"))
            {
                id.value = counterId;
            }
        });

    return id.value;
}
 
Example #17
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 #18
Source File: OrderBookDirectImpl.java    From exchange-core with Apache License 2.0 5 votes vote down vote up
@Override
public int getOrdersNum(OrderAction action) {
    final LongAdaptiveRadixTreeMap<Bucket> buckets = action == OrderAction.ASK ? askPriceBuckets : bidPriceBuckets;
    final MutableInteger accum = new MutableInteger();
    buckets.forEach((p, b) -> accum.value += b.numOrders, Integer.MAX_VALUE);
    return accum.value;
}
 
Example #19
Source File: OneToOneRingBufferConcurrentTest.java    From agrona with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldExchangeMessagesViaTryClaimCommit()
{
    new ClaimCommit().start();

    final MutableInteger count = new MutableInteger();
    final MessageHandler handler =
        (msgTypeId, buffer, index, length) ->
        {
            final int iteration = buffer.getInt(index);
            final long longVal = buffer.getLong(index + SIZE_OF_INT);

            assertEquals(count.get(), iteration);
            assertEquals(count.get() * 20L, longVal);

            count.increment();
        };

    while (count.get() < REPETITIONS)
    {
        final int readCount = ringBuffer.read(handler);
        if (0 == readCount)
        {
            Thread.yield();
        }
    }
}
 
Example #20
Source File: ClusterNodeTest.java    From aeron with Apache License 2.0 5 votes vote down vote up
private void awaitResponse(final MutableInteger messageCount)
{
    while (messageCount.get() == 0)
    {
        if (aeronCluster.pollEgress() <= 0)
        {
            Tests.yield();
        }
    }
}
 
Example #21
Source File: StopStartSecondSubscriberTest.java    From aeron with Apache License 2.0 5 votes vote down vote up
@Test
@Timeout(10)
public void shouldReceivePublishedMessage()
{
    launch(CHANNEL1, STREAM_ID1, CHANNEL2, STREAM_ID2);

    buffer.putInt(0, 1);

    final int messagesPerPublication = 1;

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

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

    final MutableInteger fragmentsRead1 = new MutableInteger();
    final MutableInteger fragmentsRead2 = new MutableInteger();
    Tests.executeUntil(
        () -> fragmentsRead1.get() >= messagesPerPublication && fragmentsRead2.get() >= messagesPerPublication,
        (i) ->
        {
            fragmentsRead1.value += subscriptionOne.poll(fragmentHandlerOne, 10);
            fragmentsRead2.value += subscriptionTwo.poll(fragmentHandlerTwo, 10);
            Thread.yield();
        },
        Integer.MAX_VALUE,
        TimeUnit.MILLISECONDS.toNanos(9900));

    assertEquals(messagesPerPublication, subOneCount.get());
    assertEquals(messagesPerPublication, subTwoCount.get());
}
 
Example #22
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));
}
 
Example #23
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 #24
Source File: SpySimulatedConnectionTest.java    From aeron with Apache License 2.0 4 votes vote down vote up
@ParameterizedTest
@MethodSource("channels")
@Timeout(10)
public void shouldSimulateConnectionWithNoNetworkSubscriptions(final String channel)
{
    final int messagesToSend = NUM_MESSAGES_PER_TERM * 3;

    driverContext
        .publicationConnectionTimeoutNs(TimeUnit.MILLISECONDS.toNanos(250))
        .timerIntervalNs(TimeUnit.MILLISECONDS.toNanos(100))
        .spiesSimulateConnection(true);

    launch();

    spy = client.addSubscription(spyForChannel(channel), STREAM_ID);
    publication = client.addPublication(channel, STREAM_ID);

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

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

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

    assertEquals(messagesToSend, fragmentCountSpy.value);
}
 
Example #25
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 #26
Source File: PubAndSubTest.java    From aeron with Apache License 2.0 4 votes vote down vote up
@ParameterizedTest
@MethodSource("channels")
@Timeout(10)
public void shouldFragmentExactMessageLengthsCorrectly(final String channel)
{
    final int termBufferLength = 64 * 1024;
    final int numFragmentsPerMessage = 2;
    final int mtuLength = context.mtuLength();
    final int frameLength = mtuLength - HEADER_LENGTH;
    final int messageLength = frameLength * numFragmentsPerMessage;
    final int numMessagesToSend = 2;
    final int numFramesToExpect = numMessagesToSend * numFragmentsPerMessage;

    context.publicationTermBufferLength(termBufferLength);

    launch(channel);

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

    final MutableInteger fragmentsRead = new MutableInteger();

    Tests.executeUntil(
        () -> fragmentsRead.value > numFramesToExpect,
        (j) ->
        {
            final int fragments = subscription.poll(fragmentHandler, 10);
            if (0 == fragments)
            {
                Thread.yield();
            }
            fragmentsRead.value += fragments;
        },
        Integer.MAX_VALUE,
        TimeUnit.MILLISECONDS.toNanos(500));

    verify(fragmentHandler, times(numFramesToExpect)).onFragment(
        any(DirectBuffer.class),
        anyInt(),
        eq(frameLength),
        any(Header.class));
}
 
Example #27
Source File: PubAndSubTest.java    From aeron with Apache License 2.0 4 votes vote down vote up
@ParameterizedTest
@MethodSource("channels")
@Timeout(10)
public void shouldReceiveOnlyAfterSendingUpToFlowControlLimit(final String channel)
{
    /*
     * The subscriber will flow control before an entire term buffer. So, send until can't send no 'more.
     * Then start up subscriber to drain.
     */
    final int termBufferLength = 64 * 1024;
    final int numMessagesPerTerm = 64;
    final int messageLength = (termBufferLength / numMessagesPerTerm) - HEADER_LENGTH;
    final int maxFails = 10000;
    int messagesSent = 0;

    context.publicationTermBufferLength(termBufferLength);

    launch(channel);

    for (int i = 0; i < numMessagesPerTerm; i++)
    {
        int offerFails = 0;

        while (publication.offer(buffer, 0, messageLength) < 0L)
        {
            if (++offerFails > maxFails)
            {
                break;
            }

            Tests.yield();
        }

        if (offerFails > maxFails)
        {
            break;
        }

        messagesSent++;
    }

    final MutableInteger fragmentsRead = new MutableInteger();
    final int messagesToReceive = messagesSent;

    Tests.executeUntil(
        () -> fragmentsRead.value >= messagesToReceive,
        (j) ->
        {
            final int fragments = subscription.poll(fragmentHandler, 10);
            if (0 == fragments)
            {
                Thread.yield();
            }
            fragmentsRead.value += fragments;
        },
        Integer.MAX_VALUE,
        TimeUnit.MILLISECONDS.toNanos(500));

    verify(fragmentHandler, times(messagesToReceive)).onFragment(
        any(DirectBuffer.class),
        anyInt(),
        eq(messageLength),
        any(Header.class));
}
 
Example #28
Source File: PubAndSubTest.java    From aeron with Apache License 2.0 4 votes vote down vote up
@ParameterizedTest
@MethodSource("channels")
@Timeout(10)
public void shouldContinueAfterBufferRolloverBatched(final String channel)
{
    final int termBufferLength = 64 * 1024;
    final int numBatchesPerTerm = 4;
    final int numMessagesPerBatch = 16;
    final int numMessagesInTermBuffer = numMessagesPerBatch * numBatchesPerTerm;
    final int messageLength = (termBufferLength / numMessagesInTermBuffer) - HEADER_LENGTH;
    final int numMessagesToSend = numMessagesInTermBuffer + 1;

    context.publicationTermBufferLength(termBufferLength);

    launch(channel);

    for (int i = 0; i < numBatchesPerTerm; i++)
    {
        for (int j = 0; j < numMessagesPerBatch; j++)
        {
            while (publication.offer(buffer, 0, messageLength) < 0L)
            {
                Tests.yield();
            }
        }

        pollForBatch(numMessagesPerBatch);
    }

    while (publication.offer(buffer, 0, messageLength) < 0L)
    {
        Tests.yield();
    }

    final MutableInteger fragmentsRead = new MutableInteger();

    Tests.executeUntil(
        () -> fragmentsRead.value > 0,
        (j) ->
        {
            final int fragments = subscription.poll(fragmentHandler, 10);
            if (0 == fragments)
            {
                Thread.yield();
            }
            fragmentsRead.value += fragments;
        },
        Integer.MAX_VALUE,
        TimeUnit.MILLISECONDS.toNanos(900));

    verify(fragmentHandler, times(numMessagesToSend)).onFragment(
        any(DirectBuffer.class),
        anyInt(),
        eq(messageLength),
        any(Header.class));
}
 
Example #29
Source File: PubAndSubTest.java    From aeron with Apache License 2.0 4 votes vote down vote up
@ParameterizedTest
@MethodSource("channels")
@Timeout(10)
public void shouldContinueAfterRolloverWithMinimalPaddingHeader(final String channel)
{
    final int termBufferLength = 64 * 1024;
    final int termBufferLengthMinusPaddingHeader = termBufferLength - HEADER_LENGTH;
    final int num1kMessagesInTermBuffer = 63;
    final int lastMessageLength =
        termBufferLengthMinusPaddingHeader - (num1kMessagesInTermBuffer * 1024) - HEADER_LENGTH;
    final int messageLength = 1024 - HEADER_LENGTH;

    context.publicationTermBufferLength(termBufferLength);

    launch(channel);

    // lock step reception until we get to within 8 messages of the end
    for (int i = 0; i < num1kMessagesInTermBuffer - 7; i++)
    {
        while (publication.offer(buffer, 0, messageLength) < 0L)
        {
            Tests.yield();
        }

        pollForFragment();
    }

    for (int i = 7; i > 0; i--)
    {
        while (publication.offer(buffer, 0, messageLength) < 0L)
        {
            Tests.yield();
        }
    }

    // small enough to leave room for padding that is just a header
    while (publication.offer(buffer, 0, lastMessageLength) < 0L)
    {
        Tests.yield();
    }

    // no roll over
    while (publication.offer(buffer, 0, messageLength) < 0L)
    {
        Tests.yield();
    }

    final MutableInteger fragmentsRead = new MutableInteger();

    Tests.executeUntil(
        () -> fragmentsRead.value == 9,
        (j) ->
        {
            final int fragments = subscription.poll(fragmentHandler, 10);
            if (0 == fragments)
            {
                Thread.yield();
            }
            fragmentsRead.value += fragments;
        },
        Integer.MAX_VALUE,
        TimeUnit.MILLISECONDS.toNanos(500));

    final InOrder inOrder = inOrder(fragmentHandler);

    inOrder.verify(fragmentHandler, times(num1kMessagesInTermBuffer)).onFragment(
        any(DirectBuffer.class),
        anyInt(),
        eq(messageLength),
        any(Header.class));
    inOrder.verify(fragmentHandler, times(1)).onFragment(
        any(DirectBuffer.class),
        anyInt(),
        eq(lastMessageLength),
        any(Header.class));
    inOrder.verify(fragmentHandler, times(1)).onFragment(
        any(DirectBuffer.class),
        anyInt(),
        eq(messageLength),
        any(Header.class));
}
 
Example #30
Source File: ExclusivePublicationTest.java    From aeron with Apache License 2.0 4 votes vote down vote up
@ParameterizedTest
@MethodSource("channels")
@Timeout(10)
public void shouldPublishFromIndependentExclusivePublications(final String channel)
{
    try (Subscription subscription = aeron.addSubscription(channel, STREAM_ID);
        ExclusivePublication publicationOne = aeron.addExclusivePublication(channel, STREAM_ID);
        ExclusivePublication publicationTwo = aeron.addExclusivePublication(channel, STREAM_ID))
    {
        final int expectedNumberOfFragments = 778;
        int totalFragmentsRead = 0;
        final MutableInteger messageCount = new MutableInteger();
        final FragmentHandler fragmentHandler =
            (buffer, offset, length, header) ->
            {
                assertEquals(MESSAGE_LENGTH, length);
                messageCount.value++;
            };

        Tests.awaitConnections(subscription, 2);

        for (int i = 0; i < expectedNumberOfFragments; i += 2)
        {
            while (publicationOne.offer(srcBuffer, 0, MESSAGE_LENGTH) < 0L)
            {
                Tests.yield();
                totalFragmentsRead += pollFragments(subscription, fragmentHandler);
            }

            while (publicationTwo.offer(srcBuffer, 0, MESSAGE_LENGTH) < 0L)
            {
                Tests.yield();
                totalFragmentsRead += pollFragments(subscription, fragmentHandler);
            }

            totalFragmentsRead += pollFragments(subscription, fragmentHandler);
        }

        do
        {
            totalFragmentsRead += pollFragments(subscription, fragmentHandler);
        }
        while (totalFragmentsRead < expectedNumberOfFragments);

        assertEquals(expectedNumberOfFragments, messageCount.value);
    }
}