|
42 | 42 | import org.ros2.rcljava.client.Client; |
43 | 43 | import org.ros2.rcljava.concurrent.RCLFuture; |
44 | 44 | import org.ros2.rcljava.consumers.Consumer; |
| 45 | +import org.ros2.rcljava.consumers.BiConsumer; |
45 | 46 | import org.ros2.rcljava.consumers.TriConsumer; |
46 | 47 | import org.ros2.rcljava.executors.Executor; |
47 | 48 | import org.ros2.rcljava.executors.MultiThreadedExecutor; |
@@ -1186,4 +1187,89 @@ public void accept(final Collection<EndpointInfo> info) { |
1186 | 1187 | subscription.dispose(); |
1187 | 1188 | subscription2.dispose(); |
1188 | 1189 | } |
| 1190 | + |
| 1191 | + @Test |
| 1192 | + public final void testGetPublisherNamesAndTypesByNode() throws Exception { |
| 1193 | + final Node remoteNode = RCLJava.createNode("test_get_publisher_names_and_types_remote_node"); |
| 1194 | + Publisher<rcljava.msg.UInt32> publisher1 = node.<rcljava.msg.UInt32>createPublisher( |
| 1195 | + rcljava.msg.UInt32.class, "test_get_publisher_names_and_types_one"); |
| 1196 | + Publisher<rcljava.msg.UInt32> publisher2 = node.<rcljava.msg.UInt32>createPublisher( |
| 1197 | + rcljava.msg.UInt32.class, "test_get_publisher_names_and_types_two"); |
| 1198 | + Publisher<rcljava.msg.UInt32> publisher3 = remoteNode.<rcljava.msg.UInt32>createPublisher( |
| 1199 | + rcljava.msg.UInt32.class, "test_get_publisher_names_and_types_two"); |
| 1200 | + Publisher<rcljava.msg.UInt32> publisher4 = remoteNode.<rcljava.msg.UInt32>createPublisher( |
| 1201 | + rcljava.msg.UInt32.class, "test_get_publisher_names_and_types_three"); |
| 1202 | + Subscription<rcljava.msg.Empty> subscription = node.<rcljava.msg.Empty>createSubscription( |
| 1203 | + rcljava.msg.Empty.class, "test_get_topic_names_and_types_this_should_not_appear", |
| 1204 | + new Consumer<rcljava.msg.Empty>() { |
| 1205 | + public void accept(final rcljava.msg.Empty msg) {} |
| 1206 | + }); |
| 1207 | + |
| 1208 | + BiConsumer<Collection<NameAndTypes>, Collection<NameAndTypes>> validateNameAndTypes = |
| 1209 | + new BiConsumer<Collection<NameAndTypes>, Collection<NameAndTypes>>() { |
| 1210 | + public void accept(final Collection<NameAndTypes> local, Collection<NameAndTypes> remote) { |
| 1211 | + // TODO(ivanpauno): Using assertj may help a lot here https://assertj.github.io/doc/. |
| 1212 | + assertEquals(local.size(), 2); |
| 1213 | + assertTrue( |
| 1214 | + "topic 'test_get_publisher_names_and_types_one' was not discovered for local node", |
| 1215 | + local.contains( |
| 1216 | + new NameAndTypes( |
| 1217 | + "/test_get_publisher_names_and_types_one", |
| 1218 | + new ArrayList(Arrays.asList("rcljava/msg/UInt32"))))); |
| 1219 | + assertTrue( |
| 1220 | + "topic 'test_get_publisher_names_and_types_two' was not discovered for local node", |
| 1221 | + local.contains( |
| 1222 | + new NameAndTypes( |
| 1223 | + "/test_get_publisher_names_and_types_two", |
| 1224 | + new ArrayList(Arrays.asList("rcljava/msg/UInt32"))))); |
| 1225 | + |
| 1226 | + assertEquals(remote.size(), 2); |
| 1227 | + assertTrue( |
| 1228 | + "topic 'test_get_publisher_names_and_types_two' was not discovered for remote node", |
| 1229 | + remote.contains( |
| 1230 | + new NameAndTypes( |
| 1231 | + "/test_get_publisher_names_and_types_two", |
| 1232 | + new ArrayList(Arrays.asList("rcljava/msg/UInt32"))))); |
| 1233 | + assertTrue( |
| 1234 | + "topic 'test_get_publisher_names_and_types_three' was not discovered for remote node", |
| 1235 | + remote.contains( |
| 1236 | + new NameAndTypes( |
| 1237 | + "/test_get_publisher_names_and_types_three", |
| 1238 | + new ArrayList(Arrays.asList("rcljava/msg/UInt32"))))); |
| 1239 | + } |
| 1240 | + }; |
| 1241 | + |
| 1242 | + long start = System.currentTimeMillis(); |
| 1243 | + boolean ok = false; |
| 1244 | + Collection<NameAndTypes> local = null; |
| 1245 | + Collection<NameAndTypes> remote = null; |
| 1246 | + do { |
| 1247 | + local = this.node.getPublisherNamesAndTypesByNode("test_node", "/"); |
| 1248 | + remote = this.node.getPublisherNamesAndTypesByNode( |
| 1249 | + "test_get_publisher_names_and_types_remote_node", "/"); |
| 1250 | + try { |
| 1251 | + validateNameAndTypes.accept(local, remote); |
| 1252 | + ok = true; |
| 1253 | + } catch (AssertionError err) { |
| 1254 | + // ignore here, it's going to be validated again at the end. |
| 1255 | + } |
| 1256 | + // TODO(ivanpauno): We could wait for the graph guard condition to be triggered if that |
| 1257 | + // would be available. |
| 1258 | + try { |
| 1259 | + TimeUnit.MILLISECONDS.sleep(100); |
| 1260 | + } catch (InterruptedException err) { |
| 1261 | + // ignore |
| 1262 | + } |
| 1263 | + } while (!ok && System.currentTimeMillis() < start + 1000); |
| 1264 | + assertNotNull(local); |
| 1265 | + assertNotNull(remote); |
| 1266 | + validateNameAndTypes.accept(local, remote); |
| 1267 | + |
| 1268 | + publisher1.dispose(); |
| 1269 | + publisher2.dispose(); |
| 1270 | + publisher3.dispose(); |
| 1271 | + publisher4.dispose(); |
| 1272 | + subscription.dispose(); |
| 1273 | + remoteNode.dispose(); |
| 1274 | + } |
1189 | 1275 | } |
0 commit comments