Skip to content

Commit

Permalink
fix unit test failures
Browse files Browse the repository at this point in the history
Signed-off-by: Miaofei <miaofei@amazon.com>
  • Loading branch information
mm318 committed Apr 8, 2020
1 parent 32a3339 commit 7e2be31
Showing 1 changed file with 31 additions and 20 deletions.
51 changes: 31 additions & 20 deletions rclpy/test/test_qos_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,30 +59,33 @@ def test_publisher_constructor(self):
liveliness_callback = Mock()
deadline_callback = Mock()
incompatible_qos_callback = Mock()
expected_num_event_handlers = 0 if self.is_fastrtps else 1

# No arg
publisher = self.node.create_publisher(EmptyMsg, self.topic_name, 10)
self.assertEqual(len(publisher.event_handlers), 0)
self.assertEqual(len(publisher.event_handlers), expected_num_event_handlers)
self.node.destroy_publisher(publisher)

# Arg with no callbacks
publisher = self.node.create_publisher(
EmptyMsg, self.topic_name, 10, event_callbacks=callbacks)
self.assertEqual(len(publisher.event_handlers), 0)
self.assertEqual(len(publisher.event_handlers), expected_num_event_handlers)
self.node.destroy_publisher(publisher)

# Arg with one of the callbacks
callbacks.deadline = deadline_callback
expected_num_event_handlers += 1
publisher = self.node.create_publisher(
EmptyMsg, self.topic_name, 10, event_callbacks=callbacks)
self.assertEqual(len(publisher.event_handlers), 1)
self.assertEqual(len(publisher.event_handlers), expected_num_event_handlers)
self.node.destroy_publisher(publisher)

# Arg with two callbacks
callbacks.liveliness = liveliness_callback
expected_num_event_handlers += 1
publisher = self.node.create_publisher(
EmptyMsg, self.topic_name, 10, event_callbacks=callbacks)
self.assertEqual(len(publisher.event_handlers), 2)
self.assertEqual(len(publisher.event_handlers), expected_num_event_handlers)
self.node.destroy_publisher(publisher)

# Arg with three callbacks
Expand All @@ -101,31 +104,34 @@ def test_subscription_constructor(self):
deadline_callback = Mock()
message_callback = Mock()
incompatible_qos_callback = Mock()
expected_num_event_handlers = 0 if self.is_fastrtps else 1

# No arg
subscription = self.node.create_subscription(
EmptyMsg, self.topic_name, message_callback, 10)
self.assertEqual(len(subscription.event_handlers), 0)
self.assertEqual(len(subscription.event_handlers), expected_num_event_handlers)
self.node.destroy_subscription(subscription)

# Arg with no callbacks
subscription = self.node.create_subscription(
EmptyMsg, self.topic_name, message_callback, 10, event_callbacks=callbacks)
self.assertEqual(len(subscription.event_handlers), 0)
self.assertEqual(len(subscription.event_handlers), expected_num_event_handlers)
self.node.destroy_subscription(subscription)

# Arg with one of the callbacks
callbacks.deadline = deadline_callback
expected_num_event_handlers += 1
subscription = self.node.create_subscription(
EmptyMsg, self.topic_name, message_callback, 10, event_callbacks=callbacks)
self.assertEqual(len(subscription.event_handlers), 1)
self.assertEqual(len(subscription.event_handlers), expected_num_event_handlers)
self.node.destroy_subscription(subscription)

# Arg with two callbacks
callbacks.liveliness = liveliness_callback
expected_num_event_handlers += 1
subscription = self.node.create_subscription(
EmptyMsg, self.topic_name, message_callback, 10, event_callbacks=callbacks)
self.assertEqual(len(subscription.event_handlers), 2)
self.assertEqual(len(subscription.event_handlers), expected_num_event_handlers)
self.node.destroy_subscription(subscription)

# Arg with three callbacks
Expand All @@ -142,22 +148,25 @@ def test_default_incompatible_qos_callbacks(self):
original_pub_logger = PublisherEventCallbacks._logger
original_sub_logger = SubscriptionEventCallbacks._logger

pub_log_msg = None
sub_log_msg = None
log_msgs_future = Future()
class MockLogger:
pub_log_msg = None
sub_log_msg = None
log_msgs_future = Future()

def __init__(self, name):
self.name = name

def warn(self, message, once=False):
nonlocal pub_log_msg, sub_log_msg, log_msgs_future

if self.name == 'PublisherEventCallbacks':
self.pub_log_msg = message
pub_log_msg = message
elif self.name == 'SubscriptionEventCallbacks':
self.sub_log_msg = message
sub_log_msg = message

if pub_log_msg is not None and sub_log_msg is not None:
log_msgs_future.set_result(True)

if self.pub_log_msg is not None and self.sub_log_msg is not None:
self.log_msgs_future.set_result(True)
PublisherEventCallbacks._logger = MockLogger('PublisherEventCallbacks')
SubscriptionEventCallbacks._logger = MockLogger('SubscriptionEventCallbacks')

Expand All @@ -172,17 +181,19 @@ def warn(self, message, once=False):
EmptyMsg, self.topic_name, message_callback, qos_profile_subscription)

executor = rclpy.executors.SingleThreadedExecutor(context=self.context)
rclpy.spin_until_future_complete(self.node, MockLogger.log_msgs_future, executor, 10.0)
rclpy.spin_until_future_complete(self.node, log_msgs_future, executor, 10.0)

if not self.is_fastrtps:
self.assertEqual(
MockLogger.pub_log_msg,
pub_log_msg,
'New subscription discovered on this topic, requesting incompatible QoS. '
'No messages will be sent to it. Last incompatible policy: DURABILITY_QOS_POLICY')
'No messages will be sent to it. '
'Last incompatible policy: DURABILITY_QOS_POLICY')
self.assertEqual(
MockLogger.sub_log_msg,
sub_log_msg,
'New publisher discovered on this topic, offering incompatible QoS. '
'No messages will be sent to it. Last incompatible policy: DURABILITY_QOS_POLICY')
'No messages will be received from it. '
'Last incompatible policy: DURABILITY_QOS_POLICY')

PublisherEventCallbacks._logger = original_pub_logger
SubscriptionEventCallbacks._logger = original_sub_logger
Expand Down

0 comments on commit 7e2be31

Please sign in to comment.