-
Notifications
You must be signed in to change notification settings - Fork 170
/
Copy pathtest_echo_pub.py
398 lines (358 loc) · 17.5 KB
/
test_echo_pub.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# Copyright 2019 Amazon.com, Inc. or its affiliates. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import functools
import sys
import unittest
from launch import LaunchDescription
from launch.actions import ExecuteProcess
import launch_testing
import launch_testing.actions
import launch_testing.asserts
import launch_testing.markers
import launch_testing.tools
import launch_testing_ros.tools
import pytest
import rclpy
from rclpy.executors import SingleThreadedExecutor
from rclpy.qos import DurabilityPolicy
from rclpy.qos import QoSProfile
from rclpy.qos import ReliabilityPolicy
from rclpy.utilities import get_rmw_implementation_identifier
from std_msgs.msg import String
# Skip cli tests on Windows while they exhibit pathological behavior
# https://github.com/ros2/build_farmer/issues/248
if sys.platform.startswith('win'):
pytest.skip(
'CLI tests can block for a pathological amount of time on Windows.',
allow_module_level=True)
TEST_NODE = 'cli_echo_pub_test_node'
TEST_NAMESPACE = 'cli_echo_pub'
@pytest.mark.rostest
@launch_testing.markers.keep_alive
def generate_test_description():
return LaunchDescription([
# Always restart daemon to isolate tests.
ExecuteProcess(
cmd=['ros2', 'daemon', 'stop'],
name='daemon-stop',
on_exit=[
ExecuteProcess(
cmd=['ros2', 'daemon', 'start'],
name='daemon-start',
on_exit=[
launch_testing.actions.ReadyToTest()
],
)
]
)
])
class TestROS2TopicEchoPub(unittest.TestCase):
# TODO(hidmic): investigate why making use of the same rclpy node, executor
# and context for all tests on a per rmw implementation basis
# makes them fail on Linux-aarch64 when using 'rmw_opensplice_cpp'.
# Presumably, interfaces creation/destruction and/or executor spinning
# on one test is affecting the other.
def setUp(self):
self.context = rclpy.context.Context()
rclpy.init(context=self.context)
self.node = rclpy.create_node(
TEST_NODE, namespace=TEST_NAMESPACE, context=self.context
)
self.executor = SingleThreadedExecutor(context=self.context)
self.executor.add_node(self.node)
def tearDown(self):
self.node.destroy_node()
rclpy.shutdown(context=self.context)
@launch_testing.markers.retry_on_failure(times=5)
def test_pub_basic(self, launch_service, proc_info, proc_output):
params = [
('/clitest/topic/pub_basic', False, True),
('/clitest/topic/pub_compatible_qos', True, True),
('/clitest/topic/pub_incompatible_qos', True, False)
]
for topic, provide_qos, compatible_qos in params:
with self.subTest(topic=topic, provide_qos=provide_qos, compatible_qos=compatible_qos):
# Check for inconsistent arguments
assert provide_qos if not compatible_qos else True
received_message_count = 0
expected_minimum_message_count = 1
expected_maximum_message_count = 5
pub_extra_options = []
subscription_qos_profile = 10
if provide_qos:
if compatible_qos:
# For compatible test, put publisher at very high quality
# and subscription at low
pub_extra_options = [
'--qos-reliability', 'reliable',
'--qos-durability', 'transient_local']
subscription_qos_profile = QoSProfile(
depth=10,
reliability=ReliabilityPolicy.BEST_EFFORT,
durability=DurabilityPolicy.VOLATILE)
else:
# For an incompatible example, reverse the quality extremes
# and expect no messages to arrive
pub_extra_options = [
'--qos-reliability', 'best_effort',
'--qos-durability', 'volatile']
subscription_qos_profile = QoSProfile(
depth=10,
reliability=ReliabilityPolicy.RELIABLE,
durability=DurabilityPolicy.TRANSIENT_LOCAL)
expected_maximum_message_count = 0
expected_minimum_message_count = 0
future = rclpy.task.Future()
def message_callback(msg):
"""If we receive one message, the test has succeeded."""
nonlocal received_message_count
received_message_count += 1
future.set_result(True)
subscription = self.node.create_subscription(
String, topic, message_callback, subscription_qos_profile)
assert subscription
try:
command_action = ExecuteProcess(
cmd=(['ros2', 'topic', 'pub'] + pub_extra_options +
[topic, 'std_msgs/String', 'data: hello']),
additional_env={
'PYTHONUNBUFFERED': '1'
},
output='screen'
)
with launch_testing.tools.launch_process(
launch_service, command_action, proc_info, proc_output,
output_filter=launch_testing_ros.tools.basic_output_filter(
filtered_rmw_implementation=get_rmw_implementation_identifier()
)
) as command:
self.executor.spin_until_future_complete(future, timeout_sec=10)
command.wait_for_shutdown(timeout=10)
# Check results
assert (
received_message_count >= expected_minimum_message_count and
received_message_count <= expected_maximum_message_count), \
('Received {} messages from pub on {},'
'which is not in expected range {}-{}').format(
received_message_count, topic,
expected_minimum_message_count,
expected_maximum_message_count
)
finally:
# Cleanup
self.node.destroy_subscription(subscription)
@launch_testing.markers.retry_on_failure(times=5)
def test_pub_times(self, launch_service, proc_info, proc_output):
command_action = ExecuteProcess(
cmd=(['ros2', 'topic', 'pub', '-t', '5', '-w', '0', '/clitest/topic/pub_times',
'std_msgs/String', 'data: hello']),
additional_env={
'PYTHONUNBUFFERED': '1'
},
output='screen'
)
with launch_testing.tools.launch_process(
launch_service, command_action, proc_info, proc_output,
output_filter=launch_testing_ros.tools.basic_output_filter(
filtered_rmw_implementation=get_rmw_implementation_identifier()
)
) as command:
assert command.wait_for_shutdown(timeout=10)
assert command.exit_code == launch_testing.asserts.EXIT_OK
assert launch_testing.tools.expect_output(
expected_lines=[
'publisher: beginning loop',
"publishing #1: std_msgs.msg.String(data='hello')",
'',
"publishing #2: std_msgs.msg.String(data='hello')",
'',
"publishing #3: std_msgs.msg.String(data='hello')",
'',
"publishing #4: std_msgs.msg.String(data='hello')",
'',
"publishing #5: std_msgs.msg.String(data='hello')",
'',
],
text=command.output,
strict=True
)
@launch_testing.markers.retry_on_failure(times=5)
def test_echo_basic(self, launch_service, proc_info, proc_output):
params = [
('/clitest/topic/echo_basic', False, True, False),
('/clitest/topic/echo_compatible_qos', True, True, False),
('/clitest/topic/echo_incompatible_qos', True, False, False),
('/clitest/topic/echo_message_lost', False, True, True),
]
for topic, provide_qos, compatible_qos, message_lost in params:
with self.subTest(topic=topic, provide_qos=provide_qos, compatible_qos=compatible_qos):
# Check for inconsistent arguments
assert provide_qos if not compatible_qos else True
echo_extra_options = []
publisher_qos_profile = 10
if provide_qos:
if compatible_qos:
# For compatible test, put publisher at very high quality
# and subscription at low
echo_extra_options = [
'--qos-reliability', 'best_effort',
'--qos-durability', 'volatile']
publisher_qos_profile = QoSProfile(
depth=10,
reliability=ReliabilityPolicy.RELIABLE,
durability=DurabilityPolicy.TRANSIENT_LOCAL)
else:
# For an incompatible example, reverse the quality extremes
# and expect no messages to arrive
echo_extra_options = [
'--qos-reliability', 'reliable',
'--qos-durability', 'transient_local']
publisher_qos_profile = QoSProfile(
depth=10,
reliability=ReliabilityPolicy.BEST_EFFORT,
durability=DurabilityPolicy.VOLATILE)
if not message_lost:
echo_extra_options.append('--no-lost-messages')
publisher = self.node.create_publisher(String, topic, publisher_qos_profile)
assert publisher
def publish_message():
publisher.publish(String(data='hello'))
publish_timer = self.node.create_timer(0.5, publish_message)
try:
command_action = ExecuteProcess(
cmd=(['ros2', 'topic', 'echo'] +
echo_extra_options +
[topic, 'std_msgs/String']),
additional_env={
'PYTHONUNBUFFERED': '1'
},
output='screen'
)
with launch_testing.tools.launch_process(
launch_service, command_action, proc_info, proc_output,
output_filter=launch_testing_ros.tools.basic_output_filter(
filtered_rmw_implementation=get_rmw_implementation_identifier()
)
) as command:
# The future won't complete - we will hit the timeout
self.executor.spin_until_future_complete(
rclpy.task.Future(), timeout_sec=5
)
command.wait_for_shutdown(timeout=10)
# Check results
if compatible_qos:
assert command.output, 'Echo CLI printed no output'
assert 'data: hello' in command.output.splitlines(), (
'Echo CLI did not print expected message'
)
else:
# TODO(mm318): remove special case for FastRTPS when
# https://github.com/ros2/rmw_fastrtps/issues/356 is resolved
if 'rmw_fastrtps' in get_rmw_implementation_identifier():
assert not command.output, (
'Echo CLI should not have received anything with incompatible QoS'
)
else:
assert command.output, (
'Echo CLI did not print incompatible QoS warning'
)
assert ("New publisher discovered on topic '{}', offering incompatible"
' QoS.'.format(topic) in command.output), (
'Echo CLI did not print expected incompatible QoS warning'
)
finally:
# Cleanup
self.node.destroy_timer(publish_timer)
self.node.destroy_publisher(publisher)
@launch_testing.markers.retry_on_failure(times=1)
def test_echo_filter(self, launch_service, proc_info, proc_output):
params = [
('/clitest/topic/echo_filter_all_pass', "m.data=='hello'", True),
('/clitest/topic/echo_filter_all_filtered', "m.data=='success'", False),
]
for topic, filter_expr, has_output in params:
with self.subTest(topic=topic, filter_expr=filter_expr, print_count=10):
# Check for inconsistent arguments
publisher = self.node.create_publisher(String, topic, 10)
assert publisher
def publish_message():
publisher.publish(String(data='hello'))
publish_timer = self.node.create_timer(0.5, publish_message)
try:
command_action = ExecuteProcess(
cmd=(['ros2', 'topic', 'echo'] +
['--filter', filter_expr] +
[topic, 'std_msgs/String']),
additional_env={
'PYTHONUNBUFFERED': '1'
},
output='screen'
)
with launch_testing.tools.launch_process(
launch_service, command_action, proc_info, proc_output,
output_filter=launch_testing_ros.tools.basic_output_filter(
filtered_rmw_implementation=get_rmw_implementation_identifier()
)
) as command:
# The future won't complete - we will hit the timeout
self.executor.spin_until_future_complete(
rclpy.task.Future(), timeout_sec=5
)
command.wait_for_shutdown(timeout=10)
# Check results
if has_output:
assert 'hello' in command.output, 'Echo CLI did not output'
else:
assert 'hello' not in command.output, 'All messages should be filtered out'
finally:
# Cleanup
self.node.destroy_timer(publish_timer)
self.node.destroy_publisher(publisher)
@launch_testing.markers.retry_on_failure(times=5)
def test_echo_raw(self, launch_service, proc_info, proc_output):
topic = '/clitest/topic/echo_raw'
publisher = self.node.create_publisher(String, topic, 10)
assert publisher
def publish_message():
publisher.publish(String(data='hello'))
publish_timer = self.node.create_timer(0.5, publish_message)
try:
command_action = ExecuteProcess(
cmd=['ros2', 'topic', 'echo', '--raw', topic, 'std_msgs/msg/String'],
additional_env={
'PYTHONUNBUFFERED': '1'
},
output='screen'
)
with launch_testing.tools.launch_process(
launch_service, command_action, proc_info, proc_output,
output_filter=launch_testing_ros.tools.basic_output_filter(
filtered_rmw_implementation=get_rmw_implementation_identifier()
)
) as command:
# The future won't complete - we will hit the timeout
self.executor.spin_until_future_complete(
rclpy.task.Future(), timeout_sec=5
)
assert command.wait_for_output(functools.partial(
launch_testing.tools.expect_output, expected_lines=[
"b'\\x00\\x01\\x00\\x00\\x06\\x00\\x00\\x00hello\\x00\\x00\\x00'",
'---',
], strict=True
), timeout=10), 'Echo CLI did not print expected message'
assert command.wait_for_shutdown(timeout=10)
finally:
# Cleanup
self.node.destroy_timer(publish_timer)
self.node.destroy_publisher(publisher)