Skip to content

Commit 1f9b34d

Browse files
Add tests for --content-filter option on echo, hz, and bw
- test_echo_content_filter: matching and non-matching DDS content filter expressions on ros2 topic echo - test_hz_content_filter: happy path for ros2 topic hz with content filter - test_bw_content_filter: happy path for ros2 topic bw with content filter
1 parent 237c9d9 commit 1f9b34d

2 files changed

Lines changed: 150 additions & 0 deletions

File tree

ros2topic/test/test_bw_delay_hz.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
from rclpy.qos import ReliabilityPolicy
4444
from rclpy.utilities import get_rmw_implementation_identifier
4545

46+
from std_msgs.msg import String
47+
4648

4749
# Skip cli tests on Windows while they exhibit pathological behavior
4850
# https://github.com/ros2/build_farmer/issues/248
@@ -504,3 +506,101 @@ def test_bw_both_all_and_topics_error(self, launch_service, proc_info, proc_outp
504506
assert 'Cannot specify both --all/-a and topic names' in command.output, (
505507
'bw command did not print expected error message'
506508
)
509+
510+
@launch_testing.markers.retry_on_failure(times=5)
511+
def test_hz_content_filter(self, launch_service, proc_info, proc_output):
512+
topic = '/clitest/topic/hz_content_filter'
513+
publisher = self.node.create_publisher(String, topic, 10)
514+
assert publisher
515+
516+
def publish_message():
517+
publisher.publish(String(data='hello'))
518+
519+
publish_timer = self.node.create_timer(0.5, publish_message)
520+
521+
# Wait for the publisher to be discovered
522+
publisher_count = 0
523+
timeout_count = 0
524+
while publisher_count == 0 and timeout_count < 10:
525+
self.executor.spin_once(timeout_sec=0.1)
526+
publisher_count = self.node.count_publishers(topic)
527+
timeout_count += 1
528+
assert publisher_count > 0, 'Publisher was not discovered'
529+
530+
try:
531+
command_action = ExecuteProcess(
532+
cmd=['ros2', 'topic', 'hz',
533+
'--content-filter', "data = 'hello'",
534+
topic],
535+
additional_env={
536+
'PYTHONUNBUFFERED': '1'
537+
},
538+
output='screen'
539+
)
540+
with launch_testing.tools.launch_process(
541+
launch_service, command_action, proc_info, proc_output,
542+
output_filter=launch_testing_ros.tools.basic_output_filter(
543+
filtered_rmw_implementation=get_rmw_implementation_identifier()
544+
)
545+
) as command:
546+
# The future won't complete - we will hit the timeout
547+
self.executor.spin_until_future_complete(
548+
rclpy.task.Future(), timeout_sec=5
549+
)
550+
command.wait_for_shutdown(timeout=10)
551+
assert command.output, 'hz with content filter printed no output'
552+
assert re.search(
553+
r'^average rate: [0-9\.]+$', command.output, flags=re.MULTILINE
554+
), 'hz with content filter did not print expected rate'
555+
finally:
556+
self.node.destroy_timer(publish_timer)
557+
self.node.destroy_publisher(publisher)
558+
559+
@launch_testing.markers.retry_on_failure(times=5)
560+
def test_bw_content_filter(self, launch_service, proc_info, proc_output):
561+
topic = '/clitest/topic/bw_content_filter'
562+
publisher = self.node.create_publisher(String, topic, 10)
563+
assert publisher
564+
565+
def publish_message():
566+
publisher.publish(String(data='hello'))
567+
568+
publish_timer = self.node.create_timer(0.5, publish_message)
569+
570+
# Wait for the publisher to be discovered
571+
publisher_count = 0
572+
timeout_count = 0
573+
while publisher_count == 0 and timeout_count < 10:
574+
self.executor.spin_once(timeout_sec=0.1)
575+
publisher_count = self.node.count_publishers(topic)
576+
timeout_count += 1
577+
assert publisher_count > 0, 'Publisher was not discovered'
578+
579+
try:
580+
command_action = ExecuteProcess(
581+
cmd=['ros2', 'topic', 'bw',
582+
'--content-filter', "data = 'hello'",
583+
topic],
584+
additional_env={
585+
'PYTHONUNBUFFERED': '1'
586+
},
587+
output='screen'
588+
)
589+
with launch_testing.tools.launch_process(
590+
launch_service, command_action, proc_info, proc_output,
591+
output_filter=launch_testing_ros.tools.basic_output_filter(
592+
filtered_rmw_implementation=get_rmw_implementation_identifier()
593+
)
594+
) as command:
595+
# The future won't complete - we will hit the timeout
596+
self.executor.spin_until_future_complete(
597+
rclpy.task.Future(), timeout_sec=5
598+
)
599+
command.wait_for_shutdown(timeout=10)
600+
assert command.output, 'bw with content filter printed no output'
601+
assert re.search(
602+
r'^[0-9]+ B/s from [0-9]+ messages$', command.output, flags=re.MULTILINE
603+
), 'bw with content filter did not print expected bandwidth'
604+
finally:
605+
self.node.destroy_timer(publish_timer)
606+
self.node.destroy_publisher(publisher)

ros2topic/test/test_echo_pub.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,56 @@ def publish_message():
502502
self.node.destroy_timer(publish_timer)
503503
self.node.destroy_publisher(publisher)
504504

505+
@launch_testing.markers.retry_on_failure(times=5)
506+
def test_echo_content_filter(self, launch_service, proc_info, proc_output):
507+
params = [
508+
('/clitest/topic/echo_cfilter_match', "data = 'hello'", True),
509+
('/clitest/topic/echo_cfilter_nomatch', "data = 'NOMATCH'", False),
510+
]
511+
for topic, filter_expr, has_output in params:
512+
with self.subTest(topic=topic, filter_expr=filter_expr):
513+
publisher = self.node.create_publisher(String, topic, 10)
514+
assert publisher
515+
516+
def publish_message():
517+
publisher.publish(String(data='hello'))
518+
519+
publish_timer = self.node.create_timer(0.5, publish_message)
520+
521+
try:
522+
command_action = ExecuteProcess(
523+
cmd=(['ros2', 'topic', 'echo'] +
524+
['--content-filter', filter_expr] +
525+
[topic, 'std_msgs/String']),
526+
additional_env={
527+
'PYTHONUNBUFFERED': '1'
528+
},
529+
output='screen'
530+
)
531+
with launch_testing.tools.launch_process(
532+
launch_service, command_action, proc_info, proc_output,
533+
output_filter=launch_testing_ros.tools.basic_output_filter(
534+
filtered_rmw_implementation=get_rmw_implementation_identifier()
535+
)
536+
) as command:
537+
# The future won't complete - we will hit the timeout
538+
self.executor.spin_until_future_complete(
539+
rclpy.task.Future(), timeout_sec=5
540+
)
541+
command.wait_for_shutdown(timeout=10)
542+
# Check results
543+
if has_output:
544+
assert 'hello' in command.output, (
545+
'Echo with content filter did not print expected message')
546+
else:
547+
assert 'hello' not in command.output, (
548+
'Content filter should have filtered all messages')
549+
550+
finally:
551+
# Cleanup
552+
self.node.destroy_timer(publish_timer)
553+
self.node.destroy_publisher(publisher)
554+
505555
@launch_testing.markers.retry_on_failure(times=5)
506556
def test_echo_raw(self, launch_service, proc_info, proc_output):
507557
topic = '/clitest/topic/echo_raw'

0 commit comments

Comments
 (0)