us.ihmc.pubsub.test.PublishSubscribeUInt64AllocationTest Maven / Gradle / Ivy
package us.ihmc.pubsub.test;
import com.eprosima.xmlschemas.fastrtps_profiles.DurabilityQosKindPolicyType;
import com.eprosima.xmlschemas.fastrtps_profiles.HistoryQosKindPolicyType;
import com.eprosima.xmlschemas.fastrtps_profiles.ReliabilityQosKindPolicyType;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import us.ihmc.commons.PrintTools;
import us.ihmc.commons.allocations.AllocationProfiler;
import us.ihmc.commons.allocations.AllocationRecord;
import us.ihmc.idl.generated.test.StatusMessage;
import us.ihmc.idl.generated.test.StatusMessagePubSubType;
import us.ihmc.log.LogTools;
import us.ihmc.pubsub.Domain;
import us.ihmc.pubsub.DomainFactory;
import us.ihmc.pubsub.attributes.ParticipantProfile;
import us.ihmc.pubsub.attributes.PublisherAttributes;
import us.ihmc.pubsub.attributes.SubscriberAttributes;
import us.ihmc.pubsub.common.LogLevel;
import us.ihmc.pubsub.common.MatchingInfo;
import us.ihmc.pubsub.common.SampleInfo;
import us.ihmc.pubsub.common.Time;
import us.ihmc.pubsub.participant.Participant;
import us.ihmc.pubsub.participant.ParticipantDiscoveryInfo;
import us.ihmc.pubsub.participant.ParticipantListener;
import us.ihmc.pubsub.publisher.Publisher;
import us.ihmc.pubsub.publisher.PublisherListener;
import us.ihmc.pubsub.subscriber.Subscriber;
import us.ihmc.pubsub.subscriber.SubscriberListener;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
public class PublishSubscribeUInt64AllocationTest
{
public static final int NUMBER_OF_MESSAGES_TO_SEND = 30;
@Tag("allocation")
@Test// timeout = 30000
public void testPublishSubscribeUInt32AllocationsFastRTPS() throws IOException
{
runAllocationTest();
}
@Disabled // intraprocess does not need to be allocation-free for now - @dcalvert
@Tag("allocation")
@Test// timeout = 30000
public void testPublishSubscribeUInt32AllocationsIntraprocess() throws IOException
{
runAllocationTest();
}
public void runAllocationTest() throws IOException
{
AllocationProfiler allocationProfiler = new AllocationProfiler();
Domain domain = DomainFactory.getDomain();
try
{
domain.setLogLevel(LogLevel.INFO);
ParticipantProfile attributes = ParticipantProfile.create()
.domainId(218)
.useOnlyIntraProcessDelivery()
.discoveryLeaseDuration(Time.Infinite)
.name("StatusTest");
Participant participant = domain.createParticipant(attributes, new ParticipantListenerImpl());
StatusMessagePubSubType dataType = new StatusMessagePubSubType();
domain.registerType(participant, dataType);
PublisherAttributes genericPublisherAttributes = PublisherAttributes.create().topicDataType(dataType).topicName("Status")
.reliabilityKind(ReliabilityQosKindPolicyType.RELIABLE)
.partitions(Collections.singletonList("us/ihmc"))
.durabilityKind(DurabilityQosKindPolicyType.TRANSIENT_LOCAL)
.historyQosPolicyKind(HistoryQosKindPolicyType.KEEP_LAST).historyDepth(50);
StatusMessagePubSubType dataType2 = new StatusMessagePubSubType();
SubscriberAttributes subscriberAttributes = SubscriberAttributes.create().topicDataType(dataType2).topicName("Status")
.reliabilityKind(ReliabilityQosKindPolicyType.RELIABLE)
.partitions(Collections.singletonList("us/ihmc"))
.durabilityKind(DurabilityQosKindPolicyType.VOLATILE)
.historyQosPolicyKind(HistoryQosKindPolicyType.KEEP_ALL);
SubscriberListenerImpl subscriberListener = new SubscriberListenerImpl();
Subscriber subscriber = domain.createSubscriber(participant, subscriberAttributes, subscriberListener);
Publisher publisher = domain.createPublisher(participant, genericPublisherAttributes, new PublisherListenerImpl());
StatusMessage msg = new StatusMessage();
msg.setPause(false);
msg.setSequenceId(0);
publishNMessages(publisher, msg, 1); // warmup
allocationProfiler.startRecordingAllocations(); // start recording
publishNMessages(publisher, msg, NUMBER_OF_MESSAGES_TO_SEND);
allocationProfiler.stopRecordingAllocations(); // stop recording
for (StatusMessage message : subscriberListener.receivedMessages)
{
if (message != null)
PrintTools.info(this, "Message received: " + message.toString());
}
List allocations = allocationProfiler.pollAllocations();
String message = "";
for (AllocationRecord allocation : allocations)
{
message += allocation.toString() + "\n";
}
System.out.println(message);
assertTrue(allocations.size() == 0, "allocated " + allocations.size() + ": \n" + message);
}
finally
{
domain.stopAll();
}
}
private void publishNMessages(Publisher publisher, StatusMessage msg, int numberOfMessagesToSend) throws IOException
{
int i = 0;
for (; i < numberOfMessagesToSend; i++)
{
try
{
msg.setPause(i % 2 == 0);
msg.setSequenceId(i);
publisher.write(msg);
Thread.sleep(1000);
++i;
}
catch (InterruptedException e)
{
}
}
}
private class SubscriberListenerImpl implements SubscriberListener
{
private final SampleInfo info = new SampleInfo();
public final StatusMessage[] receivedMessages = new StatusMessage[NUMBER_OF_MESSAGES_TO_SEND];
{
for (int i = 0; i < NUMBER_OF_MESSAGES_TO_SEND; i++)
{
receivedMessages[i] = new StatusMessage();
}
}
public int i = 0;
@Override
public void onNewDataMessage(Subscriber subscriber)
{
if (subscriber.takeNextData(receivedMessages[i++], info))
{
// do nothing, success
}
}
@Override
public void onSubscriptionMatched(Subscriber subscriber, MatchingInfo info)
{
LogTools.debug("New publisher matched");
LogTools.debug("Status: " + info.getStatus());
LogTools.debug("Guid: " + info.getGuid().toString());
}
}
private class ParticipantListenerImpl implements ParticipantListener
{
@Override
public void onParticipantDiscovery(Participant participant, ParticipantDiscoveryInfo info)
{
LogTools.debug("New participant discovered");
LogTools.debug("Status: " + info.getStatus());
LogTools.debug("Guid: " + info.getGuid().toString());
LogTools.debug("Name: " + info.getName());
}
}
private class PublisherListenerImpl implements PublisherListener
{
@Override
public void onPublicationMatched(Publisher publisher, MatchingInfo info)
{
LogTools.debug("New subscriber matched");
LogTools.debug("Status: " + info.getStatus());
LogTools.debug("Guid: " + info.getGuid().toString());
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy