|
17 | 17 |
|
18 | 18 | import static org.junit.Assert.assertEquals; |
19 | 19 | import static org.junit.Assert.assertNotEquals; |
| 20 | +import static org.junit.Assert.assertNotNull; |
20 | 21 | import static org.junit.Assert.assertTrue; |
21 | 22 |
|
22 | 23 | import org.junit.After; |
|
28 | 29 | import java.lang.ref.WeakReference; |
29 | 30 | import java.lang.reflect.Method; |
30 | 31 |
|
| 32 | +import java.util.concurrent.TimeUnit; |
| 33 | +import java.util.ArrayList; |
31 | 34 | import java.util.Arrays; |
| 35 | +import java.util.Collection; |
32 | 36 | import java.util.List; |
33 | 37 |
|
34 | 38 | import org.ros2.rcljava.RCLJava; |
|
37 | 41 | import org.ros2.rcljava.executors.Executor; |
38 | 42 | import org.ros2.rcljava.executors.MultiThreadedExecutor; |
39 | 43 | import org.ros2.rcljava.executors.SingleThreadedExecutor; |
| 44 | +import org.ros2.rcljava.graph.NameAndTypes; |
40 | 45 | import org.ros2.rcljava.node.Node; |
41 | 46 | import org.ros2.rcljava.publisher.Publisher; |
42 | 47 | import org.ros2.rcljava.subscription.Subscription; |
@@ -869,4 +874,75 @@ public Node getNode() { |
869 | 874 | subscriptionTwo.dispose(); |
870 | 875 | assertEquals(0, subscriptionTwo.getHandle()); |
871 | 876 | } |
| 877 | + |
| 878 | + @Test |
| 879 | + public final void testGetTopicNamesAndTypes() throws Exception { |
| 880 | + Publisher<rcljava.msg.UInt32> publisher = node.<rcljava.msg.UInt32>createPublisher( |
| 881 | + rcljava.msg.UInt32.class, "test_get_topic_names_and_types_one"); |
| 882 | + Publisher<rcljava.msg.UInt32> publisher2 = node.<rcljava.msg.UInt32>createPublisher( |
| 883 | + rcljava.msg.UInt32.class, "test_get_topic_names_and_types_two"); |
| 884 | + Subscription<rcljava.msg.Empty> subscription = node.<rcljava.msg.Empty>createSubscription( |
| 885 | + rcljava.msg.Empty.class, "test_get_topic_names_and_types_one", |
| 886 | + new Consumer<rcljava.msg.Empty>() { |
| 887 | + public void accept(final rcljava.msg.Empty msg) {} |
| 888 | + }); |
| 889 | + Subscription<rcljava.msg.Empty> subscription2 = node.<rcljava.msg.Empty>createSubscription( |
| 890 | + rcljava.msg.Empty.class, "test_get_topic_names_and_types_three", |
| 891 | + new Consumer<rcljava.msg.Empty>() { |
| 892 | + public void accept(final rcljava.msg.Empty msg) {} |
| 893 | + }); |
| 894 | + |
| 895 | + Consumer<Collection<NameAndTypes>> validateNameAndTypes = |
| 896 | + new Consumer<Collection<NameAndTypes>>() { |
| 897 | + public void accept(final Collection<NameAndTypes> namesAndTypes) { |
| 898 | + // TODO(ivanpauno): Using assertj may help a lot here https://assertj.github.io/doc/. |
| 899 | + assertEquals(namesAndTypes.size(), 3); |
| 900 | + assertTrue( |
| 901 | + "topic 'test_get_topic_names_and_types_one' was not discovered", |
| 902 | + namesAndTypes.contains( |
| 903 | + new NameAndTypes( |
| 904 | + "/test_get_topic_names_and_types_one", |
| 905 | + new ArrayList(Arrays.asList("rcljava/msg/Empty", "rcljava/msg/UInt32"))))); |
| 906 | + assertTrue( |
| 907 | + "topic 'test_get_topic_names_and_types_two' was not discovered", |
| 908 | + namesAndTypes.contains( |
| 909 | + new NameAndTypes( |
| 910 | + "/test_get_topic_names_and_types_two", |
| 911 | + new ArrayList(Arrays.asList("rcljava/msg/UInt32"))))); |
| 912 | + assertTrue( |
| 913 | + "topic 'test_get_topic_names_and_types_three' was not discovered", |
| 914 | + namesAndTypes.contains( |
| 915 | + new NameAndTypes( |
| 916 | + "/test_get_topic_names_and_types_three", |
| 917 | + new ArrayList(Arrays.asList("rcljava/msg/Empty"))))); |
| 918 | + } |
| 919 | + }; |
| 920 | + |
| 921 | + long start = System.currentTimeMillis(); |
| 922 | + boolean ok = false; |
| 923 | + Collection<NameAndTypes> namesAndTypes = null; |
| 924 | + do { |
| 925 | + namesAndTypes = this.node.getTopicNamesAndTypes(); |
| 926 | + try { |
| 927 | + validateNameAndTypes.accept(namesAndTypes); |
| 928 | + ok = true; |
| 929 | + } catch (AssertionError err) { |
| 930 | + // ignore here, it's going to be validated again at the end. |
| 931 | + } |
| 932 | + // TODO(ivanpauno): We could wait for the graph guard condition to be triggered if that |
| 933 | + // would be available. |
| 934 | + try { |
| 935 | + TimeUnit.MILLISECONDS.sleep(100); |
| 936 | + } catch (InterruptedException err) { |
| 937 | + // ignore |
| 938 | + } |
| 939 | + } while (!ok && System.currentTimeMillis() < start + 1000); |
| 940 | + assertNotNull(namesAndTypes); |
| 941 | + validateNameAndTypes.accept(namesAndTypes); |
| 942 | + |
| 943 | + publisher.dispose(); |
| 944 | + publisher2.dispose(); |
| 945 | + subscription.dispose(); |
| 946 | + subscription2.dispose(); |
| 947 | + } |
872 | 948 | } |
0 commit comments