Skip to content

Commit a50c76a

Browse files
committed
Test subscription events (osrf#15)
Signed-off-by: Ivan Santiago Paunovic <ivanpauno@ekumenlabs.com>
1 parent 8e8d1b0 commit a50c76a

File tree

2 files changed

+105
-15
lines changed

2 files changed

+105
-15
lines changed

rcljava/src/test/java/org/ros2/rcljava/SpinTest.java

Lines changed: 74 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.ros2.rcljava.qos.policies.Reliability;
4343
import org.ros2.rcljava.qos.QoSProfile;
4444
import org.ros2.rcljava.subscription.Subscription;
45+
import org.ros2.rcljava.subscription.statuses.RequestedQosIncompatible;
4546

4647
public class SpinTest {
4748
public static class TimerCallback implements Callback {
@@ -357,7 +358,7 @@ public Node getNode() {
357358
}
358359

359360
// custom event consumer
360-
public static class EventConsumer implements Consumer<OfferedQosIncompatible> {
361+
public static class OfferedQosIncompatibleConsumer implements Consumer<OfferedQosIncompatible> {
361362
public boolean done = false;
362363

363364
public void accept(final OfferedQosIncompatible status) {
@@ -369,35 +370,93 @@ public void accept(final OfferedQosIncompatible status) {
369370
}
370371

371372
@Test
372-
public final void testSpinEvent() {
373+
public final void testSpinPublisherEvent() {
373374
String identifier = RCLJava.getRMWIdentifier();
374375
if (identifier.equals("rmw_fastrtps_cpp") || identifier.equals("rmw_fastrtps_dynamic_cpp")) {
375-
// OfferedQosIncompatible event not supported in these implementations
376+
// OfferedQosIncompatible events is not supported in these implementations.
376377
return;
377378
}
378-
final Node node = RCLJava.createNode("test_node_spin_event");
379+
final Node node = RCLJava.createNode("test_node_spin_publisher_event");
379380
Publisher<std_msgs.msg.String> publisher = node.<std_msgs.msg.String>createPublisher(
380-
std_msgs.msg.String.class, "test_topic_spin_event", new QoSProfile(
381+
std_msgs.msg.String.class, "test_topic_spin_publisher_event", new QoSProfile(
381382
History.KEEP_LAST, 1,
382383
Reliability.BEST_EFFORT,
383384
Durability.VOLATILE,
384385
false));
385386
// create a OfferedQoSIncompatible event handler with custom event consumer
386-
EventConsumer eventConsumer = new EventConsumer();
387+
OfferedQosIncompatibleConsumer eventConsumer = new OfferedQosIncompatibleConsumer();
387388
EventHandler eventHandler = publisher.createEventHandler(
388389
OfferedQosIncompatible.factory, eventConsumer
389390
);
390391
// create an incompatible subscription (reliable vs best effort publisher)
391392
Subscription<std_msgs.msg.String> subscription = node.<std_msgs.msg.String>createSubscription(
392-
std_msgs.msg.String.class, "test_topic_spin_event",
393-
new Consumer<std_msgs.msg.String>() {
394-
public void accept(final std_msgs.msg.String msg) {}
395-
},
396-
new QoSProfile(
397-
History.KEEP_LAST, 1,
398-
Reliability.RELIABLE,
399-
Durability.VOLATILE,
400-
false));
393+
std_msgs.msg.String.class, "test_topic_spin_publisher_event",
394+
new Consumer<std_msgs.msg.String>() {
395+
public void accept(final std_msgs.msg.String msg) {}
396+
},
397+
new QoSProfile(
398+
History.KEEP_LAST, 1,
399+
Reliability.RELIABLE,
400+
Durability.VOLATILE,
401+
false));
402+
// set up executor
403+
ComposableNode composableNode = new ComposableNode() {
404+
public Node getNode() {
405+
return node;
406+
}
407+
};
408+
Executor executor = new SingleThreadedExecutor();
409+
executor.addNode(composableNode);
410+
long start = System.currentTimeMillis();
411+
do {
412+
executor.spinAll((1000 + System.currentTimeMillis() - start) * 1000 * 1000);
413+
} while (!eventConsumer.done && System.currentTimeMillis() < start + 1000);
414+
assert(eventConsumer.done);
415+
}
416+
417+
public static class RequestedQosIncompatibleConsumer
418+
implements Consumer<RequestedQosIncompatible> {
419+
public boolean done = false;
420+
421+
public void accept(final RequestedQosIncompatible status) {
422+
assertEquals(status.totalCount, 1);
423+
assertEquals(status.totalCountChange, 1);
424+
assertEquals(status.lastPolicyKind, RequestedQosIncompatible.PolicyKind.RELIABILITY);
425+
this.done = true;
426+
}
427+
}
428+
429+
@Test
430+
public final void testSpinSubscriptionEvent() {
431+
String identifier = RCLJava.getRMWIdentifier();
432+
if (identifier.equals("rmw_fastrtps_cpp") || identifier.equals("rmw_fastrtps_dynamic_cpp")) {
433+
// RequestedQosIncompatible events is not supported in these implementations.
434+
return;
435+
}
436+
final Node node = RCLJava.createNode("test_node_spin_subscription_event");
437+
// create an incompatible subscription (reliable vs best effort publisher)
438+
Subscription<std_msgs.msg.String> subscription = node.<std_msgs.msg.String>createSubscription(
439+
std_msgs.msg.String.class, "test_topic_spin_subscription_event",
440+
new Consumer<std_msgs.msg.String>() {
441+
public void accept(final std_msgs.msg.String msg) {}
442+
},
443+
new QoSProfile(
444+
History.KEEP_LAST, 1,
445+
Reliability.RELIABLE,
446+
Durability.VOLATILE,
447+
false));
448+
// create a RequestedQoSIncompatible event handler with custom event consumer
449+
RequestedQosIncompatibleConsumer eventConsumer = new RequestedQosIncompatibleConsumer();
450+
EventHandler eventHandler = subscription.createEventHandler(
451+
RequestedQosIncompatible.factory, eventConsumer
452+
);
453+
Publisher<std_msgs.msg.String> publisher = node.<std_msgs.msg.String>createPublisher(
454+
std_msgs.msg.String.class, "test_topic_spin_subscription_event", new QoSProfile(
455+
History.KEEP_LAST, 1,
456+
Reliability.BEST_EFFORT,
457+
Durability.VOLATILE,
458+
false));
459+
401460
// set up executor
402461
ComposableNode composableNode = new ComposableNode() {
403462
public Node getNode() {

rcljava/src/test/java/org/ros2/rcljava/subscription/SubscriptionTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525

2626
import org.ros2.rcljava.RCLJava;
2727
import org.ros2.rcljava.consumers.Consumer;
28+
import org.ros2.rcljava.events.EventHandler;
2829
import org.ros2.rcljava.node.Node;
30+
import org.ros2.rcljava.subscription.statuses.RequestedQosIncompatible;
2931

3032
public class SubscriptionTest {
3133
@BeforeClass
@@ -66,4 +68,33 @@ public void accept(final std_msgs.msg.String msg) {}
6668

6769
RCLJava.shutdown();
6870
}
71+
72+
@Test
73+
public final void testCreateRequestedQosIncompatibleEvent() {
74+
String identifier = RCLJava.getRMWIdentifier();
75+
if (identifier.equals("rmw_fastrtps_cpp") || identifier.equals("rmw_fastrtps_dynamic_cpp")) {
76+
// event not supported in these implementations
77+
return;
78+
}
79+
RCLJava.rclJavaInit();
80+
Node node = RCLJava.createNode("test_node");
81+
Subscription<std_msgs.msg.String> subscription = node.<std_msgs.msg.String>createSubscription(
82+
std_msgs.msg.String.class, "test_topic", new Consumer<std_msgs.msg.String>() {
83+
public void accept(final std_msgs.msg.String msg) {}
84+
});
85+
EventHandler eventHandler = subscription.createEventHandler(
86+
RequestedQosIncompatible.factory, new Consumer<RequestedQosIncompatible>() {
87+
public void accept(final RequestedQosIncompatible status) {
88+
assertEquals(status.totalCount, 0);
89+
assertEquals(status.totalCountChange, 0);
90+
assertEquals(status.lastPolicyKind, RequestedQosIncompatible.PolicyKind.INVALID);
91+
}
92+
}
93+
);
94+
assertNotEquals(0, eventHandler.getHandle());
95+
// force executing the callback, so we check that taking an event works
96+
eventHandler.executeCallback();
97+
RCLJava.shutdown();
98+
assertEquals(0, eventHandler.getHandle());
99+
}
69100
}

0 commit comments

Comments
 (0)