ros2_tracing/tracetools_test/test/test_service.py

248 lines
8.8 KiB
Python
Raw Permalink Normal View History

2019-06-18 09:10:05 +02:00
# Copyright 2019 Robert Bosch GmbH
#
# 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.
2019-06-03 11:57:19 +02:00
import unittest
2019-06-05 15:35:46 +02:00
2019-06-18 15:42:44 +02:00
from tracetools_test.case import TraceTestCase
2019-06-03 11:57:19 +02:00
2019-06-18 15:42:44 +02:00
class TestService(TraceTestCase):
2019-06-24 14:22:59 +02:00
2019-06-18 15:42:44 +02:00
def __init__(self, *args) -> None:
super().__init__(
*args,
session_name_prefix='session-test-service',
2019-06-18 15:42:44 +02:00
events_ros=[
2019-06-19 15:26:46 +02:00
'ros2:rcl_node_init',
2019-06-18 15:42:44 +02:00
'ros2:rcl_service_init',
'ros2:rclcpp_service_callback_added',
'ros2:rclcpp_callback_register',
'ros2:callback_start',
'ros2:callback_end',
2019-06-18 15:42:44 +02:00
],
nodes=['test_service_ping', 'test_service_pong'],
2019-06-18 15:42:44 +02:00
)
2019-06-03 11:57:19 +02:00
2019-06-19 15:26:46 +02:00
def test_all(self):
2019-11-14 16:15:55 -08:00
# Check events as set
self.assertEventsSet(self._events_ros)
2019-06-19 15:26:46 +02:00
# Check fields
srv_init_events = self.get_events_with_name('ros2:rcl_service_init')
2019-11-16 15:20:55 -08:00
callback_added_events = self.get_events_with_name('ros2:rclcpp_service_callback_added')
callback_register_events = self.get_events_with_name('ros2:rclcpp_callback_register')
start_events = self.get_events_with_name('ros2:callback_start')
end_events = self.get_events_with_name('ros2:callback_end')
2019-11-16 15:20:55 -08:00
2019-06-19 15:26:46 +02:00
for event in srv_init_events:
self.assertValidHandle(event, ['service_handle', 'node_handle', 'rmw_service_handle'])
self.assertStringFieldNotEmpty(event, 'service_name')
for event in callback_added_events:
self.assertValidHandle(event, ['service_handle', 'callback'])
for event in callback_register_events:
self.assertValidPointer(event, 'callback')
self.assertStringFieldNotEmpty(event, 'symbol')
for event in start_events:
self.assertValidHandle(event, 'callback')
# Should not be 1 for services (yet)
self.assertFieldEquals(
event,
'is_intra_process',
0,
'invalid value for is_intra_process',
)
for event in end_events:
self.assertValidHandle(event, 'callback')
2019-06-19 15:26:46 +02:00
# Check that the test services names exists
ping_node_srv_init_events = self.get_events_with_procname(
'test_service_ping',
srv_init_events,
)
ping_node_test_srv_init_events = self.get_events_with_field_value(
'service_name',
'/pong',
ping_node_srv_init_events,
)
self.assertEqual(
len(ping_node_test_srv_init_events),
1,
'none or more than 1 /pong service under the test_service_pong node',
)
pong_node_srv_init_events = self.get_events_with_procname(
'test_service_pong',
srv_init_events,
)
pong_node_test_srv_init_events = self.get_events_with_field_value(
2019-06-19 15:26:46 +02:00
'service_name',
'/ping',
pong_node_srv_init_events,
2019-11-16 15:20:55 -08:00
)
2019-06-19 15:26:46 +02:00
self.assertGreaterEqual(
len(pong_node_test_srv_init_events),
2019-06-19 15:26:46 +02:00
1,
2019-11-16 15:20:55 -08:00
'cannot find test service name',
)
2019-06-19 15:26:46 +02:00
# Check that the service init events have a matching node handle (with node_init events)
2019-06-19 15:26:46 +02:00
node_init_events = self.get_events_with_name('ros2:rcl_node_init')
ping_node_test_srv_init_event = ping_node_test_srv_init_events[0]
self.assertMatchingField(
ping_node_test_srv_init_event,
'node_handle',
None,
2019-11-16 15:20:55 -08:00
node_init_events,
False,
2019-11-16 15:20:55 -08:00
)
pong_node_test_srv_init_event = pong_node_test_srv_init_events[0]
2019-06-19 15:26:46 +02:00
self.assertMatchingField(
pong_node_test_srv_init_event,
2019-06-19 15:26:46 +02:00
'node_handle',
None,
node_init_events,
False,
2019-11-16 15:20:55 -08:00
)
2019-06-19 15:26:46 +02:00
# Check that there are matching rclcpp_service_callback_added events
ping_node_test_service_handle = self.get_field(
ping_node_test_srv_init_event,
2019-06-19 15:26:46 +02:00
'service_handle',
)
pong_node_test_service_handle = self.get_field(
pong_node_test_srv_init_event,
'service_handle',
)
ping_node_srv_callback_added_events = self.get_events_with_procname(
'test_service_ping',
2019-11-16 15:20:55 -08:00
callback_added_events,
)
ping_node_test_srv_callback_added_events = self.get_events_with_field_value(
'service_handle',
ping_node_test_service_handle,
ping_node_srv_callback_added_events,
)
self.assertEqual(
len(ping_node_test_srv_callback_added_events),
1,
'none or more than 1 matching callback_added events for the test_service_ping node',
)
pong_node_srv_callback_added_events = self.get_events_with_procname(
'test_service_pong',
callback_added_events,
)
pong_node_test_srv_callback_added_events = self.get_events_with_field_value(
'service_handle',
pong_node_test_service_handle,
pong_node_srv_callback_added_events,
)
self.assertEqual(
len(pong_node_test_srv_callback_added_events),
1,
'none or more than 1 matching callback_added events for the test_service_pong node',
)
# Check that there are matching rclcpp_callback_register events
ping_node_test_callback_ref = self.get_field(
ping_node_test_srv_callback_added_events[0],
'callback',
)
pong_node_test_callback_ref = self.get_field(
pong_node_test_srv_callback_added_events[0],
'callback',
)
ping_node_callback_register_events = self.get_events_with_procname(
'test_service_ping',
callback_register_events,
)
ping_node_test_callback_register_events = self.get_events_with_field_value(
'callback',
ping_node_test_callback_ref,
ping_node_callback_register_events,
)
self.assertEqual(
len(ping_node_test_callback_register_events),
1,
'none or more than 1 matching callback_register events for the test_service_ping node',
)
pong_node_callback_register_events = self.get_events_with_procname(
'test_service_pong',
callback_register_events,
)
pong_node_test_callback_register_events = self.get_events_with_field_value(
'callback',
pong_node_test_callback_ref,
pong_node_callback_register_events,
)
self.assertEqual(
len(pong_node_test_callback_register_events),
1,
'none or more than 1 matching callback_register events for the test_service_pong node',
)
# Check that there are corresponding callback_start/stop pairs
ping_node_test_callback_start_events = self.get_events_with_field_value(
'callback',
ping_node_test_callback_ref,
start_events,
)
self.assertEqual(
len(ping_node_test_callback_start_events),
1,
'none or more than 1 matching callback_start events for the test_service_ping node',
)
pong_node_test_callback_start_events = self.get_events_with_field_value(
'callback',
pong_node_test_callback_ref,
start_events,
)
self.assertEqual(
len(pong_node_test_callback_start_events),
1,
'none or more than 1 matching callback_start events for the test_service_pong node',
)
ping_node_test_callback_end_events = self.get_events_with_field_value(
'callback',
ping_node_test_callback_ref,
end_events,
)
self.assertEqual(
len(ping_node_test_callback_end_events),
1,
'none or more than 1 matching callback_end events for the test_service_ping node',
)
pong_node_test_callback_end_events = self.get_events_with_field_value(
'callback',
pong_node_test_callback_ref,
end_events,
)
self.assertEqual(
len(pong_node_test_callback_end_events),
1,
'none or more than 1 matching callback_end events for the test_service_pong node',
)
2019-06-03 11:57:19 +02:00
if __name__ == '__main__':
unittest.main()