diff --git a/rcljava/src/test/java/org/ros2/rcljava/SpinTest.java b/rcljava/src/test/java/org/ros2/rcljava/SpinTest.java index 2562d23e..aa9b51ab 100644 --- a/rcljava/src/test/java/org/ros2/rcljava/SpinTest.java +++ b/rcljava/src/test/java/org/ros2/rcljava/SpinTest.java @@ -41,6 +41,7 @@ import org.ros2.rcljava.qos.policies.Reliability; import org.ros2.rcljava.qos.QoSProfile; import org.ros2.rcljava.subscription.Subscription; +import org.ros2.rcljava.subscription.statuses.RequestedQosIncompatible; public class SpinTest { public static class TimerCallback implements Callback { @@ -345,7 +346,7 @@ public Node getNode() { } // custom event consumer - public static class EventConsumer implements Consumer { + public static class OfferedQosIncompatibleConsumer implements Consumer { public boolean done = false; public void accept(final OfferedQosIncompatible status) { @@ -357,35 +358,93 @@ public void accept(final OfferedQosIncompatible status) { } @Test - public final void testSpinEvent() { + public final void testSpinPublisherEvent() { String identifier = RCLJava.getRMWIdentifier(); if (identifier.equals("rmw_fastrtps_cpp") || identifier.equals("rmw_fastrtps_dynamic_cpp")) { - // OfferedQosIncompatible event not supported in these implementations + // OfferedQosIncompatible events is not supported in these implementations. return; } - final Node node = RCLJava.createNode("test_node_spin_event"); + final Node node = RCLJava.createNode("test_node_spin_publisher_event"); Publisher publisher = node.createPublisher( - std_msgs.msg.String.class, "test_topic_spin_event", new QoSProfile( + std_msgs.msg.String.class, "test_topic_spin_publisher_event", new QoSProfile( History.KEEP_LAST, 1, Reliability.BEST_EFFORT, Durability.VOLATILE, false)); // create a OfferedQoSIncompatible event handler with custom event consumer - EventConsumer eventConsumer = new EventConsumer(); + OfferedQosIncompatibleConsumer eventConsumer = new OfferedQosIncompatibleConsumer(); EventHandler eventHandler = publisher.createEventHandler( OfferedQosIncompatible.factory, eventConsumer ); // create an incompatible subscription (reliable vs best effort publisher) Subscription subscription = node.createSubscription( - std_msgs.msg.String.class, "test_topic_spin_event", - new Consumer() { - public void accept(final std_msgs.msg.String msg) {} - }, - new QoSProfile( - History.KEEP_LAST, 1, - Reliability.RELIABLE, - Durability.VOLATILE, - false)); + std_msgs.msg.String.class, "test_topic_spin_publisher_event", + new Consumer() { + public void accept(final std_msgs.msg.String msg) {} + }, + new QoSProfile( + History.KEEP_LAST, 1, + Reliability.RELIABLE, + Durability.VOLATILE, + false)); + // set up executor + ComposableNode composableNode = new ComposableNode() { + public Node getNode() { + return node; + } + }; + Executor executor = new SingleThreadedExecutor(); + executor.addNode(composableNode); + long start = System.currentTimeMillis(); + do { + executor.spinAll((1000 + System.currentTimeMillis() - start) * 1000 * 1000); + } while (!eventConsumer.done && System.currentTimeMillis() < start + 1000); + assert(eventConsumer.done); + } + + public static class RequestedQosIncompatibleConsumer + implements Consumer { + public boolean done = false; + + public void accept(final RequestedQosIncompatible status) { + assertEquals(status.totalCount, 1); + assertEquals(status.totalCountChange, 1); + assertEquals(status.lastPolicyKind, RequestedQosIncompatible.PolicyKind.RELIABILITY); + this.done = true; + } + } + + @Test + public final void testSpinSubscriptionEvent() { + String identifier = RCLJava.getRMWIdentifier(); + if (identifier.equals("rmw_fastrtps_cpp") || identifier.equals("rmw_fastrtps_dynamic_cpp")) { + // RequestedQosIncompatible events is not supported in these implementations. + return; + } + final Node node = RCLJava.createNode("test_node_spin_subscription_event"); + // create an incompatible subscription (reliable vs best effort publisher) + Subscription subscription = node.createSubscription( + std_msgs.msg.String.class, "test_topic_spin_subscription_event", + new Consumer() { + public void accept(final std_msgs.msg.String msg) {} + }, + new QoSProfile( + History.KEEP_LAST, 1, + Reliability.RELIABLE, + Durability.VOLATILE, + false)); + // create a RequestedQoSIncompatible event handler with custom event consumer + RequestedQosIncompatibleConsumer eventConsumer = new RequestedQosIncompatibleConsumer(); + EventHandler eventHandler = subscription.createEventHandler( + RequestedQosIncompatible.factory, eventConsumer + ); + Publisher publisher = node.createPublisher( + std_msgs.msg.String.class, "test_topic_spin_subscription_event", new QoSProfile( + History.KEEP_LAST, 1, + Reliability.BEST_EFFORT, + Durability.VOLATILE, + false)); + // set up executor ComposableNode composableNode = new ComposableNode() { public Node getNode() { diff --git a/rcljava/src/test/java/org/ros2/rcljava/subscription/SubscriptionTest.java b/rcljava/src/test/java/org/ros2/rcljava/subscription/SubscriptionTest.java index 6b01a634..4f5e1ea1 100644 --- a/rcljava/src/test/java/org/ros2/rcljava/subscription/SubscriptionTest.java +++ b/rcljava/src/test/java/org/ros2/rcljava/subscription/SubscriptionTest.java @@ -23,7 +23,9 @@ import org.ros2.rcljava.RCLJava; import org.ros2.rcljava.consumers.Consumer; +import org.ros2.rcljava.events.EventHandler; import org.ros2.rcljava.node.Node; +import org.ros2.rcljava.subscription.statuses.RequestedQosIncompatible; public class SubscriptionTest { @BeforeClass @@ -53,4 +55,33 @@ public void accept(final std_msgs.msg.String msg) {} RCLJava.shutdown(); } + + @Test + public final void testCreateRequestedQosIncompatibleEvent() { + String identifier = RCLJava.getRMWIdentifier(); + if (identifier.equals("rmw_fastrtps_cpp") || identifier.equals("rmw_fastrtps_dynamic_cpp")) { + // event not supported in these implementations + return; + } + RCLJava.rclJavaInit(); + Node node = RCLJava.createNode("test_node"); + Subscription subscription = node.createSubscription( + std_msgs.msg.String.class, "test_topic", new Consumer() { + public void accept(final std_msgs.msg.String msg) {} + }); + EventHandler eventHandler = subscription.createEventHandler( + RequestedQosIncompatible.factory, new Consumer() { + public void accept(final RequestedQosIncompatible status) { + assertEquals(status.totalCount, 0); + assertEquals(status.totalCountChange, 0); + assertEquals(status.lastPolicyKind, RequestedQosIncompatible.PolicyKind.INVALID); + } + } + ); + assertNotEquals(0, eventHandler.getHandle()); + // force executing the callback, so we check that taking an event works + eventHandler.executeCallback(); + RCLJava.shutdown(); + assertEquals(0, eventHandler.getHandle()); + } }