4242import org .ros2 .rcljava .qos .policies .Reliability ;
4343import org .ros2 .rcljava .qos .QoSProfile ;
4444import org .ros2 .rcljava .subscription .Subscription ;
45+ import org .ros2 .rcljava .subscription .statuses .RequestedQosIncompatible ;
4546
4647public 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 () {
0 commit comments